From 49854dfcfc8c16378fa893226944dc8ff18d584c Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 16 Jul 2025 16:04:19 -0400 Subject: [PATCH] feat: helper function to convert TransactionInput to PlutusData NOTE: this updates the required Go version to 1.24 because plutigo requires it Fixes #1099 Signed-off-by: Aurora Gaffney --- .github/workflows/go-test.yml | 3 +- .github/workflows/golangci-lint.yml | 2 +- go.mod | 3 +- go.sum | 4 +++ ledger/byron/byron.go | 13 ++++++++ ledger/common/tx.go | 2 ++ ledger/shelley/shelley.go | 12 +++++++ ledger/shelley/tx_test.go | 49 +++++++++++++++++++++++++++++ 8 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 ledger/shelley/tx_test.go diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index 5cac8fde..ce4c4a7c 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -16,7 +16,8 @@ jobs: name: go-test strategy: matrix: - go-version: [1.23.x, 1.24.x] + # TODO: go back to last 2 versions once 1.25 is released + go-version: [1.24.x] platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index b9724a2a..aaa73f76 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -18,6 +18,6 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.23.x + go-version: 1.24.x - name: golangci-lint uses: golangci/golangci-lint-action@v8 diff --git a/go.mod b/go.mod index ce73b67c..72aa2a86 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,13 @@ module github.com/blinklabs-io/gouroboros -go 1.23.6 +go 1.24.0 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.2-0.20250716022656-7418e5ead692 github.com/btcsuite/btcd/btcutil v1.1.6 github.com/fxamacker/cbor/v2 v2.9.0 github.com/jinzhu/copier v0.4.0 diff --git a/go.sum b/go.sum index 8a85cbb4..883d421a 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,10 @@ 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.1 h1:IBM3+dm7Sy92zgm9SBvIwVXN/73Jvg/ipDwbgRDeVFo= +github.com/blinklabs-io/plutigo v0.0.1/go.mod h1:N7id3F9kjRcvm/BMxwZsBWUSJTGsUEwVRfFz3Tt5LPM= +github.com/blinklabs-io/plutigo v0.0.2-0.20250716022656-7418e5ead692 h1:irMl0GGNv5heP8LpYBG4Z+a/RYqmmHVESZEa/Ba/67s= +github.com/blinklabs-io/plutigo v0.0.2-0.20250716022656-7418e5ead692/go.mod h1:Bh2zD801zEN90MIFv78HF1d3c/RpG/GD18wQrbdeN+0= 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= diff --git a/ledger/byron/byron.go b/ledger/byron/byron.go index 7d341ba8..88a616a6 100644 --- a/ledger/byron/byron.go +++ b/ledger/byron/byron.go @@ -19,9 +19,11 @@ import ( "errors" "fmt" "math" + "math/big" "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/plutigo/pkg/data" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -377,6 +379,17 @@ func (i ByronTransactionInput) Utxorpc() (*utxorpc.TxInput, error) { }, nil } +func (i ByronTransactionInput) ToPlutusData() data.PlutusData { + // This will never actually get called, but it's identical to Shelley + return data.NewConstr( + 0, + []data.PlutusData{ + data.NewByteString(i.TxId.Bytes()), + data.NewInteger(big.NewInt(int64(i.OutputIndex))), + }, + ) +} + func (i ByronTransactionInput) String() string { return fmt.Sprintf("%s#%d", i.TxId, i.OutputIndex) } diff --git a/ledger/common/tx.go b/ledger/common/tx.go index aed0f838..b6338942 100644 --- a/ledger/common/tx.go +++ b/ledger/common/tx.go @@ -16,6 +16,7 @@ package common import ( "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/plutigo/pkg/data" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -61,6 +62,7 @@ type TransactionInput interface { Index() uint32 String() string Utxorpc() (*utxorpc.TxInput, error) + ToPlutusData() data.PlutusData } type TransactionOutput interface { diff --git a/ledger/shelley/shelley.go b/ledger/shelley/shelley.go index 4e31f200..a6f3ed4c 100644 --- a/ledger/shelley/shelley.go +++ b/ledger/shelley/shelley.go @@ -19,9 +19,11 @@ import ( "errors" "fmt" "math" + "math/big" "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/plutigo/pkg/data" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -367,6 +369,16 @@ func (i ShelleyTransactionInput) Utxorpc() (*utxorpc.TxInput, error) { }, nil } +func (i ShelleyTransactionInput) ToPlutusData() data.PlutusData { + return data.NewConstr( + 0, + []data.PlutusData{ + data.NewByteString(i.TxId.Bytes()), + data.NewInteger(big.NewInt(int64(i.OutputIndex))), + }, + ) +} + func (i ShelleyTransactionInput) String() string { return fmt.Sprintf("%s#%d", i.TxId, i.OutputIndex) } diff --git a/ledger/shelley/tx_test.go b/ledger/shelley/tx_test.go new file mode 100644 index 00000000..e67081c7 --- /dev/null +++ b/ledger/shelley/tx_test.go @@ -0,0 +1,49 @@ +// 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 shelley_test + +import ( + "encoding/hex" + "math/big" + "reflect" + "testing" + + "github.com/blinklabs-io/gouroboros/ledger/shelley" + "github.com/blinklabs-io/plutigo/pkg/data" +) + +func TestShelleyTransactionInputToPlutusData(t *testing.T) { + testTxIdHex := "1639f61ed08f5e489dd64db20f86451a0db06e83d21ea39c73ea0a93b478a370" + testTxOutputIdx := 2 + testInput := shelley.NewShelleyTransactionInput( + testTxIdHex, + testTxOutputIdx, + ) + testTxId, err := hex.DecodeString(testTxIdHex) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + expectedData := data.NewConstr( + 0, + []data.PlutusData{ + data.NewByteString(testTxId), + data.NewInteger(big.NewInt(int64(testTxOutputIdx))), + }, + ) + tmpData := testInput.ToPlutusData() + if !reflect.DeepEqual(tmpData, expectedData) { + t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, expectedData) + } +}