feat(recovery): import keys from a legacy wallet backup file (z_importwallet)

Phase 3: for a wallet.dat the current daemon can't open, recover its keys from the OLD wallet's export file.
Settings → "Import From Backup File…" opens a dialog that takes the path to a z_exportwallet / dumpwallet
dump (or SilentDragon's "Export all private keys") and calls z_importwallet — the daemon reads every key
and does ONE rescan, far faster and simpler than importing keys one at a time. On success it nudges the user
to shield any mined coinbase from the Shield screen.

- New App::importWalletFile(path, cb): runs z_importwallet on the long-call lane (no client timeout — the
  rescan can take a long time), then refreshAddresses + markWalletMutated. Full-node only.
- Only the PATH touches GUI memory, never the keys themselves (the daemon reads the file) — less secret
  exposure than a pasted-key field.
- New renderImportWalletFileDialog (typed path + Paste/Clear, progress, result) + a Settings card.

Reuses the existing long-call + z_importwallet infra; the careful single-key import dialog is untouched.
No send/sweep/encrypt call changed. Build + ctest green; the import/rescan itself is a daemon RPC, not
pure-testable, and wasn't exercised end-to-end here.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-10-19 22:43:49 -05:00
co-authored by Claude Opus 4.8
parent 293b633a24
commit a3b4f534d5
5 changed files with 149 additions and 1 deletions
+42
View File
@@ -5746,6 +5746,48 @@ void App::importPrivateKey(const std::string& rawKey, int startHeight,
if (!key.empty()) sodium_memzero(&key[0], key.size()); // scrub the calling-frame copy
}
// Import a whole wallet-key dump file (the output of the OLD wallet's z_exportwallet / dumpwallet, or
// SilentDragon's "export all private keys") via z_importwallet: the daemon reads every key from the
// file and does ONE rescan — the right tool for recovering a legacy wallet.dat the new client can't open,
// and far faster than importing keys one by one. Only the PATH touches our memory, never the keys. Runs on
// the long-call lane (no client timeout: the rescan can take a long time). Full-node only.
void App::importWalletFile(const std::string& path, std::function<void(bool, const std::string&)> callback)
{
if (!state_.connected || !rpc_ || !worker_) {
if (callback) callback(false, TR("import_key_need_node"));
return;
}
std::string p(path);
while (!p.empty() && (p.front()==' '||p.front()=='\t'||p.front()=='\n'||p.front()=='\r')) p.erase(p.begin());
while (!p.empty() && (p.back()==' '||p.back()=='\t'||p.back()=='\n'||p.back()=='\r')) p.pop_back();
if (p.empty()) { if (callback) callback(false, TR("import_wallet_file_need_path")); return; }
{
std::string why; const char* trKey = nullptr;
if (longWalletOpRefused(&why, &trKey)) { if (callback) callback(false, TR(trKey)); return; }
}
const bool started = runLongWalletTask([this, p, callback](rpc::RPCClient& c) mutable -> rpc::RPCWorker::MainCb {
std::string err;
try {
rpc::RPCClient::TraceScope trace("Settings / Import wallet file");
c.call("z_importwallet", {p}, 0L); // reads the dump + one rescan; 0 = no client timeout
} catch (const std::exception& e) {
err = e.what();
if (!err.empty() && err.find("Cannot open") != std::string::npos)
err = TR("import_wallet_file_not_found"); // the most common cause: wrong/missing path
} catch (...) {
err = "Import failed (unknown error)";
}
return [this, err, callback]() {
if (!err.empty()) { if (callback) callback(false, err); return; }
invalidateAddressValidationCache();
refreshAddresses();
markWalletMutated(/*forceBalance=*/true); // keys added: balance changes with no tip/mempool signal
if (callback) callback(true, "");
};
});
if (!started && callback) callback(false, TR("appx_wallet_op_running"));
}
// Sweep a spending key: import it (a full rescan populates its UTXOs/notes — the stock node has no
// address index, so there is no way to enumerate them without importing) then z_sendmany ALL of the
// key's funds (balance − fee) to a destination the user already controls. The imported key is left in