|
| 1 | +// Copyright (c) 2024 The utreexo developers |
| 2 | +// Use of this source code is governed by an ISC |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package btcutil |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "io" |
| 10 | + |
| 11 | + "github.com/utreexo/utreexod/wire" |
| 12 | +) |
| 13 | + |
| 14 | +// UtreexoTx defines a utreexo bitcoin transaction that provides easier and more |
| 15 | +// efficient manipulation of raw transactions. It also memoizes the hash for the |
| 16 | +// transaction on its first access so subsequent accesses don't have to repeat |
| 17 | +// the relatively expensive hashing operations. |
| 18 | +type UtreexoTx struct { |
| 19 | + Tx |
| 20 | + msgUtreexoTx *wire.MsgUtreexoTx |
| 21 | +} |
| 22 | + |
| 23 | +// MsgUtreexoTx returns the underlying wire.MsgUtreexoTx for the utreexo transaction. |
| 24 | +func (t *UtreexoTx) MsgUtreexoTx() *wire.MsgUtreexoTx { |
| 25 | + return t.msgUtreexoTx |
| 26 | +} |
| 27 | + |
| 28 | +// NewUtreexoTx returns a new instance of a bitcoin transaction given an underlying |
| 29 | +// wire.MsgUtreexoTx. See UtreexoTx. |
| 30 | +func NewUtreexoTx(msgUtreexoTx *wire.MsgUtreexoTx) *UtreexoTx { |
| 31 | + return &UtreexoTx{ |
| 32 | + Tx: *NewTx(&msgUtreexoTx.MsgTx), |
| 33 | + msgUtreexoTx: msgUtreexoTx, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// NewUtreexoTxFromBytes returns a new instance of a bitcoin utreexo transaction given the |
| 38 | +// serialized bytes. See UtreexoTx. |
| 39 | +func NewUtreexoTxFromBytes(serializedUtreexoTx []byte) (*UtreexoTx, error) { |
| 40 | + br := bytes.NewReader(serializedUtreexoTx) |
| 41 | + return NewUtreexoTxFromReader(br) |
| 42 | +} |
| 43 | + |
| 44 | +// NewUtreexoTxFromReader returns a new instance of a bitcoin transaction given a |
| 45 | +// Reader to deserialize the transaction. See UtreexoTx. |
| 46 | +func NewUtreexoTxFromReader(r io.Reader) (*UtreexoTx, error) { |
| 47 | + // Deserialize the bytes into a MsgUtreexoTx. |
| 48 | + var msgUtreexoTx wire.MsgUtreexoTx |
| 49 | + err := msgUtreexoTx.Deserialize(r) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + tx := NewTx(&msgUtreexoTx.MsgTx) |
| 55 | + |
| 56 | + t := UtreexoTx{ |
| 57 | + Tx: *tx, |
| 58 | + msgUtreexoTx: &msgUtreexoTx, |
| 59 | + } |
| 60 | + return &t, nil |
| 61 | +} |
0 commit comments