Skip to content

Commit 4a6c664

Browse files
authored
feat: helper function to convert TransactionInput to PlutusData (#1108)
NOTE: this updates the required Go version to 1.24 because plutigo requires it Fixes #1099 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent dc04d43 commit 4a6c664

File tree

8 files changed

+85
-3
lines changed

8 files changed

+85
-3
lines changed

.github/workflows/go-test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ jobs:
1616
name: go-test
1717
strategy:
1818
matrix:
19-
go-version: [1.23.x, 1.24.x]
19+
# TODO: go back to last 2 versions once 1.25 is released
20+
go-version: [1.24.x]
2021
platform: [ubuntu-latest]
2122
runs-on: ${{ matrix.platform }}
2223
steps:

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ jobs:
1818
- uses: actions/checkout@v4
1919
- uses: actions/setup-go@v5
2020
with:
21-
go-version: 1.23.x
21+
go-version: 1.24.x
2222
- name: golangci-lint
2323
uses: golangci/golangci-lint-action@v8

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module github.com/blinklabs-io/gouroboros
22

3-
go 1.23.6
3+
go 1.24.0
44

55
toolchain go1.24.1
66

77
require (
88
filippo.io/edwards25519 v1.1.0
99
github.com/blinklabs-io/ouroboros-mock v0.3.8
10+
github.com/blinklabs-io/plutigo v0.0.2-0.20250716022656-7418e5ead692
1011
github.com/btcsuite/btcd/btcutil v1.1.6
1112
github.com/fxamacker/cbor/v2 v2.9.0
1213
github.com/jinzhu/copier v0.4.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
33
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
44
github.com/blinklabs-io/ouroboros-mock v0.3.8 h1:+DAt2rx0ouZUxee5DBMgZq3I1+ZdxFSHG9g3tYl/FKU=
55
github.com/blinklabs-io/ouroboros-mock v0.3.8/go.mod h1:UwQIf4KqZwO13P9d90fbi3UL/X7JaJfeEbqk+bEeFQA=
6+
github.com/blinklabs-io/plutigo v0.0.1 h1:IBM3+dm7Sy92zgm9SBvIwVXN/73Jvg/ipDwbgRDeVFo=
7+
github.com/blinklabs-io/plutigo v0.0.1/go.mod h1:N7id3F9kjRcvm/BMxwZsBWUSJTGsUEwVRfFz3Tt5LPM=
8+
github.com/blinklabs-io/plutigo v0.0.2-0.20250716022656-7418e5ead692 h1:irMl0GGNv5heP8LpYBG4Z+a/RYqmmHVESZEa/Ba/67s=
9+
github.com/blinklabs-io/plutigo v0.0.2-0.20250716022656-7418e5ead692/go.mod h1:Bh2zD801zEN90MIFv78HF1d3c/RpG/GD18wQrbdeN+0=
610
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
711
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
812
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd/go.mod h1:nm3Bko6zh6bWP60UxwoT5LzdGJsQJaPo6HjduXq9p6A=

ledger/byron/byron.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ import (
1919
"errors"
2020
"fmt"
2121
"math"
22+
"math/big"
2223

2324
"github.com/blinklabs-io/gouroboros/cbor"
2425
"github.com/blinklabs-io/gouroboros/ledger/common"
26+
"github.com/blinklabs-io/plutigo/pkg/data"
2527
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2628
)
2729

@@ -377,6 +379,17 @@ func (i ByronTransactionInput) Utxorpc() (*utxorpc.TxInput, error) {
377379
}, nil
378380
}
379381

382+
func (i ByronTransactionInput) ToPlutusData() data.PlutusData {
383+
// This will never actually get called, but it's identical to Shelley
384+
return data.NewConstr(
385+
0,
386+
[]data.PlutusData{
387+
data.NewByteString(i.TxId.Bytes()),
388+
data.NewInteger(big.NewInt(int64(i.OutputIndex))),
389+
},
390+
)
391+
}
392+
380393
func (i ByronTransactionInput) String() string {
381394
return fmt.Sprintf("%s#%d", i.TxId, i.OutputIndex)
382395
}

ledger/common/tx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package common
1616

1717
import (
1818
"github.com/blinklabs-io/gouroboros/cbor"
19+
"github.com/blinklabs-io/plutigo/pkg/data"
1920
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2021
)
2122

@@ -61,6 +62,7 @@ type TransactionInput interface {
6162
Index() uint32
6263
String() string
6364
Utxorpc() (*utxorpc.TxInput, error)
65+
ToPlutusData() data.PlutusData
6466
}
6567

6668
type TransactionOutput interface {

ledger/shelley/shelley.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ import (
1919
"errors"
2020
"fmt"
2121
"math"
22+
"math/big"
2223

2324
"github.com/blinklabs-io/gouroboros/cbor"
2425
"github.com/blinklabs-io/gouroboros/ledger/common"
26+
"github.com/blinklabs-io/plutigo/pkg/data"
2527
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2628
)
2729

@@ -367,6 +369,16 @@ func (i ShelleyTransactionInput) Utxorpc() (*utxorpc.TxInput, error) {
367369
}, nil
368370
}
369371

372+
func (i ShelleyTransactionInput) ToPlutusData() data.PlutusData {
373+
return data.NewConstr(
374+
0,
375+
[]data.PlutusData{
376+
data.NewByteString(i.TxId.Bytes()),
377+
data.NewInteger(big.NewInt(int64(i.OutputIndex))),
378+
},
379+
)
380+
}
381+
370382
func (i ShelleyTransactionInput) String() string {
371383
return fmt.Sprintf("%s#%d", i.TxId, i.OutputIndex)
372384
}

ledger/shelley/tx_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package shelley_test
16+
17+
import (
18+
"encoding/hex"
19+
"math/big"
20+
"reflect"
21+
"testing"
22+
23+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
24+
"github.com/blinklabs-io/plutigo/pkg/data"
25+
)
26+
27+
func TestShelleyTransactionInputToPlutusData(t *testing.T) {
28+
testTxIdHex := "1639f61ed08f5e489dd64db20f86451a0db06e83d21ea39c73ea0a93b478a370"
29+
testTxOutputIdx := 2
30+
testInput := shelley.NewShelleyTransactionInput(
31+
testTxIdHex,
32+
testTxOutputIdx,
33+
)
34+
testTxId, err := hex.DecodeString(testTxIdHex)
35+
if err != nil {
36+
t.Fatalf("unexpected error: %s", err)
37+
}
38+
expectedData := data.NewConstr(
39+
0,
40+
[]data.PlutusData{
41+
data.NewByteString(testTxId),
42+
data.NewInteger(big.NewInt(int64(testTxOutputIdx))),
43+
},
44+
)
45+
tmpData := testInput.ToPlutusData()
46+
if !reflect.DeepEqual(tmpData, expectedData) {
47+
t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, expectedData)
48+
}
49+
}

0 commit comments

Comments
 (0)