mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-12 10:30:08 +01:00
wallettool: Add a salvage command
This commit is contained in:
parent
448bdff263
commit
c87770915b
2 changed files with 34 additions and 5 deletions
|
@ -31,6 +31,7 @@ static void SetupWalletToolArgs()
|
||||||
|
|
||||||
gArgs.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
gArgs.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||||
gArgs.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
gArgs.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||||
|
gArgs.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool WalletAppInit(int argc, char* argv[])
|
static bool WalletAppInit(int argc, char* argv[])
|
||||||
|
|
|
@ -103,6 +103,29 @@ static void WalletShowInfo(CWallet* wallet_instance)
|
||||||
tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
|
tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool SalvageWallet(const fs::path& path)
|
||||||
|
{
|
||||||
|
// Create a Database handle to allow for the db to be initialized before recovery
|
||||||
|
std::unique_ptr<WalletDatabase> database = WalletDatabase::Create(path);
|
||||||
|
|
||||||
|
// Initialize the environment before recovery
|
||||||
|
bilingual_str error_string;
|
||||||
|
try {
|
||||||
|
WalletBatch::VerifyEnvironment(path, error_string);
|
||||||
|
} catch (const fs::filesystem_error& e) {
|
||||||
|
error_string = Untranslated(strprintf("Error loading wallet. %s", fsbridge::get_filesystem_error_message(e)));
|
||||||
|
}
|
||||||
|
if (!error_string.original.empty()) {
|
||||||
|
tfm::format(std::cerr, "Failed to open wallet for salvage :%s\n", error_string.original);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform the recovery
|
||||||
|
CWallet dummy_wallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy());
|
||||||
|
std::string backup_filename;
|
||||||
|
return WalletBatch::Recover(path, (void*)&dummy_wallet, WalletBatch::RecoverKeysOnlyFilter, backup_filename);
|
||||||
|
}
|
||||||
|
|
||||||
bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||||
{
|
{
|
||||||
fs::path path = fs::absolute(name, GetWalletDir());
|
fs::path path = fs::absolute(name, GetWalletDir());
|
||||||
|
@ -113,7 +136,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||||
WalletShowInfo(wallet_instance.get());
|
WalletShowInfo(wallet_instance.get());
|
||||||
wallet_instance->Flush(true);
|
wallet_instance->Flush(true);
|
||||||
}
|
}
|
||||||
} else if (command == "info") {
|
} else if (command == "info" || command == "salvage") {
|
||||||
if (!fs::exists(path)) {
|
if (!fs::exists(path)) {
|
||||||
tfm::format(std::cerr, "Error: no wallet file at %s\n", name);
|
tfm::format(std::cerr, "Error: no wallet file at %s\n", name);
|
||||||
return false;
|
return false;
|
||||||
|
@ -123,10 +146,15 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||||
tfm::format(std::cerr, "%s\nError loading %s. Is wallet being used by other process?\n", error.original, name);
|
tfm::format(std::cerr, "%s\nError loading %s. Is wallet being used by other process?\n", error.original, name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
|
|
||||||
if (!wallet_instance) return false;
|
if (command == "info") {
|
||||||
WalletShowInfo(wallet_instance.get());
|
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
|
||||||
wallet_instance->Flush(true);
|
if (!wallet_instance) return false;
|
||||||
|
WalletShowInfo(wallet_instance.get());
|
||||||
|
wallet_instance->Flush(true);
|
||||||
|
} else if (command == "salvage") {
|
||||||
|
return SalvageWallet(path);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
tfm::format(std::cerr, "Invalid command: %s\n", command);
|
tfm::format(std::cerr, "Invalid command: %s\n", command);
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue