10 #include "validation.h" 21 #include <boost/algorithm/string.hpp> 22 #include <boost/date_time/posix_time/posix_time.hpp> 26 #include <boost/foreach.hpp> 38 static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
39 static const std::locale loc(std::locale::classic(),
40 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
41 std::istringstream iss(str);
43 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
45 if (ptime.is_not_a_date_time())
47 return (ptime - epoch).total_seconds();
51 std::stringstream ret;
52 BOOST_FOREACH(
unsigned char c, str) {
53 if (c <= 32 || c >= 128 || c ==
'%') {
54 ret <<
'%' <<
HexStr(&c, &c + 1);
63 std::stringstream ret;
64 for (
unsigned int pos = 0; pos < str.length(); pos++) {
65 unsigned char c = str[pos];
66 if (c ==
'%' && pos+2 < str.length()) {
67 c = (((str[pos+1]>>6)*9+((str[pos+1]-
'0')&15)) << 4) |
68 ((str[pos+2]>>6)*9+((str[pos+2]-
'0')&15));
81 if (fHelp || params.
size() < 1 || params.
size() > 3)
83 "importprivkey \"dashprivkey\" ( \"label\" rescan )\n" 84 "\nAdds a private key (as returned by dumpprivkey) to your wallet.\n" 86 "1. \"dashprivkey\" (string, required) The private key (see dumpprivkey)\n" 87 "2. \"label\" (string, optional, default=\"\") An optional label\n" 88 "3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n" 89 "\nNote: This call can take minutes to complete if rescan is true.\n" 91 "\nDump a private key\n" 93 "\nImport the private key with rescan\n" 95 "\nImport using a label and without rescan\n" 97 "\nAs a JSON-RPC call\n" 106 string strSecret = params[0].
get_str();
107 string strLabel =
"";
108 if (params.
size() > 1)
109 strLabel = params[1].get_str();
113 if (params.
size() > 2)
114 fRescan = params[2].get_bool();
120 bool fGood = vchSecret.
SetString(strSecret);
128 assert(
key.VerifyPubKey(pubkey));
165 if (isRedeemScript) {
186 if (fHelp || params.
size() < 1 || params.
size() > 4)
188 "importaddress \"address\" ( \"label\" rescan p2sh )\n" 189 "\nAdds a script (in hex) or address that can be watched as if it were in your wallet but cannot be used to spend.\n" 191 "1. \"script\" (string, required) The hex-encoded script (or address)\n" 192 "2. \"label\" (string, optional, default=\"\") An optional label\n" 193 "3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n" 194 "4. p2sh (boolean, optional, default=false) Add the P2SH version of the script as well\n" 195 "\nNote: This call can take minutes to complete if rescan is true.\n" 196 "If you have the full public key, you should call importpublickey instead of this.\n" 198 "\nImport a script with rescan\n" 200 "\nImport using a label without rescan\n" 201 +
HelpExampleCli(
"importaddress",
"\"myscript\" \"testing\" false") +
202 "\nAs a JSON-RPC call\n" 203 +
HelpExampleRpc(
"importaddress",
"\"myscript\", \"testing\", false")
207 string strLabel =
"";
208 if (params.
size() > 1)
209 strLabel = params[1].get_str();
213 if (params.
size() > 2)
214 fRescan = params[2].get_bool();
221 if (params.
size() > 3)
222 fP2SH = params[3].get_bool();
231 }
else if (
IsHex(params[0].get_str())) {
232 std::vector<unsigned char>
data(
ParseHex(params[0].get_str()));
252 if (fHelp || params.
size() < 1 || params.
size() > 4)
254 "importpubkey \"pubkey\" ( \"label\" rescan )\n" 255 "\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend.\n" 257 "1. \"pubkey\" (string, required) The hex-encoded public key\n" 258 "2. \"label\" (string, optional, default=\"\") An optional label\n" 259 "3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n" 260 "\nNote: This call can take minutes to complete if rescan is true.\n" 262 "\nImport a public key with rescan\n" 264 "\nImport using a label without rescan\n" 265 +
HelpExampleCli(
"importpubkey",
"\"mypubkey\" \"testing\" false") +
266 "\nAs a JSON-RPC call\n" 267 +
HelpExampleRpc(
"importpubkey",
"\"mypubkey\", \"testing\", false")
271 string strLabel =
"";
272 if (params.
size() > 1)
273 strLabel = params[1].get_str();
277 if (params.
size() > 2)
278 fRescan = params[2].get_bool();
283 if (!
IsHex(params[0].get_str()))
285 std::vector<unsigned char>
data(
ParseHex(params[0].get_str()));
287 if (!pubKey.IsFullyValid())
310 if (fHelp || params.
size() != 1)
312 "importwallet \"filename\"\n" 313 "\nImports keys from a wallet dump file (see dumpwallet).\n" 315 "1. \"filename\" (string, required) The wallet file\n" 317 "\nDump the wallet\n" 319 "\nImport the wallet\n" 321 "\nImport using the json rpc call\n" 333 file.open(params[0].get_str().c_str(), std::ios::in | std::ios::ate);
341 int64_t nFilesize = std::max((int64_t)1, (int64_t)
file.tellg());
345 while (
file.good()) {
348 std::getline(
file, line);
349 if (line.empty() || line[0] ==
'#')
352 std::vector<std::string> vstr;
353 boost::split(vstr, line, boost::is_any_of(
" "));
361 assert(
key.VerifyPubKey(pubkey));
368 std::string strLabel;
370 for (
unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
371 if (boost::algorithm::starts_with(vstr[nStr],
"#"))
373 if (vstr[nStr] ==
"change=1")
375 if (vstr[nStr] ==
"reserve=1")
377 if (boost::algorithm::starts_with(vstr[nStr],
"label=")) {
390 nTimeBegin = std::min(nTimeBegin, nTime);
397 pindex = pindex->
pprev;
417 if (fHelp || params.
size() < 1 || params.
size() > 2)
419 "importelectrumwallet \"filename\" index\n" 420 "\nImports keys from an Electrum wallet export file (.csv or .json)\n" 422 "1. \"filename\" (string, required) The Electrum wallet export file, should be in csv or json format\n" 423 "2. index (numeric, optional, default=0) Rescan the wallet for transactions starting from this block index\n" 425 "\nImport the wallet\n" 428 "\nImport using the json rpc call\n" 441 std::string strFileName = params[0].
get_str();
442 size_t nDotPos = strFileName.find_last_of(
".");
443 if(nDotPos == string::npos)
446 std::string strFileExt = strFileName.substr(nDotPos+1);
447 if(strFileExt !=
"json" && strFileExt !=
"csv")
450 file.open(strFileName.c_str(), std::ios::in | std::ios::ate);
456 int64_t nFilesize = std::max((int64_t)1, (int64_t)
file.tellg());
461 if(strFileExt ==
"csv") {
462 while (
file.good()) {
465 std::getline(
file, line);
466 if (line.empty() || line ==
"address,private_key")
468 std::vector<std::string> vstr;
469 boost::split(vstr, line, boost::is_any_of(
","));
477 assert(
key.VerifyPubKey(pubkey));
491 char* buffer =
new char [nFilesize];
492 file.read(buffer, nFilesize);
494 if(!
data.read(buffer))
498 std::vector<std::string> vKeys =
data.getKeys();
500 for (
size_t i = 0; i <
data.size(); i++) {
502 if(!
data[vKeys[i]].isStr())
509 assert(
key.VerifyPubKey(pubkey));
526 int nStartHeight = 0;
527 if (params.
size() > 1)
528 nStartHeight = params[1].get_int();
533 int nTimeBegin =
chainActive[nStartHeight]->GetBlockTime();
551 if (fHelp || params.
size() != 1)
553 "dumpprivkey \"dashaddress\"\n" 554 "\nReveals the private key corresponding to 'dashaddress'.\n" 555 "Then the importprivkey can be used with this output\n" 557 "1. \"dashaddress\" (string, required) The dash address for the private key\n" 559 "\"key\" (string) The private key\n" 570 string strAddress = params[0].
get_str();
588 if (fHelp || params.
size() != 0)
591 "Returns an object containing sensitive private info about this HD wallet.\n" 594 " \"hdseed\": \"seed\", (string) The HD seed (bip32, in hex)\n" 595 " \"mnemonic\": \"words\", (string) The mnemonic for this HD wallet (bip39, english words) \n" 596 " \"mnemonicpassphrase\": \"passphrase\", (string) The mnemonic passphrase for this HD wallet (bip39)\n" 616 hdChainCurrent.
GetMnemonic(ssMnemonic, ssMnemonicPassphrase);
621 obj.
push_back(
Pair(
"mnemonicpassphrase", ssMnemonicPassphrase.c_str()));
634 if (fHelp || params.
size() != 1)
636 "dumpwallet \"filename\"\n" 637 "\nDumps all wallet keys in a human-readable format.\n" 639 "1. \"filename\" (string, required) The filename\n" 650 file.open(params[0].get_str().c_str());
654 std::map<CKeyID, int64_t> mapKeyBirth;
655 std::set<CKeyID> setKeyPool;
660 std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
661 for (std::map<CKeyID, int64_t>::const_iterator it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
662 vKeyBirth.push_back(std::make_pair(it->second, it->first));
665 std::sort(vKeyBirth.begin(), vKeyBirth.end());
684 hdChainCurrent.
GetMnemonic(ssMnemonic, ssMnemonicPassphrase);
685 file <<
"# mnemonic: " << ssMnemonic <<
"\n";
686 file <<
"# mnemonic passphrase: " << ssMnemonicPassphrase <<
"\n\n";
689 file <<
"# HD seed: " <<
HexStr(vchSeed) <<
"\n\n";
692 masterKey.
SetMaster(&vchSeed[0], vchSeed.size());
695 b58extkey.
SetKey(masterKey);
697 file <<
"# extended private masterkey: " << b58extkey.
ToString() <<
"\n";
700 masterPubkey = masterKey.
Neuter();
703 b58extpubkey.
SetKey(masterPubkey);
704 file <<
"# extended public masterkey: " << b58extpubkey.
ToString() <<
"\n\n";
713 file <<
"# WARNING: ACCOUNT " << i <<
" IS MISSING!" <<
"\n\n";
718 for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
719 const CKeyID &keyid = it->second;
727 }
else if (setKeyPool.count(keyid)) {
736 file <<
"# End of dump\n";
static std::string EncodeDumpString(const std::string &str)
bool SetString(const char *pszSecret)
std::string DecodeDumpString(const std::string &str)
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
static int64_t DecodeDumpTime(const std::string &str)
UniValue importaddress(const UniValue ¶ms, bool fHelp)
UniValue importprivkey(const UniValue ¶ms, bool fHelp)
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
void ImportScript(const CScript &script, const string &strLabel, bool isRedeemScript)
CCriticalSection cs_wallet
bool GetMnemonic(SecureVector &vchMnemonicRet, SecureVector &vchMnemonicPassphraseRet) const
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
isminetype IsMine(const CKeyStore &keystore, const CTxDestination &dest)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
SecureVector GetSeed() const
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
bool GetDecryptedHDChain(CHDChain &hdChainRet)
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
uint32_t nInternalChainCounter
bool IsHex(const string &str)
void ImportAddress(const CBitcoinAddress &address, const string &strLabel)
UniValue importpubkey(const UniValue ¶ms, bool fHelp)
bool push_back(const UniValue &val)
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Ran out of memory during operation.
std::string ToString() const
bool AddCScript(const CScript &redeemScript)
Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki.
bool GetKey(const CKeyID &address, CKey &keyOut) const
GetKey implementation that can derive a HD private key on the fly.
bool GetAccount(uint32_t nAccountIndex, CHDAccount &hdAccountRet)
bool GetHDChain(CHDChain &hdChainRet) const
CExtPubKey Neuter() const
bool AddWatchOnly(const CScript &dest)
Adds a watch-only address to the store, and saves it to disk.
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
void EnsureWalletIsUnlocked()
UniValue importelectrumwallet(const UniValue ¶ms, bool fHelp)
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
bool HaveKey(const CKeyID &address) const
HaveKey implementation that also checks the mapHdPubKeys.
std::vector< unsigned char, secure_allocator< unsigned char > > SecureVector
uint32_t nExternalChainCounter
UniValue dumpwallet(const UniValue ¶ms, bool fHelp)
Server is in safe mode, and command is not allowed in safe mode.
UniValue importwallet(const UniValue ¶ms, bool fHelp)
CBlockIndex * Genesis() const
vector< unsigned char > ParseHex(const char *psz)
void GetAllReserveKeys(std::set< CKeyID > &setAddress) const
std::string ToString() const
Unexpected type was passed as parameter.
CScript GetScriptForDestination(const CTxDestination &dest)
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
std::map< CKeyID, CHDPubKey > mapHdPubKeys
bool SetString(const char *psz, unsigned int nVersionBytes=1)
const std::string CLIENT_DATE
CTxDestination Get() const
int ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false)
uint256 GetBlockHash() const
void ReacceptWalletTransactions()
UniValue dumpprivkey(const UniValue ¶ms, bool fHelp)
const UniValue NullUniValue
bool GetKeyID(CKeyID &keyID) const
std::map< CTxDestination, CAddressBookData > mapAddressBook
CBlockIndex * Tip() const
bool EnsureWalletIsAvailable(bool avoidException)
static std::string EncodeDumpTime(int64_t nTime)
int64_t GetBlockTime() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int64_t GetTime()
For unit testing.
virtual bool HaveCScript(const CScriptID &hash) const
UniValue dumphdinfo(const UniValue ¶ms, bool fHelp)
int nHeight
height of the entry in the chain. The genesis block has height 0
virtual bool HaveWatchOnly(const CScript &dest) const
UniValue JSONRPCError(int code, const string &message)
const std::string CLIENT_BUILD
void GetKeyBirthTimes(std::map< CKeyID, int64_t > &mapKeyBirth) const
std::string get_str() const
std::string _(const char *psz)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, and saves it to disk.
void SetKey(const K &key)