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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.24.1
require (
filippo.io/edwards25519 v1.1.0
github.com/blinklabs-io/ouroboros-mock v0.3.8
github.com/blinklabs-io/plutigo v0.0.4
github.com/blinklabs-io/plutigo v0.0.5
github.com/btcsuite/btcd/btcutil v1.1.6
github.com/fxamacker/cbor/v2 v2.9.0
github.com/jinzhu/copier v0.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/blinklabs-io/ouroboros-mock v0.3.8 h1:+DAt2rx0ouZUxee5DBMgZq3I1+ZdxFSHG9g3tYl/FKU=
github.com/blinklabs-io/ouroboros-mock v0.3.8/go.mod h1:UwQIf4KqZwO13P9d90fbi3UL/X7JaJfeEbqk+bEeFQA=
github.com/blinklabs-io/plutigo v0.0.4 h1:YhSt25yTqzfyUAzO0G/UihB16/lZNsvS6J7THO0f3ss=
github.com/blinklabs-io/plutigo v0.0.4/go.mod h1:sXfzbcwvVS8AbyNNVnhyyIT18f88IZMqg5cwOBmiafU=
github.com/blinklabs-io/plutigo v0.0.5 h1:d3q7CM4kdVGa2VRt9zZHrUkkwNwLJyuMLPP2kDrIriE=
github.com/blinklabs-io/plutigo v0.0.5/go.mod h1:fXUCkc9l8lvkprHApub5HpNmakYHLNPhEuWK+49anHE=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd/go.mod h1:nm3Bko6zh6bWP60UxwoT5LzdGJsQJaPo6HjduXq9p6A=
Expand Down
8 changes: 4 additions & 4 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (o AlonzoTransactionOutput) DatumHash() *common.Blake2b256 {
return o.OutputDatumHash
}

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

Expand Down Expand Up @@ -451,7 +451,7 @@ type AlonzoRedeemer struct {
cbor.StructAsArray
Tag common.RedeemerTag
Index uint32
Data cbor.LazyValue
Data common.Datum
ExUnits common.ExUnits
}

Expand Down Expand Up @@ -522,7 +522,7 @@ type AlonzoTransactionWitnessSet struct {
WsNativeScripts []common.NativeScript `cbor:"1,keyasint,omitempty"`
BootstrapWitnesses []common.BootstrapWitness `cbor:"2,keyasint,omitempty"`
WsPlutusV1Scripts [][]byte `cbor:"3,keyasint,omitempty"`
WsPlutusData []cbor.Value `cbor:"4,keyasint,omitempty"`
WsPlutusData []common.Datum `cbor:"4,keyasint,omitempty"`
WsRedeemers AlonzoRedeemers `cbor:"5,keyasint,omitempty"`
}

Expand Down Expand Up @@ -563,7 +563,7 @@ func (w AlonzoTransactionWitnessSet) PlutusV3Scripts() [][]byte {
return nil
}

func (w AlonzoTransactionWitnessSet) PlutusData() []cbor.Value {
func (w AlonzoTransactionWitnessSet) PlutusData() []common.Datum {
return w.WsPlutusData
}

Expand Down
30 changes: 18 additions & 12 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ const (

type BabbageTransactionOutputDatumOption struct {
hash *common.Blake2b256
data *cbor.LazyValue
data *common.Datum
}

func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(data []byte) error {
datumOptionType, err := cbor.DecodeIdFromList(data)
func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(cborData []byte) error {
datumOptionType, err := cbor.DecodeIdFromList(cborData)
if err != nil {
return err
}
Expand All @@ -389,7 +389,7 @@ func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(data []byte) error {
Type int
Hash common.Blake2b256
}
if _, err := cbor.Decode(data, &tmpDatumHash); err != nil {
if _, err := cbor.Decode(cborData, &tmpDatumHash); err != nil {
return err
}
d.hash = &(tmpDatumHash.Hash)
Expand All @@ -399,14 +399,16 @@ func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(data []byte) error {
Type int
DataCbor []byte
}
if _, err := cbor.Decode(data, &tmpDatumData); err != nil {
if _, err := cbor.Decode(cborData, &tmpDatumData); err != nil {
return err
}
var datumValue cbor.LazyValue
var datumValue common.Datum
if _, err := cbor.Decode(tmpDatumData.DataCbor, &datumValue); err != nil {
return err
}
d.data = &(datumValue)
d.data = &common.Datum{
Data: datumValue.Data,
}
default:
return fmt.Errorf("unsupported datum option type: %d", datumOptionType)
}
Expand All @@ -418,7 +420,11 @@ func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {
if d.hash != nil {
tmpObj = []any{DatumOptionTypeHash, d.hash}
} else if d.data != nil {
tmpObj = []any{DatumOptionTypeData, cbor.Tag{Number: 24, Content: d.data.Cbor()}}
tmpContent, err := cbor.Encode(d.data)
if err != nil {
return nil, err
}
tmpObj = []any{DatumOptionTypeData, cbor.Tag{Number: 24, Content: tmpContent}}
} else {
return nil, errors.New("unknown datum option type")
}
Expand Down Expand Up @@ -471,7 +477,7 @@ func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
Address common.Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"`
Datum *cbor.LazyValue `json:"datum,omitempty"`
Datum *common.Datum `json:"datum,omitempty"`
DatumHash string `json:"datumHash,omitempty"`
}{
Address: o.OutputAddress,
Expand Down Expand Up @@ -560,7 +566,7 @@ func (o BabbageTransactionOutput) DatumHash() *common.Blake2b256 {
return &common.Blake2b256{}
}

func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {
func (o BabbageTransactionOutput) Datum() *common.Datum {
if o.DatumOption != nil {
return o.DatumOption.data
}
Expand Down Expand Up @@ -625,7 +631,7 @@ type BabbageTransactionWitnessSet struct {
WsNativeScripts []common.NativeScript `cbor:"1,keyasint,omitempty"`
BootstrapWitnesses []common.BootstrapWitness `cbor:"2,keyasint,omitempty"`
WsPlutusV1Scripts [][]byte `cbor:"3,keyasint,omitempty"`
WsPlutusData []cbor.Value `cbor:"4,keyasint,omitempty"`
WsPlutusData []common.Datum `cbor:"4,keyasint,omitempty"`
WsRedeemers alonzo.AlonzoRedeemers `cbor:"5,keyasint,omitempty"`
WsPlutusV2Scripts [][]byte `cbor:"6,keyasint,omitempty"`
}
Expand Down Expand Up @@ -666,7 +672,7 @@ func (w BabbageTransactionWitnessSet) PlutusV3Scripts() [][]byte {
return nil
}

func (w BabbageTransactionWitnessSet) PlutusData() []cbor.Value {
func (w BabbageTransactionWitnessSet) PlutusData() []common.Datum {
return w.WsPlutusData
}

Expand Down
2 changes: 1 addition & 1 deletion ledger/byron/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (o ByronTransactionOutput) DatumHash() *common.Blake2b256 {
return nil
}

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

Expand Down
50 changes: 50 additions & 0 deletions ledger/common/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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

import (
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/plutigo/data"
)

type DatumHash = Blake2b256

// Datum represents a Plutus datum
type Datum struct {
cbor.DecodeStoreCbor
Data data.PlutusData `json:"data"`
}

func (d *Datum) UnmarshalCBOR(cborData []byte) error {
d.SetCbor(cborData)
tmpData, err := data.Decode(cborData)
if err != nil {
return err
}
d.Data = tmpData
return nil
}

func (d *Datum) MarshalCBOR() ([]byte, error) {
tmpCbor, err := data.Encode(d.Data)
if err != nil {
return nil, err
}
return tmpCbor, nil
}

func (d *Datum) Hash() DatumHash {
return Blake2b256Hash(d.Cbor())
}
111 changes: 111 additions & 0 deletions ledger/common/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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"
"math/big"
"reflect"
"testing"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/internal/test"
"github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/plutigo/data"
)

func TestDatumHash(t *testing.T) {
testDatumBytes, _ := hex.DecodeString("d8799fd8799fd8799f581cb255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5cffd8799fd8799fd8799f581c07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349ffffffffd8799f581cf5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c582073e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abceff1b0000000148f3f3579fd8799fd8799f4040ff1a094f78d8ffd8799fd8799f581cf13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b527546535452494b45ff1af7c5c601ffffff")
expectedHash := "4dfec91f63f946d7c91af0041e5d92a45531790a4a104637dd8691f46fdce842"
var tmpDatum common.Datum
if _, err := cbor.Decode(testDatumBytes, &tmpDatum); err != nil {
t.Fatalf("unexpected error: %s", err)
}
datumHash := tmpDatum.Hash()
if datumHash.String() != expectedHash {
t.Fatalf("did not get expected datum hash: got %s, wanted %s", datumHash.String(), expectedHash)
}
}

func TestDatumDecode(t *testing.T) {
testDatumBytes, _ := hex.DecodeString("d8799fd8799fd8799f581cb255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5cffd8799fd8799fd8799f581c07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349ffffffffd8799f581cf5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c582073e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abceff1b0000000148f3f3579fd8799fd8799f4040ff1a094f78d8ffd8799fd8799f581cf13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b527546535452494b45ff1af7c5c601ffffff")
expectedDatum := common.Datum{
Data: data.NewConstr(
0,
data.NewConstr(
0,
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("b255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5c"),
),
),
data.NewConstr(
0,
data.NewConstr(
0,
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349"),
),
),
),
),
),
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("f5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c"),
),
data.NewByteString(
test.DecodeHexString("73e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abce"),
),
),
data.NewInteger(big.NewInt(5518914391)),
data.NewList(
data.NewConstr(
0,
data.NewConstr(
0,
data.NewByteString(nil),
data.NewByteString(nil),
),
data.NewInteger(big.NewInt(156203224)),
),
data.NewConstr(
0,
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("f13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b5275"),
),
data.NewByteString(
test.DecodeHexString("535452494b45"),
),
),
data.NewInteger(big.NewInt(4156933633)),
),
),
),
}
var tmpDatum common.Datum
if _, err := cbor.Decode(testDatumBytes, &tmpDatum); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(tmpDatum.Data, expectedDatum.Data) {
t.Fatalf("did not get expected datum\n got: %#v\n wanted: %#v", tmpDatum.Data, expectedDatum.Data)
}
}
2 changes: 1 addition & 1 deletion ledger/common/redeemer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ type RedeemerKey struct {

type RedeemerValue struct {
cbor.StructAsArray
Data cbor.LazyValue
Data Datum
ExUnits ExUnits
}
4 changes: 2 additions & 2 deletions ledger/common/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type TransactionOutput interface {
Address() Address
Amount() uint64
Assets() *MultiAsset[MultiAssetTypeOutput]
Datum() *cbor.LazyValue
Datum() *Datum
DatumHash() *Blake2b256
Cbor() []byte
Utxorpc() (*utxorpc.TxOutput, error)
Expand All @@ -83,7 +83,7 @@ type TransactionWitnessSet interface {
Vkey() []VkeyWitness
NativeScripts() []NativeScript
Bootstrap() []BootstrapWitness
PlutusData() []cbor.Value
PlutusData() []Datum
PlutusV1Scripts() [][]byte
PlutusV2Scripts() [][]byte
PlutusV3Scripts() [][]byte
Expand Down
4 changes: 2 additions & 2 deletions ledger/conway/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ type ConwayTransactionWitnessSet struct {
WsNativeScripts cbor.SetType[common.NativeScript] `cbor:"1,keyasint,omitempty,omitzero"`
BootstrapWitnesses cbor.SetType[common.BootstrapWitness] `cbor:"2,keyasint,omitempty,omitzero"`
WsPlutusV1Scripts cbor.SetType[[]byte] `cbor:"3,keyasint,omitempty,omitzero"`
WsPlutusData cbor.SetType[cbor.Value] `cbor:"4,keyasint,omitempty,omitzero"`
WsPlutusData cbor.SetType[common.Datum] `cbor:"4,keyasint,omitempty,omitzero"`
WsRedeemers ConwayRedeemers `cbor:"5,keyasint,omitempty,omitzero"`
WsPlutusV2Scripts cbor.SetType[[]byte] `cbor:"6,keyasint,omitempty,omitzero"`
WsPlutusV3Scripts cbor.SetType[[]byte] `cbor:"7,keyasint,omitempty,omitzero"`
Expand Down Expand Up @@ -284,7 +284,7 @@ func (w ConwayTransactionWitnessSet) PlutusV3Scripts() [][]byte {
return w.WsPlutusV3Scripts.Items()
}

func (w ConwayTransactionWitnessSet) PlutusData() []cbor.Value {
func (w ConwayTransactionWitnessSet) PlutusData() []common.Datum {
return w.WsPlutusData.Items()
}

Expand Down
2 changes: 1 addition & 1 deletion ledger/mary/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (o MaryTransactionOutput) DatumHash() *common.Blake2b256 {
return nil
}

func (o MaryTransactionOutput) Datum() *cbor.LazyValue {
func (o MaryTransactionOutput) Datum() *common.Datum {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions ledger/shelley/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func (o ShelleyTransactionOutput) DatumHash() *common.Blake2b256 {
return nil
}

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

Expand Down Expand Up @@ -474,7 +474,7 @@ func (w ShelleyTransactionWitnessSet) NativeScripts() []common.NativeScript {
return w.WsNativeScripts
}

func (w ShelleyTransactionWitnessSet) PlutusData() []cbor.Value {
func (w ShelleyTransactionWitnessSet) PlutusData() []common.Datum {
// No plutus data in Shelley
return nil
}
Expand Down