diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -98,12 +98,6 @@ } UniValue importprivkey(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importprivkey", "Adds a private key (as returned by dumpprivkey) to your wallet. " @@ -139,6 +133,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with " @@ -216,12 +216,6 @@ } UniValue abortrescan(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "abortrescan", "Stops current wallet rescan triggered by an RPC call, e.g. by an " @@ -239,6 +233,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + if (!pwallet->IsScanning() || pwallet->IsAbortingRescan()) { return false; } @@ -247,12 +247,6 @@ } UniValue importaddress(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importaddress", "Adds an address or script (in hex) that can be watched as if it " @@ -292,6 +286,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + EnsureLegacyScriptPubKeyMan(*pwallet, true); std::string strLabel; @@ -377,12 +377,6 @@ UniValue importprunedfunds(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importprunedfunds", "Imports funds without rescan. Corresponding address or script must " @@ -402,6 +396,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + CMutableTransaction tx; if (!DecodeHexTx(tx, request.params[0].get_str())) { throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); @@ -456,12 +456,6 @@ UniValue removeprunedfunds(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "removeprunedfunds", "Deletes the specified transaction from the wallet. Meant for use " @@ -482,6 +476,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + LOCK(pwallet->cs_wallet); TxId txid(ParseHashV(request.params[0], "txid")); @@ -503,12 +503,6 @@ } UniValue importpubkey(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importpubkey", "Adds a public key (in hex) that can be watched as if it were in " @@ -540,6 +534,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + EnsureLegacyScriptPubKeyMan(*wallet, true); std::string strLabel; @@ -609,12 +609,6 @@ } UniValue importwallet(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importwallet", "Imports keys from a wallet dump file (see dumpwallet). Requires a " @@ -634,6 +628,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + EnsureLegacyScriptPubKeyMan(*wallet, true); if (pwallet->chain().havePruned()) { @@ -812,12 +812,6 @@ } UniValue dumpprivkey(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - const CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "dumpprivkey", "Reveals the private key corresponding to 'address'.\n" @@ -833,6 +827,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + const CWallet *const pwallet = wallet.get(); + LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*wallet); LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore); @@ -859,12 +859,6 @@ } UniValue dumpwallet(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const pwallet = - GetWalletForJSONRPCRequest(request); - if (!pwallet) { - return NullUniValue; - } - RPCHelpMan{ "dumpwallet", "Dumps all wallet keys in a human-readable format to a server-side " @@ -891,6 +885,12 @@ } .Check(request); + std::shared_ptr const pwallet = + GetWalletForJSONRPCRequest(request); + if (!pwallet) { + return NullUniValue; + } + CWallet &wallet = *pwallet; LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(wallet); @@ -1020,12 +1020,6 @@ } static UniValue dumpcoins(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const pwallet = - GetWalletForJSONRPCRequest(request); - if (!pwallet) { - return NullUniValue; - } - RPCHelpMan{ "dumpcoins", "dump all the UTXO tracked by the wallet.\n", @@ -1058,6 +1052,12 @@ } .Check(request); + std::shared_ptr const pwallet = + GetWalletForJSONRPCRequest(request); + if (!pwallet) { + return NullUniValue; + } + CWallet &wallet = *pwallet; // Make sure the results are valid at least up to the most recent block @@ -1590,13 +1590,6 @@ } UniValue importmulti(const Config &config, const JSONRPCRequest &mainRequest) { - std::shared_ptr const wallet = - GetWalletForJSONRPCRequest(mainRequest); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importmulti", "\nImport addresses/scripts (with private or public keys, redeem " @@ -1774,6 +1767,13 @@ } .Check(mainRequest); + std::shared_ptr const wallet = + GetWalletForJSONRPCRequest(mainRequest); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ}); EnsureLegacyScriptPubKeyMan(*wallet, true); @@ -2077,14 +2077,6 @@ UniValue importdescriptors(const Config &config, const JSONRPCRequest &main_request) { - // Acquire the wallet - std::shared_ptr const wallet = - GetWalletForJSONRPCRequest(main_request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "importdescriptors", "\nImport descriptors. This will trigger a rescan of the blockchain " @@ -2195,6 +2187,13 @@ } .Check(main_request); + std::shared_ptr const wallet = + GetWalletForJSONRPCRequest(main_request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + // Make sure wallet is a descriptor wallet if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) { throw JSONRPCError( diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -205,12 +205,6 @@ static UniValue getnewaddress(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "getnewaddress", "Returns a new Bitcoin address for receiving payments.\n" @@ -234,6 +228,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + LOCK(pwallet->cs_wallet); if (!pwallet->CanGetAddresses()) { @@ -267,12 +267,6 @@ static UniValue getrawchangeaddress(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "getrawchangeaddress", "Returns a new Bitcoin address, for receiving change.\n" @@ -284,6 +278,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + LOCK(pwallet->cs_wallet); if (!pwallet->CanGetAddresses(true)) { @@ -312,12 +312,6 @@ } static UniValue setlabel(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "setlabel", "Sets the label associated with the given address.\n", @@ -337,6 +331,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + LOCK(pwallet->cs_wallet); CTxDestination dest = DecodeDestination(request.params[0].get_str(), @@ -402,12 +402,6 @@ static UniValue sendtoaddress(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "sendtoaddress", "Send an amount to a given address.\n" + HELP_REQUIRING_PASSPHRASE, @@ -454,6 +448,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + CWallet *const pwallet = wallet.get(); + // 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 pwallet->BlockUntilSyncedToCurrentChain(); @@ -502,12 +502,6 @@ static UniValue listaddressgroupings(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - const CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "listaddressgroupings", "Lists groups of addresses which have had their common ownership\n" @@ -540,6 +534,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + const CWallet *const pwallet = wallet.get(); + // 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 pwallet->BlockUntilSyncedToCurrentChain(); @@ -571,12 +571,6 @@ static UniValue signmessage(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - const CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "signmessage", "Sign a message with the private key of an address" + @@ -606,6 +600,11 @@ "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")}, } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + const CWallet *const pwallet = wallet.get(); LOCK(pwallet->cs_wallet); @@ -693,12 +692,6 @@ static UniValue getreceivedbyaddress(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - const CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "getreceivedbyaddress", "Returns the total amount received by the given address in " @@ -729,6 +722,12 @@ } .Check(request); + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) { + return NullUniValue; + } + const CWallet *const pwallet = wallet.get(); + // 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 pwallet->BlockUntilSyncedToCurrentChain(); @@ -741,12 +740,6 @@ static UniValue getreceivedbylabel(const Config &config, const JSONRPCRequest &request) { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - if (!wallet) { - return NullUniValue; - } - CWallet *const pwallet = wallet.get(); - RPCHelpMan{ "getreceivedbylabel", "Returns the total amount received by addresses with