Permalink
Browse files

Add GetTxid() which returns a non-malleable txid.

  • Loading branch information...
bitcartel committed Jul 22, 2016
1 parent 6dfc9e7 commit 49689a574cdef0d255c2bb4a8096603ee937fd08
Showing with 35 additions and 0 deletions.
  1. +28 −0 src/primitives/transaction.cpp
  2. +7 −0 src/primitives/transaction.h
@@ -223,3 +223,31 @@ std::string CTransaction::ToString() const
str += " " + vout[i].ToString() + "\n";
return str;
}

// Return a txid which is non-malleable.
// Signature data is cleared before the transaction is serialized and hashed.
uint256 CTransaction::GetTxid() const
{
// Create a deep copy of this transaction
CMutableTransaction tx(*this);

// Clear sigscript from all transaction inputs.
for (CTxIn & txIn : tx.vin) {
txIn.scriptSig.clear();
}

// Clear joinSplitSig by filling the buffer with zero
tx.joinSplitSig.assign(0);

// Return double SHA256 hash
return tx.GetHash();
}


// Return a txid which is non-malleable.
uint256 CMutableTransaction::GetTxid() const
{
CTransaction tx(*this);
return tx.GetTxid();
}

@@ -373,6 +373,10 @@ class CTransaction
}

std::string ToString() const;

// Return the txid which is the double SHA256 hash of the transaction.
uint256 GetTxid() const;

};

/** A mutable version of CTransaction. */
@@ -411,6 +415,9 @@ struct CMutableTransaction
* fly, as opposed to GetHash() in CTransaction, which uses a cached result.
*/
uint256 GetHash() const;

// Compute a non-malleable txid on the fly.
uint256 GetTxid() const;
};

#endif // BITCOIN_PRIMITIVES_TRANSACTION_H

0 comments on commit 49689a5

Please sign in to comment.