|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "base58.h"
|
|
|
|
#include "rpcserver.h"
|
|
|
|
#include "init.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "script/script.h"
|
|
|
|
#include "script/standard.h"
|
|
|
|
#include "sync.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
10 years ago
|
|
|
#include "util.h"
|
|
|
|
#include "utiltime.h"
|
|
|
|
#include "wallet.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
|
|
|
|
#include "univalue/univalue.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void EnsureWalletIsUnlocked();
|
|
|
|
bool EnsureWalletIsAvailable(bool avoidException);
|
|
|
|
|
|
|
|
UniValue dumpwallet_impl(const UniValue& params, bool fHelp, bool fDumpZKeys);
|
|
|
|
UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys);
|
|
|
|
|
|
|
|
|
|
|
|
std::string static EncodeDumpTime(int64_t nTime) {
|
|
|
|
return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t static DecodeDumpTime(const std::string &str) {
|
|
|
|
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
|
|
|
|
static const std::locale loc(std::locale::classic(),
|
|
|
|
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
|
|
|
|
std::istringstream iss(str);
|
|
|
|
iss.imbue(loc);
|
|
|
|
boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
|
|
|
|
iss >> ptime;
|
|
|
|
if (ptime.is_not_a_date_time())
|
|
|
|
return 0;
|
|
|
|
return (ptime - epoch).total_seconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string static EncodeDumpString(const std::string &str) {
|
|
|
|
std::stringstream ret;
|
|
|
|
BOOST_FOREACH(unsigned char c, str) {
|
|
|
|
if (c <= 32 || c >= 128 || c == '%') {
|
|
|
|
ret << '%' << HexStr(&c, &c + 1);
|
|
|
|
} else {
|
|
|
|
ret << c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DecodeDumpString(const std::string &str) {
|
|
|
|
std::stringstream ret;
|
|
|
|
for (unsigned int pos = 0; pos < str.length(); pos++) {
|
|
|
|
unsigned char c = str[pos];
|
|
|
|
if (c == '%' && pos+2 < str.length()) {
|
|
|
|
c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) |
|
|
|
|
((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
ret << c;
|
|
|
|
}
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue importprivkey(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() < 1 || params.size() > 3)
|
|
|
|
throw runtime_error(
|
|
|
|
"importprivkey \"zcashprivkey\" ( \"label\" rescan )\n"
|
|
|
|
"\nAdds a private key (as returned by dumpprivkey) to your wallet.\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"zcashprivkey\" (string, required) The private key (see dumpprivkey)\n"
|
|
|
|
"2. \"label\" (string, optional, default=\"\") An optional label\n"
|
|
|
|
"3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n"
|
|
|
|
"\nNote: This call can take minutes to complete if rescan is true.\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
"\nDump a private key\n"
|
|
|
|
+ HelpExampleCli("dumpprivkey", "\"myaddress\"") +
|
|
|
|
"\nImport the private key with rescan\n"
|
|
|
|
+ HelpExampleCli("importprivkey", "\"mykey\"") +
|
|
|
|
"\nImport using a label and without rescan\n"
|
|
|
|
+ HelpExampleCli("importprivkey", "\"mykey\" \"testing\" false") +
|
|
|
|
"\nAs a JSON-RPC call\n"
|
|
|
|
+ HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
|
|
|
|
);
|
|
|
|
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
EnsureWalletIsUnlocked();
|
|
|
|
|
|
|
|
string strSecret = params[0].get_str();
|
|
|
|
string strLabel = "";
|
|
|
|
if (params.size() > 1)
|
|
|
|
strLabel = params[1].get_str();
|
|
|
|
|
|
|
|
// Whether to perform rescan after import
|
|
|
|
bool fRescan = true;
|
|
|
|
if (params.size() > 2)
|
|
|
|
fRescan = params[2].get_bool();
|
|
|
|
|
|
|
|
CBitcoinSecret vchSecret;
|
|
|
|
bool fGood = vchSecret.SetString(strSecret);
|
|
|
|
|
|
|
|
if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
|
|
|
|
|
|
|
|
CKey key = vchSecret.GetKey();
|
|
|
|
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
|
|
|
|
|
|
|
|
CPubKey pubkey = key.GetPubKey();
|
|
|
|
assert(key.VerifyPubKey(pubkey));
|
|
|
|
CKeyID vchAddress = pubkey.GetID();
|
|
|
|
{
|
|
|
|
pwalletMain->MarkDirty();
|
|
|
|
pwalletMain->SetAddressBook(vchAddress, strLabel, "receive");
|
|
|
|
|
|
|
|
// Don't throw error in case a key is already there
|
|
|
|
if (pwalletMain->HaveKey(vchAddress))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;
|
|
|
|
|
|
|
|
if (!pwalletMain->AddKeyPubKey(key, pubkey))
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
|
|
|
|
|
|
|
|
// whenever a key is imported, we need to scan the whole chain
|
|
|
|
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
|
|
|
|
|
|
|
|
if (fRescan) {
|
|
|
|
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NullUniValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue importaddress(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() < 1 || params.size() > 3)
|
|
|
|
throw runtime_error(
|
|
|
|
"importaddress \"address\" ( \"label\" rescan )\n"
|
|
|
|
"\nAdds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"address\" (string, required) The address\n"
|
|
|
|
"2. \"label\" (string, optional, default=\"\") An optional label\n"
|
|
|
|
"3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n"
|
|
|
|
"\nNote: This call can take minutes to complete if rescan is true.\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
"\nImport an address with rescan\n"
|
|
|
|
+ HelpExampleCli("importaddress", "\"myaddress\"") +
|
|
|
|
"\nImport using a label without rescan\n"
|
|
|
|
+ HelpExampleCli("importaddress", "\"myaddress\" \"testing\" false") +
|
|
|
|
"\nAs a JSON-RPC call\n"
|
|
|
|
+ HelpExampleRpc("importaddress", "\"myaddress\", \"testing\", false")
|
|
|
|
);
|
|
|
|
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
CScript script;
|
|
|
|
|
|
|
|
CBitcoinAddress address(params[0].get_str());
|
|
|
|
if (address.IsValid()) {
|
|
|
|
script = GetScriptForDestination(address.Get());
|
|
|
|
} else if (IsHex(params[0].get_str())) {
|
|
|
|
std::vector<unsigned char> data(ParseHex(params[0].get_str()));
|
|
|
|
script = CScript(data.begin(), data.end());
|
|
|
|
} else {
|
|
|
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address or script");
|
|
|
|
}
|
|
|
|
|
|
|
|
string strLabel = "";
|
|
|
|
if (params.size() > 1)
|
|
|
|
strLabel = params[1].get_str();
|
|
|
|
|
|
|
|
// Whether to perform rescan after import
|
|
|
|
bool fRescan = true;
|
|
|
|
if (params.size() > 2)
|
|
|
|
fRescan = params[2].get_bool();
|
|
|
|
|
|
|
|
{
|
|
|
|
if (::IsMine(*pwalletMain, script) == ISMINE_SPENDABLE)
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
|
|
|
|
|
|
|
|
// add to address book or update label
|
|
|
|
if (address.IsValid())
|
|
|
|
pwalletMain->SetAddressBook(address.Get(), strLabel, "receive");
|
|
|
|
|
|
|
|
// Don't throw error in case an address is already there
|
|
|
|
if (pwalletMain->HaveWatchOnly(script))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
pwalletMain->MarkDirty();
|
|
|
|
|
|
|
|
if (!pwalletMain->AddWatchOnly(script))
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
|
|
|
|
|
|
|
|
if (fRescan)
|
|
|
|
{
|
|
|
|
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
|
|
|
|
pwalletMain->ReacceptWalletTransactions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NullUniValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue z_importwallet(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() != 1)
|
|
|
|
throw runtime_error(
|
|
|
|
"z_importwallet \"filename\"\n"
|
|
|
|
"\nImports taddr and zaddr keys from a wallet export file (see z_exportwallet).\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"filename\" (string, required) The wallet file\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
"\nDump the wallet\n"
|
|
|
|
+ HelpExampleCli("z_exportwallet", "\"test\"") +
|
|
|
|
"\nImport the wallet\n"
|
|
|
|
+ HelpExampleCli("z_importwallet", "\"test\"") +
|
|
|
|
"\nImport using the json rpc call\n"
|
|
|
|
+ HelpExampleRpc("z_importwallet", "\"test\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
return importwallet_impl(params, fHelp, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue importwallet(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() != 1)
|
|
|
|
throw runtime_error(
|
|
|
|
"importwallet \"filename\"\n"
|
|
|
|
"\nImports taddr keys from a wallet dump file (see dumpwallet).\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"filename\" (string, required) The wallet file\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
"\nDump the wallet\n"
|
|
|
|
+ HelpExampleCli("dumpwallet", "\"test\"") +
|
|
|
|
"\nImport the wallet\n"
|
|
|
|
+ HelpExampleCli("importwallet", "\"test\"") +
|
|
|
|
"\nImport using the json rpc call\n"
|
|
|
|
+ HelpExampleRpc("importwallet", "\"test\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
return importwallet_impl(params, fHelp, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys)
|
|
|
|
{
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
EnsureWalletIsUnlocked();
|
|
|
|
|
|
|
|
ifstream file;
|
|
|
|
file.open(params[0].get_str().c_str(), std::ios::in | std::ios::ate);
|
|
|
|
if (!file.is_open())
|
|
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
|
|
|
|
|
|
|
|
int64_t nTimeBegin = chainActive.Tip()->GetBlockTime();
|
|
|
|
|
|
|
|
bool fGood = true;
|
|
|
|
|
|
|
|
int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
|
|
|
|
file.seekg(0, file.beg);
|
|
|
|
|
|
|
|
pwalletMain->ShowProgress(_("Importing..."), 0); // show progress dialog in GUI
|
|
|
|
while (file.good()) {
|
|
|
|
pwalletMain->ShowProgress("", std::max(1, std::min(99, (int)(((double)file.tellg() / (double)nFilesize) * 100))));
|
|
|
|
std::string line;
|
|
|
|
std::getline(file, line);
|
|
|
|
if (line.empty() || line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<std::string> vstr;
|
|
|
|
boost::split(vstr, line, boost::is_any_of(" "));
|
|
|
|
if (vstr.size() < 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Let's see if the address is a valid Zcash spending key
|
|
|
|
if (fImportZKeys) {
|
|
|
|
try {
|
|
|
|
CZCSpendingKey spendingkey(vstr[0]);
|
|
|
|
libzcash::SpendingKey key = spendingkey.Get();
|
|
|
|
libzcash::PaymentAddress addr = key.address();
|
|
|
|
if (pwalletMain->HaveSpendingKey(addr)) {
|
|
|
|
LogPrint("zrpc", "Skipping import of zaddr %s (key already present)\n", CZCPaymentAddress(addr).ToString());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int64_t nTime = DecodeDumpTime(vstr[1]);
|
|
|
|
LogPrint("zrpc", "Importing zaddr %s...\n", CZCPaymentAddress(addr).ToString());
|
|
|
|
if (!pwalletMain->AddZKey(key)) {
|
|
|
|
// Something went wrong
|
|
|
|
fGood = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Successfully imported zaddr. Now import the metadata.
|
|
|
|
pwalletMain->mapZKeyMetadata[addr].nCreateTime = nTime;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (const std::runtime_error &e) {
|
|
|
|
LogPrint("zrpc","Importing detected an error: %s\n", e.what());
|
|
|
|
// Not a valid spending key, so carry on and see if it's a Zcash style address.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CBitcoinSecret vchSecret;
|
|
|
|
if (!vchSecret.SetString(vstr[0]))
|
|
|
|
continue;
|
|
|
|
CKey key = vchSecret.GetKey();
|
|
|
|
CPubKey pubkey = key.GetPubKey();
|
|
|
|
assert(key.VerifyPubKey(pubkey));
|
|
|
|
CKeyID keyid = pubkey.GetID();
|
|
|
|
if (pwalletMain->HaveKey(keyid)) {
|
|
|
|
LogPrintf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int64_t nTime = DecodeDumpTime(vstr[1]);
|
|
|
|
std::string strLabel;
|
|
|
|
bool fLabel = true;
|
|
|
|
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
|
|
|
|
if (boost::algorithm::starts_with(vstr[nStr], "#"))
|
|
|
|
break;
|
|
|
|
if (vstr[nStr] == "change=1")
|
|
|
|
fLabel = false;
|
|
|
|
if (vstr[nStr] == "reserve=1")
|
|
|
|
fLabel = false;
|
|
|
|
if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
|
|
|
|
strLabel = DecodeDumpString(vstr[nStr].substr(6));
|
|
|
|
fLabel = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogPrintf("Importing %s...\n", CBitcoinAddress(keyid).ToString());
|
|
|
|
if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
|
|
|
|
fGood = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime;
|
|
|
|
if (fLabel)
|
|
|
|
pwalletMain->SetAddressBook(keyid, strLabel, "receive");
|
|
|
|
nTimeBegin = std::min(nTimeBegin, nTime);
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
pwalletMain->ShowProgress("", 100); // hide progress dialog in GUI
|
|
|
|
|
|
|
|
CBlockIndex *pindex = chainActive.Tip();
|
|
|
|
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200)
|
|
|
|
pindex = pindex->pprev;
|
|
|
|
|
|
|
|
if (!pwalletMain->nTimeFirstKey || nTimeBegin < pwalletMain->nTimeFirstKey)
|
|
|
|
pwalletMain->nTimeFirstKey = nTimeBegin;
|
|
|
|
|
|
|
|
LogPrintf("Rescanning last %i blocks\n", chainActive.Height() - pindex->nHeight + 1);
|
|
|
|
pwalletMain->ScanForWalletTransactions(pindex);
|
|
|
|
pwalletMain->MarkDirty();
|
|
|
|
|
|
|
|
if (!fGood)
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
|
|
|
|
|
|
|
|
return NullUniValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue dumpprivkey(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() != 1)
|
|
|
|
throw runtime_error(
|
|
|
|
"dumpprivkey \"zcashaddress\"\n"
|
|
|
|
"\nReveals the private key corresponding to 'zcashaddress'.\n"
|
|
|
|
"Then the importprivkey can be used with this output\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"zcashaddress\" (string, required) The zcash address for the private key\n"
|
|
|
|
"\nResult:\n"
|
|
|
|
"\"key\" (string) The private key\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
+ HelpExampleCli("dumpprivkey", "\"myaddress\"")
|
|
|
|
+ HelpExampleCli("importprivkey", "\"mykey\"")
|
|
|
|
+ HelpExampleRpc("dumpprivkey", "\"myaddress\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
EnsureWalletIsUnlocked();
|
|
|
|
|
|
|
|
string strAddress = params[0].get_str();
|
|
|
|
CBitcoinAddress address;
|
|
|
|
if (!address.SetString(strAddress))
|
|
|
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address");
|
|
|
|
CKeyID keyID;
|
|
|
|
if (!address.GetKeyID(keyID))
|
|
|
|
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
|
|
|
|
CKey vchSecret;
|
|
|
|
if (!pwalletMain->GetKey(keyID, vchSecret))
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
|
|
|
|
return CBitcoinSecret(vchSecret).ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UniValue z_exportwallet(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() != 1)
|
|
|
|
throw runtime_error(
|
|
|
|
"z_exportwallet \"filename\"\n"
|
|
|
|
"\nExports all wallet keys, for taddr and zaddr, in a human-readable format.\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"filename\" (string, required) The filename, saved in folder set by zcashd -exportdir option\n"
|
|
|
|
"\nResult:\n"
|
|
|
|
"\"path\" (string) The full path of the destination file\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
+ HelpExampleCli("z_exportwallet", "\"test\"")
|
|
|
|
+ HelpExampleRpc("z_exportwallet", "\"test\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
return dumpwallet_impl(params, fHelp, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue dumpwallet(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() != 1)
|
|
|
|
throw runtime_error(
|
|
|
|
"dumpwallet \"filename\"\n"
|
|
|
|
"\nDumps taddr wallet keys in a human-readable format.\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"filename\" (string, required) The filename, saved in folder set by zcashd -exportdir option\n"
|
|
|
|
"\nResult:\n"
|
|
|
|
"\"path\" (string) The full path of the destination file\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
+ HelpExampleCli("dumpwallet", "\"test\"")
|
|
|
|
+ HelpExampleRpc("dumpwallet", "\"test\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
return dumpwallet_impl(params, fHelp, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue dumpwallet_impl(const UniValue& params, bool fHelp, bool fDumpZKeys)
|
|
|
|
{
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
EnsureWalletIsUnlocked();
|
|
|
|
|
|
|
|
boost::filesystem::path exportdir;
|
|
|
|
try {
|
|
|
|
exportdir = GetExportDir();
|
|
|
|
} catch (const std::runtime_error& e) {
|
|
|
|
throw JSONRPCError(RPC_INTERNAL_ERROR, e.what());
|
|
|
|
}
|
|
|
|
if (exportdir.empty()) {
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot export wallet until the zcashd -exportdir option has been set");
|
|
|
|
}
|
|
|
|
std::string unclean = params[0].get_str();
|
|
|
|
std::string clean = SanitizeFilename(unclean);
|
|
|
|
if (clean.compare(unclean) != 0) {
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Filename is invalid as only alphanumeric characters are allowed. Try '%s' instead.", clean));
|
|
|
|
}
|
|
|
|
boost::filesystem::path exportfilepath = exportdir / clean;
|
|
|
|
|
|
|
|
ofstream file;
|
|
|
|
file.open(exportfilepath.string().c_str());
|
|
|
|
if (!file.is_open())
|
|
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
|
|
|
|
|
|
|
|
std::map<CKeyID, int64_t> mapKeyBirth;
|
|
|
|
std::set<CKeyID> setKeyPool;
|
|
|
|
pwalletMain->GetKeyBirthTimes(mapKeyBirth);
|
|
|
|
pwalletMain->GetAllReserveKeys(setKeyPool);
|
|
|
|
|
|
|
|
// sort time/key pairs
|
|
|
|
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
|
|
|
|
for (std::map<CKeyID, int64_t>::const_iterator it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
|
|
|
|
vKeyBirth.push_back(std::make_pair(it->second, it->first));
|
|
|
|
}
|
|
|
|
mapKeyBirth.clear();
|
|
|
|
std::sort(vKeyBirth.begin(), vKeyBirth.end());
|
|
|
|
|
|
|
|
// produce output
|
|
|
|
file << strprintf("# Wallet dump created by Zcash %s (%s)\n", CLIENT_BUILD, CLIENT_DATE);
|
|
|
|
file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()));
|
|
|
|
file << strprintf("# * Best block at time of backup was %i (%s),\n", chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString());
|
|
|
|
file << strprintf("# mined on %s\n", EncodeDumpTime(chainActive.Tip()->GetBlockTime()));
|
|
|
|
file << "\n";
|
|
|
|
for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
|
|
|
|
const CKeyID &keyid = it->second;
|
|
|
|
std::string strTime = EncodeDumpTime(it->first);
|
|
|
|
std::string strAddr = CBitcoinAddress(keyid).ToString();
|
|
|
|
CKey key;
|
|
|
|
if (pwalletMain->GetKey(keyid, key)) {
|
|
|
|
if (pwalletMain->mapAddressBook.count(keyid)) {
|
|
|
|
file << strprintf("%s %s label=%s # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, EncodeDumpString(pwalletMain->mapAddressBook[keyid].name), strAddr);
|
|
|
|
} else if (setKeyPool.count(keyid)) {
|
|
|
|
file << strprintf("%s %s reserve=1 # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, strAddr);
|
|
|
|
} else {
|
|
|
|
file << strprintf("%s %s change=1 # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, strAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file << "\n";
|
|
|
|
|
|
|
|
if (fDumpZKeys) {
|
|
|
|
std::set<libzcash::PaymentAddress> addresses;
|
|
|
|
pwalletMain->GetPaymentAddresses(addresses);
|
|
|
|
file << "\n";
|
|
|
|
file << "# Zkeys\n";
|
|
|
|
file << "\n";
|
|
|
|
for (auto addr : addresses ) {
|
|
|
|
libzcash::SpendingKey key;
|
|
|
|
if (pwalletMain->GetSpendingKey(addr, key)) {
|
|
|
|
std::string strTime = EncodeDumpTime(pwalletMain->mapZKeyMetadata[addr].nCreateTime);
|
|
|
|
file << strprintf("%s %s # zaddr=%s\n", CZCSpendingKey(key).ToString(), strTime, CZCPaymentAddress(addr).ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
file << "# End of dump\n";
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
return exportfilepath.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UniValue z_importkey(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() < 1 || params.size() > 2)
|
|
|
|
throw runtime_error(
|
|
|
|
"z_importkey \"zkey\" ( rescan )\n"
|
|
|
|
"\nAdds a zkey (as returned by z_exportkey) to your wallet.\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"zkey\" (string, required) The zkey (see z_exportkey)\n"
|
|
|
|
"2. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n"
|
|
|
|
"\nNote: This call can take minutes to complete if rescan is true.\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
"\nExport a zkey\n"
|
|
|
|
+ HelpExampleCli("z_exportkey", "\"myaddress\"") +
|
|
|
|
"\nImport the zkey with rescan\n"
|
|
|
|
+ HelpExampleCli("z_importkey", "\"mykey\"") +
|
|
|
|
"\nAs a JSON-RPC call\n"
|
|
|
|
+ HelpExampleRpc("z_importkey", "\"mykey\", false")
|
|
|
|
);
|
|
|
|
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
EnsureWalletIsUnlocked();
|
|
|
|
|
|
|
|
// Whether to perform rescan after import
|
|
|
|
bool fRescan = true;
|
|
|
|
if (params.size() > 1)
|
|
|
|
fRescan = params[1].get_bool();
|
|
|
|
|
|
|
|
string strSecret = params[0].get_str();
|
|
|
|
CZCSpendingKey spendingkey(strSecret);
|
|
|
|
auto key = spendingkey.Get();
|
|
|
|
auto addr = key.address();
|
|
|
|
|
|
|
|
{
|
|
|
|
// Don't throw error in case a key is already there
|
|
|
|
if (pwalletMain->HaveSpendingKey(addr))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
pwalletMain->MarkDirty();
|
|
|
|
|
|
|
|
if (!pwalletMain-> AddZKey(key))
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet");
|
|
|
|
|
|
|
|
pwalletMain->mapZKeyMetadata[addr].nCreateTime = 1;
|
|
|
|
|
|
|
|
// whenever a key is imported, we need to scan the whole chain
|
|
|
|
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
|
|
|
|
|
|
|
|
// We want to scan for transactions and notes
|
|
|
|
if (fRescan) {
|
|
|
|
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NullUniValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UniValue z_exportkey(const UniValue& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (!EnsureWalletIsAvailable(fHelp))
|
|
|
|
return NullUniValue;
|
|
|
|
|
|
|
|
if (fHelp || params.size() != 1)
|
|
|
|
throw runtime_error(
|
|
|
|
"z_exportkey \"zaddr\"\n"
|
|
|
|
"\nReveals the zkey corresponding to 'zaddr'.\n"
|
|
|
|
"Then the z_importkey can be used with this output\n"
|
|
|
|
"\nArguments:\n"
|
|
|
|
"1. \"zaddr\" (string, required) The zaddr for the private key\n"
|
|
|
|
"\nResult:\n"
|
|
|
|
"\"key\" (string) The private key\n"
|
|
|
|
"\nExamples:\n"
|
|
|
|
+ HelpExampleCli("z_exportkey", "\"myaddress\"")
|
|
|
|
+ HelpExampleCli("z_importkey", "\"mykey\"")
|
|
|
|
+ HelpExampleRpc("z_exportkey", "\"myaddress\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
EnsureWalletIsUnlocked();
|
|
|
|
|
|
|
|
string strAddress = params[0].get_str();
|
|
|
|
|
|
|
|
CZCPaymentAddress address(strAddress);
|
|
|
|
auto addr = address.Get();
|
|
|
|
|
|
|
|
libzcash::SpendingKey k;
|
|
|
|
if (!pwalletMain->GetSpendingKey(addr, k))
|
|
|
|
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
|
|
|
|
|
|
|
|
CZCSpendingKey spendingkey(k);
|
|
|
|
return spendingkey.ToString();
|
|
|
|
}
|
|
|
|
|