Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose original CBOR for TX outputs #381

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/block-fetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func main() {
}
fmt.Printf(" Outputs:\n")
for _, output := range tx.Outputs() {
fmt.Printf(" Cbor (hex): %x\n", output.Cbor())
fmt.Printf(" Address: %s\n", output.Address())
fmt.Printf(" Amount: %d\n", output.Amount())
assets := output.Assets()
Expand Down
13 changes: 10 additions & 3 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,31 @@ func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
func (b *AlonzoTransactionBody) Outputs() []TransactionOutput {
ret := []TransactionOutput{}
for _, output := range b.TxOutputs {
ret = append(ret, output)
output := output
ret = append(ret, &output)
}
return ret
}

type AlonzoTransactionOutput struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
cborData []byte
OutputAddress Address
OutputAmount MaryTransactionOutputValue
TxOutputDatumHash *Blake2b256
}

func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
// Save original CBOR
o.cborData = cborData[:]
// Try to parse as Mary output first
var tmpOutput MaryTransactionOutput
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
// Copy from temp Shelley output to Alonzo format
o.OutputAddress = tmpOutput.OutputAddress
o.OutputAmount = tmpOutput.OutputAmount
} else {
return o.UnmarshalCbor(cborData, o)
return cbor.DecodeGeneric(cborData, o)
}
return nil
}
Expand All @@ -149,6 +152,10 @@ func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
return json.Marshal(&tmpObj)
}

func (o *AlonzoTransactionOutput) Cbor() []byte {
return o.cborData
}

func (o AlonzoTransactionOutput) Address() Address {
return o.OutputAddress
}
Expand Down
10 changes: 9 additions & 1 deletion ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
func (b *BabbageTransactionBody) Outputs() []TransactionOutput {
ret := []TransactionOutput{}
for _, output := range b.TxOutputs {
ret = append(ret, output)
output := output
ret = append(ret, &output)
}
return ret
}
Expand Down Expand Up @@ -214,6 +215,7 @@ func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {
}

type BabbageTransactionOutput struct {
cborData []byte
OutputAddress Address `cbor:"0,keyasint,omitempty"`
OutputAmount MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
DatumOption *BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"`
Expand All @@ -222,6 +224,8 @@ type BabbageTransactionOutput struct {
}

func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
// Save original CBOR
o.cborData = cborData[:]
// Try to parse as legacy output first
var tmpOutput AlonzoTransactionOutput
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
Expand Down Expand Up @@ -258,6 +262,10 @@ func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
return json.Marshal(&tmpObj)
}

func (o *BabbageTransactionOutput) Cbor() []byte {
return o.cborData
}

func (o BabbageTransactionOutput) Address() Address {
return o.OutputAddress
}
Expand Down
4 changes: 3 additions & 1 deletion ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (b *MaryTransactionBody) UnmarshalCBOR(cborData []byte) error {
func (b *MaryTransactionBody) Outputs() []TransactionOutput {
ret := []TransactionOutput{}
for _, output := range b.TxOutputs {
ret = append(ret, output)
output := output
ret = append(ret, &output)
}
return ret
}
Expand Down Expand Up @@ -125,6 +126,7 @@ func (t MaryTransaction) Metadata() *cbor.Value {

type MaryTransactionOutput struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
OutputAddress Address
OutputAmount MaryTransactionOutputValue
}
Expand Down
4 changes: 3 additions & 1 deletion ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ func (b *ShelleyTransactionBody) Inputs() []TransactionInput {
func (b *ShelleyTransactionBody) Outputs() []TransactionOutput {
ret := []TransactionOutput{}
for _, output := range b.TxOutputs {
ret = append(ret, output)
output := output
ret = append(ret, &output)
}
return ret
}
Expand Down Expand Up @@ -192,6 +193,7 @@ func (i ShelleyTransactionInput) MarshalJSON() ([]byte, error) {

type ShelleyTransactionOutput struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
OutputAddress Address `json:"address"`
OutputAmount uint64 `json:"amount"`
}
Expand Down
1 change: 1 addition & 0 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type TransactionOutput interface {
Assets() *MultiAsset[MultiAssetTypeOutput]
Datum() *cbor.LazyValue
DatumHash() *Blake2b256
Cbor() []byte
}

func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) {
Expand Down
Loading