Skip to content

Commit 27d60b6

Browse files
authored
Merge pull request #381 from blinklabs-io/feat/ledger-tx-output-cbor
feat: expose original CBOR for TX outputs
2 parents 1a6de6c + 91cf83b commit 27d60b6

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

cmd/block-fetch/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func main() {
100100
}
101101
fmt.Printf(" Outputs:\n")
102102
for _, output := range tx.Outputs() {
103+
fmt.Printf(" Cbor (hex): %x\n", output.Cbor())
103104
fmt.Printf(" Address: %s\n", output.Address())
104105
fmt.Printf(" Amount: %d\n", output.Amount())
105106
assets := output.Assets()

ledger/alonzo.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,31 @@ func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
106106
func (b *AlonzoTransactionBody) Outputs() []TransactionOutput {
107107
ret := []TransactionOutput{}
108108
for _, output := range b.TxOutputs {
109-
ret = append(ret, output)
109+
output := output
110+
ret = append(ret, &output)
110111
}
111112
return ret
112113
}
113114

114115
type AlonzoTransactionOutput struct {
115116
cbor.StructAsArray
116-
cbor.DecodeStoreCbor
117+
cborData []byte
117118
OutputAddress Address
118119
OutputAmount MaryTransactionOutputValue
119120
TxOutputDatumHash *Blake2b256
120121
}
121122

122123
func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
124+
// Save original CBOR
125+
o.cborData = cborData[:]
123126
// Try to parse as Mary output first
124127
var tmpOutput MaryTransactionOutput
125128
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
126129
// Copy from temp Shelley output to Alonzo format
127130
o.OutputAddress = tmpOutput.OutputAddress
128131
o.OutputAmount = tmpOutput.OutputAmount
129132
} else {
130-
return o.UnmarshalCbor(cborData, o)
133+
return cbor.DecodeGeneric(cborData, o)
131134
}
132135
return nil
133136
}
@@ -149,6 +152,10 @@ func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
149152
return json.Marshal(&tmpObj)
150153
}
151154

155+
func (o *AlonzoTransactionOutput) Cbor() []byte {
156+
return o.cborData
157+
}
158+
152159
func (o AlonzoTransactionOutput) Address() Address {
153160
return o.OutputAddress
154161
}

ledger/babbage.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
150150
func (b *BabbageTransactionBody) Outputs() []TransactionOutput {
151151
ret := []TransactionOutput{}
152152
for _, output := range b.TxOutputs {
153-
ret = append(ret, output)
153+
output := output
154+
ret = append(ret, &output)
154155
}
155156
return ret
156157
}
@@ -214,6 +215,7 @@ func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {
214215
}
215216

216217
type BabbageTransactionOutput struct {
218+
cborData []byte
217219
OutputAddress Address `cbor:"0,keyasint,omitempty"`
218220
OutputAmount MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
219221
DatumOption *BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"`
@@ -222,6 +224,8 @@ type BabbageTransactionOutput struct {
222224
}
223225

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

265+
func (o *BabbageTransactionOutput) Cbor() []byte {
266+
return o.cborData
267+
}
268+
261269
func (o BabbageTransactionOutput) Address() Address {
262270
return o.OutputAddress
263271
}

ledger/mary.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ func (b *MaryTransactionBody) UnmarshalCBOR(cborData []byte) error {
9494
func (b *MaryTransactionBody) Outputs() []TransactionOutput {
9595
ret := []TransactionOutput{}
9696
for _, output := range b.TxOutputs {
97-
ret = append(ret, output)
97+
output := output
98+
ret = append(ret, &output)
9899
}
99100
return ret
100101
}
@@ -125,6 +126,7 @@ func (t MaryTransaction) Metadata() *cbor.Value {
125126

126127
type MaryTransactionOutput struct {
127128
cbor.StructAsArray
129+
cbor.DecodeStoreCbor
128130
OutputAddress Address
129131
OutputAmount MaryTransactionOutputValue
130132
}

ledger/shelley.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ func (b *ShelleyTransactionBody) Inputs() []TransactionInput {
163163
func (b *ShelleyTransactionBody) Outputs() []TransactionOutput {
164164
ret := []TransactionOutput{}
165165
for _, output := range b.TxOutputs {
166-
ret = append(ret, output)
166+
output := output
167+
ret = append(ret, &output)
167168
}
168169
return ret
169170
}
@@ -192,6 +193,7 @@ func (i ShelleyTransactionInput) MarshalJSON() ([]byte, error) {
192193

193194
type ShelleyTransactionOutput struct {
194195
cbor.StructAsArray
196+
cbor.DecodeStoreCbor
195197
OutputAddress Address `json:"address"`
196198
OutputAmount uint64 `json:"amount"`
197199
}

ledger/tx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type TransactionOutput interface {
4545
Assets() *MultiAsset[MultiAssetTypeOutput]
4646
Datum() *cbor.LazyValue
4747
DatumHash() *Blake2b256
48+
Cbor() []byte
4849
}
4950

5051
func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) {

0 commit comments

Comments
 (0)