aboutsummaryrefslogtreecommitdiff
path: root/src/rpcdump.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/rpcdump.cpp')
-rw-r--r--src/rpcdump.cpp41
1 files changed, 24 insertions, 17 deletions
diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp
index 53cec9a6f..9e1d47846 100644
--- a/src/rpcdump.cpp
+++ b/src/rpcdump.cpp
@@ -1,11 +1,9 @@
-// Copyright (c) 2009-2013 The Bitcoin developers
+// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-
#include "base58.h"
-#include "bitcoinrpc.h"
+#include "rpcserver.h"
#include "init.h"
#include "main.h"
#include "sync.h"
@@ -25,13 +23,13 @@ using namespace std;
void EnsureWalletIsUnlocked();
std::string static EncodeDumpTime(int64_t nTime) {
- return DateTimeStrFormat("%Y-%m-%"PRId64"T%H:%M:%SZ", nTime);
+ return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
}
int64_t static DecodeDumpTime(const std::string &str) {
- static boost::posix_time::time_input_facet facet("%Y-%m-%dT%H:%M:%SZ");
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
- const std::locale loc(std::locale::classic(), &facet);
+ 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);
@@ -88,6 +86,8 @@ Value importprivkey(const Array& params, bool fHelp)
+ HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
);
+ EnsureWalletIsUnlocked();
+
string strSecret = params[0].get_str();
string strLabel = "";
if (params.size() > 1)
@@ -101,9 +101,11 @@ Value importprivkey(const Array& params, bool fHelp)
CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(strSecret);
- if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
+ 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();
CKeyID vchAddress = pubkey.GetID();
{
@@ -116,9 +118,14 @@ Value importprivkey(const Array& params, bool fHelp)
if (pwalletMain->HaveKey(vchAddress))
return Value::null;
+ 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);
pwalletMain->ReacceptWalletTransactions();
@@ -173,7 +180,7 @@ Value importwallet(const Array& params, bool fHelp)
CPubKey pubkey = key.GetPubKey();
CKeyID keyid = pubkey.GetID();
if (pwalletMain->HaveKey(keyid)) {
- LogPrintf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString().c_str());
+ LogPrintf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString());
continue;
}
int64_t nTime = DecodeDumpTime(vstr[1]);
@@ -191,7 +198,7 @@ Value importwallet(const Array& params, bool fHelp)
fLabel = true;
}
}
- LogPrintf("Importing %s...\n", CBitcoinAddress(keyid).ToString().c_str());
+ LogPrintf("Importing %s...\n", CBitcoinAddress(keyid).ToString());
if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
fGood = false;
continue;
@@ -285,10 +292,10 @@ Value dumpwallet(const Array& params, bool fHelp)
std::sort(vKeyBirth.begin(), vKeyBirth.end());
// produce output
- file << strprintf("# Wallet dump created by Bitcoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
- file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
- file << strprintf("# * Best block at time of backup was %i (%s),\n", chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString().c_str());
- file << strprintf("# mined on %s\n", EncodeDumpTime(chainActive.Tip()->nTime).c_str());
+ file << strprintf("# Wallet dump created by Bitcoin %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()->nTime));
file << "\n";
for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
const CKeyID &keyid = it->second;
@@ -297,11 +304,11 @@ Value dumpwallet(const Array& params, bool fHelp)
CKey key;
if (pwalletMain->GetKey(keyid, key)) {
if (pwalletMain->mapAddressBook.count(keyid)) {
- file << strprintf("%s %s label=%s # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), EncodeDumpString(pwalletMain->mapAddressBook[keyid].name).c_str(), strAddr.c_str());
+ 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().c_str(), strTime.c_str(), strAddr.c_str());
+ 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().c_str(), strTime.c_str(), strAddr.c_str());
+ file << strprintf("%s %s change=1 # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, strAddr);
}
}
}