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
45 changes: 45 additions & 0 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package alonzo
import (
"encoding/json"
"fmt"
"math/big"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/mary"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
"github.com/blinklabs-io/plutigo/pkg/data"
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

Expand Down Expand Up @@ -332,6 +334,49 @@ func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
return json.Marshal(&tmpObj)
}

func (o AlonzoTransactionOutput) ToPlutusData() data.PlutusData {
var valueData [][2]data.PlutusData
if o.OutputAmount.Amount > 0 {
valueData = append(
valueData,
[2]data.PlutusData{
data.NewByteString(nil),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(nil),
data.NewInteger(new(big.Int).SetUint64(o.OutputAmount.Amount)),
},
},
),
},
)
}
if o.OutputAmount.Assets != nil {
assetData := o.OutputAmount.Assets.ToPlutusData()
assetDataMap, ok := assetData.(*data.Map)
if !ok {
return nil
}
valueData = append(
valueData,
assetDataMap.Pairs...,
)
}
tmpData := data.NewConstr(
0,
o.OutputAddress.ToPlutusData(),
data.NewMap(valueData),
// Empty datum option
// TODO: implement this
data.NewConstr(0),
// Empty script ref
// TODO: implement this
data.NewConstr(1),
)
return tmpData
}

func (o AlonzoTransactionOutput) Address() common.Address {
return o.OutputAddress
}
Expand Down
172 changes: 172 additions & 0 deletions ledger/alonzo/alonzo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// 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 alonzo

import (
"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/gouroboros/ledger/mary"
"github.com/blinklabs-io/plutigo/pkg/data"
)

func TestAlonzoTransactionOutputToPlutusDataCoinOnly(t *testing.T) {
testAddr := "addr_test1vqg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygxrcya6"
var testAmount uint64 = 123_456_789
testTxOut := AlonzoTransactionOutput{
OutputAddress: func() common.Address { foo, _ := common.NewAddress(testAddr); return foo }(),
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testAmount,
},
}
expectedData := data.NewConstr(
0,
// Address
data.NewConstr(
0,
data.NewConstr(
0,
data.NewConstr(
0,
data.NewByteString(
[]byte{
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
},
),
),
),
data.NewConstr(
1,
),
),
// Value
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(nil),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(nil),
data.NewInteger(big.NewInt(int64(testAmount))),
},
},
),
},
},
),
// TODO: empty datum option
data.NewConstr(0),
// TODO: empty script ref
data.NewConstr(1),
)
tmpData := testTxOut.ToPlutusData()
if !reflect.DeepEqual(tmpData, expectedData) {
t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, expectedData)
}
}

func TestAlonzoTransactionOutputToPlutusDataCoinAssets(t *testing.T) {
testAddr := "addr_test1vqg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygxrcya6"
var testAmount uint64 = 123_456_789
testAssets := common.NewMultiAsset[common.MultiAssetTypeOutput](
map[common.Blake2b224]map[cbor.ByteString]common.MultiAssetTypeOutput{
common.NewBlake2b224(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")): {
cbor.NewByteString(test.DecodeHexString("6675726e697368613239686e")): 123456,
},
common.NewBlake2b224(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")): {
cbor.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567,
},
},
)
testTxOut := AlonzoTransactionOutput{
OutputAddress: func() common.Address { foo, _ := common.NewAddress(testAddr); return foo }(),
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testAmount,
Assets: &testAssets,
},
}
expectedData := data.NewConstr(
0,
// Address
data.NewConstr(
0,
data.NewConstr(
0,
data.NewConstr(
0,
data.NewByteString(
[]byte{
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
},
),
),
),
data.NewConstr(
1,
),
),
// Value
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(nil),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(nil),
data.NewInteger(big.NewInt(int64(testAmount))),
},
},
),
},
{
data.NewByteString(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("6675726e697368613239686e")),
data.NewInteger(big.NewInt(123456)),
},
},
),
},
{
data.NewByteString(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")),
data.NewInteger(big.NewInt(234567)),
},
},
),
},
},
),
// Empty datum option
data.NewConstr(0),
// Empty script ref
data.NewConstr(1),
)
tmpData := testTxOut.ToPlutusData()
if !reflect.DeepEqual(tmpData, expectedData) {
t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, expectedData)
}
}
45 changes: 45 additions & 0 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
"github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/mary"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
"github.com/blinklabs-io/plutigo/pkg/data"
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

Expand Down Expand Up @@ -487,6 +489,49 @@ func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
return json.Marshal(&tmpObj)
}

func (o BabbageTransactionOutput) ToPlutusData() data.PlutusData {
var valueData [][2]data.PlutusData
if o.OutputAmount.Amount > 0 {
valueData = append(
valueData,
[2]data.PlutusData{
data.NewByteString(nil),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(nil),
data.NewInteger(new(big.Int).SetUint64(o.OutputAmount.Amount)),
},
},
),
},
)
}
if o.OutputAmount.Assets != nil {
assetData := o.OutputAmount.Assets.ToPlutusData()
assetDataMap, ok := assetData.(*data.Map)
if !ok {
return nil
}
valueData = append(
valueData,
assetDataMap.Pairs...,
)
}
tmpData := data.NewConstr(
0,
o.OutputAddress.ToPlutusData(),
data.NewMap(valueData),
// Empty datum option
// TODO: implement this
data.NewConstr(0),
// Empty script ref
// TODO: implement this
data.NewConstr(1),
)
return tmpData
}

func (o BabbageTransactionOutput) Address() common.Address {
return o.OutputAddress
}
Expand Down
Loading