Skip to content

Commit 144ec35

Browse files
committed
[RPC] Add coldstaking address support in importprivkey
1 parent 51e7ea2 commit 144ec35

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
106106
{"lockunspent", 0},
107107
{"lockunspent", 1},
108108
{"importprivkey", 2},
109+
{"importprivkey", 3},
109110
{"importaddress", 2},
110111
{"verifychain", 0},
111112
{"verifychain", 1},

src/wallet/rpcdump.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,17 @@ std::string DecodeDumpString(const std::string& str)
7575

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

8484
"\nArguments:\n"
85-
"1. \"pivxprivkey\" (string, required) The private key (see dumpprivkey)\n"
85+
"1. \"pivxprivkey\" (string, required) The private key (see dumpprivkey)\n"
8686
"2. \"label\" (string, optional, default=\"\") An optional label\n"
8787
"3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n"
88+
"4. fStakingAddress (boolean, optional, default=false) Whether this key refers to a staking address\n"
8889

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

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

103104
EnsureWalletIsUnlocked();
104105

105-
std::string strSecret = params[0].get_str();
106-
std::string strLabel = "";
107-
if (params.size() > 1)
108-
strLabel = params[1].get_str();
109-
110-
// Whether to perform rescan after import
111-
bool fRescan = true;
112-
if (params.size() > 2)
113-
fRescan = params[2].get_bool();
106+
const std::string strSecret = params[0].get_str();
107+
const std::string strLabel = (params.size() > 1 ? params[1].get_str() : "");
108+
const bool fRescan = (params.size() > 2 ? params[2].get_bool() : true);
109+
const bool fStakingAddress = (params.size() > 3 ? params[3].get_bool() : false);
114110

115111
CBitcoinSecret vchSecret;
116-
bool fGood = vchSecret.SetString(strSecret);
117-
118-
if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
112+
if (!vchSecret.SetString(strSecret))
113+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
119114

120115
CKey key = vchSecret.GetKey();
121-
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
116+
if (!key.IsValid())
117+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
122118

123119
CPubKey pubkey = key.GetPubKey();
124120
assert(key.VerifyPubKey(pubkey));
125121
CKeyID vchAddress = pubkey.GetID();
126122
{
127123
pwalletMain->MarkDirty();
128-
pwalletMain->SetAddressBook(vchAddress, strLabel, AddressBook::AddressBookPurpose::RECEIVE);
124+
pwalletMain->SetAddressBook(vchAddress, strLabel, (
125+
fStakingAddress ?
126+
AddressBook::AddressBookPurpose::COLD_STAKING :
127+
AddressBook::AddressBookPurpose::RECEIVE));
129128

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

142141
if (fRescan) {
143-
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
142+
CBlockIndex *pindex = chainActive.Genesis();
143+
if (fStakingAddress) {
144+
// cold staking was activated after nBlockTimeProtocolV2. No need to scan the whole chain
145+
pindex = chainActive[Params().BlockStartTimeProtocolV2()];
146+
}
147+
pwalletMain->ScanForWalletTransactions(pindex, true);
144148
}
145149
}
146150

0 commit comments

Comments
 (0)