From 65e710395448beeaa41ed5398906841d0799e4e8 Mon Sep 17 00:00:00 2001 From: janko33bd Date: Tue, 16 Jan 2018 21:58:20 +0100 Subject: [PATCH] Add normalized transaction hash --- src/primitives/transaction.cpp | 6 ++++++ src/primitives/transaction.h | 2 ++ src/rpc/rawtransaction.cpp | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index c3577dad4..faf4ca511 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -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(&hash) = SerializeHash(*this); diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index 50fdc0d6f..7735f0335 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -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 diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 063beca3c..83ce44a96 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -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 \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 },