Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CBOR encode support for indefinite-length bytestrings #660

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions cbor/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ func EncodeGeneric(src interface{}) ([]byte, error) {
return cborData, nil
}

type IndefLengthList struct {
Items []any
}
type IndefLengthList []any

func (i *IndefLengthList) MarshalCBOR() ([]byte, error) {
func (i IndefLengthList) MarshalCBOR() ([]byte, error) {
ret := []byte{
// Start indefinite-length list
0x9f,
}
for _, item := range i.Items {
for _, item := range []any(i) {
data, err := Encode(&item)
if err != nil {
return nil, err
Expand All @@ -90,3 +88,25 @@ func (i *IndefLengthList) MarshalCBOR() ([]byte, error) {
)
return ret, nil
}

type IndefLengthByteString []any

func (i IndefLengthByteString) MarshalCBOR() ([]byte, error) {
ret := []byte{
// Start indefinite-length bytestring
0x5f,
}
for _, item := range []any(i) {
data, err := Encode(&item)
if err != nil {
return nil, err
}
ret = append(ret, data...)
}
ret = append(
ret,
// End indefinite length bytestring
byte(0xff),
)
return ret, nil
}
40 changes: 40 additions & 0 deletions cbor/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,43 @@ func TestEncode(t *testing.T) {
}
}
}

func TestEncodeIndefLengthList(t *testing.T) {
expectedCborHex := "9f1904d219162eff"
tmpData := cbor.IndefLengthList{
1234,
5678,
}
cborData, err := cbor.Encode(tmpData)
if err != nil {
t.Fatalf("failed to encode object to CBOR: %s", err)
}
cborHex := hex.EncodeToString(cborData)
if cborHex != expectedCborHex {
t.Fatalf(
"object did not encode to expected CBOR\n got %s\n wanted: %s",
cborHex,
expectedCborHex,
)
}
}

func TestEncodeIndefLengthByteString(t *testing.T) {
expectedCborHex := "5f440102030443abcdefff"
tmpData := cbor.IndefLengthByteString{
[]byte{1, 2, 3, 4},
[]byte{0xab, 0xcd, 0xef},
}
cborData, err := cbor.Encode(tmpData)
if err != nil {
t.Fatalf("failed to encode object to CBOR: %s", err)
}
cborHex := hex.EncodeToString(cborData)
if cborHex != expectedCborHex {
t.Fatalf(
"object did not encode to expected CBOR\n got %s\n wanted: %s",
cborHex,
expectedCborHex,
)
}
}
12 changes: 3 additions & 9 deletions protocol/txsubmission/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func (m *MsgReplyTxIds) MarshalCBOR() ([]byte, error) {
}
tmp := []any{
MessageTypeReplyTxIds,
cbor.IndefLengthList{
Items: items,
},
cbor.IndefLengthList(items),
}
return cbor.Encode(tmp)
}
Expand All @@ -122,9 +120,7 @@ func (m *MsgRequestTxs) MarshalCBOR() ([]byte, error) {
}
tmp := []any{
MessageTypeRequestTxs,
cbor.IndefLengthList{
Items: items,
},
cbor.IndefLengthList(items),
}
return cbor.Encode(tmp)
}
Expand All @@ -151,9 +147,7 @@ func (m *MsgReplyTxs) MarshalCBOR() ([]byte, error) {
}
tmp := []any{
MessageTypeReplyTxs,
cbor.IndefLengthList{
Items: items,
},
cbor.IndefLengthList(items),
}
return cbor.Encode(tmp)
}
Expand Down