Skip to content

Commit f201ba5

Browse files
committed
Refactor: Split up CWallet and LegacyScriptPubKeyMan and classes
This moves CWallet members and methods dealing with keys to a new LegacyScriptPubKeyMan class, and updates calling code to reference the new class instead of CWallet. Most of the changes are simple text replacements and variable substitutions easily verified with: git log -p -n1 -U0 --word-diff-regex=. The only nontrivial chunk of code added is the new LegacyScriptPubKeyMan class declaration, but this code isn't new and is just selectively copied and moved from the previous CWallet class declaration. This can be verified with: git log -p -n1 --color-moved=dimmed_zebra src/wallet/scriptpubkeyman.h src/wallet/wallet.h or git diff HEAD~1:src/wallet/wallet.h HEAD:src/wallet/scriptpubkeyman.h This commit does not change behavior.
1 parent 6702048 commit f201ba5

20 files changed

+901
-540
lines changed

src/interfaces/wallet.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, co
4646
result.txout_is_mine.emplace_back(wallet.IsMine(txout));
4747
result.txout_address.emplace_back();
4848
result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
49-
IsMine(wallet, result.txout_address.back()) :
49+
wallet.IsMine(result.txout_address.back()) :
5050
ISMINE_NO);
5151
}
5252
result.credit = wtx.GetCredit(locked_chain, ISMINE_ALL);
@@ -117,10 +117,17 @@ class WalletImpl : public Wallet
117117
std::string error;
118118
return m_wallet->GetNewDestination(type, label, dest, error);
119119
}
120-
bool getPubKey(const CKeyID& address, CPubKey& pub_key) override { return m_wallet->GetPubKey(address, pub_key); }
121-
bool getPrivKey(const CKeyID& address, CKey& key) override { return m_wallet->GetKey(address, key); }
122-
bool isSpendable(const CTxDestination& dest) override { return IsMine(*m_wallet, dest) & ISMINE_SPENDABLE; }
123-
bool haveWatchOnly() override { return m_wallet->HaveWatchOnly(); };
120+
bool getPubKey(const CKeyID& address, CPubKey& pub_key) override { return m_wallet->GetLegacyScriptPubKeyMan()->GetPubKey(address, pub_key); }
121+
bool getPrivKey(const CKeyID& address, CKey& key) override { return m_wallet->GetLegacyScriptPubKeyMan()->GetKey(address, key); }
122+
bool isSpendable(const CTxDestination& dest) override { return m_wallet->IsMine(dest) & ISMINE_SPENDABLE; }
123+
bool haveWatchOnly() override
124+
{
125+
auto spk_man = m_wallet->GetLegacyScriptPubKeyMan();
126+
if (spk_man) {
127+
return spk_man->HaveWatchOnly();
128+
}
129+
return false;
130+
};
124131
bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) override
125132
{
126133
return m_wallet->SetAddressBook(dest, name, purpose);
@@ -143,7 +150,7 @@ class WalletImpl : public Wallet
143150
*name = it->second.name;
144151
}
145152
if (is_mine) {
146-
*is_mine = IsMine(*m_wallet, dest);
153+
*is_mine = m_wallet->IsMine(dest);
147154
}
148155
if (purpose) {
149156
*purpose = it->second.purpose;
@@ -155,11 +162,11 @@ class WalletImpl : public Wallet
155162
LOCK(m_wallet->cs_wallet);
156163
std::vector<WalletAddress> result;
157164
for (const auto& item : m_wallet->mapAddressBook) {
158-
result.emplace_back(item.first, IsMine(*m_wallet, item.first), item.second.name, item.second.purpose);
165+
result.emplace_back(item.first, m_wallet->IsMine(item.first), item.second.name, item.second.purpose);
159166
}
160167
return result;
161168
}
162-
void learnRelatedScripts(const CPubKey& key, OutputType type) override { m_wallet->LearnRelatedScripts(key, type); }
169+
void learnRelatedScripts(const CPubKey& key, OutputType type) override { m_wallet->GetLegacyScriptPubKeyMan()->LearnRelatedScripts(key, type); }
163170
bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
164171
{
165172
LOCK(m_wallet->cs_wallet);
@@ -342,7 +349,7 @@ class WalletImpl : public Wallet
342349
result.balance = bal.m_mine_trusted;
343350
result.unconfirmed_balance = bal.m_mine_untrusted_pending;
344351
result.immature_balance = bal.m_mine_immature;
345-
result.have_watch_only = m_wallet->HaveWatchOnly();
352+
result.have_watch_only = haveWatchOnly();
346353
if (result.have_watch_only) {
347354
result.watch_only_balance = bal.m_watchonly_trusted;
348355
result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending;

src/qt/test/wallettests.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ void TestGUI()
138138
bool firstRun;
139139
wallet->LoadWallet(firstRun);
140140
{
141+
auto spk_man = wallet->GetLegacyScriptPubKeyMan();
141142
LOCK(wallet->cs_wallet);
143+
AssertLockHeld(spk_man->cs_wallet);
142144
wallet->SetAddressBook(GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type), "", "receive");
143-
wallet->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
145+
spk_man->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
144146
}
145147
{
146148
auto locked_chain = wallet->chain().lock();

src/test/util.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ std::string getnewaddress(CWallet& w)
3232

3333
void importaddress(CWallet& wallet, const std::string& address)
3434
{
35+
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
3536
LOCK(wallet.cs_wallet);
37+
AssertLockHeld(spk_man->cs_wallet);
3638
const auto dest = DecodeDestination(address);
3739
assert(IsValidDestination(dest));
3840
const auto script = GetScriptForDestination(dest);
3941
wallet.MarkDirty();
40-
assert(!wallet.HaveWatchOnly(script));
41-
if (!wallet.AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
42+
assert(!spk_man->HaveWatchOnly(script));
43+
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
4244
wallet.SetAddressBook(dest, /* label */ "", "receive");
4345
}
4446
#endif // ENABLE_WALLET

src/wallet/init.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <util/moneystr.h>
1111
#include <util/system.h>
1212
#include <util/translation.h>
13-
#include <wallet/scriptpubkeyman.h>
1413
#include <wallet/wallet.h>
1514
#include <walletinitinterface.h>
1615

src/wallet/ismine.h

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ enum isminetype : unsigned int
2828
/** used for bitflags of isminetype */
2929
typedef uint8_t isminefilter;
3030

31-
isminetype IsMine(const CWallet& wallet, const CScript& scriptPubKey);
32-
isminetype IsMine(const CWallet& wallet, const CTxDestination& dest);
33-
3431
/**
3532
* Cachable amount subdivided into watchonly and spendable parts.
3633
*/

src/wallet/psbtwallet.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& ps
3939
return TransactionError::SIGHASH_MISMATCH;
4040
}
4141

42-
complete &= SignPSBTInput(HidingSigningProvider(pwallet, !sign, !bip32derivs), psbtx, i, sighash_type);
42+
complete &= SignPSBTInput(HidingSigningProvider(pwallet->GetSigningProvider(), !sign, !bip32derivs), psbtx, i, sighash_type);
4343
}
4444

4545
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
4646
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
47-
UpdatePSBTOutput(HidingSigningProvider(pwallet, true, !bip32derivs), psbtx, i);
47+
UpdatePSBTOutput(HidingSigningProvider(pwallet->GetSigningProvider(), true, !bip32derivs), psbtx, i);
4848
}
4949

5050
return TransactionError::OK;

src/wallet/rpcdump.cpp

+50-16
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ static std::string DecodeDumpString(const std::string &str) {
6767
return ret.str();
6868
}
6969

70-
static bool GetWalletAddressesForKey(CWallet* const pwallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
70+
static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, CWallet* const pwallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
7171
{
7272
bool fLabelFound = false;
7373
CKey key;
74-
pwallet->GetKey(keyid, key);
74+
spk_man->GetKey(keyid, key);
7575
for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
7676
if (pwallet->mapAddressBook.count(dest)) {
7777
if (!strAddr.empty()) {
@@ -138,6 +138,11 @@ UniValue importprivkey(const JSONRPCRequest& request)
138138
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
139139
}
140140

141+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
142+
if (!spk_man) {
143+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
144+
}
145+
141146
WalletRescanReserver reserver(pwallet);
142147
bool fRescan = true;
143148
{
@@ -264,6 +269,10 @@ UniValue importaddress(const JSONRPCRequest& request)
264269
},
265270
}.Check(request);
266271

272+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
273+
if (!spk_man) {
274+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
275+
}
267276

268277
std::string strLabel;
269278
if (!request.params[1].isNull())
@@ -466,6 +475,10 @@ UniValue importpubkey(const JSONRPCRequest& request)
466475
},
467476
}.Check(request);
468477

478+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
479+
if (!spk_man) {
480+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
481+
}
469482

470483
std::string strLabel;
471484
if (!request.params[1].isNull())
@@ -549,6 +562,11 @@ UniValue importwallet(const JSONRPCRequest& request)
549562
},
550563
}.Check(request);
551564

565+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
566+
if (!spk_man) {
567+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
568+
}
569+
552570
if (pwallet->chain().havePruned()) {
553571
// Exit early and print an error.
554572
// If a block is pruned after this check, we will import the key(s),
@@ -706,6 +724,11 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
706724
},
707725
}.Check(request);
708726

727+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
728+
if (!spk_man) {
729+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
730+
}
731+
709732
auto locked_chain = pwallet->chain().lock();
710733
LOCK(pwallet->cs_wallet);
711734

@@ -716,12 +739,12 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
716739
if (!IsValidDestination(dest)) {
717740
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
718741
}
719-
auto keyid = GetKeyForDestination(*pwallet, dest);
742+
auto keyid = GetKeyForDestination(*spk_man, dest);
720743
if (keyid.IsNull()) {
721744
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
722745
}
723746
CKey vchSecret;
724-
if (!pwallet->GetKey(keyid, vchSecret)) {
747+
if (!spk_man->GetKey(keyid, vchSecret)) {
725748
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
726749
}
727750
return EncodeSecret(vchSecret);
@@ -755,8 +778,14 @@ UniValue dumpwallet(const JSONRPCRequest& request)
755778
},
756779
}.Check(request);
757780

781+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
782+
if (!spk_man) {
783+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
784+
}
785+
758786
auto locked_chain = pwallet->chain().lock();
759787
LOCK(pwallet->cs_wallet);
788+
AssertLockHeld(spk_man->cs_wallet);
760789

761790
EnsureWalletIsUnlocked(pwallet);
762791

@@ -778,10 +807,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
778807
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
779808

780809
std::map<CKeyID, int64_t> mapKeyBirth;
781-
const std::map<CKeyID, int64_t>& mapKeyPool = pwallet->GetAllReserveKeys();
810+
const std::map<CKeyID, int64_t>& mapKeyPool = spk_man->GetAllReserveKeys();
782811
pwallet->GetKeyBirthTimes(*locked_chain, mapKeyBirth);
783812

784-
std::set<CScriptID> scripts = pwallet->GetCScripts();
813+
std::set<CScriptID> scripts = spk_man->GetCScripts();
785814

786815
// sort time/key pairs
787816
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
@@ -800,11 +829,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
800829
file << "\n";
801830

802831
// add the base58check encoded extended master if the wallet uses HD
803-
CKeyID seed_id = pwallet->GetHDChain().seed_id;
832+
CKeyID seed_id = spk_man->GetHDChain().seed_id;
804833
if (!seed_id.IsNull())
805834
{
806835
CKey seed;
807-
if (pwallet->GetKey(seed_id, seed)) {
836+
if (spk_man->GetKey(seed_id, seed)) {
808837
CExtKey masterKey;
809838
masterKey.SetSeed(seed.begin(), seed.size());
810839

@@ -817,20 +846,20 @@ UniValue dumpwallet(const JSONRPCRequest& request)
817846
std::string strAddr;
818847
std::string strLabel;
819848
CKey key;
820-
if (pwallet->GetKey(keyid, key)) {
849+
if (spk_man->GetKey(keyid, key)) {
821850
file << strprintf("%s %s ", EncodeSecret(key), strTime);
822-
if (GetWalletAddressesForKey(pwallet, keyid, strAddr, strLabel)) {
851+
if (GetWalletAddressesForKey(spk_man, pwallet, keyid, strAddr, strLabel)) {
823852
file << strprintf("label=%s", strLabel);
824853
} else if (keyid == seed_id) {
825854
file << "hdseed=1";
826855
} else if (mapKeyPool.count(keyid)) {
827856
file << "reserve=1";
828-
} else if (pwallet->mapKeyMetadata[keyid].hdKeypath == "s") {
857+
} else if (spk_man->mapKeyMetadata[keyid].hdKeypath == "s") {
829858
file << "inactivehdseed=1";
830859
} else {
831860
file << "change=1";
832861
}
833-
file << strprintf(" # addr=%s%s\n", strAddr, (pwallet->mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(pwallet->mapKeyMetadata[keyid].key_origin.path) : ""));
862+
file << strprintf(" # addr=%s%s\n", strAddr, (spk_man->mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man->mapKeyMetadata[keyid].key_origin.path) : ""));
834863
}
835864
}
836865
file << "\n";
@@ -839,11 +868,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
839868
std::string create_time = "0";
840869
std::string address = EncodeDestination(ScriptHash(scriptid));
841870
// get birth times for scripts with metadata
842-
auto it = pwallet->m_script_metadata.find(scriptid);
843-
if (it != pwallet->m_script_metadata.end()) {
871+
auto it = spk_man->m_script_metadata.find(scriptid);
872+
if (it != spk_man->m_script_metadata.end()) {
844873
create_time = FormatISO8601DateTime(it->second.nCreateTime);
845874
}
846-
if(pwallet->GetCScript(scriptid, script)) {
875+
if(spk_man->GetCScript(scriptid, script)) {
847876
file << strprintf("%s %s script=1", HexStr(script.begin(), script.end()), create_time);
848877
file << strprintf(" # addr=%s\n", address);
849878
}
@@ -1219,7 +1248,7 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
12191248

12201249
// Check whether we have any work to do
12211250
for (const CScript& script : script_pub_keys) {
1222-
if (::IsMine(*pwallet, script) & ISMINE_SPENDABLE) {
1251+
if (pwallet->IsMine(script) & ISMINE_SPENDABLE) {
12231252
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script.begin(), script.end()) + "\")");
12241253
}
12251254
}
@@ -1339,6 +1368,11 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13391368

13401369
RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
13411370

1371+
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
1372+
if (!spk_man) {
1373+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
1374+
}
1375+
13421376
const UniValue& requests = mainRequest.params[0];
13431377

13441378
//Default options

0 commit comments

Comments
 (0)