Skip to content

Commit

Permalink
feat: custom CBOR marshaling for TX outputs
Browse files Browse the repository at this point in the history
This adds custom marshaling functions to various TX output types to
match the existing custom unmarshal functions

Fixes #654
  • Loading branch information
agaffney committed Jun 11, 2024
1 parent d1de397 commit 29d79fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 15 additions & 2 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,36 @@ type AlonzoTransactionOutput struct {
OutputAddress Address
OutputAmount MaryTransactionOutputValue
TxOutputDatumHash *Blake2b256
legacyOutput bool
}

func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
// Save original CBOR
o.SetCbor(cborData)
// Try to parse as Mary output first
// Try to parse as legacy Mary output first
var tmpOutput MaryTransactionOutput
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
// Copy from temp Shelley output to Alonzo format
// Copy from temp Mary output to Alonzo format
o.OutputAddress = tmpOutput.OutputAddress
o.OutputAmount = tmpOutput.OutputAmount
o.legacyOutput = true
} else {
return cbor.DecodeGeneric(cborData, o)
}
return nil
}

func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) {
if o.legacyOutput {
tmpOutput := MaryTransactionOutput{
OutputAddress: o.OutputAddress,
OutputAmount: o.OutputAmount,
}
return cbor.Encode(&tmpOutput)
}
return cbor.EncodeGeneric(o)
}

func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
tmpObj := struct {
Address Address `json:"address"`
Expand Down
11 changes: 11 additions & 0 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
return nil
}

func (o *BabbageTransactionOutput) MarshalCBOR() ([]byte, error) {
if o.legacyOutput {
tmpOutput := AlonzoTransactionOutput{
OutputAddress: o.OutputAddress,
OutputAmount: o.OutputAmount,
}
return cbor.Encode(&tmpOutput)
}
return cbor.EncodeGeneric(o)
}

func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
tmpObj := struct {
Address Address `json:"address"`
Expand Down

0 comments on commit 29d79fe

Please sign in to comment.