Skip to content

feat: support for decoding script ref #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025
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
2 changes: 1 addition & 1 deletion ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (o AlonzoTransactionOutput) Address() common.Address {
return o.OutputAddress
}

func (o AlonzoTransactionOutput) ScriptRef() *cbor.LazyValue {
func (o AlonzoTransactionOutput) ScriptRef() common.Script {
return nil
}

Expand Down
19 changes: 8 additions & 11 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {

type BabbageTransactionOutput struct {
cbor.DecodeStoreCbor
OutputAddress common.Address `cbor:"0,keyasint,omitempty"`
OutputAmount mary.MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
DatumOption *BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"`
TxScriptRef *cbor.Tag `cbor:"3,keyasint,omitempty"`
legacyOutput bool
OutputAddress common.Address `cbor:"0,keyasint,omitempty"`
OutputAmount mary.MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
DatumOption *BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"`
TxOutScriptRef *common.ScriptRef `cbor:"3,keyasint,omitempty"`
legacyOutput bool
}

func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -486,14 +486,11 @@ func (o BabbageTransactionOutput) Address() common.Address {
return o.OutputAddress
}

func (o BabbageTransactionOutput) ScriptRef() *cbor.LazyValue {
if o.TxScriptRef == nil {
func (o BabbageTransactionOutput) ScriptRef() common.Script {
if o.TxOutScriptRef == nil {
return nil
}
if lazyVal, ok := o.TxScriptRef.Content.(*cbor.LazyValue); ok {
return lazyVal
}
return nil
return o.TxOutScriptRef.Script
}

func (o BabbageTransactionOutput) Amount() uint64 {
Expand Down
2 changes: 1 addition & 1 deletion ledger/byron/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (o ByronTransactionOutput) Address() common.Address {
return o.OutputAddress
}

func (o ByronTransactionOutput) ScriptRef() *cbor.LazyValue {
func (o ByronTransactionOutput) ScriptRef() common.Script {
return nil
}

Expand Down
72 changes: 72 additions & 0 deletions ledger/common/native_script.go → ledger/common/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,87 @@
package common

import (
"errors"
"fmt"

"github.com/blinklabs-io/gouroboros/cbor"
)

const (
ScriptRefTypeNativeScript = 0
ScriptRefTypePlutusV1 = 1
ScriptRefTypePlutusV2 = 2
ScriptRefTypePlutusV3 = 3
)

type Script interface {
isScript()
}

type ScriptRef struct {
Type uint
Script Script
}

func (s *ScriptRef) UnmarshalCBOR(data []byte) error {
// Unwrap outer CBOR tag
var tmpTag cbor.Tag
if _, err := cbor.Decode(data, &tmpTag); err != nil {
return err
}
innerCbor, ok := tmpTag.Content.([]byte)
if !ok {
return errors.New("unexpected tag type")
}
// Determine script type
var rawScript struct {
cbor.StructAsArray
Type uint
Raw cbor.RawMessage
}
if _, err := cbor.Decode(innerCbor, &rawScript); err != nil {
return err
}
var tmpScript Script
switch rawScript.Type {
case ScriptRefTypeNativeScript:
tmpScript = &NativeScript{}
case ScriptRefTypePlutusV1:
tmpScript = &PlutusV1Script{}
case ScriptRefTypePlutusV2:
tmpScript = &PlutusV2Script{}
case ScriptRefTypePlutusV3:
tmpScript = &PlutusV3Script{}
default:
return fmt.Errorf("unknown script type %d", rawScript.Type)
}
// Decode script
if _, err := cbor.Decode(rawScript.Raw, tmpScript); err != nil {
return err
}
s.Type = rawScript.Type
s.Script = tmpScript
return nil
}

type PlutusV1Script []byte

func (PlutusV1Script) isScript() {}

type PlutusV2Script []byte

func (PlutusV2Script) isScript() {}

type PlutusV3Script []byte

func (PlutusV3Script) isScript() {}

type NativeScript struct {
item any
}

func (NativeScript) isScript() {}

func (n *NativeScript) Item() any {
return n.item
}
Expand Down
38 changes: 38 additions & 0 deletions ledger/common/script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common_test

import (
"encoding/hex"
"reflect"
"testing"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

func TestScriptRefDecode(t *testing.T) {
// 24_0(<<[3, h'480123456789abcdef']>>)
testCbor, _ := hex.DecodeString("d8184c820349480123456789abcdef")
scriptCbor, _ := hex.DecodeString("480123456789abcdef")
expectedScript := common.PlutusV3Script(scriptCbor)
var testScriptRef common.ScriptRef
if _, err := cbor.Decode(testCbor, &testScriptRef); err != nil {
t.Fatalf("unexpected error decoding script ref CBOR: %s", err)
}
if !reflect.DeepEqual(testScriptRef.Script, &expectedScript) {
t.Fatalf("did not get expected script\n got: %#v\n wanted: %#v", testScriptRef.Script, &expectedScript)
}
}
2 changes: 1 addition & 1 deletion ledger/common/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type TransactionOutput interface {
DatumHash() *Blake2b256
Cbor() []byte
Utxorpc() (*utxorpc.TxOutput, error)
ScriptRef() *cbor.LazyValue
ScriptRef() Script
}

type TransactionWitnessSet interface {
Expand Down
2 changes: 1 addition & 1 deletion ledger/mary/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (o MaryTransactionOutput) Address() common.Address {
return o.OutputAddress
}

func (txo MaryTransactionOutput) ScriptRef() *cbor.LazyValue {
func (txo MaryTransactionOutput) ScriptRef() common.Script {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion ledger/shelley/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (o ShelleyTransactionOutput) Address() common.Address {
return o.OutputAddress
}

func (o ShelleyTransactionOutput) ScriptRef() *cbor.LazyValue {
func (o ShelleyTransactionOutput) ScriptRef() common.Script {
return nil
}

Expand Down
Loading