From 8ede6dea0c55bb4afefa790b83dd4da48a2f84da Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Fri, 5 Jan 2024 18:42:22 -0500 Subject: [PATCH] wallet, rpc: Remove legacy wallet only RPCs --- src/qt/rpcconsole.cpp | 3 - src/qt/test/rpcnestedtests.cpp | 8 - src/rpc/client.cpp | 10 - src/test/fuzz/rpc.cpp | 2 - src/wallet/rpc/addresses.cpp | 150 ---- src/wallet/rpc/backup.cpp | 1321 ------------------------------ src/wallet/rpc/spend.cpp | 8 +- src/wallet/rpc/transactions.cpp | 4 +- src/wallet/rpc/util.cpp | 22 - src/wallet/rpc/util.h | 2 - src/wallet/rpc/wallet.cpp | 92 +-- src/wallet/test/wallet_tests.cpp | 3 - 12 files changed, 7 insertions(+), 1618 deletions(-) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 9d7c17ac91..2a688e9eac 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -72,9 +72,6 @@ namespace { // don't add private key handling cmd's to the history const QStringList historyFilter = QStringList() - << "importprivkey" - << "importmulti" - << "sethdseed" << "signmessagewithprivkey" << "signrawtransactionwithkey" << "walletpassphrase" diff --git a/src/qt/test/rpcnestedtests.cpp b/src/qt/test/rpcnestedtests.cpp index eb74ef8559..0857a4ebd5 100644 --- a/src/qt/test/rpcnestedtests.cpp +++ b/src/qt/test/rpcnestedtests.cpp @@ -85,8 +85,6 @@ void RPCNestedTests::rpcNestedTests() QVERIFY(result == "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]"); - RPCConsole::RPCParseCommandLine(nullptr, result, "importprivkey", false, &filtered); - QVERIFY(filtered == "importprivkey(…)"); RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc", false, &filtered); QVERIFY(filtered == "signmessagewithprivkey(…)"); RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc,def", false, &filtered); @@ -99,12 +97,6 @@ void RPCNestedTests::rpcNestedTests() QVERIFY(filtered == "walletpassphrasechange(…)"); RPCConsole::RPCParseCommandLine(nullptr, result, "help(encryptwallet(abc, def))", false, &filtered); QVERIFY(filtered == "help(encryptwallet(…))"); - RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey())", false, &filtered); - QVERIFY(filtered == "help(importprivkey(…))"); - RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey(help()))", false, &filtered); - QVERIFY(filtered == "help(importprivkey(…))"); - RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey(abc), walletpassphrase(def))", false, &filtered); - QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))"); RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest"); QVERIFY(result == "[]"); diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 1b711e3c5b..08bd1142fc 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -48,7 +48,6 @@ static const CRPCConvertParam vRPCConvertParams[] = { "sendtoaddress", 9, "fee_rate"}, { "sendtoaddress", 10, "verbose"}, { "settxfee", 0, "amount" }, - { "sethdseed", 0, "newkeypool" }, { "getreceivedbyaddress", 1, "minconf" }, { "getreceivedbyaddress", 2, "include_immature_coinbase" }, { "getreceivedbylabel", 1, "minconf" }, @@ -96,8 +95,6 @@ static const CRPCConvertParam vRPCConvertParams[] = { "getdescriptoractivity", 1, "scanobjects" }, { "getdescriptoractivity", 2, "include_mempool" }, { "scantxoutset", 1, "scanobjects" }, - { "addmultisigaddress", 0, "nrequired" }, - { "addmultisigaddress", 1, "keys" }, { "createmultisig", 0, "nrequired" }, { "createmultisig", 1, "keys" }, { "listunspent", 0, "minconf" }, @@ -236,17 +233,10 @@ static const CRPCConvertParam vRPCConvertParams[] = { "simulaterawtransaction", 0, "rawtxs" }, { "simulaterawtransaction", 1, "options" }, { "simulaterawtransaction", 1, "include_watchonly"}, - { "importprivkey", 2, "rescan" }, - { "importaddress", 2, "rescan" }, - { "importaddress", 3, "p2sh" }, - { "importpubkey", 2, "rescan" }, { "importmempool", 1, "options" }, { "importmempool", 1, "apply_fee_delta_priority" }, { "importmempool", 1, "use_current_time" }, { "importmempool", 1, "apply_unbroadcast_set" }, - { "importmulti", 0, "requests" }, - { "importmulti", 1, "options" }, - { "importmulti", 1, "rescan" }, { "importdescriptors", 0, "requests" }, { "listdescriptors", 0, "private" }, { "verifychain", 0, "checklevel" }, diff --git a/src/test/fuzz/rpc.cpp b/src/test/fuzz/rpc.cpp index 7bf90b9b77..580a6338a8 100644 --- a/src/test/fuzz/rpc.cpp +++ b/src/test/fuzz/rpc.cpp @@ -75,14 +75,12 @@ const std::vector RPC_COMMANDS_NOT_SAFE_FOR_FUZZING{ "addnode", // avoid DNS lookups "addpeeraddress", // avoid DNS lookups "dumptxoutset", // avoid writing to disk - "dumpwallet", // avoid writing to disk "enumeratesigners", "echoipc", // avoid assertion failure (Assertion `"EnsureAnyNodeContext(request.context).init" && check' failed.) "generatetoaddress", // avoid prohibitively slow execution (when `num_blocks` is large) "generatetodescriptor", // avoid prohibitively slow execution (when `nblocks` is large) "gettxoutproof", // avoid prohibitively slow execution "importmempool", // avoid reading from disk - "importwallet", // avoid reading from disk "loadtxoutset", // avoid reading from disk "loadwallet", // avoid reading from disk "savemempool", // disabled as a precautionary measure: may take a file path argument in the future diff --git a/src/wallet/rpc/addresses.cpp b/src/wallet/rpc/addresses.cpp index 1c2951deee..5937f4dfa9 100644 --- a/src/wallet/rpc/addresses.cpp +++ b/src/wallet/rpc/addresses.cpp @@ -215,124 +215,6 @@ RPCHelpMan listaddressgroupings() }; } -RPCHelpMan addmultisigaddress() -{ - return RPCHelpMan{"addmultisigaddress", - "\nAdd an nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.\n" - "Each key is a Bitcoin address or hex-encoded public key.\n" - "This functionality is only intended for use with non-watchonly addresses.\n" - "See `importaddress` for watchonly p2sh address support.\n" - "If 'label' is specified, assign address to that label.\n" - "Note: This command is only compatible with legacy wallets.\n", - { - {"nrequired", RPCArg::Type::NUM, RPCArg::Optional::NO, "The number of required signatures out of the n keys or addresses."}, - {"keys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The bitcoin addresses or hex-encoded public keys", - { - {"key", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "bitcoin address or hex-encoded public key"}, - }, - }, - {"label", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "A label to assign the addresses to."}, - {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -addresstype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."}, - }, - RPCResult{ - RPCResult::Type::OBJ, "", "", - { - {RPCResult::Type::STR, "address", "The value of the new multisig address"}, - {RPCResult::Type::STR_HEX, "redeemScript", "The string value of the hex-encoded redemption script"}, - {RPCResult::Type::STR, "descriptor", "The descriptor for this multisig"}, - {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Any warnings resulting from the creation of this multisig", - { - {RPCResult::Type::STR, "", ""}, - }}, - } - }, - RPCExamples{ - "\nAdd a multisig address from 2 addresses\n" - + HelpExampleCli("addmultisigaddress", "2 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"") + - "\nAs a JSON-RPC call\n" - + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet); - - LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore); - - const std::string label{LabelFromValue(request.params[2])}; - - int required = request.params[0].getInt(); - - // Get the public keys - const UniValue& keys_or_addrs = request.params[1].get_array(); - std::vector pubkeys; - for (unsigned int i = 0; i < keys_or_addrs.size(); ++i) { - if (IsHex(keys_or_addrs[i].get_str()) && (keys_or_addrs[i].get_str().length() == 66 || keys_or_addrs[i].get_str().length() == 130)) { - pubkeys.push_back(HexToPubKey(keys_or_addrs[i].get_str())); - } else { - pubkeys.push_back(AddrToPubKey(spk_man, keys_or_addrs[i].get_str())); - } - } - - OutputType output_type = pwallet->m_default_address_type; - if (!request.params[3].isNull()) { - std::optional parsed = ParseOutputType(request.params[3].get_str()); - if (!parsed) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[3].get_str())); - } else if (parsed.value() == OutputType::BECH32M) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m multisig addresses cannot be created with legacy wallets"); - } - output_type = parsed.value(); - } - - // Construct multisig scripts - FlatSigningProvider provider; - CScript inner; - CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, provider, inner); - - // Import scripts into the wallet - for (const auto& [id, script] : provider.scripts) { - // Due to a bug in the legacy wallet, the p2sh maximum script size limit is also imposed on 'p2sh-segwit' and 'bech32' redeem scripts. - // Even when redeem scripts over MAX_SCRIPT_ELEMENT_SIZE bytes are valid for segwit output types, we don't want to - // enable it because: - // 1) It introduces a compatibility-breaking change requiring downgrade protection; older wallets would be unable to interact with these "new" legacy wallets. - // 2) Considering the ongoing deprecation of the legacy spkm, this issue adds another good reason to transition towards descriptors. - if (script.size() > MAX_SCRIPT_ELEMENT_SIZE) throw JSONRPCError(RPC_WALLET_ERROR, "Unsupported multisig script size for legacy wallet. Upgrade to descriptors to overcome this limitation for p2sh-segwit or bech32 scripts"); - - if (!spk_man.AddCScript(script)) { - if (CScript inner_script; spk_man.GetCScript(CScriptID(script), inner_script)) { - CHECK_NONFATAL(inner_script == script); // Nothing to add, script already contained by the wallet - continue; - } - throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error importing script into the wallet")); - } - } - - // Store destination in the addressbook - pwallet->SetAddressBook(dest, label, AddressPurpose::SEND); - - // Make the descriptor - std::unique_ptr descriptor = InferDescriptor(GetScriptForDestination(dest), spk_man); - - UniValue result(UniValue::VOBJ); - result.pushKV("address", EncodeDestination(dest)); - result.pushKV("redeemScript", HexStr(inner)); - result.pushKV("descriptor", descriptor->ToString()); - - UniValue warnings(UniValue::VARR); - if (descriptor->GetOutputType() != output_type) { - // Only warns if the user has explicitly chosen an address type we cannot generate - warnings.push_back("Unable to make chosen address type, please ensure no uncompressed public keys are present."); - } - PushWarnings(warnings, result); - - return result; -}, - }; -} - RPCHelpMan keypoolrefill() { return RPCHelpMan{"keypoolrefill", @@ -377,38 +259,6 @@ RPCHelpMan keypoolrefill() }; } -RPCHelpMan newkeypool() -{ - return RPCHelpMan{"newkeypool", - "\nEntirely clears and refills the keypool.\n" - "WARNING: On non-HD wallets, this will require a new backup immediately, to include the new keys.\n" - "When restoring a backup of an HD wallet created before the newkeypool command is run, funds received to\n" - "new addresses may not appear automatically. They have not been lost, but the wallet may not find them.\n" - "This can be fixed by running the newkeypool command on the backup and then rescanning, so the wallet\n" - "re-generates the required keys." + - HELP_REQUIRING_PASSPHRASE, - {}, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{ - HelpExampleCli("newkeypool", "") - + HelpExampleRpc("newkeypool", "") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - LOCK(pwallet->cs_wallet); - - LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet, true); - spk_man.NewKeyPool(); - - return UniValue::VNULL; -}, - }; -} - - class DescribeWalletAddressVisitor { public: diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp index 39d8509007..d5b1ddb9fb 100644 --- a/src/wallet/rpc/backup.cpp +++ b/src/wallet/rpc/backup.cpp @@ -2,8 +2,6 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include // IWYU pragma: keep - #include #include #include @@ -34,290 +32,8 @@ using interfaces::FoundBlock; -using util::SplitString; namespace wallet { -std::string static EncodeDumpString(const std::string &str) { - std::stringstream ret; - for (const unsigned char c : str) { - if (c <= 32 || c >= 128 || c == '%') { - ret << '%' << HexStr({&c, 1}); - } else { - ret << c; - } - } - return ret.str(); -} - -static std::string DecodeDumpString(const std::string &str) { - std::stringstream ret; - for (unsigned int pos = 0; pos < str.length(); pos++) { - unsigned char c = str[pos]; - if (c == '%' && pos+2 < str.length()) { - c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) | - ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15)); - pos += 2; - } - ret << c; - } - return ret.str(); -} - -static bool GetWalletAddressesForKey(const LegacyScriptPubKeyMan* spk_man, const CWallet& wallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) -{ - bool fLabelFound = false; - CKey key; - spk_man->GetKey(keyid, key); - for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) { - const auto* address_book_entry = wallet.FindAddressBookEntry(dest); - if (address_book_entry) { - if (!strAddr.empty()) { - strAddr += ","; - } - strAddr += EncodeDestination(dest); - strLabel = EncodeDumpString(address_book_entry->GetLabel()); - fLabelFound = true; - } - } - if (!fLabelFound) { - strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), wallet.m_default_address_type)); - } - return fLabelFound; -} - -static const int64_t TIMESTAMP_MIN = 0; - -static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver, int64_t time_begin = TIMESTAMP_MIN, bool update = true) -{ - int64_t scanned_time = wallet.RescanFromTime(time_begin, reserver, update); - if (wallet.IsAbortingRescan()) { - throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user."); - } else if (scanned_time > time_begin) { - throw JSONRPCError(RPC_WALLET_ERROR, "Rescan was unable to fully rescan the blockchain. Some transactions may be missing."); - } -} - -static void EnsureBlockDataFromTime(const CWallet& wallet, int64_t timestamp) -{ - auto& chain{wallet.chain()}; - if (!chain.havePruned()) { - return; - } - - int height{0}; - const bool found{chain.findFirstBlockWithTimeAndHeight(timestamp - TIMESTAMP_WINDOW, 0, FoundBlock().height(height))}; - - uint256 tip_hash{WITH_LOCK(wallet.cs_wallet, return wallet.GetLastBlockHash())}; - if (found && !chain.hasBlocks(tip_hash, height)) { - throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Pruned blocks from height %d required to import keys. Use RPC call getblockchaininfo to determine your pruned height.", height)); - } -} - -RPCHelpMan importprivkey() -{ - return RPCHelpMan{"importprivkey", - "\nAdds a private key (as returned by dumpprivkey) to your wallet. Requires a new wallet backup.\n" - "Hint: use importmulti to import more than one private key.\n" - "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n" - "may report that the imported key exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n" - "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n" - "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n" - "Note: Use \"getwalletinfo\" to query the scanning progress.\n" - "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" with \"combo(X)\" for descriptor wallets.\n", - { - {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key (see dumpprivkey)"}, - {"label", RPCArg::Type::STR, RPCArg::DefaultHint{"current label if address exists, otherwise \"\""}, "An optional label"}, - {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."}, - }, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{ - "\nDump a private key\n" - + HelpExampleCli("dumpprivkey", "\"myaddress\"") + - "\nImport the private key with rescan\n" - + HelpExampleCli("importprivkey", "\"mykey\"") + - "\nImport using a label and without rescan\n" - + HelpExampleCli("importprivkey", "\"mykey\" \"testing\" false") + - "\nImport using default blank label and without rescan\n" - + HelpExampleCli("importprivkey", "\"mykey\" \"\" false") + - "\nAs a JSON-RPC call\n" - + HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled"); - } - - EnsureLegacyScriptPubKeyMan(*pwallet, true); - - WalletRescanReserver reserver(*pwallet); - bool fRescan = true; - { - LOCK(pwallet->cs_wallet); - - EnsureWalletIsUnlocked(*pwallet); - - std::string strSecret = request.params[0].get_str(); - const std::string strLabel{LabelFromValue(request.params[1])}; - - // Whether to perform rescan after import - if (!request.params[2].isNull()) - fRescan = request.params[2].get_bool(); - - if (fRescan && pwallet->chain().havePruned()) { - // Exit early and print an error. - // If a block is pruned after this check, we will import the key(s), - // but fail the rescan with a generic error. - throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned"); - } - - if (fRescan && !reserver.reserve()) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); - } - - CKey key = DecodeSecret(strSecret); - if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding"); - - CPubKey pubkey = key.GetPubKey(); - CHECK_NONFATAL(key.VerifyPubKey(pubkey)); - CKeyID vchAddress = pubkey.GetID(); - { - pwallet->MarkDirty(); - - // We don't know which corresponding address will be used; - // label all new addresses, and label existing addresses if a - // label was passed. - for (const auto& dest : GetAllDestinationsForKey(pubkey)) { - if (!request.params[1].isNull() || !pwallet->FindAddressBookEntry(dest)) { - pwallet->SetAddressBook(dest, strLabel, AddressPurpose::RECEIVE); - } - } - - // Use timestamp of 1 to scan the whole chain - if (!pwallet->ImportPrivKeys({{vchAddress, key}}, 1)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet"); - } - - // Add the wpkh script for this key if possible - if (pubkey.IsCompressed()) { - pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))}, /*timestamp=*/0); - } - } - } - if (fRescan) { - RescanWallet(*pwallet, reserver); - } - - return UniValue::VNULL; -}, - }; -} - -RPCHelpMan importaddress() -{ - return RPCHelpMan{"importaddress", - "\nAdds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n" - "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n" - "may report that the imported address exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n" - "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n" - "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n" - "If you have the full public key, you should call importpubkey instead of this.\n" - "Hint: use importmulti to import more than one address.\n" - "\nNote: If you import a non-standard raw script in hex form, outputs sending to it will be treated\n" - "as change, and not show up in many RPCs.\n" - "Note: Use \"getwalletinfo\" to query the scanning progress.\n" - "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" for descriptor wallets.\n", - { - {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Bitcoin address (or hex-encoded script)"}, - {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"}, - {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."}, - {"p2sh", RPCArg::Type::BOOL, RPCArg::Default{false}, "Add the P2SH version of the script as well"}, - }, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{ - "\nImport an address with rescan\n" - + HelpExampleCli("importaddress", "\"myaddress\"") + - "\nImport using a label without rescan\n" - + HelpExampleCli("importaddress", "\"myaddress\" \"testing\" false") + - "\nAs a JSON-RPC call\n" - + HelpExampleRpc("importaddress", "\"myaddress\", \"testing\", false") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - EnsureLegacyScriptPubKeyMan(*pwallet, true); - - const std::string strLabel{LabelFromValue(request.params[1])}; - - // Whether to perform rescan after import - bool fRescan = true; - if (!request.params[2].isNull()) - fRescan = request.params[2].get_bool(); - - if (fRescan && pwallet->chain().havePruned()) { - // Exit early and print an error. - // If a block is pruned after this check, we will import the key(s), - // but fail the rescan with a generic error. - throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned"); - } - - WalletRescanReserver reserver(*pwallet); - if (fRescan && !reserver.reserve()) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); - } - - // Whether to import a p2sh version, too - bool fP2SH = false; - if (!request.params[3].isNull()) - fP2SH = request.params[3].get_bool(); - - { - LOCK(pwallet->cs_wallet); - - CTxDestination dest = DecodeDestination(request.params[0].get_str()); - if (IsValidDestination(dest)) { - if (fP2SH) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead"); - } - if (OutputTypeFromDestination(dest) == OutputType::BECH32M) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m addresses cannot be imported into legacy wallets"); - } - - pwallet->MarkDirty(); - - pwallet->ImportScriptPubKeys(strLabel, {GetScriptForDestination(dest)}, /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1); - } else if (IsHex(request.params[0].get_str())) { - std::vector data(ParseHex(request.params[0].get_str())); - CScript redeem_script(data.begin(), data.end()); - - std::set scripts = {redeem_script}; - pwallet->ImportScripts(scripts, /*timestamp=*/0); - - if (fP2SH) { - scripts.insert(GetScriptForDestination(ScriptHash(redeem_script))); - } - - pwallet->ImportScriptPubKeys(strLabel, scripts, /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1); - } else { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script"); - } - } - if (fRescan) - { - RescanWallet(*pwallet, reserver); - pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true); - } - - return UniValue::VNULL; -}, - }; -} - RPCHelpMan importprunedfunds() { return RPCHelpMan{"importprunedfunds", @@ -406,842 +122,6 @@ RPCHelpMan removeprunedfunds() }; } -RPCHelpMan importpubkey() -{ - return RPCHelpMan{"importpubkey", - "\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n" - "Hint: use importmulti to import more than one public key.\n" - "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n" - "may report that the imported pubkey exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n" - "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n" - "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n" - "Note: Use \"getwalletinfo\" to query the scanning progress.\n" - "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" with \"combo(X)\" for descriptor wallets.\n", - { - {"pubkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The hex-encoded public key"}, - {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"}, - {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."}, - }, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{ - "\nImport a public key with rescan\n" - + HelpExampleCli("importpubkey", "\"mypubkey\"") + - "\nImport using a label without rescan\n" - + HelpExampleCli("importpubkey", "\"mypubkey\" \"testing\" false") + - "\nAs a JSON-RPC call\n" - + HelpExampleRpc("importpubkey", "\"mypubkey\", \"testing\", false") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - EnsureLegacyScriptPubKeyMan(*pwallet, true); - - const std::string strLabel{LabelFromValue(request.params[1])}; - - // Whether to perform rescan after import - bool fRescan = true; - if (!request.params[2].isNull()) - fRescan = request.params[2].get_bool(); - - if (fRescan && pwallet->chain().havePruned()) { - // Exit early and print an error. - // If a block is pruned after this check, we will import the key(s), - // but fail the rescan with a generic error. - throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned"); - } - - WalletRescanReserver reserver(*pwallet); - if (fRescan && !reserver.reserve()) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); - } - - CPubKey pubKey = HexToPubKey(request.params[0].get_str()); - - { - LOCK(pwallet->cs_wallet); - - std::set script_pub_keys; - for (const auto& dest : GetAllDestinationsForKey(pubKey)) { - script_pub_keys.insert(GetScriptForDestination(dest)); - } - - pwallet->MarkDirty(); - - pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, /*have_solving_data=*/true, /*apply_label=*/true, /*timestamp=*/1); - - pwallet->ImportPubKeys({{pubKey.GetID(), false}}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*timestamp=*/1); - } - if (fRescan) - { - RescanWallet(*pwallet, reserver); - pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true); - } - - return UniValue::VNULL; -}, - }; -} - - -RPCHelpMan importwallet() -{ - return RPCHelpMan{"importwallet", - "\nImports keys from a wallet dump file (see dumpwallet). Requires a new wallet backup to include imported keys.\n" - "Note: Blockchain and Mempool will be rescanned after a successful import. Use \"getwalletinfo\" to query the scanning progress.\n" - "Note: This command is only compatible with legacy wallets.\n", - { - {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet file"}, - }, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{ - "\nDump the wallet\n" - + HelpExampleCli("dumpwallet", "\"test\"") + - "\nImport the wallet\n" - + HelpExampleCli("importwallet", "\"test\"") + - "\nImport using the json rpc call\n" - + HelpExampleRpc("importwallet", "\"test\"") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - EnsureLegacyScriptPubKeyMan(*pwallet, true); - - WalletRescanReserver reserver(*pwallet); - if (!reserver.reserve()) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); - } - - int64_t nTimeBegin = 0; - bool fGood = true; - { - LOCK(pwallet->cs_wallet); - - EnsureWalletIsUnlocked(*pwallet); - - std::ifstream file; - file.open(fs::u8path(request.params[0].get_str()), std::ios::in | std::ios::ate); - if (!file.is_open()) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file"); - } - CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nTimeBegin))); - - int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg()); - file.seekg(0, file.beg); - - // Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which - // we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button. - pwallet->chain().showProgress(strprintf("%s %s", pwallet->GetDisplayName(), _("Importing…")), 0, false); // show progress dialog in GUI - std::vector> keys; - std::vector> scripts; - while (file.good()) { - pwallet->chain().showProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false); - std::string line; - std::getline(file, line); - if (line.empty() || line[0] == '#') - continue; - - std::vector vstr = SplitString(line, ' '); - if (vstr.size() < 2) - continue; - CKey key = DecodeSecret(vstr[0]); - if (key.IsValid()) { - int64_t nTime{ParseISO8601DateTime(vstr[1]).value_or(0)}; - std::string strLabel; - bool fLabel = true; - for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) { - if (vstr[nStr].front() == '#') - break; - if (vstr[nStr] == "change=1") - fLabel = false; - if (vstr[nStr] == "reserve=1") - fLabel = false; - if (vstr[nStr].starts_with("label=")) { - strLabel = DecodeDumpString(vstr[nStr].substr(6)); - fLabel = true; - } - } - nTimeBegin = std::min(nTimeBegin, nTime); - keys.emplace_back(key, nTime, fLabel, strLabel); - } else if(IsHex(vstr[0])) { - std::vector vData(ParseHex(vstr[0])); - CScript script = CScript(vData.begin(), vData.end()); - int64_t birth_time{ParseISO8601DateTime(vstr[1]).value_or(0)}; - if (birth_time > 0) nTimeBegin = std::min(nTimeBegin, birth_time); - scripts.emplace_back(script, birth_time); - } - } - file.close(); - EnsureBlockDataFromTime(*pwallet, nTimeBegin); - // We now know whether we are importing private keys, so we can error if private keys are disabled - if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { - pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI - throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled"); - } - double total = (double)(keys.size() + scripts.size()); - double progress = 0; - for (const auto& key_tuple : keys) { - pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false); - const CKey& key = std::get<0>(key_tuple); - int64_t time = std::get<1>(key_tuple); - bool has_label = std::get<2>(key_tuple); - std::string label = std::get<3>(key_tuple); - - CPubKey pubkey = key.GetPubKey(); - CHECK_NONFATAL(key.VerifyPubKey(pubkey)); - CKeyID keyid = pubkey.GetID(); - - pwallet->WalletLogPrintf("Importing %s...\n", EncodeDestination(PKHash(keyid))); - - if (!pwallet->ImportPrivKeys({{keyid, key}}, time)) { - pwallet->WalletLogPrintf("Error importing key for %s\n", EncodeDestination(PKHash(keyid))); - fGood = false; - continue; - } - - if (has_label) - pwallet->SetAddressBook(PKHash(keyid), label, AddressPurpose::RECEIVE); - progress++; - } - for (const auto& script_pair : scripts) { - pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false); - const CScript& script = script_pair.first; - int64_t time = script_pair.second; - - if (!pwallet->ImportScripts({script}, time)) { - pwallet->WalletLogPrintf("Error importing script %s\n", HexStr(script)); - fGood = false; - continue; - } - - progress++; - } - pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI - } - pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI - RescanWallet(*pwallet, reserver, nTimeBegin, /*update=*/false); - pwallet->MarkDirty(); - - if (!fGood) - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys/scripts to wallet"); - - return UniValue::VNULL; -}, - }; -} - -RPCHelpMan dumpprivkey() -{ - return RPCHelpMan{"dumpprivkey", - "\nReveals the private key corresponding to 'address'.\n" - "Then the importprivkey can be used with this output\n" - "Note: This command is only compatible with legacy wallets.\n", - { - {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for the private key"}, - }, - RPCResult{ - RPCResult::Type::STR, "key", "The private key" - }, - RPCExamples{ - HelpExampleCli("dumpprivkey", "\"myaddress\"") - + HelpExampleCli("importprivkey", "\"mykey\"") - + HelpExampleRpc("dumpprivkey", "\"myaddress\"") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - const std::shared_ptr pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(*pwallet); - - LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore); - - EnsureWalletIsUnlocked(*pwallet); - - std::string strAddress = request.params[0].get_str(); - CTxDestination dest = DecodeDestination(strAddress); - if (!IsValidDestination(dest)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address"); - } - auto keyid = GetKeyForDestination(spk_man, dest); - if (keyid.IsNull()) { - throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key"); - } - CKey vchSecret; - if (!spk_man.GetKey(keyid, vchSecret)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known"); - } - return EncodeSecret(vchSecret); -}, - }; -} - - -RPCHelpMan dumpwallet() -{ - return RPCHelpMan{"dumpwallet", - "\nDumps all wallet keys in a human-readable format to a server-side file. This does not allow overwriting existing files.\n" - "Imported scripts are included in the dumpfile, but corresponding BIP173 addresses, etc. may not be added automatically by importwallet.\n" - "Note that if your wallet contains keys which are not derived from your HD seed (e.g. imported keys), these are not covered by\n" - "only backing up the seed itself, and must be backed up too (e.g. ensure you back up the whole dumpfile).\n" - "Note: This command is only compatible with legacy wallets.\n", - { - {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The filename with path (absolute path recommended)"}, - }, - RPCResult{ - RPCResult::Type::OBJ, "", "", - { - {RPCResult::Type::STR, "filename", "The filename with full absolute path"}, - } - }, - RPCExamples{ - HelpExampleCli("dumpwallet", "\"test\"") - + HelpExampleRpc("dumpwallet", "\"test\"") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - const std::shared_ptr pwallet = GetWalletForJSONRPCRequest(request); - if (!pwallet) return UniValue::VNULL; - - const CWallet& wallet = *pwallet; - const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(wallet); - - // Make sure the results are valid at least up to the most recent block - // the user could have gotten from another RPC command prior to now - wallet.BlockUntilSyncedToCurrentChain(); - - LOCK(wallet.cs_wallet); - - EnsureWalletIsUnlocked(wallet); - - fs::path filepath = fs::u8path(request.params[0].get_str()); - filepath = fs::absolute(filepath); - - /* Prevent arbitrary files from being overwritten. There have been reports - * that users have overwritten wallet files this way: - * https://github.com/bitcoin/bitcoin/issues/9934 - * It may also avoid other security issues. - */ - if (fs::exists(filepath)) { - throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.utf8string() + " already exists. If you are sure this is what you want, move it out of the way first"); - } - - std::ofstream file; - file.open(filepath); - if (!file.is_open()) - throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file"); - - std::map mapKeyBirth; - wallet.GetKeyBirthTimes(mapKeyBirth); - - int64_t block_time = 0; - CHECK_NONFATAL(wallet.chain().findBlock(wallet.GetLastBlockHash(), FoundBlock().time(block_time))); - - // Note: To avoid a lock order issue, access to cs_main must be locked before cs_KeyStore. - // So we do the two things in this function that lock cs_main first: GetKeyBirthTimes, and findBlock. - LOCK(spk_man.cs_KeyStore); - - const std::map& mapKeyPool = spk_man.GetAllReserveKeys(); - std::set scripts = spk_man.GetCScripts(); - - // sort time/key pairs - std::vector > vKeyBirth; - vKeyBirth.reserve(mapKeyBirth.size()); - for (const auto& entry : mapKeyBirth) { - vKeyBirth.emplace_back(entry.second, entry.first); - } - mapKeyBirth.clear(); - std::sort(vKeyBirth.begin(), vKeyBirth.end()); - - // produce output - file << strprintf("# Wallet dump created by %s %s\n", CLIENT_NAME, FormatFullVersion()); - file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime())); - file << strprintf("# * Best block at time of backup was %i (%s),\n", wallet.GetLastBlockHeight(), wallet.GetLastBlockHash().ToString()); - file << strprintf("# mined on %s\n", FormatISO8601DateTime(block_time)); - file << "\n"; - - // add the base58check encoded extended master if the wallet uses HD - CKeyID seed_id = spk_man.GetHDChain().seed_id; - if (!seed_id.IsNull()) - { - CKey seed; - if (spk_man.GetKey(seed_id, seed)) { - CExtKey masterKey; - masterKey.SetSeed(seed); - - file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n"; - } - } - for (std::vector >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) { - const CKeyID &keyid = it->second; - std::string strTime = FormatISO8601DateTime(it->first); - std::string strAddr; - std::string strLabel; - CKey key; - if (spk_man.GetKey(keyid, key)) { - CKeyMetadata metadata; - const auto it{spk_man.mapKeyMetadata.find(keyid)}; - if (it != spk_man.mapKeyMetadata.end()) metadata = it->second; - file << strprintf("%s %s ", EncodeSecret(key), strTime); - if (GetWalletAddressesForKey(&spk_man, wallet, keyid, strAddr, strLabel)) { - file << strprintf("label=%s", strLabel); - } else if (keyid == seed_id) { - file << "hdseed=1"; - } else if (mapKeyPool.count(keyid)) { - file << "reserve=1"; - } else if (metadata.hdKeypath == "s") { - file << "inactivehdseed=1"; - } else { - file << "change=1"; - } - file << strprintf(" # addr=%s%s\n", strAddr, (metadata.has_key_origin ? " hdkeypath="+WriteHDKeypath(metadata.key_origin.path, /*apostrophe=*/true) : "")); - } - } - file << "\n"; - for (const CScriptID &scriptid : scripts) { - CScript script; - std::string create_time = "0"; - std::string address = EncodeDestination(ScriptHash(scriptid)); - // get birth times for scripts with metadata - auto it = spk_man.m_script_metadata.find(scriptid); - if (it != spk_man.m_script_metadata.end()) { - create_time = FormatISO8601DateTime(it->second.nCreateTime); - } - if(spk_man.GetCScript(scriptid, script)) { - file << strprintf("%s %s script=1", HexStr(script), create_time); - file << strprintf(" # addr=%s\n", address); - } - } - file << "\n"; - file << "# End of dump\n"; - file.close(); - - UniValue reply(UniValue::VOBJ); - reply.pushKV("filename", filepath.utf8string()); - - return reply; -}, - }; -} - -struct ImportData -{ - // Input data - std::unique_ptr redeemscript; //!< Provided redeemScript; will be moved to `import_scripts` if relevant. - std::unique_ptr witnessscript; //!< Provided witnessScript; will be moved to `import_scripts` if relevant. - - // Output data - std::set import_scripts; - std::map used_keys; //!< Import these private keys if available (the value indicates whether if the key is required for solvability) - std::map> key_origins; -}; - -enum class ScriptContext -{ - TOP, //!< Top-level scriptPubKey - P2SH, //!< P2SH redeemScript - WITNESS_V0, //!< P2WSH witnessScript -}; - -// Analyse the provided scriptPubKey, determining which keys and which redeem scripts from the ImportData struct are needed to spend it, and mark them as used. -// Returns an error string, or the empty string for success. -// NOLINTNEXTLINE(misc-no-recursion) -static std::string RecurseImportData(const CScript& script, ImportData& import_data, const ScriptContext script_ctx) -{ - // Use Solver to obtain script type and parsed pubkeys or hashes: - std::vector> solverdata; - TxoutType script_type = Solver(script, solverdata); - - switch (script_type) { - case TxoutType::PUBKEY: { - CPubKey pubkey(solverdata[0]); - import_data.used_keys.emplace(pubkey.GetID(), false); - return ""; - } - case TxoutType::PUBKEYHASH: { - CKeyID id = CKeyID(uint160(solverdata[0])); - import_data.used_keys[id] = true; - return ""; - } - case TxoutType::SCRIPTHASH: { - if (script_ctx == ScriptContext::P2SH) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside another P2SH"); - if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside a P2WSH"); - CHECK_NONFATAL(script_ctx == ScriptContext::TOP); - CScriptID id = CScriptID(uint160(solverdata[0])); - auto subscript = std::move(import_data.redeemscript); // Remove redeemscript from import_data to check for superfluous script later. - if (!subscript) return "missing redeemscript"; - if (CScriptID(*subscript) != id) return "redeemScript does not match the scriptPubKey"; - import_data.import_scripts.emplace(*subscript); - return RecurseImportData(*subscript, import_data, ScriptContext::P2SH); - } - case TxoutType::MULTISIG: { - for (size_t i = 1; i + 1< solverdata.size(); ++i) { - CPubKey pubkey(solverdata[i]); - import_data.used_keys.emplace(pubkey.GetID(), false); - } - return ""; - } - case TxoutType::WITNESS_V0_SCRIPTHASH: { - if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WSH inside another P2WSH"); - CScriptID id{RIPEMD160(solverdata[0])}; - auto subscript = std::move(import_data.witnessscript); // Remove redeemscript from import_data to check for superfluous script later. - if (!subscript) return "missing witnessscript"; - if (CScriptID(*subscript) != id) return "witnessScript does not match the scriptPubKey or redeemScript"; - if (script_ctx == ScriptContext::TOP) { - import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WSH requires the TOP script imported (see script/ismine.cpp) - } - import_data.import_scripts.emplace(*subscript); - return RecurseImportData(*subscript, import_data, ScriptContext::WITNESS_V0); - } - case TxoutType::WITNESS_V0_KEYHASH: { - if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WPKH inside P2WSH"); - CKeyID id = CKeyID(uint160(solverdata[0])); - import_data.used_keys[id] = true; - if (script_ctx == ScriptContext::TOP) { - import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WPKH requires the TOP script imported (see script/ismine.cpp) - } - return ""; - } - case TxoutType::NULL_DATA: - return "unspendable script"; - case TxoutType::NONSTANDARD: - case TxoutType::WITNESS_UNKNOWN: - case TxoutType::WITNESS_V1_TAPROOT: - case TxoutType::ANCHOR: - return "unrecognized script"; - } // no default case, so the compiler can warn about missing cases - NONFATAL_UNREACHABLE(); -} - -static UniValue ProcessImportLegacy(ImportData& import_data, std::map& pubkey_map, std::map& privkey_map, std::set& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector>& ordered_pubkeys) -{ - UniValue warnings(UniValue::VARR); - - // First ensure scriptPubKey has either a script or JSON with "address" string - const UniValue& scriptPubKey = data["scriptPubKey"]; - bool isScript = scriptPubKey.getType() == UniValue::VSTR; - if (!isScript && !(scriptPubKey.getType() == UniValue::VOBJ && scriptPubKey.exists("address"))) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "scriptPubKey must be string with script or JSON with address string"); - } - const std::string& output = isScript ? scriptPubKey.get_str() : scriptPubKey["address"].get_str(); - - // Optional fields. - const std::string& strRedeemScript = data.exists("redeemscript") ? data["redeemscript"].get_str() : ""; - const std::string& witness_script_hex = data.exists("witnessscript") ? data["witnessscript"].get_str() : ""; - const UniValue& pubKeys = data.exists("pubkeys") ? data["pubkeys"].get_array() : UniValue(); - const UniValue& keys = data.exists("keys") ? data["keys"].get_array() : UniValue(); - const bool internal = data.exists("internal") ? data["internal"].get_bool() : false; - const bool watchOnly = data.exists("watchonly") ? data["watchonly"].get_bool() : false; - - if (data.exists("range")) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for a non-descriptor import"); - } - - // Generate the script and destination for the scriptPubKey provided - CScript script; - if (!isScript) { - CTxDestination dest = DecodeDestination(output); - if (!IsValidDestination(dest)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address \"" + output + "\""); - } - if (OutputTypeFromDestination(dest) == OutputType::BECH32M) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m addresses cannot be imported into legacy wallets"); - } - script = GetScriptForDestination(dest); - } else { - if (!IsHex(output)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid scriptPubKey \"" + output + "\""); - } - std::vector vData(ParseHex(output)); - script = CScript(vData.begin(), vData.end()); - CTxDestination dest; - if (!ExtractDestination(script, dest) && !internal) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal must be set to true for nonstandard scriptPubKey imports."); - } - } - script_pub_keys.emplace(script); - - // Parse all arguments - if (strRedeemScript.size()) { - if (!IsHex(strRedeemScript)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid redeem script \"" + strRedeemScript + "\": must be hex string"); - } - auto parsed_redeemscript = ParseHex(strRedeemScript); - import_data.redeemscript = std::make_unique(parsed_redeemscript.begin(), parsed_redeemscript.end()); - } - if (witness_script_hex.size()) { - if (!IsHex(witness_script_hex)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid witness script \"" + witness_script_hex + "\": must be hex string"); - } - auto parsed_witnessscript = ParseHex(witness_script_hex); - import_data.witnessscript = std::make_unique(parsed_witnessscript.begin(), parsed_witnessscript.end()); - } - for (size_t i = 0; i < pubKeys.size(); ++i) { - CPubKey pubkey = HexToPubKey(pubKeys[i].get_str()); - pubkey_map.emplace(pubkey.GetID(), pubkey); - ordered_pubkeys.emplace_back(pubkey.GetID(), internal); - } - for (size_t i = 0; i < keys.size(); ++i) { - const auto& str = keys[i].get_str(); - CKey key = DecodeSecret(str); - if (!key.IsValid()) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding"); - } - CPubKey pubkey = key.GetPubKey(); - CKeyID id = pubkey.GetID(); - if (pubkey_map.count(id)) { - pubkey_map.erase(id); - } - privkey_map.emplace(id, key); - } - - - // Verify and process input data - have_solving_data = import_data.redeemscript || import_data.witnessscript || pubkey_map.size() || privkey_map.size(); - if (have_solving_data) { - // Match up data in import_data with the scriptPubKey in script. - auto error = RecurseImportData(script, import_data, ScriptContext::TOP); - - // Verify whether the watchonly option corresponds to the availability of private keys. - bool spendable = std::all_of(import_data.used_keys.begin(), import_data.used_keys.end(), [&](const std::pair& used_key){ return privkey_map.count(used_key.first) > 0; }); - if (!watchOnly && !spendable) { - warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."); - } - if (watchOnly && spendable) { - warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag."); - } - - // Check that all required keys for solvability are provided. - if (error.empty()) { - for (const auto& require_key : import_data.used_keys) { - if (!require_key.second) continue; // Not a required key - if (pubkey_map.count(require_key.first) == 0 && privkey_map.count(require_key.first) == 0) { - error = "some required keys are missing"; - } - } - } - - if (!error.empty()) { - warnings.push_back("Importing as non-solvable: " + error + ". If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript."); - import_data = ImportData(); - pubkey_map.clear(); - privkey_map.clear(); - have_solving_data = false; - } else { - // RecurseImportData() removes any relevant redeemscript/witnessscript from import_data, so we can use that to discover if a superfluous one was provided. - if (import_data.redeemscript) warnings.push_back("Ignoring redeemscript as this is not a P2SH script."); - if (import_data.witnessscript) warnings.push_back("Ignoring witnessscript as this is not a (P2SH-)P2WSH script."); - for (auto it = privkey_map.begin(); it != privkey_map.end(); ) { - auto oldit = it++; - if (import_data.used_keys.count(oldit->first) == 0) { - warnings.push_back("Ignoring irrelevant private key."); - privkey_map.erase(oldit); - } - } - for (auto it = pubkey_map.begin(); it != pubkey_map.end(); ) { - auto oldit = it++; - auto key_data_it = import_data.used_keys.find(oldit->first); - if (key_data_it == import_data.used_keys.end() || !key_data_it->second) { - warnings.push_back("Ignoring public key \"" + HexStr(oldit->first) + "\" as it doesn't appear inside P2PKH or P2WPKH."); - pubkey_map.erase(oldit); - } - } - } - } - - return warnings; -} - -static UniValue ProcessImportDescriptor(ImportData& import_data, std::map& pubkey_map, std::map& privkey_map, std::set& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector>& ordered_pubkeys) -{ - UniValue warnings(UniValue::VARR); - - const std::string& descriptor = data["desc"].get_str(); - FlatSigningProvider keys; - std::string error; - auto parsed_descs = Parse(descriptor, keys, error, /* require_checksum = */ true); - if (parsed_descs.empty()) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); - } - if (parsed_descs.at(0)->GetOutputType() == OutputType::BECH32M) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m descriptors cannot be imported into legacy wallets"); - } - - std::optional internal; - if (data.exists("internal")) { - if (parsed_descs.size() > 1) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot have multipath descriptor while also specifying \'internal\'"); - } - internal = data["internal"].get_bool(); - } - - have_solving_data = parsed_descs.at(0)->IsSolvable(); - const bool watch_only = data.exists("watchonly") ? data["watchonly"].get_bool() : false; - - int64_t range_start = 0, range_end = 0; - if (!parsed_descs.at(0)->IsRange() && data.exists("range")) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor"); - } else if (parsed_descs.at(0)->IsRange()) { - if (!data.exists("range")) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor is ranged, please specify the range"); - } - std::tie(range_start, range_end) = ParseDescriptorRange(data["range"]); - } - - // Only single key descriptors are allowed to be imported to a legacy wallet's keypool - bool can_keypool = parsed_descs.at(0)->IsSingleKey(); - - const UniValue& priv_keys = data.exists("keys") ? data["keys"].get_array() : UniValue(); - - for (size_t j = 0; j < parsed_descs.size(); ++j) { - const auto& parsed_desc = parsed_descs.at(j); - bool desc_internal = internal.has_value() && internal.value(); - if (parsed_descs.size() == 2) { - desc_internal = j == 1; - } else if (parsed_descs.size() > 2) { - CHECK_NONFATAL(!desc_internal); - } - // Expand all descriptors to get public keys and scripts, and private keys if available. - for (int i = range_start; i <= range_end; ++i) { - FlatSigningProvider out_keys; - std::vector scripts_temp; - parsed_desc->Expand(i, keys, scripts_temp, out_keys); - std::copy(scripts_temp.begin(), scripts_temp.end(), std::inserter(script_pub_keys, script_pub_keys.end())); - if (can_keypool) { - for (const auto& key_pair : out_keys.pubkeys) { - ordered_pubkeys.emplace_back(key_pair.first, desc_internal); - } - } - - for (const auto& x : out_keys.scripts) { - import_data.import_scripts.emplace(x.second); - } - - parsed_desc->ExpandPrivate(i, keys, out_keys); - - std::copy(out_keys.pubkeys.begin(), out_keys.pubkeys.end(), std::inserter(pubkey_map, pubkey_map.end())); - std::copy(out_keys.keys.begin(), out_keys.keys.end(), std::inserter(privkey_map, privkey_map.end())); - import_data.key_origins.insert(out_keys.origins.begin(), out_keys.origins.end()); - } - } - - for (size_t i = 0; i < priv_keys.size(); ++i) { - const auto& str = priv_keys[i].get_str(); - CKey key = DecodeSecret(str); - if (!key.IsValid()) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding"); - } - CPubKey pubkey = key.GetPubKey(); - CKeyID id = pubkey.GetID(); - - // Check if this private key corresponds to a public key from the descriptor - if (!pubkey_map.count(id)) { - warnings.push_back("Ignoring irrelevant private key."); - } else { - privkey_map.emplace(id, key); - } - } - - // Check if all the public keys have corresponding private keys in the import for spendability. - // This does not take into account threshold multisigs which could be spendable without all keys. - // Thus, threshold multisigs without all keys will be considered not spendable here, even if they are, - // perhaps triggering a false warning message. This is consistent with the current wallet IsMine check. - bool spendable = std::all_of(pubkey_map.begin(), pubkey_map.end(), - [&](const std::pair& used_key) { - return privkey_map.count(used_key.first) > 0; - }) && std::all_of(import_data.key_origins.begin(), import_data.key_origins.end(), - [&](const std::pair>& entry) { - return privkey_map.count(entry.first) > 0; - }); - if (!watch_only && !spendable) { - warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."); - } - if (watch_only && spendable) { - warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag."); - } - - return warnings; -} - -static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) -{ - UniValue warnings(UniValue::VARR); - UniValue result(UniValue::VOBJ); - - try { - const bool internal = data.exists("internal") ? data["internal"].get_bool() : false; - // Internal addresses should not have a label - if (internal && data.exists("label")) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label"); - } - const std::string label{LabelFromValue(data["label"])}; - const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false; - - // Add to keypool only works with privkeys disabled - if (add_keypool && !wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Keys can only be imported to the keypool when private keys are disabled"); - } - - ImportData import_data; - std::map pubkey_map; - std::map privkey_map; - std::set script_pub_keys; - std::vector> ordered_pubkeys; - bool have_solving_data; - - if (data.exists("scriptPubKey") && data.exists("desc")) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Both a descriptor and a scriptPubKey should not be provided."); - } else if (data.exists("scriptPubKey")) { - warnings = ProcessImportLegacy(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys); - } else if (data.exists("desc")) { - warnings = ProcessImportDescriptor(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys); - } else { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Either a descriptor or scriptPubKey must be provided."); - } - - // If private keys are disabled, abort if private keys are being imported - if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) { - throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled"); - } - - // Check whether we have any work to do - for (const CScript& script : script_pub_keys) { - if (wallet.IsMine(script) & ISMINE_SPENDABLE) { - throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script) + "\")"); - } - } - - // All good, time to import - wallet.MarkDirty(); - if (!wallet.ImportScripts(import_data.import_scripts, timestamp)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet"); - } - if (!wallet.ImportPrivKeys(privkey_map, timestamp)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet"); - } - if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, timestamp)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); - } - if (!wallet.ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); - } - - result.pushKV("success", UniValue(true)); - } catch (const UniValue& e) { - result.pushKV("success", UniValue(false)); - result.pushKV("error", e); - } catch (...) { - result.pushKV("success", UniValue(false)); - - result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, "Missing required fields")); - } - PushWarnings(warnings, result); - return result; -} - static int64_t GetImportTimestamp(const UniValue& data, int64_t now) { if (data.exists("timestamp")) { @@ -1256,207 +136,6 @@ static int64_t GetImportTimestamp(const UniValue& data, int64_t now) throw JSONRPCError(RPC_TYPE_ERROR, "Missing required timestamp field for key"); } -RPCHelpMan importmulti() -{ - return RPCHelpMan{"importmulti", - "\nImport addresses/scripts (with private or public keys, redeem script (P2SH)), optionally rescanning the blockchain from the earliest creation time of the imported scripts. Requires a new wallet backup.\n" - "If an address/script is imported without all of the private keys required to spend from that address, it will be watchonly. The 'watchonly' option must be set to true in this case or a warning will be returned.\n" - "Conversely, if all the private keys are provided and the address/script is spendable, the watchonly option must be set to false, or a warning will be returned.\n" - "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n" - "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n" - "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n" - "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n" - "Note: Use \"getwalletinfo\" to query the scanning progress.\n" - "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" for descriptor wallets.\n", - { - {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported", - { - {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "", - { - {"desc", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Descriptor to import. If using descriptor, do not also provide address/scriptPubKey, scripts, or pubkeys"}, - {"scriptPubKey", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of scriptPubKey (string for script, json for address). Should not be provided if using a descriptor", - RPCArgOptions{.type_str={"\"