Add normalized transaction hash

This commit is contained in:
janko33bd
2018-01-16 21:58:20 +01:00
parent 6484a9e9b2
commit 65e7103954
3 changed files with 24 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
#include "hash.h"
#include "tinyformat.h"
#include "utilstrencodings.h"
#include "script/interpreter.h"
std::string COutPoint::ToString() const
{
@@ -68,6 +69,11 @@ uint256 CMutableTransaction::GetHash() const
return SerializeHash(*this);
}
uint256 CMutableTransaction::GetNormalizedHash() const
{
return SignatureHash(CScript(), *this, 0, SIGHASH_ALL);
}
void CTransaction::UpdateHash() const
{
*const_cast<uint256*>(&hash) = SerializeHash(*this);

View File

@@ -338,6 +338,8 @@ struct CMutableTransaction
* fly, as opposed to GetHash() in CTransaction, which uses a cached result.
*/
uint256 GetHash() const;
uint256 GetNormalizedHash() const;
};
#endif // BITCOIN_PRIMITIVES_TRANSACTION_H

View File

@@ -676,6 +676,21 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
vErrorsRet.push_back(entry);
}
UniValue getnormalizedtxid(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"getnormalizedtxid <hex string>\n"
"Return the normalized transaction ID.");
// parse hex string from parameter
CTransaction tx;
if (!DecodeHexTx(tx, params[0].get_str()))
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
uint256 hashNormalized = CMutableTransaction(tx).GetNormalizedHash();
return hashNormalized.GetHex();
}
UniValue signrawtransaction(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 4)
@@ -1003,6 +1018,7 @@ static const CRPCCommand commands[] =
{ "rawtransactions", "decodescript", &decodescript, true },
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false },
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false }, /* uses wallet if enabled */
{ "rawtransactions", "getnormalizedtxid", &getnormalizedtxid, true },
{ "blockchain", "gettxoutproof", &gettxoutproof, true },
{ "blockchain", "verifytxoutproof", &verifytxoutproof, true },