Skip to content

Commit d99f377

Browse files
authored
feat: define TX witness types (#897)
Fixes #891 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 3fe0648 commit d99f377

File tree

9 files changed

+143
-20
lines changed

9 files changed

+143
-20
lines changed

ledger/alonzo/alonzo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ type AlonzoRedeemer struct {
311311

312312
type AlonzoTransactionWitnessSet struct {
313313
shelley.ShelleyTransactionWitnessSet
314-
PlutusScripts []cbor.RawMessage `cbor:"3,keyasint,omitempty"`
315-
PlutusData []cbor.RawMessage `cbor:"4,keyasint,omitempty"`
316-
Redeemers []AlonzoRedeemer `cbor:"5,keyasint,omitempty"`
314+
PlutusV1Scripts [][]byte `cbor:"3,keyasint,omitempty"`
315+
PlutusData []cbor.Value `cbor:"4,keyasint,omitempty"`
316+
Redeemers []AlonzoRedeemer `cbor:"5,keyasint,omitempty"`
317317
}
318318

319319
func (t *AlonzoTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {

ledger/babbage/babbage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
491491

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

497497
func (t *BabbageTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {

ledger/babbage/babbage_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ func TestBabbageBlockTransactions(t *testing.T) {
4848
b.TransactionWitnessSets[i] = BabbageTransactionWitnessSet{
4949
AlonzoTransactionWitnessSet: alonzo.AlonzoTransactionWitnessSet{
5050
ShelleyTransactionWitnessSet: shelley.ShelleyTransactionWitnessSet{
51-
VkeyWitnesses: []interface{}{
52-
append(make([]byte, 95), 1<<i),
51+
VkeyWitnesses: []common.VkeyWitness{
52+
{
53+
Vkey: append(make([]byte, 31), 1<<i)[:],
54+
Signature: append(make([]byte, 63), 1<<i)[:],
55+
},
5356
},
5457
},
5558
},

ledger/common/native_script.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 common
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/cbor"
21+
)
22+
23+
type NativeScript struct {
24+
item any
25+
}
26+
27+
func (n *NativeScript) Item() any {
28+
return n.item
29+
}
30+
31+
func (n *NativeScript) UnmarshalCBOR(data []byte) error {
32+
id, err := cbor.DecodeIdFromList(data)
33+
if err != nil {
34+
return err
35+
}
36+
var tmpData any
37+
switch id {
38+
case 0:
39+
tmpData = &NativeScriptPubkey{}
40+
case 1:
41+
tmpData = &NativeScriptAll{}
42+
case 2:
43+
tmpData = &NativeScriptAny{}
44+
case 3:
45+
tmpData = &NativeScriptNofK{}
46+
case 4:
47+
tmpData = &NativeScriptInvalidBefore{}
48+
case 5:
49+
tmpData = &NativeScriptInvalidHereafter{}
50+
default:
51+
return fmt.Errorf("unknown native script type %d", id)
52+
}
53+
if _, err := cbor.Decode(data, tmpData); err != nil {
54+
return err
55+
}
56+
return nil
57+
}
58+
59+
type NativeScriptPubkey struct {
60+
cbor.StructAsArray
61+
Type uint
62+
Hash []byte
63+
}
64+
65+
type NativeScriptAll struct {
66+
cbor.StructAsArray
67+
Type uint
68+
Scripts []NativeScript
69+
}
70+
71+
type NativeScriptAny struct {
72+
cbor.StructAsArray
73+
Type uint
74+
Scripts []NativeScript
75+
}
76+
77+
type NativeScriptNofK struct {
78+
cbor.StructAsArray
79+
Type uint
80+
N uint
81+
Scripts []NativeScript
82+
}
83+
84+
type NativeScriptInvalidBefore struct {
85+
cbor.StructAsArray
86+
Type uint
87+
Slot uint64
88+
}
89+
90+
type NativeScriptInvalidHereafter struct {
91+
cbor.StructAsArray
92+
Type uint
93+
Slot uint64
94+
}

ledger/common/witness.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 common
16+
17+
import (
18+
"github.com/blinklabs-io/gouroboros/cbor"
19+
)
20+
21+
type VkeyWitness struct {
22+
cbor.StructAsArray
23+
Vkey []byte
24+
Signature []byte
25+
}
26+
27+
type BootstrapWitness struct {
28+
cbor.StructAsArray
29+
PublicKey []byte
30+
Signature []byte
31+
ChainCode []byte
32+
Attributes []byte
33+
}

ledger/conway/conway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ func (r *ConwayRedeemers) UnmarshalCBOR(cborData []byte) error {
191191

192192
type ConwayTransactionWitnessSet struct {
193193
babbage.BabbageTransactionWitnessSet
194-
Redeemers ConwayRedeemers `cbor:"5,keyasint,omitempty"`
195-
PlutusV3Scripts []cbor.RawMessage `cbor:"7,keyasint,omitempty"`
194+
Redeemers ConwayRedeemers `cbor:"5,keyasint,omitempty"`
195+
PlutusV3Scripts [][]byte `cbor:"7,keyasint,omitempty"`
196196
}
197197

198198
func (t *ConwayTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {

ledger/mary.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type MaryTransaction = mary.MaryTransaction
2626
type MaryTransactionBody = mary.MaryTransactionBody
2727
type MaryTransactionOutput = mary.MaryTransactionOutput
2828
type MaryTransactionOutputValue = mary.MaryTransactionOutputValue
29-
type MaryTransactionWitnessSet = mary.MaryTransactionWitnessSet
3029
type MaryProtocolParameters = mary.MaryProtocolParameters
3130
type MaryProtocolParameterUpdate = mary.MaryProtocolParameterUpdate
3231

ledger/mary/mary.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type MaryBlock struct {
5454
cbor.DecodeStoreCbor
5555
BlockHeader *MaryBlockHeader
5656
TransactionBodies []MaryTransactionBody
57-
TransactionWitnessSets []MaryTransactionWitnessSet
57+
TransactionWitnessSets []shelley.ShelleyTransactionWitnessSet
5858
TransactionMetadataSet map[uint]*cbor.LazyValue
5959
}
6060

@@ -180,20 +180,14 @@ type MaryTransaction struct {
180180
cbor.StructAsArray
181181
cbor.DecodeStoreCbor
182182
Body MaryTransactionBody
183-
WitnessSet MaryTransactionWitnessSet
183+
WitnessSet shelley.ShelleyTransactionWitnessSet
184184
TxMetadata *cbor.LazyValue
185185
}
186186

187187
func (MaryTransaction) Type() int {
188188
return TxTypeMary
189189
}
190190

191-
type MaryTransactionWitnessSet struct {
192-
shelley.ShelleyTransactionWitnessSet
193-
Script []interface{} `cbor:"4,keyasint,omitempty"`
194-
PlutusScripts []interface{} `cbor:"5,keyasint,omitempty"`
195-
}
196-
197191
func (t MaryTransaction) Hash() string {
198192
return t.Body.Hash()
199193
}

ledger/shelley/shelley.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,9 @@ func (o ShelleyTransactionOutput) Utxorpc() *utxorpc.TxOutput {
468468

469469
type ShelleyTransactionWitnessSet struct {
470470
cbor.DecodeStoreCbor
471-
VkeyWitnesses []interface{} `cbor:"0,keyasint,omitempty"`
472-
MultisigScripts []interface{} `cbor:"1,keyasint,omitempty"`
473-
BootstrapWitnesses []interface{} `cbor:"2,keyasint,omitempty"`
471+
VkeyWitnesses []common.VkeyWitness `cbor:"0,keyasint,omitempty"`
472+
NativeScripts []common.NativeScript `cbor:"1,keyasint,omitempty"`
473+
BootstrapWitnesses []common.BootstrapWitness `cbor:"2,keyasint,omitempty"`
474474
}
475475

476476
func (t *ShelleyTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {

0 commit comments

Comments
 (0)