From 340ea4124298ff8489bac42a4f0511590b79ca0f Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 20 Aug 2025 16:56:22 -0400 Subject: [PATCH] fix: script ref CBOR marshaling Fixes #1149 Signed-off-by: Aurora Gaffney --- ledger/common/script.go | 16 ++++++++++++++++ ledger/common/script_test.go | 12 ++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ledger/common/script.go b/ledger/common/script.go index 6c0ddb00..9212de83 100644 --- a/ledger/common/script.go +++ b/ledger/common/script.go @@ -82,6 +82,22 @@ func (s *ScriptRef) UnmarshalCBOR(data []byte) error { return nil } +func (s *ScriptRef) MarshalCBOR() ([]byte, error) { + tmpData := []any{ + s.Type, + s.Script, + } + tmpDataCbor, err := cbor.Encode(tmpData) + if err != nil { + return nil, err + } + tmpTag := cbor.Tag{ + Number: 24, + Content: tmpDataCbor, + } + return cbor.Encode(tmpTag) +} + type PlutusV1Script []byte func (PlutusV1Script) isScript() {} diff --git a/ledger/common/script_test.go b/ledger/common/script_test.go index a1b05f12..83f8d5df 100644 --- a/ledger/common/script_test.go +++ b/ledger/common/script_test.go @@ -23,9 +23,10 @@ import ( "github.com/blinklabs-io/gouroboros/ledger/common" ) -func TestScriptRefDecode(t *testing.T) { +func TestScriptRefDecodeEncode(t *testing.T) { // 24_0(<<[3, h'480123456789abcdef']>>) - testCbor, _ := hex.DecodeString("d8184c820349480123456789abcdef") + testCborHex := "d8184c820349480123456789abcdef" + testCbor, _ := hex.DecodeString(testCborHex) scriptCbor, _ := hex.DecodeString("480123456789abcdef") expectedScript := common.PlutusV3Script(scriptCbor) var testScriptRef common.ScriptRef @@ -39,6 +40,13 @@ func TestScriptRefDecode(t *testing.T) { &expectedScript, ) } + scriptRefCbor, err := cbor.Encode(testScriptRef) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + if hex.EncodeToString(scriptRefCbor) != testCborHex { + t.Fatalf("did not get expected CBOR\n got: %x\n wanted: %s", scriptRefCbor, testCborHex) + } } func TestNativeScriptHash(t *testing.T) {