MCPcopy Create free account
hub / github.com/ElementsProject/elements / importpubkey

Function importpubkey

src/wallet/rpc/backup.cpp:414–497  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

412}
413
414RPCHelpMan importpubkey()
415{
416 return RPCHelpMan{"importpubkey",
417 "\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
418 "Hint: use importmulti to import more than one public key.\n"
419 "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
420 "may report that the imported pubkey exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
421 "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
422 {
423 {"pubkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The hex-encoded public key"},
424 {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"},
425 {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Rescan the wallet for transactions"},
426 },
427 RPCResult{RPCResult::Type::NONE, "", ""},
428 RPCExamples{
429 "\nImport a public key with rescan\n"
430 + HelpExampleCli("importpubkey", "\"mypubkey\"") +
431 "\nImport using a label without rescan\n"
432 + HelpExampleCli("importpubkey", "\"mypubkey\" \"testing\" false") +
433 "\nAs a JSON-RPC call\n"
434 + HelpExampleRpc("importpubkey", "\"mypubkey\", \"testing\", false")
435 },
436 [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
437{
438 std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
439 if (!pwallet) return NullUniValue;
440
441 EnsureLegacyScriptPubKeyMan(*pwallet, true);
442
443 std::string strLabel;
444 if (!request.params[1].isNull())
445 strLabel = request.params[1].get_str();
446
447 // Whether to perform rescan after import
448 bool fRescan = true;
449 if (!request.params[2].isNull())
450 fRescan = request.params[2].get_bool();
451
452 if (fRescan && pwallet->chain().havePruned()) {
453 // Exit early and print an error.
454 // If a block is pruned after this check, we will import the key(s),
455 // but fail the rescan with a generic error.
456 throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
457 }
458
459 WalletRescanReserver reserver(*pwallet);
460 if (fRescan && !reserver.reserve()) {
461 throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
462 }
463
464 if (!IsHex(request.params[0].get_str()))
465 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey must be a hex string");
466 std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
467 CPubKey pubKey(data);
468 if (!pubKey.IsFullyValid())
469 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
470
471 {

Callers

nothing calls this directly

Calls 15

HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
JSONRPCErrorFunction · 0.85
IsHexFunction · 0.85
ParseHexFunction · 0.85
GetAllDestinationsForKeyFunction · 0.85
GetScriptForDestinationFunction · 0.85
RescanWalletFunction · 0.85
get_boolMethod · 0.80
havePrunedMethod · 0.80
IsFullyValidMethod · 0.80

Tested by

no test coverage detected