Skip to content

Commit c42ab7a

Browse files
authored
Merge pull request #356 from blinklabs-io/feat/tx-from-cbor-interface
feat: use interfaces for NewTransaction(Body)FromCbor functions
2 parents 1c524f0 + 174b27f commit c42ab7a

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

ledger/byron.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,42 @@ func (h *ByronMainBlockHeader) Era() Era {
100100
return eras[ERA_ID_BYRON]
101101
}
102102

103-
// TODO: flesh this out
104-
type ByronTransactionBody interface{}
103+
type ByronTransaction struct {
104+
cbor.DecodeStoreCbor
105+
// TODO: flesh these out
106+
TxInputs []any
107+
TxOutputs []any
108+
Attributes cbor.Value
109+
}
105110

106-
// TODO: flesh this out
107-
type ByronTransaction interface{}
111+
func (t *ByronTransaction) Hash() string {
112+
// TODO
113+
return ""
114+
}
115+
116+
func (t *ByronTransaction) Inputs() []TransactionInput {
117+
// TODO
118+
return nil
119+
}
120+
121+
func (t *ByronTransaction) Outputs() []TransactionOutput {
122+
// TODO
123+
return nil
124+
}
125+
126+
func (t *ByronTransaction) Metadata() cbor.Value {
127+
return t.Attributes
128+
}
108129

109130
type ByronMainBlockBody struct {
110131
cbor.StructAsArray
111-
TxPayload []ByronTransactionBody
132+
// TODO: split this to its own type
133+
TxPayload []struct {
134+
cbor.StructAsArray
135+
Transaction ByronTransaction
136+
// TODO: figure out what this field actually is
137+
Twit []cbor.Value
138+
}
112139
SscPayload cbor.Value
113140
DlgPayload []interface{}
114141
UpdPayload []interface{}
@@ -257,14 +284,6 @@ func NewByronMainBlockHeaderFromCbor(data []byte) (*ByronMainBlockHeader, error)
257284
return &byronMainBlockHeader, nil
258285
}
259286

260-
func NewByronTransactionBodyFromCbor(data []byte) (*ByronTransactionBody, error) {
261-
var byronTx ByronTransactionBody
262-
if _, err := cbor.Decode(data, &byronTx); err != nil {
263-
return nil, fmt.Errorf("Byron transaction body decode error: %s", err)
264-
}
265-
return &byronTx, nil
266-
}
267-
268287
func NewByronTransactionFromCbor(data []byte) (*ByronTransaction, error) {
269288
var byronTx ByronTransaction
270289
if _, err := cbor.Decode(data, &byronTx); err != nil {

ledger/tx.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type TransactionOutput interface {
4747
DatumHash() *Blake2b256
4848
}
4949

50-
func NewTransactionFromCbor(txType uint, data []byte) (interface{}, error) {
50+
func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) {
5151
switch txType {
5252
case TX_TYPE_BYRON:
5353
return NewByronTransactionFromCbor(data)
@@ -65,10 +65,10 @@ func NewTransactionFromCbor(txType uint, data []byte) (interface{}, error) {
6565
return nil, fmt.Errorf("unknown transaction type: %d", txType)
6666
}
6767

68-
func NewTransactionBodyFromCbor(txType uint, data []byte) (interface{}, error) {
68+
func NewTransactionBodyFromCbor(txType uint, data []byte) (TransactionBody, error) {
6969
switch txType {
7070
case TX_TYPE_BYRON:
71-
return NewByronTransactionBodyFromCbor(data)
71+
return nil, fmt.Errorf("Byron transactions do not contain a body")
7272
case TX_TYPE_SHELLEY:
7373
return NewShelleyTransactionBodyFromCbor(data)
7474
case TX_TYPE_ALLEGRA:

0 commit comments

Comments
 (0)