Skip to content

Commit 52cf68f

Browse files
committed
Refactor: Add GetLegacyScriptPubKeyMan helper
Suggested by João Barbosa <[email protected]> bitcoin/bitcoin#17260 (comment)
1 parent 6a97e8a commit 52cf68f

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

src/wallet/rpcdump.cpp

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ 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+
9099
UniValue importprivkey(const JSONRPCRequest& request)
91100
{
92101
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -125,10 +134,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
125134
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
126135
}
127136

128-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
129-
if (!spk_man) {
130-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
131-
}
137+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
132138

133139
WalletRescanReserver reserver(pwallet);
134140
bool fRescan = true;
@@ -256,10 +262,7 @@ UniValue importaddress(const JSONRPCRequest& request)
256262
},
257263
}.Check(request);
258264

259-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
260-
if (!spk_man) {
261-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
262-
}
265+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*pwallet);
263266

264267
std::string strLabel;
265268
if (!request.params[1].isNull())
@@ -462,10 +465,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
462465
},
463466
}.Check(request);
464467

465-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
466-
if (!spk_man) {
467-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
468-
}
468+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
469469

470470
std::string strLabel;
471471
if (!request.params[1].isNull())
@@ -549,10 +549,7 @@ UniValue importwallet(const JSONRPCRequest& request)
549549
},
550550
}.Check(request);
551551

552-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
553-
if (!spk_man) {
554-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
555-
}
552+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
556553

557554
if (pwallet->chain().havePruned()) {
558555
// Exit early and print an error.
@@ -711,10 +708,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
711708
},
712709
}.Check(request);
713710

714-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
715-
if (!spk_man) {
716-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
717-
}
711+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
718712

719713
auto locked_chain = pwallet->chain().lock();
720714
LOCK(pwallet->cs_wallet);
@@ -726,12 +720,12 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
726720
if (!IsValidDestination(dest)) {
727721
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
728722
}
729-
auto keyid = GetKeyForDestination(*spk_man, dest);
723+
auto keyid = GetKeyForDestination(spk_man, dest);
730724
if (keyid.IsNull()) {
731725
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
732726
}
733727
CKey vchSecret;
734-
if (!spk_man->GetKey(keyid, vchSecret)) {
728+
if (!spk_man.GetKey(keyid, vchSecret)) {
735729
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
736730
}
737731
return EncodeSecret(vchSecret);
@@ -765,14 +759,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
765759
},
766760
}.Check(request);
767761

768-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
769-
if (!spk_man) {
770-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
771-
}
762+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
772763

773764
auto locked_chain = pwallet->chain().lock();
774765
LOCK(pwallet->cs_wallet);
775-
AssertLockHeld(spk_man->cs_wallet);
766+
AssertLockHeld(spk_man.cs_wallet);
776767

777768
EnsureWalletIsUnlocked(pwallet);
778769

@@ -794,10 +785,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
794785
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
795786

796787
std::map<CKeyID, int64_t> mapKeyBirth;
797-
const std::map<CKeyID, int64_t>& mapKeyPool = spk_man->GetAllReserveKeys();
788+
const std::map<CKeyID, int64_t>& mapKeyPool = spk_man.GetAllReserveKeys();
798789
pwallet->GetKeyBirthTimes(*locked_chain, mapKeyBirth);
799790

800-
std::set<CScriptID> scripts = spk_man->GetCScripts();
791+
std::set<CScriptID> scripts = spk_man.GetCScripts();
801792

802793
// sort time/key pairs
803794
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
@@ -816,11 +807,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
816807
file << "\n";
817808

818809
// add the base58check encoded extended master if the wallet uses HD
819-
CKeyID seed_id = spk_man->GetHDChain().seed_id;
810+
CKeyID seed_id = spk_man.GetHDChain().seed_id;
820811
if (!seed_id.IsNull())
821812
{
822813
CKey seed;
823-
if (spk_man->GetKey(seed_id, seed)) {
814+
if (spk_man.GetKey(seed_id, seed)) {
824815
CExtKey masterKey;
825816
masterKey.SetSeed(seed.begin(), seed.size());
826817

@@ -833,20 +824,20 @@ UniValue dumpwallet(const JSONRPCRequest& request)
833824
std::string strAddr;
834825
std::string strLabel;
835826
CKey key;
836-
if (spk_man->GetKey(keyid, key)) {
827+
if (spk_man.GetKey(keyid, key)) {
837828
file << strprintf("%s %s ", EncodeSecret(key), strTime);
838-
if (GetWalletAddressesForKey(spk_man, pwallet, keyid, strAddr, strLabel)) {
829+
if (GetWalletAddressesForKey(&spk_man, pwallet, keyid, strAddr, strLabel)) {
839830
file << strprintf("label=%s", strLabel);
840831
} else if (keyid == seed_id) {
841832
file << "hdseed=1";
842833
} else if (mapKeyPool.count(keyid)) {
843834
file << "reserve=1";
844-
} else if (spk_man->mapKeyMetadata[keyid].hdKeypath == "s") {
835+
} else if (spk_man.mapKeyMetadata[keyid].hdKeypath == "s") {
845836
file << "inactivehdseed=1";
846837
} else {
847838
file << "change=1";
848839
}
849-
file << strprintf(" # addr=%s%s\n", strAddr, (spk_man->mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man->mapKeyMetadata[keyid].key_origin.path) : ""));
840+
file << strprintf(" # addr=%s%s\n", strAddr, (spk_man.mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man.mapKeyMetadata[keyid].key_origin.path) : ""));
850841
}
851842
}
852843
file << "\n";
@@ -855,11 +846,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
855846
std::string create_time = "0";
856847
std::string address = EncodeDestination(ScriptHash(scriptid));
857848
// get birth times for scripts with metadata
858-
auto it = spk_man->m_script_metadata.find(scriptid);
859-
if (it != spk_man->m_script_metadata.end()) {
849+
auto it = spk_man.m_script_metadata.find(scriptid);
850+
if (it != spk_man.m_script_metadata.end()) {
860851
create_time = FormatISO8601DateTime(it->second.nCreateTime);
861852
}
862-
if(spk_man->GetCScript(scriptid, script)) {
853+
if(spk_man.GetCScript(scriptid, script)) {
863854
file << strprintf("%s %s script=1", HexStr(script.begin(), script.end()), create_time);
864855
file << strprintf(" # addr=%s\n", address);
865856
}
@@ -1355,10 +1346,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13551346

13561347
RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
13571348

1358-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
1359-
if (!spk_man) {
1360-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
1361-
}
1349+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
13621350

13631351
const UniValue& requests = mainRequest.params[0];
13641352

0 commit comments

Comments
 (0)