Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcdump.cpp
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <core_io.h> | #include <core_io.h> | ||||
#include <interfaces/chain.h> | |||||
#include <key_io.h> | #include <key_io.h> | ||||
#include <merkleblock.h> | #include <merkleblock.h> | ||||
#include <rpc/server.h> | #include <rpc/server.h> | ||||
#include <script/script.h> | #include <script/script.h> | ||||
#include <script/standard.h> | #include <script/standard.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Cannot import private keys to a wallet with " | "Cannot import private keys to a wallet with " | ||||
"private keys disabled"); | "private keys disabled"); | ||||
} | } | ||||
WalletRescanReserver reserver(pwallet); | WalletRescanReserver reserver(pwallet); | ||||
bool fRescan = true; | bool fRescan = true; | ||||
{ | { | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
std::string strSecret = request.params[0].get_str(); | std::string strSecret = request.params[0].get_str(); | ||||
std::string strLabel = ""; | std::string strLabel = ""; | ||||
if (!request.params[1].isNull()) { | if (!request.params[1].isNull()) { | ||||
strLabel = request.params[1].get_str(); | strLabel = request.params[1].get_str(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 195 Lines • ▼ Show 20 Lines | UniValue importaddress(const Config &config, const JSONRPCRequest &request) { | ||||
// Whether to import a p2sh version, too | // Whether to import a p2sh version, too | ||||
bool fP2SH = false; | bool fP2SH = false; | ||||
if (!request.params[3].isNull()) { | if (!request.params[3].isNull()) { | ||||
fP2SH = request.params[3].get_bool(); | fP2SH = request.params[3].get_bool(); | ||||
} | } | ||||
{ | { | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
CTxDestination dest = DecodeDestination(request.params[0].get_str(), | CTxDestination dest = DecodeDestination(request.params[0].get_str(), | ||||
config.GetChainParams()); | config.GetChainParams()); | ||||
if (IsValidDestination(dest)) { | if (IsValidDestination(dest)) { | ||||
if (fP2SH) { | if (fP2SH) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Cannot use the p2sh flag with an address - " | "Cannot use the p2sh flag with an address - " | ||||
"use a script instead"); | "use a script instead"); | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | UniValue importprunedfunds(const Config &config, | ||||
// Search partial merkle tree in proof for our transaction and index in | // Search partial merkle tree in proof for our transaction and index in | ||||
// valid block | // valid block | ||||
std::vector<uint256> vMatch; | std::vector<uint256> vMatch; | ||||
std::vector<size_t> vIndex; | std::vector<size_t> vIndex; | ||||
size_t txnIndex = 0; | size_t txnIndex = 0; | ||||
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == | if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == | ||||
merkleBlock.header.hashMerkleRoot) { | merkleBlock.header.hashMerkleRoot) { | ||||
LOCK(cs_main); | auto locked_chain = pwallet->chain().lock(); | ||||
const CBlockIndex *pindex = | const CBlockIndex *pindex = | ||||
LookupBlockIndex(merkleBlock.header.GetHash()); | LookupBlockIndex(merkleBlock.header.GetHash()); | ||||
if (!pindex || !chainActive.Contains(pindex)) { | if (!pindex || !chainActive.Contains(pindex)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Block not found in chain"); | "Block not found in chain"); | ||||
} | } | ||||
std::vector<uint256>::const_iterator it; | std::vector<uint256>::const_iterator it; | ||||
if ((it = std::find(vMatch.begin(), vMatch.end(), txid)) == | if ((it = std::find(vMatch.begin(), vMatch.end(), txid)) == | ||||
vMatch.end()) { | vMatch.end()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Transaction given doesn't exist in proof"); | "Transaction given doesn't exist in proof"); | ||||
} | } | ||||
txnIndex = vIndex[it - vMatch.begin()]; | txnIndex = vIndex[it - vMatch.begin()]; | ||||
} else { | } else { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Something wrong with merkleblock"); | "Something wrong with merkleblock"); | ||||
} | } | ||||
wtx.nIndex = txnIndex; | wtx.nIndex = txnIndex; | ||||
wtx.hashBlock = merkleBlock.header.GetHash(); | wtx.hashBlock = merkleBlock.header.GetHash(); | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
if (pwallet->IsMine(*wtx.tx)) { | if (pwallet->IsMine(*wtx.tx)) { | ||||
pwallet->AddToWallet(wtx, false); | pwallet->AddToWallet(wtx, false); | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
throw JSONRPCError( | throw JSONRPCError( | ||||
RPC_INVALID_ADDRESS_OR_KEY, | RPC_INVALID_ADDRESS_OR_KEY, | ||||
Show All 22 Lines | if (request.fHelp || request.params.size() != 1) { | ||||
"ce581bebf46446a512166eae762873" | "ce581bebf46446a512166eae762873" | ||||
"4ea0a5\"") + | "4ea0a5\"") + | ||||
"\nAs a JSON-RPC call\n" + | "\nAs a JSON-RPC call\n" + | ||||
HelpExampleRpc("removeprunedfunds", | HelpExampleRpc("removeprunedfunds", | ||||
"\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166" | "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166" | ||||
"eae7628734ea0a5\"")); | "eae7628734ea0a5\"")); | ||||
} | } | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
TxId txid; | TxId txid; | ||||
txid.SetHex(request.params[0].get_str()); | txid.SetHex(request.params[0].get_str()); | ||||
std::vector<TxId> txIds; | std::vector<TxId> txIds; | ||||
txIds.push_back(txid); | txIds.push_back(txid); | ||||
std::vector<TxId> txIdsOut; | std::vector<TxId> txIdsOut; | ||||
if (pwallet->ZapSelectTx(txIds, txIdsOut) != DBErrors::LOAD_OK) { | if (pwallet->ZapSelectTx(txIds, txIdsOut) != DBErrors::LOAD_OK) { | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | UniValue importpubkey(const Config &config, const JSONRPCRequest &request) { | ||||
std::vector<uint8_t> data(ParseHex(request.params[0].get_str())); | std::vector<uint8_t> data(ParseHex(request.params[0].get_str())); | ||||
CPubKey pubKey(data.begin(), data.end()); | CPubKey pubKey(data.begin(), data.end()); | ||||
if (!pubKey.IsFullyValid()) { | if (!pubKey.IsFullyValid()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Pubkey is not a valid public key"); | "Pubkey is not a valid public key"); | ||||
} | } | ||||
{ | { | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
for (const auto &dest : GetAllDestinationsForKey(pubKey)) { | for (const auto &dest : GetAllDestinationsForKey(pubKey)) { | ||||
ImportAddress(pwallet, dest, strLabel); | ImportAddress(pwallet, dest, strLabel); | ||||
} | } | ||||
ImportScript(pwallet, GetScriptForRawPubKey(pubKey), strLabel, false); | ImportScript(pwallet, GetScriptForRawPubKey(pubKey), strLabel, false); | ||||
pwallet->LearnAllRelatedScripts(pubKey); | pwallet->LearnAllRelatedScripts(pubKey); | ||||
} | } | ||||
if (fRescan) { | if (fRescan) { | ||||
Show All 36 Lines | if (!reserver.reserve()) { | ||||
throw JSONRPCError( | throw JSONRPCError( | ||||
RPC_WALLET_ERROR, | RPC_WALLET_ERROR, | ||||
"Wallet is currently rescanning. Abort existing rescan or wait."); | "Wallet is currently rescanning. Abort existing rescan or wait."); | ||||
} | } | ||||
int64_t nTimeBegin = 0; | int64_t nTimeBegin = 0; | ||||
bool fGood = true; | bool fGood = true; | ||||
{ | { | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
std::ifstream file; | std::ifstream file; | ||||
file.open(request.params[0].get_str().c_str(), | file.open(request.params[0].get_str().c_str(), | ||||
std::ios::in | std::ios::ate); | std::ios::in | std::ios::ate); | ||||
if (!file.is_open()) { | if (!file.is_open()) { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | throw JSONRPCError(RPC_INVALID_PARAMETER, | ||||
▲ Show 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | if (request.fHelp || request.params.size() != 1) { | ||||
"\nResult:\n" | "\nResult:\n" | ||||
"\"key\" (string) The private key\n" | "\"key\" (string) The private key\n" | ||||
"\nExamples:\n" + | "\nExamples:\n" + | ||||
HelpExampleCli("dumpprivkey", "\"myaddress\"") + | HelpExampleCli("dumpprivkey", "\"myaddress\"") + | ||||
HelpExampleCli("importprivkey", "\"mykey\"") + | HelpExampleCli("importprivkey", "\"mykey\"") + | ||||
HelpExampleRpc("dumpprivkey", "\"myaddress\"")); | HelpExampleRpc("dumpprivkey", "\"myaddress\"")); | ||||
} | } | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
std::string strAddress = request.params[0].get_str(); | std::string strAddress = request.params[0].get_str(); | ||||
CTxDestination dest = | CTxDestination dest = | ||||
DecodeDestination(strAddress, config.GetChainParams()); | DecodeDestination(strAddress, config.GetChainParams()); | ||||
if (!IsValidDestination(dest)) { | if (!IsValidDestination(dest)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
Show All 37 Lines | if (request.fHelp || request.params.size() != 1) | ||||
"{ (json object)\n" | "{ (json object)\n" | ||||
" \"filename\" : { (string) The filename with full " | " \"filename\" : { (string) The filename with full " | ||||
"absolute path\n" | "absolute path\n" | ||||
"}\n" | "}\n" | ||||
"\nExamples:\n" + | "\nExamples:\n" + | ||||
HelpExampleCli("dumpwallet", "\"test\"") + | HelpExampleCli("dumpwallet", "\"test\"") + | ||||
HelpExampleRpc("dumpwallet", "\"test\"")); | HelpExampleRpc("dumpwallet", "\"test\"")); | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
fs::path filepath = request.params[0].get_str(); | fs::path filepath = request.params[0].get_str(); | ||||
filepath = fs::absolute(filepath); | filepath = fs::absolute(filepath); | ||||
/** | /** | ||||
* Prevent arbitrary files from being overwritten. There have been reports | * Prevent arbitrary files from being overwritten. There have been reports | ||||
▲ Show 20 Lines • Show All 564 Lines • ▼ Show 20 Lines | if (fRescan && !reserver.reserve()) { | ||||
"Wallet is currently rescanning. Abort existing rescan or wait."); | "Wallet is currently rescanning. Abort existing rescan or wait."); | ||||
} | } | ||||
int64_t now = 0; | int64_t now = 0; | ||||
bool fRunScan = false; | bool fRunScan = false; | ||||
int64_t nLowestTimestamp = 0; | int64_t nLowestTimestamp = 0; | ||||
UniValue response(UniValue::VARR); | UniValue response(UniValue::VARR); | ||||
{ | { | ||||
LOCK2(cs_main, pwallet->cs_wallet); | auto locked_chain = pwallet->chain().lock(); | ||||
LOCK(pwallet->cs_wallet); | |||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
// Verify all timestamps are present before importing any keys. | // Verify all timestamps are present before importing any keys. | ||||
now = chainActive.Tip() ? chainActive.Tip()->GetMedianTimePast() : 0; | now = chainActive.Tip() ? chainActive.Tip()->GetMedianTimePast() : 0; | ||||
for (const UniValue &data : requests.getValues()) { | for (const UniValue &data : requests.getValues()) { | ||||
GetImportTimestamp(data, now); | GetImportTimestamp(data, now); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |