Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcdump.cpp
Show First 20 Lines • Show All 144 Lines • ▼ Show 20 Lines | if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | ||||
"private keys disabled"); | "private keys disabled"); | ||||
} | } | ||||
EnsureLegacyScriptPubKeyMan(*wallet, true); | EnsureLegacyScriptPubKeyMan(*wallet, true); | ||||
WalletRescanReserver reserver(*pwallet); | WalletRescanReserver reserver(*pwallet); | ||||
bool fRescan = true; | bool fRescan = true; | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | 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 160 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(); | ||||
} | } | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
CTxDestination dest = | CTxDestination dest = | ||||
DecodeDestination(request.params[0].get_str(), wallet->chainParams); | DecodeDestination(request.params[0].get_str(), wallet->chainParams); | ||||
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 - " | ||||
Show All 24 Lines | if (!request.params[3].isNull()) { | ||||
} else { | } else { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Invalid Bitcoin address or script"); | "Invalid Bitcoin address or script"); | ||||
} | } | ||||
} | } | ||||
if (fRescan) { | if (fRescan) { | ||||
RescanWallet(*pwallet, reserver); | RescanWallet(*pwallet, reserver); | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
pwallet->ReacceptWalletTransactions(); | pwallet->ReacceptWalletTransactions(); | ||||
} | } | ||||
} | } | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | UniValue importprunedfunds(const Config &config, | ||||
std::vector<uint256> vMatch; | std::vector<uint256> vMatch; | ||||
std::vector<size_t> vIndex; | std::vector<size_t> vIndex; | ||||
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != | if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != | ||||
merkleBlock.header.hashMerkleRoot) { | merkleBlock.header.hashMerkleRoot) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Something wrong with merkleblock"); | "Something wrong with merkleblock"); | ||||
} | } | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
int height; | int height; | ||||
if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), | if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), | ||||
merkleBlock.header.GetHash(), | merkleBlock.header.GetHash(), | ||||
FoundBlock().height(height))) { | FoundBlock().height(height))) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Block not found in chain"); | "Block not found in chain"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | RPCHelpMan{ | ||||
"6a512166eae7628734ea0a5\"") + | "6a512166eae7628734ea0a5\"") + | ||||
"\nAs a JSON-RPC call\n" + | "\nAs a JSON-RPC call\n" + | ||||
HelpExampleRpc("removeprunedfunds", | HelpExampleRpc("removeprunedfunds", | ||||
"\"a8d0c0184dde994a09ec054286f1ce581bebf4644" | "\"a8d0c0184dde994a09ec054286f1ce581bebf4644" | ||||
"6a512166eae7628734ea0a5\"")}, | "6a512166eae7628734ea0a5\"")}, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
TxId txid(ParseHashV(request.params[0], "txid")); | TxId txid(ParseHashV(request.params[0], "txid")); | ||||
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 82 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"); | ||||
} | } | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
std::set<CScript> script_pub_keys; | std::set<CScript> script_pub_keys; | ||||
for (const auto &dest : GetAllDestinationsForKey(pubKey)) { | for (const auto &dest : GetAllDestinationsForKey(pubKey)) { | ||||
script_pub_keys.insert(GetScriptForDestination(dest)); | script_pub_keys.insert(GetScriptForDestination(dest)); | ||||
} | } | ||||
pwallet->MarkDirty(); | pwallet->MarkDirty(); | ||||
pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, | pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, | ||||
true /* have_solving_data */, | true /* have_solving_data */, | ||||
true /* apply_label */, 1 /* timestamp */); | true /* apply_label */, 1 /* timestamp */); | ||||
pwallet->ImportPubKeys({pubKey.GetID()}, {{pubKey.GetID(), pubKey}}, | pwallet->ImportPubKeys({pubKey.GetID()}, {{pubKey.GetID(), pubKey}}, | ||||
{} /* key_origins */, false /* add_keypool */, | {} /* key_origins */, false /* add_keypool */, | ||||
false /* internal */, 1 /* timestamp */); | false /* internal */, 1 /* timestamp */); | ||||
} | } | ||||
if (fRescan) { | if (fRescan) { | ||||
RescanWallet(*pwallet, reserver); | RescanWallet(*pwallet, reserver); | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
pwallet->ReacceptWalletTransactions(); | pwallet->ReacceptWalletTransactions(); | ||||
} | } | ||||
} | } | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
Show All 39 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; | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
fsbridge::ifstream file; | fsbridge::ifstream file; | ||||
file.open(request.params[0].get_str(), std::ios::in | std::ios::ate); | file.open(request.params[0].get_str(), 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 164 Lines • ▼ Show 20 Lines | RPCHelpMan{ | ||||
RPCExamples{HelpExampleCli("dumpprivkey", "\"myaddress\"") + | RPCExamples{HelpExampleCli("dumpprivkey", "\"myaddress\"") + | ||||
HelpExampleCli("importprivkey", "\"mykey\"") + | HelpExampleCli("importprivkey", "\"mykey\"") + | ||||
HelpExampleRpc("dumpprivkey", "\"myaddress\"")}, | HelpExampleRpc("dumpprivkey", "\"myaddress\"")}, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*wallet); | LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(*wallet); | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore); | LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore); | ||||
EnsureWalletIsUnlocked(pwallet); | EnsureWalletIsUnlocked(pwallet); | ||||
std::string strAddress = request.params[0].get_str(); | std::string strAddress = request.params[0].get_str(); | ||||
CTxDestination dest = DecodeDestination(strAddress, wallet->chainParams); | CTxDestination dest = DecodeDestination(strAddress, wallet->chainParams); | ||||
if (!IsValidDestination(dest)) { | if (!IsValidDestination(dest)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | UniValue dumpwallet(const Config &config, const JSONRPCRequest &request) { | ||||
CWallet &wallet = *pwallet; | CWallet &wallet = *pwallet; | ||||
LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(wallet); | LegacyScriptPubKeyMan &spk_man = EnsureLegacyScriptPubKeyMan(wallet); | ||||
// Make sure the results are valid at least up to the most recent block | // Make sure the results are valid at least up to the most recent block | ||||
// the user could have gotten from another RPC command prior to now | // the user could have gotten from another RPC command prior to now | ||||
wallet.BlockUntilSyncedToCurrentChain(); | wallet.BlockUntilSyncedToCurrentChain(); | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK2(wallet.cs_wallet, spk_man.cs_KeyStore); | LOCK2(wallet.cs_wallet, spk_man.cs_KeyStore); | ||||
EnsureWalletIsUnlocked(&wallet); | EnsureWalletIsUnlocked(&wallet); | ||||
fs::path filepath = request.params[0].get_str(); | fs::path filepath = request.params[0].get_str(); | ||||
filepath = fs::absolute(filepath); | filepath = fs::absolute(filepath); | ||||
/** | /** | ||||
Show All 13 Lines | UniValue dumpwallet(const Config &config, const JSONRPCRequest &request) { | ||||
file.open(filepath); | file.open(filepath); | ||||
if (!file.is_open()) { | if (!file.is_open()) { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | throw JSONRPCError(RPC_INVALID_PARAMETER, | ||||
"Cannot open wallet dump file"); | "Cannot open wallet dump file"); | ||||
} | } | ||||
std::map<CKeyID, int64_t> mapKeyBirth; | std::map<CKeyID, int64_t> mapKeyBirth; | ||||
const std::map<CKeyID, int64_t> &mapKeyPool = spk_man.GetAllReserveKeys(); | const std::map<CKeyID, int64_t> &mapKeyPool = spk_man.GetAllReserveKeys(); | ||||
wallet.GetKeyBirthTimes(*locked_chain, mapKeyBirth); | wallet.GetKeyBirthTimes(mapKeyBirth); | ||||
std::set<CScriptID> scripts = spk_man.GetCScripts(); | std::set<CScriptID> scripts = spk_man.GetCScripts(); | ||||
// sort time/key pairs | // sort time/key pairs | ||||
std::vector<std::pair<int64_t, CKeyID>> vKeyBirth; | std::vector<std::pair<int64_t, CKeyID>> vKeyBirth; | ||||
for (const auto &entry : mapKeyBirth) { | for (const auto &entry : mapKeyBirth) { | ||||
vKeyBirth.push_back(std::make_pair(entry.second, entry.first)); | vKeyBirth.push_back(std::make_pair(entry.second, entry.first)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 769 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); | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | 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. | ||||
CHECK_NONFATAL(pwallet->chain().findBlock( | CHECK_NONFATAL(pwallet->chain().findBlock( | ||||
pwallet->GetLastBlockHash(), | pwallet->GetLastBlockHash(), | ||||
FoundBlock().time(nLowestTimestamp).mtpTime(now))); | FoundBlock().time(nLowestTimestamp).mtpTime(now))); | ||||
for (const UniValue &data : requests.getValues()) { | for (const UniValue &data : requests.getValues()) { | ||||
Show All 22 Lines | UniValue response(UniValue::VARR); | ||||
nLowestTimestamp = timestamp; | nLowestTimestamp = timestamp; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (fRescan && fRunScan && requests.size()) { | if (fRescan && fRunScan && requests.size()) { | ||||
int64_t scannedTime = pwallet->RescanFromTime( | int64_t scannedTime = pwallet->RescanFromTime( | ||||
nLowestTimestamp, reserver, true /* update */); | nLowestTimestamp, reserver, true /* update */); | ||||
{ | { | ||||
auto locked_chain = pwallet->chain().lock(); | |||||
LOCK(pwallet->cs_wallet); | LOCK(pwallet->cs_wallet); | ||||
pwallet->ReacceptWalletTransactions(); | pwallet->ReacceptWalletTransactions(); | ||||
} | } | ||||
if (pwallet->IsAbortingRescan()) { | if (pwallet->IsAbortingRescan()) { | ||||
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user."); | throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user."); | ||||
} | } | ||||
if (scannedTime > nLowestTimestamp) { | if (scannedTime > nLowestTimestamp) { | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |