| 412 | } |
| 413 | |
| 414 | RPCHelpMan 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 | { |
nothing calls this directly
no test coverage detected