From 215caba4ed4547d6f2a0954fa9fe1ae78f4a7c40 Mon Sep 17 00:00:00 2001 From: Pedro Branco Date: Wed, 19 Oct 2016 15:17:42 +0100 Subject: Add consistency check to RPC call importmulti --- src/wallet/rpcdump.cpp | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 0297337c2a..7b16b4adfb 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -641,9 +641,6 @@ UniValue dumpwallet(const JSONRPCRequest& request) UniValue processImport(const UniValue& data) { - // TODO List: - // - Check consistency between pubkeys/privkeys and scriptPubKey/redeemScript. - try { bool success = false; @@ -713,8 +710,6 @@ UniValue processImport(const UniValue& data) { // P2SH if (isP2SH) { - // TODO: check consistency between private keys and p2sh redeemscript + p2sh address - // Import redeem script. std::vector vData(ParseHex(strRedeemScript)); CScript redeemScript = CScript(vData.begin(), vData.end()); @@ -795,8 +790,6 @@ UniValue processImport(const UniValue& data) { success = true; } else { - // TODO: check consistency between private/public keys and scriptPubKey / address - // Import public keys. if (pubKeys.size() && keys.size() == 0) { const string& strPubKey = pubKeys[0].get_str(); @@ -813,6 +806,25 @@ UniValue processImport(const UniValue& data) { } CBitcoinAddress pubKeyAddress = CBitcoinAddress(pubKey.GetID()); + + // Consistency check. + if (!isScript && pubKeyAddress.Get() != address.Get()) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Consistency check failed"); + } + + // Consistency check. + if (isScript) { + CBitcoinAddress scriptAddress; + CTxDestination destination; + + if (ExtractDestination(script, destination)) { + scriptAddress = CBitcoinAddress(destination); + if (scriptAddress.Get() != pubKeyAddress.Get()) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Consistency check failed"); + } + } + } + CScript pubKeyScript = GetScriptForDestination(pubKeyAddress.Get()); if (::IsMine(*pwalletMain, pubKeyScript) == ISMINE_SPENDABLE) { @@ -866,7 +878,27 @@ UniValue processImport(const UniValue& data) { CPubKey pubKey = key.GetPubKey(); assert(key.VerifyPubKey(pubKey)); - CKeyID vchAddress = pubkey.GetID(); + CBitcoinAddress pubKeyAddress = CBitcoinAddress(pubKey.GetID()); + + // Consistency check. + if (!isScript && pubKeyAddress.Get() != address.Get()) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Consistency check failed"); + } + + // Consistency check. + if (isScript) { + CBitcoinAddress scriptAddress; + CTxDestination destination; + + if (ExtractDestination(script, destination)) { + scriptAddress = CBitcoinAddress(destination); + if (scriptAddress.Get() != pubKeyAddress.Get()) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Consistency check failed"); + } + } + } + + CKeyID vchAddress = pubKey.GetID(); pwalletMain->MarkDirty(); pwalletMain->SetAddressBook(vchAddress, label, "receive"); -- cgit v1.2.3