Skip to content

Commit 1a2ae72

Browse files
authored
Merge pull request #468 from blinklabs-io/feat/parse-utxo
feat: support for parsing transaction output from CBOR
2 parents cfd1b9d + a265ec4 commit 1a2ae72

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

ledger/alonzo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,11 @@ func NewAlonzoTransactionFromCbor(data []byte) (*AlonzoTransaction, error) {
276276
}
277277
return &alonzoTx, nil
278278
}
279+
280+
func NewAlonzoTransactionOutputFromCbor(data []byte) (*AlonzoTransactionOutput, error) {
281+
var alonzoTxOutput AlonzoTransactionOutput
282+
if _, err := cbor.Decode(data, &alonzoTxOutput); err != nil {
283+
return nil, fmt.Errorf("Alonzo transaction output decode error: %s", err)
284+
}
285+
return &alonzoTxOutput, nil
286+
}

ledger/babbage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,11 @@ func NewBabbageTransactionFromCbor(data []byte) (*BabbageTransaction, error) {
406406
}
407407
return &babbageTx, nil
408408
}
409+
410+
func NewBabbageTransactionOutputFromCbor(data []byte) (*BabbageTransactionOutput, error) {
411+
var babbageTxOutput BabbageTransactionOutput
412+
if _, err := cbor.Decode(data, &babbageTxOutput); err != nil {
413+
return nil, fmt.Errorf("Babbage transaction output decode error: %s", err)
414+
}
415+
return &babbageTxOutput, nil
416+
}

ledger/mary.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,11 @@ func NewMaryTransactionFromCbor(data []byte) (*MaryTransaction, error) {
254254
}
255255
return &maryTx, nil
256256
}
257+
258+
func NewMaryTransactionOutputFromCbor(data []byte) (*MaryTransactionOutput, error) {
259+
var maryTxOutput MaryTransactionOutput
260+
if _, err := cbor.Decode(data, &maryTxOutput); err != nil {
261+
return nil, fmt.Errorf("Mary transaction output decode error: %s", err)
262+
}
263+
return &maryTxOutput, nil
264+
}

ledger/shelley.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,11 @@ func NewShelleyTransactionFromCbor(data []byte) (*ShelleyTransaction, error) {
344344
}
345345
return &shelleyTx, nil
346346
}
347+
348+
func NewShelleyTransactionOutputFromCbor(data []byte) (*ShelleyTransactionOutput, error) {
349+
var shelleyTxOutput ShelleyTransactionOutput
350+
if _, err := cbor.Decode(data, &shelleyTxOutput); err != nil {
351+
return nil, fmt.Errorf("Shelley transaction output decode error: %s", err)
352+
}
353+
return &shelleyTxOutput, nil
354+
}

ledger/tx.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ func NewTransactionBodyFromCbor(
9393
return nil, fmt.Errorf("unknown transaction type: %d", txType)
9494
}
9595

96+
// NewTransactionOutputFromCbor attempts to parse the provided arbitrary CBOR data as a transaction output from
97+
// each of the eras, returning the first one that we can successfully decode
98+
func NewTransactionOutputFromCbor(data []byte) (TransactionOutput, error) {
99+
// TODO: add Byron transaction output support
100+
if txOut, err := NewShelleyTransactionOutputFromCbor(data); err == nil {
101+
return txOut, nil
102+
}
103+
if txOut, err := NewMaryTransactionOutputFromCbor(data); err == nil {
104+
return txOut, nil
105+
}
106+
if txOut, err := NewAlonzoTransactionOutputFromCbor(data); err == nil {
107+
return txOut, nil
108+
}
109+
if txOut, err := NewBabbageTransactionOutputFromCbor(data); err == nil {
110+
return txOut, nil
111+
}
112+
return nil, fmt.Errorf("unknown transaction output type")
113+
}
114+
96115
func DetermineTransactionType(data []byte) (uint, error) {
97116
// TODO: uncomment this once the following issue is resolved:
98117
// https://github.com/blinklabs-io/gouroboros/issues/206

0 commit comments

Comments
 (0)