Skip to content

Commit 4a7b118

Browse files
authored
fix: support for decode/encode of Constr with tag 102 (#110)
Fixes #109 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent d7774b3 commit 4a7b118

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

data/data_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ var testDefs = []struct {
9898
),
9999
CborHex: "d8799fa1d8799f00190196ffd8799f1b17f2495b03141751ffff",
100100
},
101+
{
102+
Data: NewConstr(
103+
999,
104+
NewInteger(big.NewInt(6)),
105+
NewInteger(big.NewInt(7)),
106+
),
107+
// 102([999, [_ 6, 7]])
108+
CborHex: "d866821903e79f0607ff",
109+
},
101110
}
102111

103112
func TestPlutusDataEncode(t *testing.T) {

data/decode.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package data
22

33
import (
4-
"errors"
54
"fmt"
65
"math/big"
76

@@ -197,8 +196,15 @@ func decodeRawTag(tag cbor.RawTag) (PlutusData, error) {
197196
ret, retErr = decodeBignum(tag.Content, true)
198197

199198
case 102:
200-
return nil, errors.New("tagged data (tag 102) not implemented")
201-
199+
var tmpData struct {
200+
_ struct{} `cbor:",toarray"`
201+
Alternative uint64
202+
FieldsRaw cbor.RawMessage
203+
}
204+
if err := cbor.Unmarshal(tag.Content, &tmpData); err != nil {
205+
return nil, err
206+
}
207+
ret, retErr = decodeConstr(tmpData.Alternative, tmpData.FieldsRaw)
202208
default:
203209
return nil, fmt.Errorf("unknown CBOR tag for PlutusData: %d", tag.Number)
204210
}

data/encode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package data
22

33
import (
4-
"errors"
54
"fmt"
65
"math/big"
76
"slices"
@@ -78,8 +77,9 @@ func encodeConstr(c *Constr) (any, error) {
7877
case c.Tag >= 7 && c.Tag <= 127:
7978
// Tags 7-127 map to CBOR tags 1280-1400
8079
cborTag = 1280 + uint64(c.Tag-7)
81-
case c.Tag == 102:
82-
return nil, errors.New("tagged data (tag 102) not implemented")
80+
case c.Tag >= 128:
81+
cborTag = 102
82+
fields = []any{c.Tag, fields}
8383
default:
8484
return nil, fmt.Errorf("unsupported Constr tag: %d", c.Tag)
8585
}

0 commit comments

Comments
 (0)