Skip to content

Commit

Permalink
RPC: Do all wallet access through new GetWalletForJSONRPCRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Feb 27, 2017
1 parent eca550f commit d77ad6d
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 285 deletions.
42 changes: 26 additions & 16 deletions src/rpc/misc.cpp
Expand Up @@ -70,7 +70,9 @@ UniValue getinfo(const JSONRPCRequest& request)
);

#ifdef ENABLE_WALLET
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);

LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
#else
LOCK(cs_main);
#endif
Expand All @@ -82,9 +84,9 @@ UniValue getinfo(const JSONRPCRequest& request)
obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
#ifdef ENABLE_WALLET
if (pwalletMain) {
obj.push_back(Pair("walletversion", pwalletMain->GetVersion()));
obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance())));
if (pwallet) {
obj.push_back(Pair("walletversion", pwallet->GetVersion()));
obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
}
#endif
obj.push_back(Pair("blocks", (int)chainActive.Height()));
Expand All @@ -95,11 +97,11 @@ UniValue getinfo(const JSONRPCRequest& request)
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET));
#ifdef ENABLE_WALLET
if (pwalletMain) {
obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime()));
obj.push_back(Pair("keypoolsize", (int)pwalletMain->GetKeyPoolSize()));
if (pwallet) {
obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
obj.push_back(Pair("keypoolsize", (int)pwallet->GetKeyPoolSize()));
}
if (pwalletMain && pwalletMain->IsCrypted())
if (pwallet && pwallet->IsCrypted())
obj.push_back(Pair("unlocked_until", nWalletUnlockTime));
obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
#endif
Expand Down Expand Up @@ -181,7 +183,9 @@ UniValue validateaddress(const JSONRPCRequest& request)
);

#ifdef ENABLE_WALLET
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);

LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
#else
LOCK(cs_main);
#endif
Expand All @@ -201,16 +205,16 @@ UniValue validateaddress(const JSONRPCRequest& request)
ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));

#ifdef ENABLE_WALLET
isminetype mine = pwalletMain ? IsMine(*pwalletMain, dest) : ISMINE_NO;
isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO;
ret.push_back(Pair("ismine", (mine & ISMINE_SPENDABLE) ? true : false));
ret.push_back(Pair("iswatchonly", (mine & ISMINE_WATCH_ONLY) ? true: false));
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwalletMain), dest);
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwallet), dest);
ret.pushKVs(detail);
if (pwalletMain && pwalletMain->mapAddressBook.count(dest))
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
if (pwallet && pwallet->mapAddressBook.count(dest))
ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
CKeyID keyID;
if (pwalletMain) {
const auto& meta = pwalletMain->mapKeyMetadata;
if (pwallet) {
const auto& meta = pwallet->mapKeyMetadata;
auto it = address.GetKeyID(keyID) ? meta.find(keyID) : meta.end();
if (it == meta.end()) {
it = meta.find(CScriptID(scriptPubKey));
Expand Down Expand Up @@ -294,6 +298,12 @@ CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& pa

UniValue createmultisig(const JSONRPCRequest& request)
{
#ifdef ENABLE_WALLET
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
#else
CWallet * const pwallet = NULL;
#endif

if (request.fHelp || request.params.size() < 2 || request.params.size() > 2)
{
string msg = "createmultisig nrequired [\"key\",...]\n"
Expand Down Expand Up @@ -324,7 +334,7 @@ UniValue createmultisig(const JSONRPCRequest& request)
}

// Construct using pay-to-script-hash:
CScript inner = _createmultisig_redeemScript(pwalletMain, request.params);
CScript inner = _createmultisig_redeemScript(pwallet, request.params);
CScriptID innerID(inner);
CBitcoinAddress address(innerID);

Expand Down
14 changes: 9 additions & 5 deletions src/rpc/rawtransaction.cpp
Expand Up @@ -594,6 +594,10 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::

UniValue signrawtransaction(const JSONRPCRequest& request)
{
#ifdef ENABLE_WALLET
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
#endif

if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
throw runtime_error(
"signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
Expand All @@ -603,7 +607,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
"The third optional argument (may be null) is an array of base58-encoded private\n"
"keys that, if given, will be the only keys used to sign the transaction.\n"
#ifdef ENABLE_WALLET
+ HelpRequiringPassphrase(pwalletMain) + "\n"
+ HelpRequiringPassphrase(pwallet) + "\n"
#endif

"\nArguments:\n"
Expand Down Expand Up @@ -654,7 +658,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
);

#ifdef ENABLE_WALLET
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
#else
LOCK(cs_main);
#endif
Expand Down Expand Up @@ -717,8 +721,8 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
}
}
#ifdef ENABLE_WALLET
else if (pwalletMain)
EnsureWalletIsUnlocked(pwalletMain);
else if (pwallet)
EnsureWalletIsUnlocked(pwallet);
#endif

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

#ifdef ENABLE_WALLET
const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain);
const CKeyStore& keystore = ((fGivenKeys || !pwallet) ? tempKeystore : *pwallet);
#else
const CKeyStore& keystore = tempKeystore;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Expand Up @@ -202,6 +202,7 @@ class CWallet;

#ifdef ENABLE_WALLET
// New code should accessing the wallet should be under the ../wallet/ directory
CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest&);
std::string HelpRequiringPassphrase(CWallet *);
void EnsureWalletIsUnlocked(CWallet *);
bool EnsureWalletIsAvailable(CWallet *, bool avoidException);
Expand Down

0 comments on commit d77ad6d

Please sign in to comment.