Skip to content

Commit bd46964

Browse files
authored
feat: CBOR encode support for indefinite-length bytestrings (#660)
This also slightly changes the way that indefinite-length lists work, as well as adds tests for indefinite-length lists Fixes #659
1 parent 3f47cef commit bd46964

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

cbor/encode.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ func EncodeGeneric(src interface{}) ([]byte, error) {
6767
return cborData, nil
6868
}
6969

70-
type IndefLengthList struct {
71-
Items []any
72-
}
70+
type IndefLengthList []any
7371

74-
func (i *IndefLengthList) MarshalCBOR() ([]byte, error) {
72+
func (i IndefLengthList) MarshalCBOR() ([]byte, error) {
7573
ret := []byte{
7674
// Start indefinite-length list
7775
0x9f,
7876
}
79-
for _, item := range i.Items {
77+
for _, item := range []any(i) {
8078
data, err := Encode(&item)
8179
if err != nil {
8280
return nil, err
@@ -90,3 +88,25 @@ func (i *IndefLengthList) MarshalCBOR() ([]byte, error) {
9088
)
9189
return ret, nil
9290
}
91+
92+
type IndefLengthByteString []any
93+
94+
func (i IndefLengthByteString) MarshalCBOR() ([]byte, error) {
95+
ret := []byte{
96+
// Start indefinite-length bytestring
97+
0x5f,
98+
}
99+
for _, item := range []any(i) {
100+
data, err := Encode(&item)
101+
if err != nil {
102+
return nil, err
103+
}
104+
ret = append(ret, data...)
105+
}
106+
ret = append(
107+
ret,
108+
// End indefinite length bytestring
109+
byte(0xff),
110+
)
111+
return ret, nil
112+
}

cbor/encode_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,43 @@ func TestEncode(t *testing.T) {
5050
}
5151
}
5252
}
53+
54+
func TestEncodeIndefLengthList(t *testing.T) {
55+
expectedCborHex := "9f1904d219162eff"
56+
tmpData := cbor.IndefLengthList{
57+
1234,
58+
5678,
59+
}
60+
cborData, err := cbor.Encode(tmpData)
61+
if err != nil {
62+
t.Fatalf("failed to encode object to CBOR: %s", err)
63+
}
64+
cborHex := hex.EncodeToString(cborData)
65+
if cborHex != expectedCborHex {
66+
t.Fatalf(
67+
"object did not encode to expected CBOR\n got %s\n wanted: %s",
68+
cborHex,
69+
expectedCborHex,
70+
)
71+
}
72+
}
73+
74+
func TestEncodeIndefLengthByteString(t *testing.T) {
75+
expectedCborHex := "5f440102030443abcdefff"
76+
tmpData := cbor.IndefLengthByteString{
77+
[]byte{1, 2, 3, 4},
78+
[]byte{0xab, 0xcd, 0xef},
79+
}
80+
cborData, err := cbor.Encode(tmpData)
81+
if err != nil {
82+
t.Fatalf("failed to encode object to CBOR: %s", err)
83+
}
84+
cborHex := hex.EncodeToString(cborData)
85+
if cborHex != expectedCborHex {
86+
t.Fatalf(
87+
"object did not encode to expected CBOR\n got %s\n wanted: %s",
88+
cborHex,
89+
expectedCborHex,
90+
)
91+
}
92+
}

protocol/txsubmission/messages.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ func (m *MsgReplyTxIds) MarshalCBOR() ([]byte, error) {
9393
}
9494
tmp := []any{
9595
MessageTypeReplyTxIds,
96-
cbor.IndefLengthList{
97-
Items: items,
98-
},
96+
cbor.IndefLengthList(items),
9997
}
10098
return cbor.Encode(tmp)
10199
}
@@ -122,9 +120,7 @@ func (m *MsgRequestTxs) MarshalCBOR() ([]byte, error) {
122120
}
123121
tmp := []any{
124122
MessageTypeRequestTxs,
125-
cbor.IndefLengthList{
126-
Items: items,
127-
},
123+
cbor.IndefLengthList(items),
128124
}
129125
return cbor.Encode(tmp)
130126
}
@@ -151,9 +147,7 @@ func (m *MsgReplyTxs) MarshalCBOR() ([]byte, error) {
151147
}
152148
tmp := []any{
153149
MessageTypeReplyTxs,
154-
cbor.IndefLengthList{
155-
Items: items,
156-
},
150+
cbor.IndefLengthList(items),
157151
}
158152
return cbor.Encode(tmp)
159153
}

0 commit comments

Comments
 (0)