From 1d01c657dd1168193abea47bb46756510957f522 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 18 Oct 2016 18:07:09 -0700 Subject: [PATCH] BIP0141+wire: add a WitnessHash method to tx --- wire/msgtx.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/wire/msgtx.go b/wire/msgtx.go index a7784433..af8abf61 100644 --- a/wire/msgtx.go +++ b/wire/msgtx.go @@ -314,6 +314,21 @@ func (msg *MsgTx) TxHash() chainhash.Hash { return chainhash.DoubleHashH(buf.Bytes()) } +// WitnessHash generates the hash of the transaction serialized according to +// the new witness serialization defined in BIP0141 and BIP0144. The final +// output is used within the Segregated Witness commitment of all the witnesses +// within a block. If a transaction has no witness data, then the witness hash, +// is the same as its txid. +func (msg *MsgTx) WitnessHash() chainhash.Hash { + if msg.HasWitness() { + buf := bytes.NewBuffer(make([]byte, 0, msg.SerializeSize())) + _ = msg.Serialize(buf) + return chainhash.DoubleHashH(buf.Bytes()) + } + + return msg.TxHash() +} + // Copy creates a deep copy of a transaction so that the original does not get // modified when the copy is manipulated. func (msg *MsgTx) Copy() *MsgTx {