Skip to content

Commit 2f1c60c

Browse files
authored
fix: CBOR encoding for TX inputs (#1062)
Fixes #1057 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent ba18252 commit 2f1c60c

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

ledger/conway/conway.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package conway
1616

1717
import (
18+
"errors"
1819
"fmt"
1920

2021
"github.com/blinklabs-io/gouroboros/cbor"
@@ -275,7 +276,8 @@ func (w ConwayTransactionWitnessSet) Redeemers() common.TransactionWitnessRedeem
275276
}
276277

277278
type ConwayTransactionInputSet struct {
278-
items []shelley.ShelleyTransactionInput
279+
useSet bool
280+
items []shelley.ShelleyTransactionInput
279281
}
280282

281283
func NewConwayTransactionInputSet(
@@ -288,7 +290,16 @@ func NewConwayTransactionInputSet(
288290
}
289291

290292
func (s *ConwayTransactionInputSet) UnmarshalCBOR(data []byte) error {
291-
// This overrides the Shelley behavior that explicitly disallowed tag-wrapped sets
293+
// Check if the set is wrapped in a CBOR tag
294+
// This is mostly needed so we can remember whether it was Set-wrapped for CBOR encoding
295+
var tmpTag cbor.RawTag
296+
if _, err := cbor.Decode(data, &tmpTag); err == nil {
297+
if tmpTag.Number != cbor.CborTagSet {
298+
return errors.New("unexpected tag type")
299+
}
300+
data = []byte(tmpTag.Content)
301+
s.useSet = true
302+
}
292303
var tmpData []shelley.ShelleyTransactionInput
293304
if _, err := cbor.Decode(data, &tmpData); err != nil {
294305
return err
@@ -297,6 +308,18 @@ func (s *ConwayTransactionInputSet) UnmarshalCBOR(data []byte) error {
297308
return nil
298309
}
299310

311+
func (s *ConwayTransactionInputSet) MarshalCBOR() ([]byte, error) {
312+
tmpItems := make([]any, len(s.items))
313+
for i, item := range s.items {
314+
tmpItems[i] = item
315+
}
316+
var tmpData any = tmpItems
317+
if s.useSet {
318+
tmpData = cbor.Set(tmpItems)
319+
}
320+
return cbor.Encode(tmpData)
321+
}
322+
300323
func (s *ConwayTransactionInputSet) Items() []shelley.ShelleyTransactionInput {
301324
return s.items
302325
}

ledger/shelley/shelley.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ func (s *ShelleyTransactionInputSet) UnmarshalCBOR(data []byte) error {
316316
return nil
317317
}
318318

319+
func (s *ShelleyTransactionInputSet) MarshalCBOR() ([]byte, error) {
320+
return cbor.Encode(s.items)
321+
}
322+
319323
func (s *ShelleyTransactionInputSet) Items() []ShelleyTransactionInput {
320324
return s.items
321325
}

0 commit comments

Comments
 (0)