| 112 | } |
| 113 | |
| 114 | RPCHelpMan importprivkey() |
| 115 | { |
| 116 | return RPCHelpMan{"importprivkey", |
| 117 | "\nAdds a private key (as returned by dumpprivkey) to your wallet. Requires a new wallet backup.\n" |
| 118 | "Hint: use importmulti to import more than one private key.\n" |
| 119 | "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n" |
| 120 | "may report that the imported key exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n" |
| 121 | "Note: Use \"getwalletinfo\" to query the scanning progress.\n", |
| 122 | { |
| 123 | {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key (see dumpprivkey)"}, |
| 124 | {"label", RPCArg::Type::STR, RPCArg::DefaultHint{"current label if address exists, otherwise \"\""}, "An optional label"}, |
| 125 | {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Rescan the wallet for transactions"}, |
| 126 | }, |
| 127 | RPCResult{RPCResult::Type::NONE, "", ""}, |
| 128 | RPCExamples{ |
| 129 | "\nDump a private key\n" |
| 130 | + HelpExampleCli("dumpprivkey", "\"myaddress\"") + |
| 131 | "\nImport the private key with rescan\n" |
| 132 | + HelpExampleCli("importprivkey", "\"mykey\"") + |
| 133 | "\nImport using a label and without rescan\n" |
| 134 | + HelpExampleCli("importprivkey", "\"mykey\" \"testing\" false") + |
| 135 | "\nImport using default blank label and without rescan\n" |
| 136 | + HelpExampleCli("importprivkey", "\"mykey\" \"\" false") + |
| 137 | "\nAs a JSON-RPC call\n" |
| 138 | + HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false") |
| 139 | }, |
| 140 | [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 141 | { |
| 142 | std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request); |
| 143 | if (!pwallet) return NullUniValue; |
| 144 | |
| 145 | if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { |
| 146 | throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled"); |
| 147 | } |
| 148 | |
| 149 | EnsureLegacyScriptPubKeyMan(*pwallet, true); |
| 150 | |
| 151 | WalletRescanReserver reserver(*pwallet); |
| 152 | bool fRescan = true; |
| 153 | { |
| 154 | LOCK(pwallet->cs_wallet); |
| 155 | |
| 156 | EnsureWalletIsUnlocked(*pwallet); |
| 157 | |
| 158 | std::string strSecret = request.params[0].get_str(); |
| 159 | std::string strLabel = ""; |
| 160 | if (!request.params[1].isNull()) |
| 161 | strLabel = request.params[1].get_str(); |
| 162 | |
| 163 | // Whether to perform rescan after import |
| 164 | if (!request.params[2].isNull()) |
| 165 | fRescan = request.params[2].get_bool(); |
| 166 | |
| 167 | if (fRescan && pwallet->chain().havePruned()) { |
| 168 | // Exit early and print an error. |
| 169 | // If a block is pruned after this check, we will import the key(s), |
| 170 | // but fail the rescan with a generic error. |
| 171 | throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned"); |
nothing calls this directly
no test coverage detected