Skip to content

Commit 84f31d4

Browse files
committed
fix: prevent Conway TX inputs from being decoded as Shelley TX input
Conway-era TX inputs using CBOR tag 258 (sets) could be implicitly decoded by earlier eras, but this doesn't match the behavior of cardano-node. We explicitly prevent this from happening to allow proper identification of TX types
1 parent 0b3d6ee commit 84f31d4

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

ledger/conway/conway.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ func (t *ConwayTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
199199
return t.UnmarshalCbor(cborData, t)
200200
}
201201

202+
type ConwayTransactionInput struct {
203+
shelley.ShelleyTransactionInput
204+
}
205+
206+
func NewConwayTransactionInput(hash string, idx int) ConwayTransactionInput {
207+
tmpHash, err := hex.DecodeString(hash)
208+
if err != nil {
209+
panic(fmt.Sprintf("failed to decode transaction hash: %s", err))
210+
}
211+
return ConwayTransactionInput{
212+
ShelleyTransactionInput: shelley.ShelleyTransactionInput{
213+
TxId: common.Blake2b256(tmpHash),
214+
OutputIndex: uint32(idx),
215+
},
216+
}
217+
}
218+
219+
func (i *ConwayTransactionInput) UnmarshalCBOR(data []byte) error {
220+
// We override the unmarshal function from ShelleyTransactionInput
221+
return cbor.DecodeGeneric(data, &i.ShelleyTransactionInput)
222+
}
223+
202224
type ConwayTransactionBody struct {
203225
babbage.BabbageTransactionBody
204226
TxVotingProcedures common.VotingProcedures `cbor:"19,keyasint,omitempty"`

ledger/shelley/shelley.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,23 @@ func (i ShelleyTransactionInput) Utxorpc() *utxorpc.TxInput {
386386
return &utxorpc.TxInput{
387387
TxHash: i.TxId.Bytes(),
388388
OutputIndex: i.OutputIndex,
389-
// AsOutput: i.AsOutput,
390-
// Redeemer: i.Redeemer,
391389
}
392390
}
393391

394392
func (i ShelleyTransactionInput) String() string {
395393
return fmt.Sprintf("%s#%d", i.TxId, i.OutputIndex)
396394
}
397395

396+
func (i *ShelleyTransactionInput) UnmarshalCBOR(data []byte) error {
397+
// Make sure this isn't a tag-wrapped set
398+
// This is needed to prevent Conway+ TXs from being decoded as an earlier type
399+
var tmpTag cbor.RawTag
400+
if _, err := cbor.Decode(data, &tmpTag); err == nil {
401+
return fmt.Errorf("did not expect CBOR tag")
402+
}
403+
return cbor.DecodeGeneric(data, i)
404+
}
405+
398406
func (i ShelleyTransactionInput) MarshalJSON() ([]byte, error) {
399407
return []byte("\"" + i.String() + "\""), nil
400408
}

0 commit comments

Comments
 (0)