| 504 | |
| 505 | |
| 506 | UniValue importwallet(const JSONRPCRequest& request) |
| 507 | { |
| 508 | std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); |
| 509 | CWallet* const pwallet = wallet.get(); |
| 510 | if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { |
| 511 | return NullUniValue; |
| 512 | } |
| 513 | |
| 514 | if (request.fHelp || request.params.size() != 1) |
| 515 | throw std::runtime_error( |
| 516 | "importwallet \"filename\"\n" |
| 517 | "\nImports keys from a wallet dump file (see dumpwallet). Requires a new wallet backup to include imported keys.\n" |
| 518 | "\nArguments:\n" |
| 519 | "1. \"filename\" (string, required) The wallet file\n" |
| 520 | "\nExamples:\n" |
| 521 | "\nDump the wallet\n" |
| 522 | + HelpExampleCli("dumpwallet", "\"test\"") + |
| 523 | "\nImport the wallet\n" |
| 524 | + HelpExampleCli("importwallet", "\"test\"") + |
| 525 | "\nImport using the json rpc call\n" |
| 526 | + HelpExampleRpc("importwallet", "\"test\"") |
| 527 | ); |
| 528 | |
| 529 | if (fPruneMode) |
| 530 | throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled in pruned mode"); |
| 531 | |
| 532 | WalletRescanReserver reserver(pwallet); |
| 533 | if (!reserver.reserve()) { |
| 534 | throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); |
| 535 | } |
| 536 | |
| 537 | int64_t nTimeBegin = 0; |
| 538 | bool fGood = true; |
| 539 | { |
| 540 | LOCK2(cs_main, pwallet->cs_wallet); |
| 541 | |
| 542 | EnsureWalletIsUnlocked(pwallet); |
| 543 | |
| 544 | std::ifstream file; |
| 545 | file.open(request.params[0].get_str().c_str(), std::ios::in | std::ios::ate); |
| 546 | if (!file.is_open()) { |
| 547 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file"); |
| 548 | } |
| 549 | nTimeBegin = chainActive.Tip()->GetBlockTime(); |
| 550 | |
| 551 | int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg()); |
| 552 | file.seekg(0, file.beg); |
| 553 | |
| 554 | // Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which |
| 555 | // we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button. |
| 556 | uiInterface.ShowProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI |
| 557 | while (file.good()) { |
| 558 | uiInterface.ShowProgress("", std::max(1, std::min(99, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false); |
| 559 | std::string line; |
| 560 | std::getline(file, line); |
| 561 | if (line.empty() || line[0] == '#') |
| 562 | continue; |
| 563 | |