diff --git a/cbor/bytestring.go b/cbor/bytestring.go index f2412bd7..e1b5c582 100644 --- a/cbor/bytestring.go +++ b/cbor/bytestring.go @@ -16,34 +16,23 @@ package cbor import ( "encoding/hex" + + _cbor "github.com/fxamacker/cbor/v2" ) // Wrapper for bytestrings that allows them to be used as keys for a map -// We use a string because []byte isn't comparable, which means it can't be used as a map key -type ByteString string +// This was originally a full implementation, but now it just extends the upstream +// type +type ByteString struct { + _cbor.ByteString +} func NewByteString(data []byte) ByteString { - bs := ByteString(data) + bs := ByteString{ByteString: _cbor.ByteString(data)} return bs } -func (bs *ByteString) UnmarshalCBOR(data []byte) error { - tmpValue := []byte{} - if _, err := Decode(data, &tmpValue); err != nil { - return err - } - *bs = ByteString(tmpValue) - return nil -} - -func (bs ByteString) MarshalCBOR() ([]byte, error) { - return Encode([]byte(bs)) -} - -func (bs ByteString) Bytes() []byte { - return []byte(bs) -} - +// String returns a hex-encoded representation of the bytestring func (bs ByteString) String() string { - return hex.EncodeToString([]byte(bs)) + return hex.EncodeToString(bs.Bytes()) } diff --git a/cbor/value_test.go b/cbor/value_test.go index 4ad92192..a8436569 100644 --- a/cbor/value_test.go +++ b/cbor/value_test.go @@ -18,6 +18,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "io" "math/big" "reflect" "strings" @@ -43,7 +44,7 @@ var testDefs = []struct { { cborHex: "81", expectedObject: nil, - expectedDecodeError: fmt.Errorf("EOF"), + expectedDecodeError: io.ErrUnexpectedEOF, }, // Invalid map key type { diff --git a/go.mod b/go.mod index a6d18603..96b00443 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/blinklabs-io/gouroboros go 1.20 require ( - github.com/fxamacker/cbor/v2 v2.4.0 + github.com/fxamacker/cbor/v2 v2.5.0 github.com/jinzhu/copier v0.4.0 golang.org/x/crypto v0.13.0 ) diff --git a/go.sum b/go.sum index 6ede5b8c..54c98a99 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88= -github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= +github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE= +github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= diff --git a/ledger/common.go b/ledger/common.go index 2f3080da..111cb0d1 100644 --- a/ledger/common.go +++ b/ledger/common.go @@ -105,7 +105,7 @@ func (m MultiAsset[T]) MarshalJSON() ([]byte, error) { for policyId, policyData := range m.data { for assetName, amount := range policyData { tmpObj := multiAssetJson[T]{ - Name: string(assetName), + Name: string(assetName.Bytes()), NameHex: hex.EncodeToString(assetName.Bytes()), Amount: amount, PolicyId: policyId.String(), @@ -142,7 +142,7 @@ func (m *MultiAsset[T]) Asset(policyId Blake2b224, assetName []byte) T { if !ok { return 0 } - return policy[cbor.ByteString(assetName)] + return policy[cbor.NewByteString(assetName)] } type AssetFingerprint struct { diff --git a/ledger/common_test.go b/ledger/common_test.go index 921ff17f..2c9e9435 100644 --- a/ledger/common_test.go +++ b/ledger/common_test.go @@ -57,7 +57,7 @@ func TestMultiAssetJson(t *testing.T) { multiAssetObj: MultiAsset[MultiAssetTypeOutput]{ data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{ NewBlake2b224(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")): { - cbor.ByteString(test.DecodeHexString("6675726e697368613239686e")): 123456, + cbor.NewByteString(test.DecodeHexString("6675726e697368613239686e")): 123456, }, }, }, @@ -67,7 +67,7 @@ func TestMultiAssetJson(t *testing.T) { multiAssetObj: MultiAsset[MultiAssetTypeOutput]{ data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{ NewBlake2b224(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")): { - cbor.ByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567, + cbor.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567, }, }, }, @@ -77,7 +77,7 @@ func TestMultiAssetJson(t *testing.T) { multiAssetObj: MultiAsset[MultiAssetTypeOutput]{ data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{ NewBlake2b224(test.DecodeHexString("cf78aeb9736e8aa94ce8fab44da86b522fa9b1c56336b92a28420525")): { - cbor.ByteString(test.DecodeHexString("363438346330393264363164373033656236333233346461")): 12345678, + cbor.NewByteString(test.DecodeHexString("363438346330393264363164373033656236333233346461")): 12345678, }, }, }, diff --git a/ledger/mary_test.go b/ledger/mary_test.go index ecdc83ad..63ccfefc 100644 --- a/ledger/mary_test.go +++ b/ledger/mary_test.go @@ -27,7 +27,7 @@ func createMaryTransactionOutputValueAssets(policyId []byte, assetName []byte, a data := map[Blake2b224]map[cbor.ByteString]uint64{} policyIdKey := Blake2b224{} copy(policyIdKey[:], policyId) - assetKey := cbor.ByteString(assetName) + assetKey := cbor.NewByteString(assetName) data[policyIdKey] = map[cbor.ByteString]uint64{ assetKey: amount, } diff --git a/protocol/protocol.go b/protocol/protocol.go index eb9cd06e..d3ca5c32 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -330,7 +330,7 @@ func (p *Protocol) recvLoop() { var tmpMsg []cbor.RawMessage numBytesRead, err := cbor.Decode(p.recvBuffer.Bytes(), &tmpMsg) if err != nil { - if err == io.EOF && p.recvBuffer.Len() > 0 { + if err == io.ErrUnexpectedEOF && p.recvBuffer.Len() > 0 { // This is probably a multi-part message, so we wait until we get more of the message // before trying to process it p.recvReadyChan <- true