@@ -237,6 +237,63 @@ UniValue importaddress(const UniValue& params, bool fHelp)
237
237
return NullUniValue;
238
238
}
239
239
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
+ " \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 "
249
+ " \n Arguments:\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
+ " \n Note: This call can take minutes to complete if rescan is true.\n "
254
+ " \n Examples:\n "
255
+ " \n Import a public key with rescan\n "
256
+ + HelpExampleCli (" importpubkey" , " \" mypubkey\" " ) +
257
+ " \n Import using a label without rescan\n "
258
+ + HelpExampleCli (" importpubkey" , " \" mypubkey\" \" testing\" false" ) +
259
+ " \n As 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
+
240
297
UniValue importwallet (const UniValue& params, bool fHelp )
241
298
{
242
299
if (!EnsureWalletIsAvailable (fHelp ))
0 commit comments