Skip to content

Commit

Permalink
feat: support for parsing transaction output from CBOR
Browse files Browse the repository at this point in the history
Fixes #467
  • Loading branch information
agaffney committed Dec 30, 2023
1 parent cfd1b9d commit a265ec4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,11 @@ func NewAlonzoTransactionFromCbor(data []byte) (*AlonzoTransaction, error) {
}
return &alonzoTx, nil
}

func NewAlonzoTransactionOutputFromCbor(data []byte) (*AlonzoTransactionOutput, error) {
var alonzoTxOutput AlonzoTransactionOutput
if _, err := cbor.Decode(data, &alonzoTxOutput); err != nil {
return nil, fmt.Errorf("Alonzo transaction output decode error: %s", err)
}
return &alonzoTxOutput, nil
}
8 changes: 8 additions & 0 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,11 @@ func NewBabbageTransactionFromCbor(data []byte) (*BabbageTransaction, error) {
}
return &babbageTx, nil
}

func NewBabbageTransactionOutputFromCbor(data []byte) (*BabbageTransactionOutput, error) {
var babbageTxOutput BabbageTransactionOutput
if _, err := cbor.Decode(data, &babbageTxOutput); err != nil {
return nil, fmt.Errorf("Babbage transaction output decode error: %s", err)
}
return &babbageTxOutput, nil
}
8 changes: 8 additions & 0 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,11 @@ func NewMaryTransactionFromCbor(data []byte) (*MaryTransaction, error) {
}
return &maryTx, nil
}

func NewMaryTransactionOutputFromCbor(data []byte) (*MaryTransactionOutput, error) {
var maryTxOutput MaryTransactionOutput
if _, err := cbor.Decode(data, &maryTxOutput); err != nil {
return nil, fmt.Errorf("Mary transaction output decode error: %s", err)
}
return &maryTxOutput, nil
}
8 changes: 8 additions & 0 deletions ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,11 @@ func NewShelleyTransactionFromCbor(data []byte) (*ShelleyTransaction, error) {
}
return &shelleyTx, nil
}

func NewShelleyTransactionOutputFromCbor(data []byte) (*ShelleyTransactionOutput, error) {
var shelleyTxOutput ShelleyTransactionOutput
if _, err := cbor.Decode(data, &shelleyTxOutput); err != nil {
return nil, fmt.Errorf("Shelley transaction output decode error: %s", err)
}
return &shelleyTxOutput, nil
}
19 changes: 19 additions & 0 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ func NewTransactionBodyFromCbor(
return nil, fmt.Errorf("unknown transaction type: %d", txType)
}

// NewTransactionOutputFromCbor attempts to parse the provided arbitrary CBOR data as a transaction output from
// each of the eras, returning the first one that we can successfully decode
func NewTransactionOutputFromCbor(data []byte) (TransactionOutput, error) {
// TODO: add Byron transaction output support
if txOut, err := NewShelleyTransactionOutputFromCbor(data); err == nil {
return txOut, nil
}
if txOut, err := NewMaryTransactionOutputFromCbor(data); err == nil {
return txOut, nil
}
if txOut, err := NewAlonzoTransactionOutputFromCbor(data); err == nil {
return txOut, nil
}
if txOut, err := NewBabbageTransactionOutputFromCbor(data); err == nil {
return txOut, nil
}
return nil, fmt.Errorf("unknown transaction output type")
}

func DetermineTransactionType(data []byte) (uint, error) {
// TODO: uncomment this once the following issue is resolved:
// https://github.com/blinklabs-io/gouroboros/issues/206
Expand Down

0 comments on commit a265ec4

Please sign in to comment.