mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
wallettool: Add dump command
Adds a new dump command to bitcoin-wallet which prints out all of the wallet's records in hex.
This commit is contained in:
parent
ad3d4b3929
commit
e1e7a90d5f
@ -249,6 +249,7 @@ BITCOIN_CORE_H = \
|
||||
wallet/context.h \
|
||||
wallet/crypter.h \
|
||||
wallet/db.h \
|
||||
wallet/dump.h \
|
||||
wallet/feebumper.h \
|
||||
wallet/fees.h \
|
||||
wallet/ismine.h \
|
||||
@ -361,6 +362,7 @@ libbitcoin_wallet_a_SOURCES = \
|
||||
wallet/context.cpp \
|
||||
wallet/crypter.cpp \
|
||||
wallet/db.cpp \
|
||||
wallet/dump.cpp \
|
||||
wallet/feebumper.cpp \
|
||||
wallet/fees.cpp \
|
||||
wallet/interfaces.cpp \
|
||||
|
@ -27,6 +27,7 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
|
||||
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
||||
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
||||
argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
|
||||
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file.", ArgsManager::ALLOW_STRING, OptionsCategory::OPTIONS);
|
||||
argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
|
||||
argsman.AddArg("-descriptors", "Create descriptors wallet. Only for create", ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);
|
||||
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
|
||||
@ -34,6 +35,7 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
|
||||
argsman.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||
argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||
argsman.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||
argsman.AddArg("dump", "Print out all of the wallet key-value records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||
}
|
||||
|
||||
static bool WalletAppInit(int argc, char* argv[])
|
||||
|
96
src/wallet/dump.cpp
Normal file
96
src/wallet/dump.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright (c) 2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <wallet/dump.h>
|
||||
|
||||
#include <util/translation.h>
|
||||
#include <wallet/wallet.h>
|
||||
|
||||
static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
|
||||
uint32_t DUMP_VERSION = 1;
|
||||
|
||||
bool DumpWallet(CWallet& wallet, bilingual_str& error)
|
||||
{
|
||||
// Get the dumpfile
|
||||
std::string dump_filename = gArgs.GetArg("-dumpfile", "");
|
||||
if (dump_filename.empty()) {
|
||||
error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
|
||||
return false;
|
||||
}
|
||||
|
||||
fs::path path = dump_filename;
|
||||
path = fs::absolute(path);
|
||||
if (fs::exists(path)) {
|
||||
error = strprintf(_("File %s already exists. If you are sure this is what you want, move it out of the way first."), path.string());
|
||||
return false;
|
||||
}
|
||||
fsbridge::ofstream dump_file;
|
||||
dump_file.open(path);
|
||||
if (dump_file.fail()) {
|
||||
error = strprintf(_("Unable to open %s for writing"), path.string());
|
||||
return false;
|
||||
}
|
||||
|
||||
CHashWriter hasher(0, 0);
|
||||
|
||||
WalletDatabase& db = wallet.GetDatabase();
|
||||
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
|
||||
|
||||
bool ret = true;
|
||||
if (!batch->StartCursor()) {
|
||||
error = _("Error: Couldn't create cursor into database");
|
||||
ret = false;
|
||||
}
|
||||
|
||||
// Write out a magic string with version
|
||||
std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION);
|
||||
dump_file.write(line.data(), line.size());
|
||||
hasher.write(line.data(), line.size());
|
||||
|
||||
// Write out the file format
|
||||
line = strprintf("%s,%s\n", "format", db.Format());
|
||||
dump_file.write(line.data(), line.size());
|
||||
hasher.write(line.data(), line.size());
|
||||
|
||||
if (ret) {
|
||||
|
||||
// Read the records
|
||||
while (true) {
|
||||
CDataStream ss_key(SER_DISK, CLIENT_VERSION);
|
||||
CDataStream ss_value(SER_DISK, CLIENT_VERSION);
|
||||
bool complete;
|
||||
ret = batch->ReadAtCursor(ss_key, ss_value, complete);
|
||||
if (complete) {
|
||||
ret = true;
|
||||
break;
|
||||
} else if (!ret) {
|
||||
error = _("Error reading next record from wallet database");
|
||||
break;
|
||||
}
|
||||
std::string key_str = HexStr(ss_key);
|
||||
std::string value_str = HexStr(ss_value);
|
||||
line = strprintf("%s,%s\n", key_str, value_str);
|
||||
dump_file.write(line.data(), line.size());
|
||||
hasher.write(line.data(), line.size());
|
||||
}
|
||||
}
|
||||
|
||||
batch->CloseCursor();
|
||||
batch.reset();
|
||||
|
||||
// Close the wallet after we're done with it. The caller won't be doing this
|
||||
wallet.Close();
|
||||
|
||||
if (ret) {
|
||||
// Write the hash
|
||||
tfm::format(dump_file, "checksum,%s\n", HexStr(hasher.GetHash()));
|
||||
dump_file.close();
|
||||
} else {
|
||||
// Remove the dumpfile on failure
|
||||
dump_file.close();
|
||||
fs::remove(path);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
14
src/wallet/dump.h
Normal file
14
src/wallet/dump.h
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_WALLET_DUMP_H
|
||||
#define BITCOIN_WALLET_DUMP_H
|
||||
|
||||
class CWallet;
|
||||
|
||||
struct bilingual_str;
|
||||
|
||||
bool DumpWallet(CWallet& wallet, bilingual_str& error);
|
||||
|
||||
#endif // BITCOIN_WALLET_DUMP_H
|
@ -5,6 +5,7 @@
|
||||
#include <fs.h>
|
||||
#include <util/system.h>
|
||||
#include <util/translation.h>
|
||||
#include <wallet/dump.h>
|
||||
#include <wallet/salvage.h>
|
||||
#include <wallet/wallet.h>
|
||||
#include <wallet/walletutil.h>
|
||||
@ -106,6 +107,12 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||
{
|
||||
fs::path path = fs::absolute(name, GetWalletDir());
|
||||
|
||||
// -dumpfile is only allowed with dump and createfromdump. Disallow it for all other commands.
|
||||
if (gArgs.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
|
||||
tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (command == "create") {
|
||||
DatabaseOptions options;
|
||||
options.require_create = true;
|
||||
@ -119,33 +126,44 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||
WalletShowInfo(wallet_instance.get());
|
||||
wallet_instance->Close();
|
||||
}
|
||||
} else if (command == "info" || command == "salvage") {
|
||||
if (command == "info") {
|
||||
DatabaseOptions options;
|
||||
options.require_existing = true;
|
||||
std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
|
||||
if (!wallet_instance) return false;
|
||||
WalletShowInfo(wallet_instance.get());
|
||||
wallet_instance->Close();
|
||||
} else if (command == "salvage") {
|
||||
} else if (command == "info") {
|
||||
DatabaseOptions options;
|
||||
options.require_existing = true;
|
||||
std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
|
||||
if (!wallet_instance) return false;
|
||||
WalletShowInfo(wallet_instance.get());
|
||||
wallet_instance->Close();
|
||||
} else if (command == "salvage") {
|
||||
#ifdef USE_BDB
|
||||
bilingual_str error;
|
||||
std::vector<bilingual_str> warnings;
|
||||
bool ret = RecoverDatabaseFile(path, error, warnings);
|
||||
if (!ret) {
|
||||
for (const auto& warning : warnings) {
|
||||
tfm::format(std::cerr, "%s\n", warning.original);
|
||||
}
|
||||
if (!error.empty()) {
|
||||
tfm::format(std::cerr, "%s\n", error.original);
|
||||
}
|
||||
bilingual_str error;
|
||||
std::vector<bilingual_str> warnings;
|
||||
bool ret = RecoverDatabaseFile(path, error, warnings);
|
||||
if (!ret) {
|
||||
for (const auto& warning : warnings) {
|
||||
tfm::format(std::cerr, "%s\n", warning.original);
|
||||
}
|
||||
if (!error.empty()) {
|
||||
tfm::format(std::cerr, "%s\n", error.original);
|
||||
}
|
||||
return ret;
|
||||
#else
|
||||
tfm::format(std::cerr, "Salvage command is not available as BDB support is not compiled");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
#else
|
||||
tfm::format(std::cerr, "Salvage command is not available as BDB support is not compiled");
|
||||
return false;
|
||||
#endif
|
||||
} else if (command == "dump") {
|
||||
DatabaseOptions options;
|
||||
options.require_existing = true;
|
||||
std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
|
||||
if (!wallet_instance) return false;
|
||||
bilingual_str error;
|
||||
bool ret = DumpWallet(*wallet_instance, error);
|
||||
if (!ret && !error.empty()) {
|
||||
tfm::format(std::cerr, "%s\n", error.original);
|
||||
return ret;
|
||||
}
|
||||
tfm::format(std::cout, "The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n");
|
||||
return ret;
|
||||
} else {
|
||||
tfm::format(std::cerr, "Invalid command: %s\n", command);
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user