Skip to content

Commit b07b07c

Browse files
committed
Add EnsureLegacyScriptPubKeyMan and use in rpcwallet.cpp
This also fixes unused variable warnings in rpcdump.cpp
1 parent e204dc1 commit b07b07c

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

src/wallet/rpcdump.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@ static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver,
8787
}
8888
}
8989

90-
static LegacyScriptPubKeyMan& GetLegacyScriptPubKeyMan(CWallet& wallet)
91-
{
92-
LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
93-
if (!spk_man) {
94-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
95-
}
96-
return *spk_man;
97-
}
98-
9990
UniValue importprivkey(const JSONRPCRequest& request)
10091
{
10192
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -134,7 +125,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
134125
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
135126
}
136127

137-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
128+
EnsureLegacyScriptPubKeyMan(*wallet);
138129

139130
WalletRescanReserver reserver(pwallet);
140131
bool fRescan = true;
@@ -262,7 +253,7 @@ UniValue importaddress(const JSONRPCRequest& request)
262253
},
263254
}.Check(request);
264255

265-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*pwallet);
256+
EnsureLegacyScriptPubKeyMan(*pwallet);
266257

267258
std::string strLabel;
268259
if (!request.params[1].isNull())
@@ -465,7 +456,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
465456
},
466457
}.Check(request);
467458

468-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
459+
EnsureLegacyScriptPubKeyMan(*wallet);
469460

470461
std::string strLabel;
471462
if (!request.params[1].isNull())
@@ -549,7 +540,7 @@ UniValue importwallet(const JSONRPCRequest& request)
549540
},
550541
}.Check(request);
551542

552-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
543+
EnsureLegacyScriptPubKeyMan(*wallet);
553544

554545
if (pwallet->chain().havePruned()) {
555546
// Exit early and print an error.
@@ -708,7 +699,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
708699
},
709700
}.Check(request);
710701

711-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
702+
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
712703

713704
auto locked_chain = pwallet->chain().lock();
714705
LOCK(pwallet->cs_wallet);
@@ -759,7 +750,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
759750
},
760751
}.Check(request);
761752

762-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
753+
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
763754

764755
auto locked_chain = pwallet->chain().lock();
765756
LOCK(pwallet->cs_wallet);
@@ -1346,7 +1337,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13461337

13471338
RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
13481339

1349-
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
1340+
EnsureLegacyScriptPubKeyMan(*wallet);
13501341

13511342
const UniValue& requests = mainRequest.params[0];
13521343

src/wallet/rpcwallet.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ void EnsureWalletIsUnlocked(const CWallet* pwallet)
124124
}
125125
}
126126

127+
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet)
128+
{
129+
LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
130+
if (!spk_man) {
131+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
132+
}
133+
return *spk_man;
134+
}
135+
127136
static void WalletTxToJSON(interfaces::Chain& chain, interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx, UniValue& entry)
128137
{
129138
int confirms = wtx.GetDepthInMainChain(locked_chain);
@@ -966,10 +975,7 @@ static UniValue addmultisigaddress(const JSONRPCRequest& request)
966975
},
967976
}.Check(request);
968977

969-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
970-
if (!spk_man) {
971-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
972-
}
978+
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
973979

974980
auto locked_chain = pwallet->chain().lock();
975981
LOCK(pwallet->cs_wallet);
@@ -987,7 +993,7 @@ static UniValue addmultisigaddress(const JSONRPCRequest& request)
987993
if (IsHex(keys_or_addrs[i].get_str()) && (keys_or_addrs[i].get_str().length() == 66 || keys_or_addrs[i].get_str().length() == 130)) {
988994
pubkeys.push_back(HexToPubKey(keys_or_addrs[i].get_str()));
989995
} else {
990-
pubkeys.push_back(AddrToPubKey(spk_man, keys_or_addrs[i].get_str()));
996+
pubkeys.push_back(AddrToPubKey(&spk_man, keys_or_addrs[i].get_str()));
991997
}
992998
}
993999

@@ -1000,7 +1006,7 @@ static UniValue addmultisigaddress(const JSONRPCRequest& request)
10001006

10011007
// Construct using pay-to-script-hash:
10021008
CScript inner;
1003-
CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, *spk_man, inner);
1009+
CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, spk_man, inner);
10041010
pwallet->SetAddressBook(dest, label, "send");
10051011

10061012
UniValue result(UniValue::VOBJ);
@@ -3933,10 +3939,7 @@ UniValue sethdseed(const JSONRPCRequest& request)
39333939
},
39343940
}.Check(request);
39353941

3936-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
3937-
if (!spk_man) {
3938-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
3939-
}
3942+
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
39403943

39413944
if (pwallet->chain().isInitialBlockDownload()) {
39423945
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Cannot set a new HD seed while still in Initial Block Download");
@@ -3963,22 +3966,22 @@ UniValue sethdseed(const JSONRPCRequest& request)
39633966

39643967
CPubKey master_pub_key;
39653968
if (request.params[1].isNull()) {
3966-
master_pub_key = spk_man->GenerateNewSeed();
3969+
master_pub_key = spk_man.GenerateNewSeed();
39673970
} else {
39683971
CKey key = DecodeSecret(request.params[1].get_str());
39693972
if (!key.IsValid()) {
39703973
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
39713974
}
39723975

3973-
if (HaveKey(*spk_man, key)) {
3976+
if (HaveKey(spk_man, key)) {
39743977
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Already have this key (either as an HD seed or as a loose private key)");
39753978
}
39763979

3977-
master_pub_key = spk_man->DeriveNewSeed(key);
3980+
master_pub_key = spk_man.DeriveNewSeed(key);
39783981
}
39793982

3980-
spk_man->SetHDSeed(master_pub_key);
3981-
if (flush_key_pool) spk_man->NewKeyPool();
3983+
spk_man.SetHDSeed(master_pub_key);
3984+
if (flush_key_pool) spk_man.NewKeyPool();
39823985

39833986
return NullUniValue;
39843987
}

src/wallet/rpcwallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
class CRPCTable;
1313
class CWallet;
1414
class JSONRPCRequest;
15+
class LegacyScriptPubKeyMan;
1516
class UniValue;
1617
struct PartiallySignedTransaction;
1718
class CTransaction;
@@ -40,6 +41,7 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
4041
std::string HelpRequiringPassphrase(const CWallet*);
4142
void EnsureWalletIsUnlocked(const CWallet*);
4243
bool EnsureWalletIsAvailable(const CWallet*, bool avoidException);
44+
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet);
4345

4446
UniValue getaddressinfo(const JSONRPCRequest& request);
4547
UniValue signrawtransactionwithwallet(const JSONRPCRequest& request);

0 commit comments

Comments
 (0)