Skip to content

Commit 1a91b95

Browse files
authored
feat: common interface for accessing TX witnesses (#899)
Fixes #664 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 967978e commit 1a91b95

File tree

9 files changed

+184
-24
lines changed

9 files changed

+184
-24
lines changed

ledger/allegra/allegra.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -290,6 +290,10 @@ func (t AllegraTransaction) ProtocolParameterUpdates() (uint64, map[common.Blake
290290
return t.Body.ProtocolParameterUpdates()
291291
}
292292

293+
func (t AllegraTransaction) Witnesses() common.TransactionWitnessSet {
294+
return t.WitnessSet
295+
}
296+
293297
func (t *AllegraTransaction) Cbor() []byte {
294298
// Return stored CBOR if we have any
295299
cborData := t.DecodeStoreCbor.Cbor()

ledger/alonzo/alonzo.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -303,21 +303,54 @@ func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
303303

304304
type AlonzoRedeemer struct {
305305
cbor.StructAsArray
306-
Tag uint8
306+
Tag common.RedeemerTag
307307
Index uint32
308-
Data cbor.RawMessage
308+
Data cbor.LazyValue
309309
ExUnits common.RedeemerExUnits
310310
}
311311

312+
type AlonzoRedeemers []AlonzoRedeemer
313+
314+
func (r AlonzoRedeemers) Indexes(tag common.RedeemerTag) []uint {
315+
ret := []uint{}
316+
for _, redeemer := range r {
317+
if redeemer.Tag == tag {
318+
ret = append(ret, uint(redeemer.Index))
319+
}
320+
}
321+
return ret
322+
}
323+
324+
func (r AlonzoRedeemers) Value(index uint, tag common.RedeemerTag) (cbor.LazyValue, common.RedeemerExUnits) {
325+
for _, redeemer := range r {
326+
if redeemer.Tag == tag && uint(redeemer.Index) == index {
327+
return redeemer.Data, redeemer.ExUnits
328+
}
329+
}
330+
return cbor.LazyValue{}, common.RedeemerExUnits{}
331+
}
332+
312333
type AlonzoTransactionWitnessSet struct {
313334
shelley.ShelleyTransactionWitnessSet
314-
PlutusV1Scripts [][]byte `cbor:"3,keyasint,omitempty"`
315-
PlutusData []cbor.Value `cbor:"4,keyasint,omitempty"`
316-
Redeemers []AlonzoRedeemer `cbor:"5,keyasint,omitempty"`
335+
WsPlutusV1Scripts [][]byte `cbor:"3,keyasint,omitempty"`
336+
WsPlutusData []cbor.Value `cbor:"4,keyasint,omitempty"`
337+
WsRedeemers AlonzoRedeemers `cbor:"5,keyasint,omitempty"`
338+
}
339+
340+
func (w *AlonzoTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
341+
return w.UnmarshalCbor(cborData, w)
342+
}
343+
344+
func (w AlonzoTransactionWitnessSet) PlutusV1Scripts() [][]byte {
345+
return w.WsPlutusV1Scripts
317346
}
318347

319-
func (t *AlonzoTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
320-
return t.UnmarshalCbor(cborData, t)
348+
func (w AlonzoTransactionWitnessSet) PlutusData() []cbor.Value {
349+
return w.WsPlutusData
350+
}
351+
352+
func (w AlonzoTransactionWitnessSet) Redeemers() common.TransactionWitnessRedeemers {
353+
return w.WsRedeemers
321354
}
322355

323356
type AlonzoTransaction struct {
@@ -452,6 +485,10 @@ func (t AlonzoTransaction) Produced() []common.Utxo {
452485
}
453486
}
454487

488+
func (t AlonzoTransaction) Witnesses() common.TransactionWitnessSet {
489+
return t.WitnessSet
490+
}
491+
455492
func (t *AlonzoTransaction) Cbor() []byte {
456493
// Return stored CBOR if we have any
457494
cborData := t.DecodeStoreCbor.Cbor()

ledger/babbage/babbage.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -491,11 +491,15 @@ func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
491491

492492
type BabbageTransactionWitnessSet struct {
493493
alonzo.AlonzoTransactionWitnessSet
494-
PlutusV2Scripts [][]byte `cbor:"6,keyasint,omitempty"`
494+
WsPlutusV2Scripts [][]byte `cbor:"6,keyasint,omitempty"`
495495
}
496496

497-
func (t *BabbageTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
498-
return t.UnmarshalCbor(cborData, t)
497+
func (w *BabbageTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
498+
return w.UnmarshalCbor(cborData, w)
499+
}
500+
501+
func (w BabbageTransactionWitnessSet) PlutusV2Scripts() [][]byte {
502+
return w.WsPlutusV2Scripts
499503
}
500504

501505
type BabbageTransaction struct {
@@ -637,6 +641,10 @@ func (t BabbageTransaction) Produced() []common.Utxo {
637641
}
638642
}
639643

644+
func (t BabbageTransaction) Witnesses() common.TransactionWitnessSet {
645+
return t.WitnessSet
646+
}
647+
640648
func (t *BabbageTransaction) Cbor() []byte {
641649
// Return stored CBOR if we have any
642650
cborData := t.DecodeStoreCbor.Cbor()

ledger/byron/byron.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ func (t *ByronTransaction) Produced() []common.Utxo {
288288
return ret
289289
}
290290

291+
func (t ByronTransaction) Witnesses() common.TransactionWitnessSet {
292+
// TODO: implement once we properly support decoding Byron TX witnesses (#853)
293+
return nil
294+
}
295+
291296
func (t *ByronTransaction) Utxorpc() *utxorpc.Tx {
292297
return &utxorpc.Tx{}
293298
}

ledger/common/tx.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ type Transaction interface {
2828
IsValid() bool
2929
Consumed() []TransactionInput
3030
Produced() []Utxo
31+
Witnesses() TransactionWitnessSet
3132
}
3233

3334
type TransactionBody interface {
@@ -73,6 +74,22 @@ type TransactionOutput interface {
7374
Utxorpc() *utxorpc.TxOutput
7475
}
7576

77+
type TransactionWitnessSet interface {
78+
Vkey() []VkeyWitness
79+
NativeScripts() []NativeScript
80+
Bootstrap() []BootstrapWitness
81+
PlutusData() []cbor.Value
82+
PlutusV1Scripts() [][]byte
83+
PlutusV2Scripts() [][]byte
84+
PlutusV3Scripts() [][]byte
85+
Redeemers() TransactionWitnessRedeemers
86+
}
87+
88+
type TransactionWitnessRedeemers interface {
89+
Indexes(RedeemerTag) []uint
90+
Value(uint, RedeemerTag) (cbor.LazyValue, RedeemerExUnits)
91+
}
92+
7693
type Utxo struct {
7794
Id TransactionInput
7895
Output TransactionOutput

ledger/common/witness.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ import (
1818
"github.com/blinklabs-io/gouroboros/cbor"
1919
)
2020

21+
type RedeemerTag uint8
22+
23+
const (
24+
RedeemerTagSpend RedeemerTag = 0
25+
RedeemerTagMint RedeemerTag = 1
26+
RedeemerTagCert RedeemerTag = 2
27+
RedeemerTagReward RedeemerTag = 3
28+
RedeemerTagVoting RedeemerTag = 4
29+
RedeemerTagProposing RedeemerTag = 5
30+
)
31+
2132
type VkeyWitness struct {
2233
cbor.StructAsArray
2334
Vkey []byte

ledger/conway/conway.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -149,13 +149,13 @@ func (h *ConwayBlockHeader) Era() common.Era {
149149

150150
type ConwayRedeemerKey struct {
151151
cbor.StructAsArray
152-
Tag uint8
152+
Tag common.RedeemerTag
153153
Index uint32
154154
}
155155

156156
type ConwayRedeemerValue struct {
157157
cbor.StructAsArray
158-
Data cbor.RawMessage
158+
Data cbor.LazyValue
159159
ExUnits common.RedeemerExUnits
160160
}
161161

@@ -189,14 +189,43 @@ func (r *ConwayRedeemers) UnmarshalCBOR(cborData []byte) error {
189189
return nil
190190
}
191191

192+
func (r ConwayRedeemers) Indexes(tag common.RedeemerTag) []uint {
193+
ret := []uint{}
194+
for key := range r.Redeemers {
195+
if key.Tag == tag {
196+
ret = append(ret, uint(key.Index))
197+
}
198+
}
199+
return ret
200+
}
201+
202+
func (r ConwayRedeemers) Value(index uint, tag common.RedeemerTag) (cbor.LazyValue, common.RedeemerExUnits) {
203+
redeemer, ok := r.Redeemers[ConwayRedeemerKey{
204+
Tag: tag,
205+
Index: uint32(index),
206+
}]
207+
if ok {
208+
return redeemer.Data, redeemer.ExUnits
209+
}
210+
return cbor.LazyValue{}, common.RedeemerExUnits{}
211+
}
212+
192213
type ConwayTransactionWitnessSet struct {
193214
babbage.BabbageTransactionWitnessSet
194-
Redeemers ConwayRedeemers `cbor:"5,keyasint,omitempty"`
195-
PlutusV3Scripts [][]byte `cbor:"7,keyasint,omitempty"`
215+
WsRedeemers ConwayRedeemers `cbor:"5,keyasint,omitempty"`
216+
WsPlutusV3Scripts [][]byte `cbor:"7,keyasint,omitempty"`
217+
}
218+
219+
func (w *ConwayTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
220+
return w.UnmarshalCbor(cborData, w)
196221
}
197222

198-
func (t *ConwayTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
199-
return t.UnmarshalCbor(cborData, t)
223+
func (w ConwayTransactionWitnessSet) PlutusV3Scripts() [][]byte {
224+
return w.WsPlutusV3Scripts
225+
}
226+
227+
func (w ConwayTransactionWitnessSet) Redeemers() common.TransactionWitnessRedeemers {
228+
return w.WsRedeemers
200229
}
201230

202231
type ConwayTransactionInputSet struct {
@@ -401,6 +430,10 @@ func (t ConwayTransaction) Produced() []common.Utxo {
401430
}
402431
}
403432

433+
func (t ConwayTransaction) Witnesses() common.TransactionWitnessSet {
434+
return t.WitnessSet
435+
}
436+
404437
func (t *ConwayTransaction) Cbor() []byte {
405438
// Return stored CBOR if we have any
406439
cborData := t.DecodeStoreCbor.Cbor()

ledger/mary/mary.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -298,6 +298,10 @@ func (t MaryTransaction) Produced() []common.Utxo {
298298
return ret
299299
}
300300

301+
func (t MaryTransaction) Witnesses() common.TransactionWitnessSet {
302+
return t.WitnessSet
303+
}
304+
301305
func (t *MaryTransaction) Cbor() []byte {
302306
// Return stored CBOR if we have any
303307
cborData := t.DecodeStoreCbor.Cbor()

ledger/shelley/shelley.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -469,14 +469,51 @@ func (o ShelleyTransactionOutput) Utxorpc() *utxorpc.TxOutput {
469469
type ShelleyTransactionWitnessSet struct {
470470
cbor.DecodeStoreCbor
471471
VkeyWitnesses []common.VkeyWitness `cbor:"0,keyasint,omitempty"`
472-
NativeScripts []common.NativeScript `cbor:"1,keyasint,omitempty"`
472+
WsNativeScripts []common.NativeScript `cbor:"1,keyasint,omitempty"`
473473
BootstrapWitnesses []common.BootstrapWitness `cbor:"2,keyasint,omitempty"`
474474
}
475475

476476
func (t *ShelleyTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
477477
return t.UnmarshalCbor(cborData, t)
478478
}
479479

480+
func (w ShelleyTransactionWitnessSet) Vkey() []common.VkeyWitness {
481+
return w.VkeyWitnesses
482+
}
483+
484+
func (w ShelleyTransactionWitnessSet) Bootstrap() []common.BootstrapWitness {
485+
return w.BootstrapWitnesses
486+
}
487+
488+
func (w ShelleyTransactionWitnessSet) NativeScripts() []common.NativeScript {
489+
return w.WsNativeScripts
490+
}
491+
492+
func (w ShelleyTransactionWitnessSet) PlutusData() []cbor.Value {
493+
// No plutus data in Shelley
494+
return nil
495+
}
496+
497+
func (w ShelleyTransactionWitnessSet) PlutusV1Scripts() [][]byte {
498+
// No plutus v1 scripts in Shelley
499+
return nil
500+
}
501+
502+
func (w ShelleyTransactionWitnessSet) PlutusV2Scripts() [][]byte {
503+
// No plutus v2 scripts in Shelley
504+
return nil
505+
}
506+
507+
func (w ShelleyTransactionWitnessSet) PlutusV3Scripts() [][]byte {
508+
// No plutus v3 scripts in Shelley
509+
return nil
510+
}
511+
512+
func (w ShelleyTransactionWitnessSet) Redeemers() common.TransactionWitnessRedeemers {
513+
// No redeemers in Shelley
514+
return nil
515+
}
516+
480517
type ShelleyTransaction struct {
481518
cbor.StructAsArray
482519
cbor.DecodeStoreCbor
@@ -595,6 +632,10 @@ func (t ShelleyTransaction) Produced() []common.Utxo {
595632
return ret
596633
}
597634

635+
func (t ShelleyTransaction) Witnesses() common.TransactionWitnessSet {
636+
return t.WitnessSet
637+
}
638+
598639
func (t ShelleyTransaction) Utxorpc() *utxorpc.Tx {
599640
return t.Body.Utxorpc()
600641
}

0 commit comments

Comments
 (0)