Skip to content

Commit

Permalink
Merge pull request #356 from blinklabs-io/feat/tx-from-cbor-interface
Browse files Browse the repository at this point in the history
feat: use interfaces for NewTransaction(Body)FromCbor functions
  • Loading branch information
agaffney authored Jul 3, 2023
2 parents 1c524f0 + 174b27f commit c42ab7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
45 changes: 32 additions & 13 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,42 @@ func (h *ByronMainBlockHeader) Era() Era {
return eras[ERA_ID_BYRON]
}

// TODO: flesh this out
type ByronTransactionBody interface{}
type ByronTransaction struct {
cbor.DecodeStoreCbor
// TODO: flesh these out
TxInputs []any
TxOutputs []any
Attributes cbor.Value
}

// TODO: flesh this out
type ByronTransaction interface{}
func (t *ByronTransaction) Hash() string {
// TODO
return ""
}

func (t *ByronTransaction) Inputs() []TransactionInput {
// TODO
return nil
}

func (t *ByronTransaction) Outputs() []TransactionOutput {
// TODO
return nil
}

func (t *ByronTransaction) Metadata() cbor.Value {
return t.Attributes
}

type ByronMainBlockBody struct {
cbor.StructAsArray
TxPayload []ByronTransactionBody
// TODO: split this to its own type
TxPayload []struct {
cbor.StructAsArray
Transaction ByronTransaction
// TODO: figure out what this field actually is
Twit []cbor.Value
}
SscPayload cbor.Value
DlgPayload []interface{}
UpdPayload []interface{}
Expand Down Expand Up @@ -257,14 +284,6 @@ func NewByronMainBlockHeaderFromCbor(data []byte) (*ByronMainBlockHeader, error)
return &byronMainBlockHeader, nil
}

func NewByronTransactionBodyFromCbor(data []byte) (*ByronTransactionBody, error) {
var byronTx ByronTransactionBody
if _, err := cbor.Decode(data, &byronTx); err != nil {
return nil, fmt.Errorf("Byron transaction body decode error: %s", err)
}
return &byronTx, nil
}

func NewByronTransactionFromCbor(data []byte) (*ByronTransaction, error) {
var byronTx ByronTransaction
if _, err := cbor.Decode(data, &byronTx); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type TransactionOutput interface {
DatumHash() *Blake2b256
}

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

func NewTransactionBodyFromCbor(txType uint, data []byte) (interface{}, error) {
func NewTransactionBodyFromCbor(txType uint, data []byte) (TransactionBody, error) {
switch txType {
case TX_TYPE_BYRON:
return NewByronTransactionBodyFromCbor(data)
return nil, fmt.Errorf("Byron transactions do not contain a body")
case TX_TYPE_SHELLEY:
return NewShelleyTransactionBodyFromCbor(data)
case TX_TYPE_ALLEGRA:
Expand Down

0 comments on commit c42ab7a

Please sign in to comment.