Browse Source

RPC: Do all wallet access through new GetWalletForJSONRPCRequest

0.15
Luke Dashjr 8 years ago
parent
commit
d77ad6d416
  1. 42
      src/rpc/misc.cpp
  2. 14
      src/rpc/rawtransaction.cpp
  3. 1
      src/rpc/server.h
  4. 148
      src/wallet/rpcdump.cpp
  5. 480
      src/wallet/rpcwallet.cpp

42
src/rpc/misc.cpp

@ -70,7 +70,9 @@ UniValue getinfo(const JSONRPCRequest& request)
); );
#ifdef ENABLE_WALLET #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 #else
LOCK(cs_main); LOCK(cs_main);
#endif #endif
@ -82,9 +84,9 @@ UniValue getinfo(const JSONRPCRequest& request)
obj.push_back(Pair("version", CLIENT_VERSION)); obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("protocolversion", PROTOCOL_VERSION)); obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (pwalletMain) { if (pwallet) {
obj.push_back(Pair("walletversion", pwalletMain->GetVersion())); obj.push_back(Pair("walletversion", pwallet->GetVersion()));
obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance()))); obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
} }
#endif #endif
obj.push_back(Pair("blocks", (int)chainActive.Height())); obj.push_back(Pair("blocks", (int)chainActive.Height()));
@ -95,11 +97,11 @@ UniValue getinfo(const JSONRPCRequest& request)
obj.push_back(Pair("difficulty", (double)GetDifficulty())); obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET)); obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET));
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (pwalletMain) { if (pwallet) {
obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime())); obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
obj.push_back(Pair("keypoolsize", (int)pwalletMain->GetKeyPoolSize())); 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("unlocked_until", nWalletUnlockTime));
obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK()))); obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
#endif #endif
@ -181,7 +183,9 @@ UniValue validateaddress(const JSONRPCRequest& request)
); );
#ifdef ENABLE_WALLET #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 #else
LOCK(cs_main); LOCK(cs_main);
#endif #endif
@ -201,16 +205,16 @@ UniValue validateaddress(const JSONRPCRequest& request)
ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
#ifdef ENABLE_WALLET #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("ismine", (mine & ISMINE_SPENDABLE) ? true : false));
ret.push_back(Pair("iswatchonly", (mine & ISMINE_WATCH_ONLY) ? 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); ret.pushKVs(detail);
if (pwalletMain && pwalletMain->mapAddressBook.count(dest)) if (pwallet && pwallet->mapAddressBook.count(dest))
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name)); ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
CKeyID keyID; CKeyID keyID;
if (pwalletMain) { if (pwallet) {
const auto& meta = pwalletMain->mapKeyMetadata; const auto& meta = pwallet->mapKeyMetadata;
auto it = address.GetKeyID(keyID) ? meta.find(keyID) : meta.end(); auto it = address.GetKeyID(keyID) ? meta.find(keyID) : meta.end();
if (it == meta.end()) { if (it == meta.end()) {
it = meta.find(CScriptID(scriptPubKey)); it = meta.find(CScriptID(scriptPubKey));
@ -294,6 +298,12 @@ CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& pa
UniValue createmultisig(const JSONRPCRequest& request) 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) if (request.fHelp || request.params.size() < 2 || request.params.size() > 2)
{ {
string msg = "createmultisig nrequired [\"key\",...]\n" string msg = "createmultisig nrequired [\"key\",...]\n"
@ -324,7 +334,7 @@ UniValue createmultisig(const JSONRPCRequest& request)
} }
// Construct using pay-to-script-hash: // Construct using pay-to-script-hash:
CScript inner = _createmultisig_redeemScript(pwalletMain, request.params); CScript inner = _createmultisig_redeemScript(pwallet, request.params);
CScriptID innerID(inner); CScriptID innerID(inner);
CBitcoinAddress address(innerID); CBitcoinAddress address(innerID);

14
src/rpc/rawtransaction.cpp

@ -594,6 +594,10 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
UniValue signrawtransaction(const JSONRPCRequest& request) 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) if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
throw runtime_error( throw runtime_error(
"signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n" "signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
@ -603,7 +607,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
"The third optional argument (may be null) is an array of base58-encoded private\n" "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" "keys that, if given, will be the only keys used to sign the transaction.\n"
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
+ HelpRequiringPassphrase(pwalletMain) + "\n" + HelpRequiringPassphrase(pwallet) + "\n"
#endif #endif
"\nArguments:\n" "\nArguments:\n"
@ -654,7 +658,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
); );
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL); LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
#else #else
LOCK(cs_main); LOCK(cs_main);
#endif #endif
@ -717,8 +721,8 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
} }
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
else if (pwalletMain) else if (pwallet)
EnsureWalletIsUnlocked(pwalletMain); EnsureWalletIsUnlocked(pwallet);
#endif #endif
// Add previous txouts given in the RPC call: // Add previous txouts given in the RPC call:
@ -785,7 +789,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain); const CKeyStore& keystore = ((fGivenKeys || !pwallet) ? tempKeystore : *pwallet);
#else #else
const CKeyStore& keystore = tempKeystore; const CKeyStore& keystore = tempKeystore;
#endif #endif

1
src/rpc/server.h

@ -202,6 +202,7 @@ class CWallet;
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
// New code should accessing the wallet should be under the ../wallet/ directory // New code should accessing the wallet should be under the ../wallet/ directory
CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest&);
std::string HelpRequiringPassphrase(CWallet *); std::string HelpRequiringPassphrase(CWallet *);
void EnsureWalletIsUnlocked(CWallet *); void EnsureWalletIsUnlocked(CWallet *);
bool EnsureWalletIsAvailable(CWallet *, bool avoidException); bool EnsureWalletIsAvailable(CWallet *, bool avoidException);

148
src/wallet/rpcdump.cpp

@ -74,7 +74,9 @@ std::string DecodeDumpString(const std::string &str) {
UniValue importprivkey(const JSONRPCRequest& request) UniValue importprivkey(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() < 1 || request.params.size() > 3) if (request.fHelp || request.params.size() < 1 || request.params.size() > 3)
@ -98,9 +100,9 @@ UniValue importprivkey(const JSONRPCRequest& request)
); );
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwalletMain); EnsureWalletIsUnlocked(pwallet);
string strSecret = request.params[0].get_str(); string strSecret = request.params[0].get_str();
string strLabel = ""; string strLabel = "";
@ -127,23 +129,23 @@ UniValue importprivkey(const JSONRPCRequest& request)
assert(key.VerifyPubKey(pubkey)); assert(key.VerifyPubKey(pubkey));
CKeyID vchAddress = pubkey.GetID(); CKeyID vchAddress = pubkey.GetID();
{ {
pwalletMain->MarkDirty(); pwallet->MarkDirty();
pwalletMain->SetAddressBook(vchAddress, strLabel, "receive"); pwallet->SetAddressBook(vchAddress, strLabel, "receive");
// Don't throw error in case a key is already there // Don't throw error in case a key is already there
if (pwalletMain->HaveKey(vchAddress)) if (pwallet->HaveKey(vchAddress))
return NullUniValue; return NullUniValue;
pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1; pwallet->mapKeyMetadata[vchAddress].nCreateTime = 1;
if (!pwalletMain->AddKeyPubKey(key, pubkey)) if (!pwallet->AddKeyPubKey(key, pubkey))
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet"); throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
// whenever a key is imported, we need to scan the whole chain // whenever a key is imported, we need to scan the whole chain
pwalletMain->UpdateTimeFirstKey(1); pwallet->UpdateTimeFirstKey(1);
if (fRescan) { if (fRescan) {
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); pwallet->ScanForWalletTransactions(chainActive.Genesis(), true);
} }
} }
@ -184,7 +186,9 @@ void ImportAddress(CWallet * const pwallet, const CBitcoinAddress& address, cons
UniValue importaddress(const JSONRPCRequest& request) UniValue importaddress(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4) if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
@ -227,24 +231,24 @@ UniValue importaddress(const JSONRPCRequest& request)
if (request.params.size() > 3) if (request.params.size() > 3)
fP2SH = request.params[3].get_bool(); fP2SH = request.params[3].get_bool();
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
CBitcoinAddress address(request.params[0].get_str()); CBitcoinAddress address(request.params[0].get_str());
if (address.IsValid()) { if (address.IsValid()) {
if (fP2SH) if (fP2SH)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead");
ImportAddress(pwalletMain, address, strLabel); ImportAddress(pwallet, address, strLabel);
} else if (IsHex(request.params[0].get_str())) { } else if (IsHex(request.params[0].get_str())) {
std::vector<unsigned char> data(ParseHex(request.params[0].get_str())); std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
ImportScript(pwalletMain, CScript(data.begin(), data.end()), strLabel, fP2SH); ImportScript(pwallet, CScript(data.begin(), data.end()), strLabel, fP2SH);
} else { } else {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
} }
if (fRescan) if (fRescan)
{ {
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); pwallet->ScanForWalletTransactions(chainActive.Genesis(), true);
pwalletMain->ReacceptWalletTransactions(); pwallet->ReacceptWalletTransactions();
} }
return NullUniValue; return NullUniValue;
@ -252,7 +256,9 @@ UniValue importaddress(const JSONRPCRequest& request)
UniValue importprunedfunds(const JSONRPCRequest& request) UniValue importprunedfunds(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() != 2) if (request.fHelp || request.params.size() != 2)
@ -268,7 +274,7 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
if (!DecodeHexTx(tx, request.params[0].get_str())) if (!DecodeHexTx(tx, request.params[0].get_str()))
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
uint256 hashTx = tx.GetHash(); uint256 hashTx = tx.GetHash();
CWalletTx wtx(pwalletMain, MakeTransactionRef(std::move(tx))); CWalletTx wtx(pwallet, MakeTransactionRef(std::move(tx)));
CDataStream ssMB(ParseHexV(request.params[1], "proof"), SER_NETWORK, PROTOCOL_VERSION); CDataStream ssMB(ParseHexV(request.params[1], "proof"), SER_NETWORK, PROTOCOL_VERSION);
CMerkleBlock merkleBlock; CMerkleBlock merkleBlock;
@ -299,10 +305,10 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
wtx.nIndex = txnIndex; wtx.nIndex = txnIndex;
wtx.hashBlock = merkleBlock.header.GetHash(); wtx.hashBlock = merkleBlock.header.GetHash();
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
if (pwalletMain->IsMine(wtx)) { if (pwallet->IsMine(wtx)) {
pwalletMain->AddToWallet(wtx, false); pwallet->AddToWallet(wtx, false);
return NullUniValue; return NullUniValue;
} }
@ -311,7 +317,9 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
UniValue removeprunedfunds(const JSONRPCRequest& request) UniValue removeprunedfunds(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() != 1) if (request.fHelp || request.params.size() != 1)
@ -326,7 +334,7 @@ UniValue removeprunedfunds(const JSONRPCRequest& request)
+ HelpExampleRpc("removprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"") + HelpExampleRpc("removprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"")
); );
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
uint256 hash; uint256 hash;
hash.SetHex(request.params[0].get_str()); hash.SetHex(request.params[0].get_str());
@ -334,7 +342,7 @@ UniValue removeprunedfunds(const JSONRPCRequest& request)
vHash.push_back(hash); vHash.push_back(hash);
vector<uint256> vHashOut; vector<uint256> vHashOut;
if(pwalletMain->ZapSelectTx(vHash, vHashOut) != DB_LOAD_OK) { if(pwallet->ZapSelectTx(vHash, vHashOut) != DB_LOAD_OK) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Could not properly delete the transaction."); throw JSONRPCError(RPC_INTERNAL_ERROR, "Could not properly delete the transaction.");
} }
@ -347,7 +355,9 @@ UniValue removeprunedfunds(const JSONRPCRequest& request)
UniValue importpubkey(const JSONRPCRequest& request) UniValue importpubkey(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4) if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
@ -388,15 +398,15 @@ UniValue importpubkey(const JSONRPCRequest& request)
if (!pubKey.IsFullyValid()) if (!pubKey.IsFullyValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
ImportAddress(pwalletMain, CBitcoinAddress(pubKey.GetID()), strLabel); ImportAddress(pwallet, CBitcoinAddress(pubKey.GetID()), strLabel);
ImportScript(pwalletMain, GetScriptForRawPubKey(pubKey), strLabel, false); ImportScript(pwallet, GetScriptForRawPubKey(pubKey), strLabel, false);
if (fRescan) if (fRescan)
{ {
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); pwallet->ScanForWalletTransactions(chainActive.Genesis(), true);
pwalletMain->ReacceptWalletTransactions(); pwallet->ReacceptWalletTransactions();
} }
return NullUniValue; return NullUniValue;
@ -405,7 +415,9 @@ UniValue importpubkey(const JSONRPCRequest& request)
UniValue importwallet(const JSONRPCRequest& request) UniValue importwallet(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() != 1) if (request.fHelp || request.params.size() != 1)
@ -426,9 +438,9 @@ UniValue importwallet(const JSONRPCRequest& request)
if (fPruneMode) if (fPruneMode)
throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled in pruned mode"); throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled in pruned mode");
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwalletMain); EnsureWalletIsUnlocked(pwallet);
ifstream file; ifstream file;
file.open(request.params[0].get_str().c_str(), std::ios::in | std::ios::ate); file.open(request.params[0].get_str().c_str(), std::ios::in | std::ios::ate);
@ -442,9 +454,9 @@ UniValue importwallet(const JSONRPCRequest& request)
int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg()); int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
file.seekg(0, file.beg); file.seekg(0, file.beg);
pwalletMain->ShowProgress(_("Importing..."), 0); // show progress dialog in GUI pwallet->ShowProgress(_("Importing..."), 0); // show progress dialog in GUI
while (file.good()) { while (file.good()) {
pwalletMain->ShowProgress("", std::max(1, std::min(99, (int)(((double)file.tellg() / (double)nFilesize) * 100)))); pwallet->ShowProgress("", std::max(1, std::min(99, (int)(((double)file.tellg() / (double)nFilesize) * 100))));
std::string line; std::string line;
std::getline(file, line); std::getline(file, line);
if (line.empty() || line[0] == '#') if (line.empty() || line[0] == '#')
@ -461,7 +473,7 @@ UniValue importwallet(const JSONRPCRequest& request)
CPubKey pubkey = key.GetPubKey(); CPubKey pubkey = key.GetPubKey();
assert(key.VerifyPubKey(pubkey)); assert(key.VerifyPubKey(pubkey));
CKeyID keyid = pubkey.GetID(); CKeyID keyid = pubkey.GetID();
if (pwalletMain->HaveKey(keyid)) { if (pwallet->HaveKey(keyid)) {
LogPrintf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString()); LogPrintf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString());
continue; continue;
} }
@ -481,27 +493,27 @@ UniValue importwallet(const JSONRPCRequest& request)
} }
} }
LogPrintf("Importing %s...\n", CBitcoinAddress(keyid).ToString()); LogPrintf("Importing %s...\n", CBitcoinAddress(keyid).ToString());
if (!pwalletMain->AddKeyPubKey(key, pubkey)) { if (!pwallet->AddKeyPubKey(key, pubkey)) {
fGood = false; fGood = false;
continue; continue;
} }
pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime; pwallet->mapKeyMetadata[keyid].nCreateTime = nTime;
if (fLabel) if (fLabel)
pwalletMain->SetAddressBook(keyid, strLabel, "receive"); pwallet->SetAddressBook(keyid, strLabel, "receive");
nTimeBegin = std::min(nTimeBegin, nTime); nTimeBegin = std::min(nTimeBegin, nTime);
} }
file.close(); file.close();
pwalletMain->ShowProgress("", 100); // hide progress dialog in GUI pwallet->ShowProgress("", 100); // hide progress dialog in GUI
CBlockIndex *pindex = chainActive.Tip(); CBlockIndex *pindex = chainActive.Tip();
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200) while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200)
pindex = pindex->pprev; pindex = pindex->pprev;
pwalletMain->UpdateTimeFirstKey(nTimeBegin); pwallet->UpdateTimeFirstKey(nTimeBegin);
LogPrintf("Rescanning last %i blocks\n", chainActive.Height() - pindex->nHeight + 1); LogPrintf("Rescanning last %i blocks\n", chainActive.Height() - pindex->nHeight + 1);
pwalletMain->ScanForWalletTransactions(pindex); pwallet->ScanForWalletTransactions(pindex);
pwalletMain->MarkDirty(); pwallet->MarkDirty();
if (!fGood) if (!fGood)
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet"); throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
@ -511,7 +523,9 @@ UniValue importwallet(const JSONRPCRequest& request)
UniValue dumpprivkey(const JSONRPCRequest& request) UniValue dumpprivkey(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() != 1) if (request.fHelp || request.params.size() != 1)
@ -529,9 +543,9 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
+ HelpExampleRpc("dumpprivkey", "\"myaddress\"") + HelpExampleRpc("dumpprivkey", "\"myaddress\"")
); );
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwalletMain); EnsureWalletIsUnlocked(pwallet);
string strAddress = request.params[0].get_str(); string strAddress = request.params[0].get_str();
CBitcoinAddress address; CBitcoinAddress address;
@ -541,7 +555,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
if (!address.GetKeyID(keyID)) if (!address.GetKeyID(keyID))
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key"); throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
CKey vchSecret; CKey vchSecret;
if (!pwalletMain->GetKey(keyID, vchSecret)) if (!pwallet->GetKey(keyID, vchSecret))
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known"); throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
return CBitcoinSecret(vchSecret).ToString(); return CBitcoinSecret(vchSecret).ToString();
} }
@ -549,7 +563,9 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
UniValue dumpwallet(const JSONRPCRequest& request) UniValue dumpwallet(const JSONRPCRequest& request)
{ {
if (!EnsureWalletIsAvailable(pwalletMain, request.fHelp)) CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
return NullUniValue; return NullUniValue;
if (request.fHelp || request.params.size() != 1) if (request.fHelp || request.params.size() != 1)
@ -563,9 +579,9 @@ UniValue dumpwallet(const JSONRPCRequest& request)
+ HelpExampleRpc("dumpwallet", "\"test\"") + HelpExampleRpc("dumpwallet", "\"test\"")
); );
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwalletMain); EnsureWalletIsUnlocked(pwallet);
ofstream file; ofstream file;
file.open(request.params[0].get_str().c_str()); file.open(request.params[0].get_str().c_str());
@ -574,8 +590,8 @@ UniValue dumpwallet(const JSONRPCRequest& request)
std::map<CTxDestination, int64_t> mapKeyBirth; std::map<CTxDestination, int64_t> mapKeyBirth;
std::set<CKeyID> setKeyPool; std::set<CKeyID> setKeyPool;
pwalletMain->GetKeyBirthTimes(mapKeyBirth); pwallet->GetKeyBirthTimes(mapKeyBirth);
pwalletMain->GetAllReserveKeys(setKeyPool); pwallet->GetAllReserveKeys(setKeyPool);
// sort time/key pairs // sort time/key pairs
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth; std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
@ -595,11 +611,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
file << "\n"; file << "\n";
// add the base58check encoded extended master if the wallet uses HD // add the base58check encoded extended master if the wallet uses HD
CKeyID masterKeyID = pwalletMain->GetHDChain().masterKeyID; CKeyID masterKeyID = pwallet->GetHDChain().masterKeyID;
if (!masterKeyID.IsNull()) if (!masterKeyID.IsNull())
{ {
CKey key; CKey key;
if (pwalletMain->GetKey(masterKeyID, key)) if (pwallet->GetKey(masterKeyID, key))
{ {
CExtKey masterKey; CExtKey masterKey;
masterKey.SetMaster(key.begin(), key.size()); masterKey.SetMaster(key.begin(), key.size());
@ -615,20 +631,20 @@ UniValue dumpwallet(const JSONRPCRequest& request)
std::string strTime = EncodeDumpTime(it->first); std::string strTime = EncodeDumpTime(it->first);
std::string strAddr = CBitcoinAddress(keyid).ToString(); std::string strAddr = CBitcoinAddress(keyid).ToString();
CKey key; CKey key;
if (pwalletMain->GetKey(keyid, key)) { if (pwallet->GetKey(keyid, key)) {
file << strprintf("%s %s ", CBitcoinSecret(key).ToString(), strTime); file << strprintf("%s %s ", CBitcoinSecret(key).ToString(), strTime);
if (pwalletMain->mapAddressBook.count(keyid)) { if (pwallet->mapAddressBook.count(keyid)) {
file << strprintf("label=%s", EncodeDumpString(pwalletMain->mapAddressBook[keyid].name)); file << strprintf("label=%s", EncodeDumpString(pwallet->mapAddressBook[keyid].name));
} else if (keyid == masterKeyID) { } else if (keyid == masterKeyID) {
file << "hdmaster=1"; file << "hdmaster=1";
} else if (setKeyPool.count(keyid)) { } else if (setKeyPool.count(keyid)) {
file << "reserve=1"; file << "reserve=1";
} else if (pwalletMain->mapKeyMetadata[keyid].hdKeypath == "m") { } else if (pwallet->mapKeyMetadata[keyid].hdKeypath == "m") {
file << "inactivehdmaster=1"; file << "inactivehdmaster=1";
} else { } else {
file << "change=1"; file << "change=1";
} }
file << strprintf(" # addr=%s%s\n", strAddr, (pwalletMain->mapKeyMetadata[keyid].hdKeypath.size() > 0 ? " hdkeypath="+pwalletMain->mapKeyMetadata[keyid].hdKeypath : "")); file << strprintf(" # addr=%s%s\n", strAddr, (pwallet->mapKeyMetadata[keyid].hdKeypath.size() > 0 ? " hdkeypath="+pwallet->mapKeyMetadata[keyid].hdKeypath : ""));
} }
} }
file << "\n"; file << "\n";
@ -971,6 +987,8 @@ int64_t GetImportTimestamp(const UniValue& data, int64_t now)
UniValue importmulti(const JSONRPCRequest& mainRequest) UniValue importmulti(const JSONRPCRequest& mainRequest)
{ {
CWallet * const pwallet = GetWalletForJSONRPCRequest(mainRequest);
// clang-format off // clang-format off
if (mainRequest.fHelp || mainRequest.params.size() < 1 || mainRequest.params.size() > 2) if (mainRequest.fHelp || mainRequest.params.size() < 1 || mainRequest.params.size() > 2)
throw runtime_error( throw runtime_error(
@ -1009,7 +1027,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
" [{ \"success\": true } , { \"success\": false, \"error\": { \"code\": -1, \"message\": \"Internal Server Error\"} }, ... ]\n"); " [{ \"success\": true } , { \"success\": false, \"error\": { \"code\": -1, \"message\": \"Internal Server Error\"} }, ... ]\n");
// clang-format on // clang-format on
if (!EnsureWalletIsAvailable(pwalletMain, mainRequest.fHelp)) { if (!EnsureWalletIsAvailable(pwallet, mainRequest.fHelp)) {
return NullUniValue; return NullUniValue;
} }
@ -1028,8 +1046,8 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
} }
} }
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwalletMain); EnsureWalletIsUnlocked(pwallet);
// Verify all timestamps are present before importing any keys. // Verify all timestamps are present before importing any keys.
const int64_t now = chainActive.Tip() ? chainActive.Tip()->GetMedianTimePast() : 0; const int64_t now = chainActive.Tip() ? chainActive.Tip()->GetMedianTimePast() : 0;
@ -1051,7 +1069,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
BOOST_FOREACH (const UniValue& data, requests.getValues()) { BOOST_FOREACH (const UniValue& data, requests.getValues()) {
const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp); const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
const UniValue result = ProcessImport(pwalletMain, data, timestamp); const UniValue result = ProcessImport(pwallet, data, timestamp);
response.push_back(result); response.push_back(result);
if (!fRescan) { if (!fRescan) {
@ -1073,8 +1091,8 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
CBlockIndex* pindex = nLowestTimestamp > minimumTimestamp ? chainActive.FindEarliestAtLeast(std::max<int64_t>(nLowestTimestamp - 7200, 0)) : chainActive.Genesis(); CBlockIndex* pindex = nLowestTimestamp > minimumTimestamp ? chainActive.FindEarliestAtLeast(std::max<int64_t>(nLowestTimestamp - 7200, 0)) : chainActive.Genesis();
CBlockIndex* scannedRange = nullptr; CBlockIndex* scannedRange = nullptr;
if (pindex) { if (pindex) {
scannedRange = pwalletMain->ScanForWalletTransactions(pindex, true); scannedRange = pwallet->ScanForWalletTransactions(pindex, true);
pwalletMain->ReacceptWalletTransactions(); pwallet->ReacceptWalletTransactions();
} }
if (!scannedRange || scannedRange->nHeight > pindex->nHeight) { if (!scannedRange || scannedRange->nHeight > pindex->nHeight) {

480
src/wallet/rpcwallet.cpp

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save