15 #include <boost/algorithm/string.hpp>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 #include "json/json_spirit_value.h"
29 static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
30 static const std::locale loc(std::locale::classic(),
31 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
32 std::istringstream iss(str);
34 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
36 if (ptime.is_not_a_date_time())
38 return (ptime - epoch).total_seconds();
42 std::stringstream ret;
43 BOOST_FOREACH(
unsigned char c, str) {
44 if (c <= 32 || c >= 128 || c ==
'%') {
45 ret <<
'%' <<
HexStr(&c, &c + 1);
54 std::stringstream ret;
55 for (
unsigned int pos = 0; pos < str.length(); pos++) {
56 unsigned char c = str[pos];
57 if (c ==
'%' && pos+2 < str.length()) {
58 c = (((str[pos+1]>>6)*9+((str[pos+1]-
'0')&15)) << 4) |
59 ((str[pos+2]>>6)*9+((str[pos+2]-
'0')&15));
69 if (fHelp || params.size() < 1 || params.size() > 3)
71 "importprivkey \"bitcoinprivkey\" ( \"label\" rescan )\n"
72 "\nAdds a private key (as returned by dumpprivkey) to your wallet.\n"
74 "1. \"bitcoinprivkey\" (string, required) The private key (see dumpprivkey)\n"
75 "2. \"label\" (string, optional) an optional label\n"
76 "3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n"
78 "\nDump a private key\n"
80 "\nImport the private key\n"
82 "\nImport using a label\n"
84 "\nAs a json rpc call\n"
90 string strSecret = params[0].get_str();
92 if (params.size() > 1)
93 strLabel = params[1].get_str();
97 if (params.size() > 2)
98 fRescan = params[2].get_bool();
101 bool fGood = vchSecret.
SetString(strSecret);
138 if (fHelp || params.size() != 1)
140 "importwallet \"filename\"\n"
141 "\nImports keys from a wallet dump file (see dumpwallet).\n"
143 "1. \"filename\" (string, required) The wallet file\n"
145 "\nDump the wallet\n"
147 "\nImport the wallet\n"
149 "\nImport using the json rpc call\n"
156 file.open(params[0].get_str().c_str(), std::ios::in | std::ios::ate);
164 int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
165 file.seekg(0, file.beg);
168 while (file.good()) {
169 pwalletMain->
ShowProgress(
"", std::max(1, std::min(99, (
int)(((
double)file.tellg() / (double)nFilesize) * 100))));
171 std::getline(file, line);
172 if (line.empty() || line[0] ==
'#')
175 std::vector<std::string> vstr;
176 boost::split(vstr, line, boost::is_any_of(
" "));
190 std::string strLabel;
192 for (
unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
193 if (boost::algorithm::starts_with(vstr[nStr],
"#"))
195 if (vstr[nStr] ==
"change=1")
197 if (vstr[nStr] ==
"reserve=1")
199 if (boost::algorithm::starts_with(vstr[nStr],
"label=")) {
212 nTimeBegin = std::min(nTimeBegin, nTime);
218 while (pindex && pindex->
pprev && pindex->
nTime > nTimeBegin - 7200)
219 pindex = pindex->
pprev;
236 if (fHelp || params.size() != 1)
238 "dumpprivkey \"bitcoinaddress\"\n"
239 "\nReveals the private key corresponding to 'bitcoinaddress'.\n"
240 "Then the importprivkey can be used with this output\n"
242 "1. \"bitcoinaddress\" (string, required) The bitcoin address for the private key\n"
244 "\"key\" (string) The private key\n"
253 string strAddress = params[0].get_str();
269 if (fHelp || params.size() != 1)
271 "dumpwallet \"filename\"\n"
272 "\nDumps all wallet keys in a human-readable format.\n"
274 "1. \"filename\" (string, required) The filename\n"
283 file.open(params[0].get_str().c_str());
287 std::map<CKeyID, int64_t> mapKeyBirth;
288 std::set<CKeyID> setKeyPool;
293 std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
294 for (std::map<CKeyID, int64_t>::const_iterator it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
295 vKeyBirth.push_back(std::make_pair(it->second, it->first));
298 std::sort(vKeyBirth.begin(), vKeyBirth.end());
306 for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
307 const CKeyID &keyid = it->second;
314 }
else if (setKeyPool.count(keyid)) {
322 file <<
"# End of dump\n";
static std::string EncodeDumpString(const std::string &str)
std::string DecodeDumpString(const std::string &str)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
static int64_t DecodeDumpTime(const std::string &str)
bool HaveKey(const CKeyID &address) const
std::map< CTxDestination, CAddressBookData > mapAddressBook
CCriticalSection cs_wallet
Main wallet lock.
std::string HelpExampleRpc(string methodname, string args)
Value importwallet(const Array ¶ms, bool fHelp)
void GetAllReserveKeys(std::set< CKeyID > &setAddress) const
Value dumpwallet(const Array ¶ms, bool fHelp)
Object JSONRPCError(int code, const string &message)
base58-encoded Bitcoin addresses.
void GetKeyBirthTimes(std::map< CKeyID, int64_t > &mapKeyBirth) const
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
bool GetKey(const CKeyID &address, CKey &keyOut) const
CChain chainActive
The currently-connected chain of blocks.
Value importprivkey(const Array ¶ms, bool fHelp)
bool GetKeyID(CKeyID &keyID) const
bool SetString(const char *pszSecret)
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
int Height() const
Return the maximal height in the chain.
void EnsureWalletIsUnlocked()
CPubKey GetPubKey() const
A base58-encoded secret key.
An encapsulated public key.
std::string ToString() const
bool SetString(const char *psz, unsigned int nVersionBytes=1)
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or NULL if none.
int ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false)
The block chain is a tree shaped structure starting with the genesis block at the root...
const std::string CLIENT_BUILD
std::string ToString() const
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
A reference to a CKey: the Hash160 of its serialized public key.
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
static std::string EncodeDumpTime(int64_t nTime)
std::string HelpExampleCli(string methodname, string args)
An encapsulated private key.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
const std::string CLIENT_DATE
uint256 GetBlockHash() const
Value dumpprivkey(const Array ¶ms, bool fHelp)