Skip to content

Commit 7b4eb6d

Browse files
committed
Add importpubkey method to import a watch-only pubkey
- backports bitcoin/bitcoin@a1d7df3
1 parent 816dabb commit 7b4eb6d

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
113113
{"importprivkey", 3},
114114
{"importaddress", 2},
115115
{"importaddress", 3},
116+
{"importpubkey", 2},
116117
{"verifychain", 0},
117118
{"verifychain", 1},
118119
{"keypoolrefill", 0},

src/rpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ static const CRPCCommand vRPCCommands[] =
425425
{"wallet", "importprivkey", &importprivkey, true, false, true},
426426
{"wallet", "importwallet", &importwallet, true, false, true},
427427
{"wallet", "importaddress", &importaddress, true, false, true},
428+
{"wallet", "importpubkey", &importpubkey, true, false, true},
428429
{"wallet", "keypoolrefill", &keypoolrefill, true, false, true},
429430
{"wallet", "listaccounts", &listaccounts, false, false, true},
430431
{"wallet", "listdelegators", &listdelegators, false, false, true},

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ extern UniValue clearbanned(const UniValue& params, bool fHelp);
195195
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
196196
extern UniValue importprivkey(const UniValue& params, bool fHelp);
197197
extern UniValue importaddress(const UniValue& params, bool fHelp);
198+
extern UniValue importpubkey(const UniValue& params, bool fHelp);
198199
extern UniValue dumpwallet(const UniValue& params, bool fHelp);
199200
extern UniValue importwallet(const UniValue& params, bool fHelp);
200201
extern UniValue bip38encrypt(const UniValue& params, bool fHelp);

src/wallet/rpcdump.cpp

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

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

0 commit comments

Comments
 (0)