Skip to content

Commit a1d7df3

Browse files
committed
Add importpubkey method to import a watch-only pubkey
1 parent 907a425 commit a1d7df3

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/rpcclient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
8888
{ "importprivkey", 2 },
8989
{ "importaddress", 2 },
9090
{ "importaddress", 3 },
91+
{ "importpubkey", 2 },
9192
{ "verifychain", 0 },
9293
{ "verifychain", 1 },
9394
{ "keypoolrefill", 0 },

src/rpcserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ static const CRPCCommand vRPCCommands[] =
359359
{ "wallet", "importprivkey", &importprivkey, true },
360360
{ "wallet", "importwallet", &importwallet, true },
361361
{ "wallet", "importaddress", &importaddress, true },
362+
{ "wallet", "importpubkey", &importpubkey, true },
362363
{ "wallet", "keypoolrefill", &keypoolrefill, true },
363364
{ "wallet", "listaccounts", &listaccounts, false },
364365
{ "wallet", "listaddressgroupings", &listaddressgroupings, false },

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ extern UniValue clearbanned(const UniValue& params, bool fHelp);
161161
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
162162
extern UniValue importprivkey(const UniValue& params, bool fHelp);
163163
extern UniValue importaddress(const UniValue& params, bool fHelp);
164+
extern UniValue importpubkey(const UniValue& params, bool fHelp);
164165
extern UniValue dumpwallet(const UniValue& params, bool fHelp);
165166
extern UniValue importwallet(const UniValue& params, bool fHelp);
166167

src/wallet/rpcdump.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,63 @@ UniValue importaddress(const UniValue& params, bool fHelp)
237237
return NullUniValue;
238238
}
239239

240+
UniValue importpubkey(const UniValue& params, bool fHelp)
241+
{
242+
if (!EnsureWalletIsAvailable(fHelp))
243+
return NullUniValue;
244+
245+
if (fHelp || params.size() < 1 || params.size() > 4)
246+
throw runtime_error(
247+
"importpubkey \"pubkey\" ( \"label\" rescan )\n"
248+
"\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend.\n"
249+
"\nArguments:\n"
250+
"1. \"pubkey\" (string, required) The hex-encoded public key\n"
251+
"2. \"label\" (string, optional, default=\"\") An optional label\n"
252+
"3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n"
253+
"\nNote: This call can take minutes to complete if rescan is true.\n"
254+
"\nExamples:\n"
255+
"\nImport a public key with rescan\n"
256+
+ HelpExampleCli("importpubkey", "\"mypubkey\"") +
257+
"\nImport using a label without rescan\n"
258+
+ HelpExampleCli("importpubkey", "\"mypubkey\" \"testing\" false") +
259+
"\nAs a JSON-RPC call\n"
260+
+ HelpExampleRpc("importpubkey", "\"mypubkey\", \"testing\", false")
261+
);
262+
263+
if (fPruneMode)
264+
throw JSONRPCError(RPC_WALLET_ERROR, "Importing public keys is disabled in pruned mode");
265+
266+
string strLabel = "";
267+
if (params.size() > 1)
268+
strLabel = params[1].get_str();
269+
270+
// Whether to perform rescan after import
271+
bool fRescan = true;
272+
if (params.size() > 2)
273+
fRescan = params[2].get_bool();
274+
275+
if (!IsHex(params[0].get_str()))
276+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey must be a hex string");
277+
std::vector<unsigned char> data(ParseHex(params[0].get_str()));
278+
CPubKey pubKey(data.begin(), data.end());
279+
if (!pubKey.IsFullyValid())
280+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
281+
282+
LOCK2(cs_main, pwalletMain->cs_wallet);
283+
284+
ImportAddress(CBitcoinAddress(pubKey.GetID()), strLabel);
285+
ImportScript(GetScriptForRawPubKey(pubKey), strLabel, false);
286+
287+
if (fRescan)
288+
{
289+
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
290+
pwalletMain->ReacceptWalletTransactions();
291+
}
292+
293+
return NullUniValue;
294+
}
295+
296+
240297
UniValue importwallet(const UniValue& params, bool fHelp)
241298
{
242299
if (!EnsureWalletIsAvailable(fHelp))

0 commit comments

Comments
 (0)