diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -94,15 +94,6 @@ } } -static LegacyScriptPubKeyMan &GetLegacyScriptPubKeyMan(CWallet &wallet) { - LegacyScriptPubKeyMan *spk_man = wallet.GetLegacyScriptPubKeyMan(); - if (!spk_man) { - throw JSONRPCError(RPC_WALLET_ERROR, - "This type of wallet does not support this command"); - } - return *spk_man; -} - UniValue importprivkey(const Config &config, const JSONRPCRequest &request) { std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); CWallet *const pwallet = wallet.get(); @@ -151,7 +142,7 @@ "private keys disabled"); } - GetLegacyScriptPubKeyMan(*wallet); + EnsureLegacyScriptPubKeyMan(*wallet); WalletRescanReserver reserver(pwallet); bool fRescan = true; @@ -298,7 +289,7 @@ } .Check(request); - GetLegacyScriptPubKeyMan(*pwallet); + EnsureLegacyScriptPubKeyMan(*pwallet); std::string strLabel; if (!request.params[1].isNull()) { @@ -552,7 +543,7 @@ } .Check(request); - GetLegacyScriptPubKeyMan(*wallet); + EnsureLegacyScriptPubKeyMan(*wallet); std::string strLabel; if (!request.params[1].isNull()) { @@ -648,7 +639,7 @@ } .Check(request); - GetLegacyScriptPubKeyMan(*wallet); + EnsureLegacyScriptPubKeyMan(*wallet); if (pwallet->chain().havePruned()) { // Exit early and print an error. @@ -848,7 +839,7 @@ } .Check(request); - LegacyScriptPubKeyMan &spk_man = GetLegacyScriptPubKeyMan(*wallet); + LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*wallet); auto locked_chain = pwallet->chain().lock(); LOCK(pwallet->cs_wallet); @@ -905,7 +896,7 @@ } .Check(request); - LegacyScriptPubKeyMan &spk_man = GetLegacyScriptPubKeyMan(*wallet); + LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*wallet); auto locked_chain = pwallet->chain().lock(); LOCK(pwallet->cs_wallet); @@ -1691,7 +1682,7 @@ RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ}); - GetLegacyScriptPubKeyMan(*wallet); + EnsureLegacyScriptPubKeyMan(*wallet); const UniValue &requests = mainRequest.params[0]; diff --git a/src/wallet/rpcwallet.h b/src/wallet/rpcwallet.h --- a/src/wallet/rpcwallet.h +++ b/src/wallet/rpcwallet.h @@ -16,6 +16,7 @@ class CTransaction; class CWallet; class JSONRPCRequest; +class LegacyScriptPubKeyMan; struct PartiallySignedTransaction; class UniValue; @@ -46,6 +47,7 @@ std::string HelpRequiringPassphrase(const CWallet *); void EnsureWalletIsUnlocked(const CWallet *); bool EnsureWalletIsAvailable(const CWallet *, bool avoidException); +LegacyScriptPubKeyMan &EnsureLegacyScriptPubKeyMan(CWallet &wallet); UniValue signrawtransactionwithwallet(const Config &config, const JSONRPCRequest &request); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -143,6 +143,15 @@ } } +LegacyScriptPubKeyMan &EnsureLegacyScriptPubKeyMan(CWallet &wallet) { + LegacyScriptPubKeyMan *spk_man = wallet.GetLegacyScriptPubKeyMan(); + if (!spk_man) { + throw JSONRPCError(RPC_WALLET_ERROR, + "This type of wallet does not support this command"); + } + return *spk_man; +} + static void WalletTxToJSON(interfaces::Chain &chain, interfaces::Chain::Lock &locked_chain, const CWalletTx &wtx, UniValue &entry) { @@ -1115,11 +1124,7 @@ } .Check(request); - LegacyScriptPubKeyMan *spk_man = pwallet->GetLegacyScriptPubKeyMan(); - if (!spk_man) { - throw JSONRPCError(RPC_WALLET_ERROR, - "This type of wallet does not support this command"); - } + LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*pwallet); auto locked_chain = pwallet->chain().lock(); LOCK(pwallet->cs_wallet); @@ -1140,7 +1145,7 @@ keys_or_addrs[i].get_str().length() == 130)) { pubkeys.push_back(HexToPubKey(keys_or_addrs[i].get_str())); } else { - pubkeys.push_back(AddrToPubKey(config.GetChainParams(), spk_man, + pubkeys.push_back(AddrToPubKey(config.GetChainParams(), &spk_man, keys_or_addrs[i].get_str())); } } @@ -1150,7 +1155,7 @@ // Construct using pay-to-script-hash: CScript inner; CTxDestination dest = AddAndGetMultisigDestination( - required, pubkeys, output_type, *spk_man, inner); + required, pubkeys, output_type, spk_man, inner); pwallet->SetAddressBook(dest, label, "send"); UniValue result(UniValue::VOBJ); @@ -4418,11 +4423,7 @@ } .Check(request); - LegacyScriptPubKeyMan *spk_man = pwallet->GetLegacyScriptPubKeyMan(); - if (!spk_man) { - throw JSONRPCError(RPC_WALLET_ERROR, - "This type of wallet does not support this command"); - } + LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*pwallet); if (pwallet->chain().isInitialBlockDownload()) { throw JSONRPCError( @@ -4456,7 +4457,7 @@ CPubKey master_pub_key; if (request.params[1].isNull()) { - master_pub_key = spk_man->GenerateNewSeed(); + master_pub_key = spk_man.GenerateNewSeed(); } else { CKey key = DecodeSecret(request.params[1].get_str()); if (!key.IsValid()) { @@ -4464,18 +4465,18 @@ "Invalid private key"); } - if (HaveKey(*spk_man, key)) { + if (HaveKey(spk_man, key)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Already have this key (either as an HD seed or " "as a loose private key)"); } - master_pub_key = spk_man->DeriveNewSeed(key); + master_pub_key = spk_man.DeriveNewSeed(key); } - spk_man->SetHDSeed(master_pub_key); + spk_man.SetHDSeed(master_pub_key); if (flush_key_pool) { - spk_man->NewKeyPool(); + spk_man.NewKeyPool(); } return NullUniValue;