@@ -215,7 +215,7 @@ func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {
215215}
216216
217217type BabbageTransactionOutput struct {
218- cborData [] byte
218+ cbor. DecodeStoreCbor
219219 OutputAddress Address `cbor:"0,keyasint,omitempty"`
220220 OutputAmount MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
221221 DatumOption * BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"`
@@ -225,7 +225,7 @@ type BabbageTransactionOutput struct {
225225
226226func (o * BabbageTransactionOutput ) UnmarshalCBOR (cborData []byte ) error {
227227 // Save original CBOR
228- o .cborData = cborData [:]
228+ o .SetCbor ( cborData )
229229 // Try to parse as legacy output first
230230 var tmpOutput AlonzoTransactionOutput
231231 if _ , err := cbor .Decode (cborData , & tmpOutput ); err == nil {
@@ -262,10 +262,6 @@ func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
262262 return json .Marshal (& tmpObj )
263263}
264264
265- func (o * BabbageTransactionOutput ) Cbor () []byte {
266- return o .cborData
267- }
268-
269265func (o BabbageTransactionOutput ) Address () Address {
270266 return o .OutputAddress
271267}
@@ -297,6 +293,10 @@ type BabbageTransactionWitnessSet struct {
297293 PlutusV2Scripts []cbor.RawMessage `cbor:"6,keyasint,omitempty"`
298294}
299295
296+ func (t * BabbageTransactionWitnessSet ) UnmarshalCBOR (cborData []byte ) error {
297+ return t .UnmarshalCbor (cborData , t )
298+ }
299+
300300type BabbageTransaction struct {
301301 cbor.StructAsArray
302302 cbor.DecodeStoreCbor
@@ -322,6 +322,33 @@ func (t BabbageTransaction) Metadata() *cbor.Value {
322322 return t .TxMetadata
323323}
324324
325+ func (t * BabbageTransaction ) Cbor () []byte {
326+ // Return stored CBOR if we have any
327+ cborData := t .DecodeStoreCbor .Cbor ()
328+ if cborData != nil {
329+ return cborData [:]
330+ }
331+ // Return immediately if the body CBOR is also empty, which implies an empty TX object
332+ if t .Body .Cbor () == nil {
333+ return nil
334+ }
335+ // Generate our own CBOR
336+ // This is necessary when a transaction is put together from pieces stored separately in a block
337+ tmpObj := []any {
338+ cbor .RawMessage (t .Body .Cbor ()),
339+ cbor .RawMessage (t .WitnessSet .Cbor ()),
340+ t .IsValid ,
341+ }
342+ if t .TxMetadata != nil {
343+ tmpObj = append (tmpObj , cbor .RawMessage (t .TxMetadata .Cbor ()))
344+ } else {
345+ tmpObj = append (tmpObj , nil )
346+ }
347+ // This should never fail, since we're only encoding a list and a bool value
348+ cborData , _ = cbor .Encode (& tmpObj )
349+ return cborData
350+ }
351+
325352func NewBabbageBlockFromCbor (data []byte ) (* BabbageBlock , error ) {
326353 var babbageBlock BabbageBlock
327354 if _ , err := cbor .Decode (data , & babbageBlock ); err != nil {
0 commit comments