From b539d776bd86df9224d97048f26f33d2750d1c92 Mon Sep 17 00:00:00 2001 From: Bitflash Date: Thu, 30 Jul 2026 12:06:08 -0300 Subject: wallet: export and import keys as text (#49) The last door out. Everything else assumes Berkeley DB will cooperate. When it will not -- a build that cannot read the file, a truncation, an environment past recovering -- the keys are usually still perfectly intact and were unreachable anyway. #40 is one person's account of exactly that, and #46 only rescues the cases where BDB still opens the file at all. bitflash -dumpwallet=keys.txt bitflash -importwallet=keys.txt One key per line with its address and label. No database, no environment, no version coupling: anything that can read a line can recover from it. The format is the DER private key in hex, which round-trips exactly through SetPrivKey. Not interchangeable with other wallets -- worth having eventually, but a WIF encoder I got subtly wrong would be worse than no encoder at all, and this file exists for the case where nothing else works. Care taken, because the failure modes here cost money: - Refuses to overwrite an existing file. A mistyped path must not eat the only copy of something irreplaceable. - Written owner-only on POSIX, before any key reaches it. Windows inherits the folder's ACL; the README says so rather than pretending otherwise. - Newlines in labels are flattened, so a label cannot forge an extra entry on read-back. - Import skips keys already present, so running it twice does nothing, and one unreadable line is reported and stepped over instead of costing the reader every other key in the file. Verified on Linux and Windows: a key exported from one wallet imports into a different one and its address then appears there; re-importing adds nothing; a corrupted line is skipped while the rest still load; the dump is 0600 and refuses to overwrite. This does not solve #47. Keys still are not derived from a seed, so a dump is as much a snapshot as any other backup. What it does is make the keys reachable no matter what state the database is in. --- src/main_gui.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/main_gui.cpp') diff --git a/src/main_gui.cpp b/src/main_gui.cpp index 976cd42..2484592 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -70,6 +70,9 @@ static void PrintUsage() printf("Wallet:\n"); printf(" /backupwallet=FILE (write a wallet.dat that opens on its own,\n"); printf(" then exit; run it again after new addresses)\n"); + printf(" /dumpwallet=FILE (export private keys as text -- readable by\n"); + printf(" anyone, so guard it like cash)\n"); + printf(" /importwallet=FILE (load keys from such a file back in)\n"); printf("\n"); printf("Each option also accepts '-' instead of '/'.\n"); } @@ -245,6 +248,36 @@ int main(int argc, char* argv[]) return fOk ? 0 : 1; } + string strDump = argval2(argc, argv, "/dumpwallet", "-dumpwallet"); + if (!strDump.empty()) + { + bool fOk = DumpWallet(strDump); + if (fOk) + { + printf("Wrote %s\n", strDump.c_str()); + printf("It holds your private keys as readable text. Anyone with the file " + "can spend these coins -- keep it off shared storage and delete it " + "once you have it somewhere safe.\n"); + } + else + fprintf(stderr, "Export failed -- nothing was written.\n"); + DBFlush(true); + return fOk ? 0 : 1; + } + + string strImport = argval2(argc, argv, "/importwallet", "-importwallet"); + if (!strImport.empty()) + { + int nAdded = 0, nSkipped = 0; + bool fOk = ImportWallet(strImport, nAdded, nSkipped); + if (fOk) + printf("Imported %d key(s); %d were already here.\n", nAdded, nSkipped); + else + fprintf(stderr, "Import failed.\n"); + DBFlush(true); + return fOk ? 0 : 1; + } + ReacceptWalletTransactions(); if (!StartNode(strErrors)) { fprintf(stderr,"StartNode: %s\n",strErrors.c_str()); return 1; } -- cgit v1.2.3