@@ -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+ " \n Adds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend.\n "
246+ " \n Arguments:\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+ " \n Note: This call can take minutes to complete if rescan is true.\n "
251+ " \n Examples:\n "
252+ " \n Import a public key with rescan\n "
253+ + HelpExampleCli (" importpubkey" , " \" mypubkey\" " ) +
254+ " \n Import using a label without rescan\n "
255+ + HelpExampleCli (" importpubkey" , " \" mypubkey\" \" testing\" false" ) +
256+ " \n As 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.
241285UniValue importwallet (const UniValue& params, bool fHelp )
242286{
0 commit comments