Skip to content

Commit d77ad6d

Browse files
committed
RPC: Do all wallet access through new GetWalletForJSONRPCRequest
1 parent eca550f commit d77ad6d

File tree

5 files changed

+400
-285
lines changed

5 files changed

+400
-285
lines changed

src/rpc/misc.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ UniValue getinfo(const JSONRPCRequest& request)
7070
);
7171

7272
#ifdef ENABLE_WALLET
73-
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
73+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
74+
75+
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
7476
#else
7577
LOCK(cs_main);
7678
#endif
@@ -82,9 +84,9 @@ UniValue getinfo(const JSONRPCRequest& request)
8284
obj.push_back(Pair("version", CLIENT_VERSION));
8385
obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
8486
#ifdef ENABLE_WALLET
85-
if (pwalletMain) {
86-
obj.push_back(Pair("walletversion", pwalletMain->GetVersion()));
87-
obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance())));
87+
if (pwallet) {
88+
obj.push_back(Pair("walletversion", pwallet->GetVersion()));
89+
obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
8890
}
8991
#endif
9092
obj.push_back(Pair("blocks", (int)chainActive.Height()));
@@ -95,11 +97,11 @@ UniValue getinfo(const JSONRPCRequest& request)
9597
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
9698
obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET));
9799
#ifdef ENABLE_WALLET
98-
if (pwalletMain) {
99-
obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime()));
100-
obj.push_back(Pair("keypoolsize", (int)pwalletMain->GetKeyPoolSize()));
100+
if (pwallet) {
101+
obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
102+
obj.push_back(Pair("keypoolsize", (int)pwallet->GetKeyPoolSize()));
101103
}
102-
if (pwalletMain && pwalletMain->IsCrypted())
104+
if (pwallet && pwallet->IsCrypted())
103105
obj.push_back(Pair("unlocked_until", nWalletUnlockTime));
104106
obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
105107
#endif
@@ -181,7 +183,9 @@ UniValue validateaddress(const JSONRPCRequest& request)
181183
);
182184

183185
#ifdef ENABLE_WALLET
184-
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
186+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
187+
188+
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
185189
#else
186190
LOCK(cs_main);
187191
#endif
@@ -201,16 +205,16 @@ UniValue validateaddress(const JSONRPCRequest& request)
201205
ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
202206

203207
#ifdef ENABLE_WALLET
204-
isminetype mine = pwalletMain ? IsMine(*pwalletMain, dest) : ISMINE_NO;
208+
isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO;
205209
ret.push_back(Pair("ismine", (mine & ISMINE_SPENDABLE) ? true : false));
206210
ret.push_back(Pair("iswatchonly", (mine & ISMINE_WATCH_ONLY) ? true: false));
207-
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwalletMain), dest);
211+
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwallet), dest);
208212
ret.pushKVs(detail);
209-
if (pwalletMain && pwalletMain->mapAddressBook.count(dest))
210-
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
213+
if (pwallet && pwallet->mapAddressBook.count(dest))
214+
ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
211215
CKeyID keyID;
212-
if (pwalletMain) {
213-
const auto& meta = pwalletMain->mapKeyMetadata;
216+
if (pwallet) {
217+
const auto& meta = pwallet->mapKeyMetadata;
214218
auto it = address.GetKeyID(keyID) ? meta.find(keyID) : meta.end();
215219
if (it == meta.end()) {
216220
it = meta.find(CScriptID(scriptPubKey));
@@ -294,6 +298,12 @@ CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& pa
294298

295299
UniValue createmultisig(const JSONRPCRequest& request)
296300
{
301+
#ifdef ENABLE_WALLET
302+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
303+
#else
304+
CWallet * const pwallet = NULL;
305+
#endif
306+
297307
if (request.fHelp || request.params.size() < 2 || request.params.size() > 2)
298308
{
299309
string msg = "createmultisig nrequired [\"key\",...]\n"
@@ -324,7 +334,7 @@ UniValue createmultisig(const JSONRPCRequest& request)
324334
}
325335

326336
// Construct using pay-to-script-hash:
327-
CScript inner = _createmultisig_redeemScript(pwalletMain, request.params);
337+
CScript inner = _createmultisig_redeemScript(pwallet, request.params);
328338
CScriptID innerID(inner);
329339
CBitcoinAddress address(innerID);
330340

src/rpc/rawtransaction.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
594594

595595
UniValue signrawtransaction(const JSONRPCRequest& request)
596596
{
597+
#ifdef ENABLE_WALLET
598+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
599+
#endif
600+
597601
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
598602
throw runtime_error(
599603
"signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
@@ -603,7 +607,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
603607
"The third optional argument (may be null) is an array of base58-encoded private\n"
604608
"keys that, if given, will be the only keys used to sign the transaction.\n"
605609
#ifdef ENABLE_WALLET
606-
+ HelpRequiringPassphrase(pwalletMain) + "\n"
610+
+ HelpRequiringPassphrase(pwallet) + "\n"
607611
#endif
608612

609613
"\nArguments:\n"
@@ -654,7 +658,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
654658
);
655659

656660
#ifdef ENABLE_WALLET
657-
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
661+
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
658662
#else
659663
LOCK(cs_main);
660664
#endif
@@ -717,8 +721,8 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
717721
}
718722
}
719723
#ifdef ENABLE_WALLET
720-
else if (pwalletMain)
721-
EnsureWalletIsUnlocked(pwalletMain);
724+
else if (pwallet)
725+
EnsureWalletIsUnlocked(pwallet);
722726
#endif
723727

724728
// Add previous txouts given in the RPC call:
@@ -785,7 +789,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
785789
}
786790

787791
#ifdef ENABLE_WALLET
788-
const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain);
792+
const CKeyStore& keystore = ((fGivenKeys || !pwallet) ? tempKeystore : *pwallet);
789793
#else
790794
const CKeyStore& keystore = tempKeystore;
791795
#endif

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class CWallet;
202202

203203
#ifdef ENABLE_WALLET
204204
// New code should accessing the wallet should be under the ../wallet/ directory
205+
CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest&);
205206
std::string HelpRequiringPassphrase(CWallet *);
206207
void EnsureWalletIsUnlocked(CWallet *);
207208
bool EnsureWalletIsAvailable(CWallet *, bool avoidException);

0 commit comments

Comments
 (0)