Skip to content

Commit eb28184

Browse files
committed
Merge #8775: RPC refactoring: Access wallet using new GetWalletForJSONRPCRequest
d678771 Wallet: Sanitise -wallet parameter (Luke Dashjr) 9756be3 Wallet/RPC: Use filename rather than CWallet pointer, for lockwallet RPCRunLater job name (Luke Dashjr) 86be48a More tightly couple EnsureWalletIsAvailable with GetWalletForJSONRPCRequest where appropriate (Luke Dashjr) a435632 Move wallet RPC declarations to rpcwallet.h (Luke Dashjr) ad15734 RPC: Pass on JSONRPCRequest metadata (URI/user/etc) for "help" method (Luke Dashjr) bf8a04a Reformat touched lines with C++11 (Luke Dashjr) 2e518e3 Move nWalletUnlockTime to CWallet::nRelockTime, and name timed task unique per CWallet (Luke Dashjr) d77ad6d RPC: Do all wallet access through new GetWalletForJSONRPCRequest (Luke Dashjr) eca550f RPC/Wallet: Pass CWallet as pointer to helper functions (Luke Dashjr) Tree-SHA512: bfd592da841693390e16f83b451503eb5cedb71208089aa32b3fc45e973555584a3ed7696dd239f6409324464d565dacf0f3d0e36e8e13ae6a7843848465f960
2 parents 58861ad + d678771 commit eb28184

File tree

11 files changed

+600
-423
lines changed

11 files changed

+600
-423
lines changed

src/rpc/misc.cpp

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "util.h"
1515
#include "utilstrencodings.h"
1616
#ifdef ENABLE_WALLET
17+
#include "wallet/rpcwallet.h"
1718
#include "wallet/wallet.h"
1819
#include "wallet/walletdb.h"
1920
#endif
@@ -70,7 +71,9 @@ UniValue getinfo(const JSONRPCRequest& request)
7071
);
7172

7273
#ifdef ENABLE_WALLET
73-
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
74+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
75+
76+
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
7477
#else
7578
LOCK(cs_main);
7679
#endif
@@ -82,9 +85,9 @@ UniValue getinfo(const JSONRPCRequest& request)
8285
obj.push_back(Pair("version", CLIENT_VERSION));
8386
obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
8487
#ifdef ENABLE_WALLET
85-
if (pwalletMain) {
86-
obj.push_back(Pair("walletversion", pwalletMain->GetVersion()));
87-
obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance())));
88+
if (pwallet) {
89+
obj.push_back(Pair("walletversion", pwallet->GetVersion()));
90+
obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
8891
}
8992
#endif
9093
obj.push_back(Pair("blocks", (int)chainActive.Height()));
@@ -95,12 +98,13 @@ UniValue getinfo(const JSONRPCRequest& request)
9598
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
9699
obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET));
97100
#ifdef ENABLE_WALLET
98-
if (pwalletMain) {
99-
obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime()));
100-
obj.push_back(Pair("keypoolsize", (int)pwalletMain->GetKeyPoolSize()));
101+
if (pwallet) {
102+
obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
103+
obj.push_back(Pair("keypoolsize", (int)pwallet->GetKeyPoolSize()));
104+
}
105+
if (pwallet && pwallet->IsCrypted()) {
106+
obj.push_back(Pair("unlocked_until", pwallet->nRelockTime));
101107
}
102-
if (pwalletMain && pwalletMain->IsCrypted())
103-
obj.push_back(Pair("unlocked_until", nWalletUnlockTime));
104108
obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
105109
#endif
106110
obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
@@ -112,13 +116,17 @@ UniValue getinfo(const JSONRPCRequest& request)
112116
class DescribeAddressVisitor : public boost::static_visitor<UniValue>
113117
{
114118
public:
119+
CWallet * const pwallet;
120+
121+
DescribeAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {}
122+
115123
UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); }
116124

117125
UniValue operator()(const CKeyID &keyID) const {
118126
UniValue obj(UniValue::VOBJ);
119127
CPubKey vchPubKey;
120128
obj.push_back(Pair("isscript", false));
121-
if (pwalletMain && pwalletMain->GetPubKey(keyID, vchPubKey)) {
129+
if (pwallet && pwallet->GetPubKey(keyID, vchPubKey)) {
122130
obj.push_back(Pair("pubkey", HexStr(vchPubKey)));
123131
obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
124132
}
@@ -129,7 +137,7 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
129137
UniValue obj(UniValue::VOBJ);
130138
CScript subscript;
131139
obj.push_back(Pair("isscript", true));
132-
if (pwalletMain && pwalletMain->GetCScript(scriptID, subscript)) {
140+
if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
133141
std::vector<CTxDestination> addresses;
134142
txnouttype whichType;
135143
int nRequired;
@@ -177,7 +185,9 @@ UniValue validateaddress(const JSONRPCRequest& request)
177185
);
178186

179187
#ifdef ENABLE_WALLET
180-
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
188+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
189+
190+
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
181191
#else
182192
LOCK(cs_main);
183193
#endif
@@ -197,16 +207,17 @@ UniValue validateaddress(const JSONRPCRequest& request)
197207
ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
198208

199209
#ifdef ENABLE_WALLET
200-
isminetype mine = pwalletMain ? IsMine(*pwalletMain, dest) : ISMINE_NO;
210+
isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO;
201211
ret.push_back(Pair("ismine", (mine & ISMINE_SPENDABLE) ? true : false));
202212
ret.push_back(Pair("iswatchonly", (mine & ISMINE_WATCH_ONLY) ? true: false));
203-
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(), dest);
213+
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwallet), dest);
204214
ret.pushKVs(detail);
205-
if (pwalletMain && pwalletMain->mapAddressBook.count(dest))
206-
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
215+
if (pwallet && pwallet->mapAddressBook.count(dest)) {
216+
ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
217+
}
207218
CKeyID keyID;
208-
if (pwalletMain) {
209-
const auto& meta = pwalletMain->mapKeyMetadata;
219+
if (pwallet) {
220+
const auto& meta = pwallet->mapKeyMetadata;
210221
auto it = address.GetKeyID(keyID) ? meta.find(keyID) : meta.end();
211222
if (it == meta.end()) {
212223
it = meta.find(CScriptID(scriptPubKey));
@@ -224,10 +235,13 @@ UniValue validateaddress(const JSONRPCRequest& request)
224235
return ret;
225236
}
226237

238+
// Needed even with !ENABLE_WALLET, to pass (ignored) pointers around
239+
class CWallet;
240+
227241
/**
228242
* Used by addmultisigaddress / createmultisig:
229243
*/
230-
CScript _createmultisig_redeemScript(const UniValue& params)
244+
CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& params)
231245
{
232246
int nRequired = params[0].get_int();
233247
const UniValue& keys = params[1].get_array();
@@ -249,16 +263,16 @@ CScript _createmultisig_redeemScript(const UniValue& params)
249263
#ifdef ENABLE_WALLET
250264
// Case 1: Bitcoin address and we have full public key:
251265
CBitcoinAddress address(ks);
252-
if (pwalletMain && address.IsValid())
253-
{
266+
if (pwallet && address.IsValid()) {
254267
CKeyID keyID;
255268
if (!address.GetKeyID(keyID))
256269
throw runtime_error(
257270
strprintf("%s does not refer to a key",ks));
258271
CPubKey vchPubKey;
259-
if (!pwalletMain->GetPubKey(keyID, vchPubKey))
272+
if (!pwallet->GetPubKey(keyID, vchPubKey)) {
260273
throw runtime_error(
261274
strprintf("no full public key for address %s",ks));
275+
}
262276
if (!vchPubKey.IsFullyValid())
263277
throw runtime_error(" Invalid public key: "+ks);
264278
pubkeys[i] = vchPubKey;
@@ -290,6 +304,12 @@ CScript _createmultisig_redeemScript(const UniValue& params)
290304

291305
UniValue createmultisig(const JSONRPCRequest& request)
292306
{
307+
#ifdef ENABLE_WALLET
308+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
309+
#else
310+
CWallet * const pwallet = NULL;
311+
#endif
312+
293313
if (request.fHelp || request.params.size() < 2 || request.params.size() > 2)
294314
{
295315
string msg = "createmultisig nrequired [\"key\",...]\n"
@@ -320,7 +340,7 @@ UniValue createmultisig(const JSONRPCRequest& request)
320340
}
321341

322342
// Construct using pay-to-script-hash:
323-
CScript inner = _createmultisig_redeemScript(request.params);
343+
CScript inner = _createmultisig_redeemScript(pwallet, request.params);
324344
CScriptID innerID(inner);
325345
CBitcoinAddress address(innerID);
326346

src/rpc/rawtransaction.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "uint256.h"
2525
#include "utilstrencodings.h"
2626
#ifdef ENABLE_WALLET
27+
#include "wallet/rpcwallet.h"
2728
#include "wallet/wallet.h"
2829
#endif
2930

@@ -594,6 +595,10 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
594595

595596
UniValue signrawtransaction(const JSONRPCRequest& request)
596597
{
598+
#ifdef ENABLE_WALLET
599+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
600+
#endif
601+
597602
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
598603
throw runtime_error(
599604
"signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
@@ -603,7 +608,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
603608
"The third optional argument (may be null) is an array of base58-encoded private\n"
604609
"keys that, if given, will be the only keys used to sign the transaction.\n"
605610
#ifdef ENABLE_WALLET
606-
+ HelpRequiringPassphrase() + "\n"
611+
+ HelpRequiringPassphrase(pwallet) + "\n"
607612
#endif
608613

609614
"\nArguments:\n"
@@ -654,7 +659,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
654659
);
655660

656661
#ifdef ENABLE_WALLET
657-
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
662+
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
658663
#else
659664
LOCK(cs_main);
660665
#endif
@@ -717,8 +722,9 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
717722
}
718723
}
719724
#ifdef ENABLE_WALLET
720-
else if (pwalletMain)
721-
EnsureWalletIsUnlocked();
725+
else if (pwallet) {
726+
EnsureWalletIsUnlocked(pwallet);
727+
}
722728
#endif
723729

724730
// Add previous txouts given in the RPC call:
@@ -785,7 +791,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
785791
}
786792

787793
#ifdef ENABLE_WALLET
788-
const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain);
794+
const CKeyStore& keystore = ((fGivenKeys || !pwallet) ? tempKeystore : *pwallet);
789795
#else
790796
const CKeyStore& keystore = tempKeystore;
791797
#endif

src/rpc/server.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ vector<unsigned char> ParseHexO(const UniValue& o, string strKey)
178178
* Note: This interface may still be subject to change.
179179
*/
180180

181-
std::string CRPCTable::help(const std::string& strCommand) const
181+
std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest& helpreq) const
182182
{
183183
string strRet;
184184
string category;
@@ -189,16 +189,19 @@ std::string CRPCTable::help(const std::string& strCommand) const
189189
vCommands.push_back(make_pair(mi->second->category + mi->first, mi->second));
190190
sort(vCommands.begin(), vCommands.end());
191191

192+
JSONRPCRequest jreq(helpreq);
193+
jreq.fHelp = true;
194+
jreq.params = UniValue();
195+
192196
BOOST_FOREACH(const PAIRTYPE(string, const CRPCCommand*)& command, vCommands)
193197
{
194198
const CRPCCommand *pcmd = command.second;
195199
string strMethod = pcmd->name;
196200
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
197201
continue;
202+
jreq.strMethod = strMethod;
198203
try
199204
{
200-
JSONRPCRequest jreq;
201-
jreq.fHelp = true;
202205
rpcfn_type pfn = pcmd->actor;
203206
if (setDone.insert(pfn).second)
204207
(*pfn)(jreq);
@@ -247,7 +250,7 @@ UniValue help(const JSONRPCRequest& jsonRequest)
247250
if (jsonRequest.params.size() > 0)
248251
strCommand = jsonRequest.params[0].get_str();
249252

250-
return tableRPC.help(strCommand);
253+
return tableRPC.help(strCommand, jsonRequest);
251254
}
252255

253256

src/rpc/server.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class CRPCTable
154154
public:
155155
CRPCTable();
156156
const CRPCCommand* operator[](const std::string& name) const;
157-
std::string help(const std::string& name) const;
157+
std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
158158

159159
/**
160160
* Execute a method.
@@ -190,16 +190,12 @@ extern uint256 ParseHashO(const UniValue& o, std::string strKey);
190190
extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
191191
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
192192

193-
extern int64_t nWalletUnlockTime;
194193
extern CAmount AmountFromValue(const UniValue& value);
195194
extern UniValue ValueFromAmount(const CAmount& amount);
196195
extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
197-
extern std::string HelpRequiringPassphrase();
198196
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
199197
extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
200198

201-
extern void EnsureWalletIsUnlocked();
202-
203199
bool StartRPC();
204200
void InterruptRPC();
205201
void StopRPC();

src/utilstrencodings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ static const string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO
1919
static const string SAFE_CHARS[] =
2020
{
2121
CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
22-
CHARS_ALPHA_NUM + " .,;-_?@" // SAFE_CHARS_UA_COMMENT
22+
CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
23+
CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
2324
};
2425

2526
string SanitizeString(const string& str, int rule)

src/utilstrencodings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
enum SafeChars
2727
{
2828
SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
29-
SAFE_CHARS_UA_COMMENT //!< BIP-0014 subset
29+
SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
30+
SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
3031
};
3132

3233
/**

0 commit comments

Comments
 (0)