Skip to content

Commit

Permalink
btcutil: add utreexotx
Browse files Browse the repository at this point in the history
UtreexoTx is a wrapper around MsgUtreexoTx and adds helpful method just
like Tx adds helpful methods for MsgTx.
  • Loading branch information
kcalvinalvin committed Nov 1, 2024
1 parent a76f89e commit f653651
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions btcutil/utreexotx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2024 The utreexo developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package btcutil

import (
"bytes"
"io"

"github.com/utreexo/utreexod/wire"
)

// Tx defines a bitcoin transaction that provides easier and more efficient
// manipulation of raw transactions. It also memoizes the hash for the
// transaction on its first access so subsequent accesses don't have to repeat
// the relatively expensive hashing operations.
type UtreexoTx struct {
Tx
msgUtreexoTx *wire.MsgUtreexoTx
}

// MsgUtreexoTx returns the underlying wire.MsgUtreexoTx for the utreexo transaction.
func (t *UtreexoTx) MsgUtreexoTx() *wire.UData {
return &t.msgUtreexoTx.UData
}

// NewUtreexoTx returns a new instance of a bitcoin transaction given an underlying
// wire.MsgUtreexoTx. See UtreexoTx.
func NewUtreexoTx(msgUtreexoTx *wire.MsgUtreexoTx) *UtreexoTx {
return &UtreexoTx{
Tx: *NewTx(&msgUtreexoTx.MsgTx),
msgUtreexoTx: msgUtreexoTx,
}
}

// NewUtreexoTxFromBytes returns a new instance of a bitcoin utreexo transaction given the
// serialized bytes. See UtreexoTx.
func NewUtreexoTxFromBytes(serializedUtreexoTx []byte) (*UtreexoTx, error) {
br := bytes.NewReader(serializedUtreexoTx)
return NewUtreexoTxFromReader(br)
}

// NewUtreexoTxFromReader returns a new instance of a bitcoin transaction given a
// Reader to deserialize the transaction. See UtreexoTx.
func NewUtreexoTxFromReader(r io.Reader) (*UtreexoTx, error) {
// Deserialize the bytes into a MsgUtreexoTx.
var msgUtreexoTx wire.MsgUtreexoTx
err := msgUtreexoTx.Deserialize(r)
if err != nil {
return nil, err
}

tx := NewTx(&msgUtreexoTx.MsgTx)

t := UtreexoTx{
Tx: *tx,
msgUtreexoTx: &msgUtreexoTx,
}
return &t, nil
}

0 comments on commit f653651

Please sign in to comment.