Skip to content
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
16 changes: 16 additions & 0 deletions ledger/common/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
12 changes: 10 additions & 2 deletions ledger/common/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down