Skip to content

Commit 42b1155

Browse files
authored
fix: script ref CBOR marshaling (#1150)
Fixes #1149 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent d2a4f8d commit 42b1155

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

ledger/common/script.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ func (s *ScriptRef) UnmarshalCBOR(data []byte) error {
8282
return nil
8383
}
8484

85+
func (s *ScriptRef) MarshalCBOR() ([]byte, error) {
86+
tmpData := []any{
87+
s.Type,
88+
s.Script,
89+
}
90+
tmpDataCbor, err := cbor.Encode(tmpData)
91+
if err != nil {
92+
return nil, err
93+
}
94+
tmpTag := cbor.Tag{
95+
Number: 24,
96+
Content: tmpDataCbor,
97+
}
98+
return cbor.Encode(tmpTag)
99+
}
100+
85101
type PlutusV1Script []byte
86102

87103
func (PlutusV1Script) isScript() {}

ledger/common/script_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
"github.com/blinklabs-io/gouroboros/ledger/common"
2424
)
2525

26-
func TestScriptRefDecode(t *testing.T) {
26+
func TestScriptRefDecodeEncode(t *testing.T) {
2727
// 24_0(<<[3, h'480123456789abcdef']>>)
28-
testCbor, _ := hex.DecodeString("d8184c820349480123456789abcdef")
28+
testCborHex := "d8184c820349480123456789abcdef"
29+
testCbor, _ := hex.DecodeString(testCborHex)
2930
scriptCbor, _ := hex.DecodeString("480123456789abcdef")
3031
expectedScript := common.PlutusV3Script(scriptCbor)
3132
var testScriptRef common.ScriptRef
@@ -39,6 +40,13 @@ func TestScriptRefDecode(t *testing.T) {
3940
&expectedScript,
4041
)
4142
}
43+
scriptRefCbor, err := cbor.Encode(testScriptRef)
44+
if err != nil {
45+
t.Fatalf("unexpected error: %s", err)
46+
}
47+
if hex.EncodeToString(scriptRefCbor) != testCborHex {
48+
t.Fatalf("did not get expected CBOR\n got: %x\n wanted: %s", scriptRefCbor, testCborHex)
49+
}
4250
}
4351

4452
func TestNativeScriptHash(t *testing.T) {

0 commit comments

Comments
 (0)