diff --git a/tx.go b/tx.go index 7399ca0..13c05e4 100644 --- a/tx.go +++ b/tx.go @@ -25,6 +25,7 @@ type Tx struct { msgTx *wire.MsgTx // Underlying MsgTx txHash *chainhash.Hash // Cached transaction hash txHashWitness *chainhash.Hash // Cached transaction witness hash + txHasWitness *bool // If the transaction has witness data txIndex int // Position within a block or TxIndexUnknown } @@ -64,6 +65,20 @@ func (t *Tx) WitnessHash() *chainhash.Hash { return &hash } +// 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 +} + // 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 {