Skip to content

Commit

Permalink
[RPC] Add coldstaking address support in importprivkey
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Jan 7, 2020
1 parent 51e7ea2 commit 144ec35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{"lockunspent", 0},
{"lockunspent", 1},
{"importprivkey", 2},
{"importprivkey", 3},
{"importaddress", 2},
{"verifychain", 0},
{"verifychain", 1},
Expand Down
38 changes: 21 additions & 17 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ std::string DecodeDumpString(const std::string& str)

UniValue importprivkey(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 3)
if (fHelp || params.size() < 1 || params.size() > 4)
throw std::runtime_error(
"importprivkey \"pivxprivkey\" ( \"label\" rescan )\n"
"\nAdds a private key (as returned by dumpprivkey) to your wallet.\n" +
HelpRequiringPassphrase() + "\n"

"\nArguments:\n"
"1. \"pivxprivkey\" (string, required) The private key (see dumpprivkey)\n"
"1. \"pivxprivkey\" (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"
"4. fStakingAddress (boolean, optional, default=false) Whether this key refers to a staking address\n"

"\nNote: This call can take minutes to complete if rescan is true.\n"

Expand All @@ -102,30 +103,28 @@ UniValue importprivkey(const UniValue& params, bool fHelp)

EnsureWalletIsUnlocked();

std::string strSecret = params[0].get_str();
std::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();
const std::string strSecret = params[0].get_str();
const std::string strLabel = (params.size() > 1 ? params[1].get_str() : "");
const bool fRescan = (params.size() > 2 ? params[2].get_bool() : true);
const bool fStakingAddress = (params.size() > 3 ? params[3].get_bool() : false);

CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(strSecret);

if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
if (!vchSecret.SetString(strSecret))
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");
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, AddressBook::AddressBookPurpose::RECEIVE);
pwalletMain->SetAddressBook(vchAddress, strLabel, (
fStakingAddress ?
AddressBook::AddressBookPurpose::COLD_STAKING :
AddressBook::AddressBookPurpose::RECEIVE));

// Don't throw error in case a key is already there
if (pwalletMain->HaveKey(vchAddress))
Expand All @@ -140,7 +139,12 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'

if (fRescan) {
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
CBlockIndex *pindex = chainActive.Genesis();
if (fStakingAddress) {
// cold staking was activated after nBlockTimeProtocolV2. No need to scan the whole chain
pindex = chainActive[Params().BlockStartTimeProtocolV2()];
}
pwalletMain->ScanForWalletTransactions(pindex, true);
}
}

Expand Down

0 comments on commit 144ec35

Please sign in to comment.