2016-08-08 19:38:16 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-10-27 18:17:06 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-06-24 05:01:21 +02:00
|
|
|
"io"
|
2014-07-03 02:29:48 +02:00
|
|
|
|
2016-08-08 19:38:16 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2015-02-05 21:48:38 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-10-27 18:17:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TxIndexUnknown is the value returned for a transaction index that is unknown.
|
|
|
|
// This is typically because the transaction has not been inserted into a block
|
|
|
|
// yet.
|
|
|
|
const TxIndexUnknown = -1
|
|
|
|
|
|
|
|
// 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 Tx struct {
|
2016-10-13 21:56:12 +02:00
|
|
|
msgTx *wire.MsgTx // Underlying MsgTx
|
|
|
|
txHash *chainhash.Hash // Cached transaction hash
|
|
|
|
txHashWitness *chainhash.Hash // Cached transaction witness hash
|
2017-07-02 02:39:31 +02:00
|
|
|
txHasWitness *bool // If the transaction has witness data
|
2016-10-13 21:56:12 +02:00
|
|
|
txIndex int // Position within a block or TxIndexUnknown
|
2013-10-27 18:17:06 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 21:48:38 +01:00
|
|
|
// MsgTx returns the underlying wire.MsgTx for the transaction.
|
|
|
|
func (t *Tx) MsgTx() *wire.MsgTx {
|
2013-10-27 18:17:06 +01:00
|
|
|
// Return the cached transaction.
|
|
|
|
return t.msgTx
|
|
|
|
}
|
|
|
|
|
2016-08-08 19:38:16 +02:00
|
|
|
// Hash returns the hash of the transaction. This is equivalent to
|
|
|
|
// calling TxHash on the underlying wire.MsgTx, however it caches the
|
2013-10-27 18:17:06 +01:00
|
|
|
// result so subsequent calls are more efficient.
|
2016-08-08 19:38:16 +02:00
|
|
|
func (t *Tx) Hash() *chainhash.Hash {
|
2013-10-27 18:17:06 +01:00
|
|
|
// Return the cached hash if it has already been generated.
|
2016-08-08 19:38:16 +02:00
|
|
|
if t.txHash != nil {
|
|
|
|
return t.txHash
|
2013-10-27 18:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the hash and return it.
|
2016-08-08 19:38:16 +02:00
|
|
|
hash := t.msgTx.TxHash()
|
|
|
|
t.txHash = &hash
|
|
|
|
return &hash
|
2013-10-27 18:17:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:56:12 +02:00
|
|
|
// WitnessHash returns the witness hash (wtxid) of the transaction. This is
|
|
|
|
// equivalent to calling WitnessHash on the underlying wire.MsgTx, however it
|
|
|
|
// caches the result so subsequent calls are more efficient.
|
|
|
|
func (t *Tx) WitnessHash() *chainhash.Hash {
|
|
|
|
// Return the cached hash if it has already been generated.
|
|
|
|
if t.txHashWitness != nil {
|
|
|
|
return t.txHashWitness
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the hash and return it.
|
|
|
|
hash := t.msgTx.WitnessHash()
|
|
|
|
t.txHashWitness = &hash
|
|
|
|
return &hash
|
|
|
|
}
|
|
|
|
|
2017-07-02 02:39:31 +02:00
|
|
|
// HasWitness returns false if none of the inputs within the transaction
|
|
|
|
// contain witness data, true false otherwise. This equivalent to calling
|
|
|
|
// HasWitness on the underlying wire.MsgTx, however it caches the result so
|
|
|
|
// subsequent calls are more efficient.
|
|
|
|
func (t *Tx) HasWitness() bool {
|
|
|
|
if t.txHashWitness != nil {
|
|
|
|
return *t.txHasWitness
|
|
|
|
}
|
|
|
|
|
|
|
|
hasWitness := t.msgTx.HasWitness()
|
|
|
|
t.txHasWitness = &hasWitness
|
|
|
|
return hasWitness
|
|
|
|
}
|
|
|
|
|
2013-10-27 18:17:06 +01:00
|
|
|
// Index returns the saved index of the transaction within a block. This value
|
|
|
|
// will be TxIndexUnknown if it hasn't already explicitly been set.
|
|
|
|
func (t *Tx) Index() int {
|
|
|
|
return t.txIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIndex sets the index of the transaction in within a block.
|
|
|
|
func (t *Tx) SetIndex(index int) {
|
|
|
|
t.txIndex = index
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTx returns a new instance of a bitcoin transaction given an underlying
|
2015-02-05 21:48:38 +01:00
|
|
|
// wire.MsgTx. See Tx.
|
|
|
|
func NewTx(msgTx *wire.MsgTx) *Tx {
|
2013-10-27 18:17:06 +01:00
|
|
|
return &Tx{
|
|
|
|
msgTx: msgTx,
|
|
|
|
txIndex: TxIndexUnknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTxFromBytes returns a new instance of a bitcoin transaction given the
|
|
|
|
// serialized bytes. See Tx.
|
|
|
|
func NewTxFromBytes(serializedTx []byte) (*Tx, error) {
|
2014-06-24 05:01:21 +02:00
|
|
|
br := bytes.NewReader(serializedTx)
|
|
|
|
return NewTxFromReader(br)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTxFromReader returns a new instance of a bitcoin transaction given a
|
|
|
|
// Reader to deserialize the transaction. See Tx.
|
|
|
|
func NewTxFromReader(r io.Reader) (*Tx, error) {
|
2013-10-27 18:17:06 +01:00
|
|
|
// Deserialize the bytes into a MsgTx.
|
2015-02-05 21:48:38 +01:00
|
|
|
var msgTx wire.MsgTx
|
2014-06-24 05:01:21 +02:00
|
|
|
err := msgTx.Deserialize(r)
|
2013-10-27 18:17:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := Tx{
|
2014-06-24 05:01:21 +02:00
|
|
|
msgTx: &msgTx,
|
|
|
|
txIndex: TxIndexUnknown,
|
2013-10-27 18:17:06 +01:00
|
|
|
}
|
|
|
|
return &t, nil
|
|
|
|
}
|