mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-02 09:46:14 +00:00
db2effaca4cf82bf806596d16f9797d3692e2da7 scripted-diff: refactor: CWallet::Create() -> CreateNew() (David Gumberg)
27e021ebc0dd3517a71f3ddb38ed265a19693d4c wallet: Correctly log stats for encrypted messages. (David Gumberg)
d8bec61be233b9cb6d5db886e8f1c1f058288fb5 wallet: remove loading logic from CWallet::Create (David Gumberg)
f35acc893fb3378b2ad39608fe254d33af6cce9f refactor: wallet: Factor out `WriteVersion()` from `PopulateWalletFromDB()` (David Gumberg)
e12ff8aca049ec7b054cb3047a167c7ce8dbd421 test: wallet: Split create and load (David Gumberg)
70dbc79b09acf7b1515532ee20c7533c938ffb70 wallet: Use CWallet::LoadExisting() for loading existing wallets. (David Gumberg)
ae66e011646266abb67b31027bc29e0ce1d08ad4 wallet: Create separate function for wallet load (David Gumberg)
bc69070416c62a88d8f4029280ec10d6f9ec8d20 refactor: Wallet stats logging in its own function (David Gumberg)
a9d64cd49c69dafd6496ccb5aef4cd6d8898966b wallet: Remove redundant birth time update (David Gumberg)
b4a49cc7275efc16d4a4179ed34b50de5bb7367e wallet: Move argument parsing to before DB load (David Gumberg)
b15a94a618c53041e97ccfface3045a0642777e1 refactor: Split out wallet argument loading (David Gumberg)
a02c4a82d88a3e9a24ec2aa0b828b8cc533dde58 refactor: Move -walletbroadcast setting init (David Gumberg)
411caf72815bdf2e176e790a4c63f745517c4bb4 wallet: refactor: PopulateWalletFromDB use switch statement. (David Gumberg)
a48e23f566ccaf9b81fe0684885972d9ee34afd3 refactor: wallet: move error handling to PopulateWalletFromDB() (David Gumberg)
0972785fd723b9b3c84844bf999d6e08e163ef9d wallet: Delete unnecessary PopulateWalletFromDB() calls (David Gumberg)
f0a046094e4c4b5f3af0e453492077f4911e0132 scripted-diff: refactor: CWallet::LoadWallet->PopulateWalletFromDB (David Gumberg)
Pull request description:
This PR is mostly a refactor which splits out logic used for creating wallets and for loading wallets, both of which are presently contained in `CWallet::Create()` into `CWallet::CreateNew()` and `CWallet::LoadExisting()`
The real win of this PR is that `CWallet::Create()` uses a very bad heuristic for trying to guess whether or not it is supposed to be creating a new wallet or loading an existing wallet:
370c592612/src/wallet/wallet.cpp (L2882-L2885)
This heuristic assumes that wallets with no `ScriptPubKeyMans` are being created, which sounds reasonable, but as demonstrated in #32112 and #32111, this can happen when the user tries to load a wallet file that is corrupted, both issues are fixed by this PR and any other misbehavior for wallet files which succeeded the broken heuristic's sniff test for new wallets.
It was already the case that every caller of `CWallet::Create()` knows whether it is creating a wallet or loading one, so we can avoid replacing this bad heuristic with another one, and just shift the burden to the caller.
ACKs for top commit:
achow101:
ACK db2effaca4cf82bf806596d16f9797d3692e2da7
polespinasa:
approach ACK db2effaca4cf82bf806596d16f9797d3692e2da7
w0xlt:
reACK db2effaca4cf82bf806596d16f9797d3692e2da7
murchandamus:
ACK db2effaca4cf82bf806596d16f9797d3692e2da7
rkrux:
ACK db2effaca4cf82bf806596d16f9797d3692e2da7
Tree-SHA512: c28d60e0a3001058da3fd2bdbe0726c7ebe742a4b900a1dee2e5132eccc22e49619cb747a99b4032b000eafd4aa2fdd4ec244c32be2012aba809fdc94b5f6ecd
293 lines
10 KiB
C++
293 lines
10 KiB
C++
// Copyright (c) 2020-present 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 <common/args.h>
|
|
#include <util/fs.h>
|
|
#include <util/translation.h>
|
|
#include <wallet/wallet.h>
|
|
#include <wallet/walletdb.h>
|
|
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace wallet {
|
|
static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
|
|
uint32_t DUMP_VERSION = 1;
|
|
|
|
bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error)
|
|
{
|
|
// Get the dumpfile
|
|
std::string dump_filename = args.GetArg("-dumpfile", "");
|
|
if (dump_filename.empty()) {
|
|
error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
|
|
return false;
|
|
}
|
|
|
|
fs::path path = fs::PathFromString(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."), fs::PathToString(path));
|
|
return false;
|
|
}
|
|
std::ofstream dump_file;
|
|
dump_file.open(path.std_path());
|
|
if (dump_file.fail()) {
|
|
error = strprintf(_("Unable to open %s for writing"), fs::PathToString(path));
|
|
return false;
|
|
}
|
|
|
|
HashWriter hasher{};
|
|
|
|
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
|
|
|
|
bool ret = true;
|
|
std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
|
|
if (!cursor) {
|
|
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 << std::span{line};
|
|
|
|
// Write out the file format
|
|
std::string format = db.Format();
|
|
// BDB files that are opened using BerkeleyRODatabase have its format as "bdb_ro"
|
|
// We want to override that format back to "bdb"
|
|
if (format == "bdb_ro") {
|
|
format = "bdb";
|
|
}
|
|
line = strprintf("%s,%s\n", "format", format);
|
|
dump_file.write(line.data(), line.size());
|
|
hasher << std::span{line};
|
|
|
|
if (ret) {
|
|
|
|
// Read the records
|
|
while (true) {
|
|
DataStream ss_key{};
|
|
DataStream ss_value{};
|
|
DatabaseCursor::Status status = cursor->Next(ss_key, ss_value);
|
|
if (status == DatabaseCursor::Status::DONE) {
|
|
ret = true;
|
|
break;
|
|
} else if (status == DatabaseCursor::Status::FAIL) {
|
|
error = _("Error reading next record from wallet database");
|
|
ret = false;
|
|
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 << std::span{line};
|
|
}
|
|
}
|
|
|
|
cursor.reset();
|
|
batch.reset();
|
|
|
|
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;
|
|
}
|
|
|
|
// The standard wallet deleter function blocks on the validation interface
|
|
// queue, which doesn't exist for the bitcoin-wallet. Define our own
|
|
// deleter here.
|
|
static void WalletToolReleaseWallet(CWallet* wallet)
|
|
{
|
|
wallet->WalletLogPrintf("Releasing wallet\n");
|
|
wallet->Close();
|
|
delete wallet;
|
|
}
|
|
|
|
bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings)
|
|
{
|
|
if (name.empty()) {
|
|
tfm::format(std::cerr, "Wallet name cannot be empty\n");
|
|
return false;
|
|
}
|
|
|
|
// Get the dumpfile
|
|
std::string dump_filename = args.GetArg("-dumpfile", "");
|
|
if (dump_filename.empty()) {
|
|
error = _("No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.");
|
|
return false;
|
|
}
|
|
|
|
fs::path dump_path = fs::PathFromString(dump_filename);
|
|
dump_path = fs::absolute(dump_path);
|
|
if (!fs::exists(dump_path)) {
|
|
error = strprintf(_("Dump file %s does not exist."), fs::PathToString(dump_path));
|
|
return false;
|
|
}
|
|
std::ifstream dump_file{dump_path.std_path()};
|
|
|
|
// Compute the checksum
|
|
HashWriter hasher{};
|
|
uint256 checksum;
|
|
|
|
// Check the magic and version
|
|
std::string magic_key;
|
|
std::getline(dump_file, magic_key, ',');
|
|
std::string version_value;
|
|
std::getline(dump_file, version_value, '\n');
|
|
if (magic_key != DUMP_MAGIC) {
|
|
error = strprintf(_("Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key, DUMP_MAGIC);
|
|
dump_file.close();
|
|
return false;
|
|
}
|
|
// Check the version number (value of first record)
|
|
const auto ver{ToIntegral<uint32_t>(version_value)};
|
|
if (!ver) {
|
|
error = strprintf(_("Error: Unable to parse version %u as a uint32_t"), version_value);
|
|
dump_file.close();
|
|
return false;
|
|
}
|
|
if (*ver != DUMP_VERSION) {
|
|
error = strprintf(_("Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version %s"), version_value);
|
|
dump_file.close();
|
|
return false;
|
|
}
|
|
std::string magic_hasher_line = strprintf("%s,%s\n", magic_key, version_value);
|
|
hasher << std::span{magic_hasher_line};
|
|
|
|
// Get the stored file format
|
|
std::string format_key;
|
|
std::getline(dump_file, format_key, ',');
|
|
std::string format_value;
|
|
std::getline(dump_file, format_value, '\n');
|
|
if (format_key != "format") {
|
|
error = strprintf(_("Error: Dumpfile format record is incorrect. Got \"%s\", expected \"format\"."), format_key);
|
|
dump_file.close();
|
|
return false;
|
|
}
|
|
// Make sure that the dump was created from a sqlite database only as that is the only
|
|
// type of database that we still support.
|
|
// Other formats such as BDB should not be loaded into a sqlite database since they also
|
|
// use a different type of wallet entirely which is no longer compatible with this software.
|
|
if (format_value != "sqlite") {
|
|
error = strprintf(_("Error: Dumpfile specifies an unsupported database format (%s). Only sqlite database dumps are supported"), format_value);
|
|
return false;
|
|
}
|
|
std::string format_hasher_line = strprintf("%s,%s\n", format_key, format_value);
|
|
hasher << std::span{format_hasher_line};
|
|
|
|
DatabaseOptions options;
|
|
DatabaseStatus status;
|
|
ReadDatabaseArgs(args, options);
|
|
options.require_create = true;
|
|
options.require_format = DatabaseFormat::SQLITE;
|
|
std::unique_ptr<WalletDatabase> database = MakeDatabase(wallet_path, options, status, error);
|
|
if (!database) return false;
|
|
|
|
// dummy chain interface
|
|
bool ret = true;
|
|
std::shared_ptr<CWallet> wallet(new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet);
|
|
{
|
|
// Get the database handle
|
|
WalletDatabase& db = wallet->GetDatabase();
|
|
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
|
|
batch->TxnBegin();
|
|
|
|
// Read the records from the dump file and write them to the database
|
|
while (dump_file.good()) {
|
|
std::string key;
|
|
std::getline(dump_file, key, ',');
|
|
std::string value;
|
|
std::getline(dump_file, value, '\n');
|
|
|
|
if (key == "checksum") {
|
|
std::vector<unsigned char> parsed_checksum = ParseHex(value);
|
|
if (parsed_checksum.size() != checksum.size()) {
|
|
error = Untranslated("Error: Checksum is not the correct size");
|
|
ret = false;
|
|
break;
|
|
}
|
|
std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.begin());
|
|
break;
|
|
}
|
|
|
|
std::string line = strprintf("%s,%s\n", key, value);
|
|
hasher << std::span{line};
|
|
|
|
if (key.empty() || value.empty()) {
|
|
continue;
|
|
}
|
|
|
|
if (!IsHex(key)) {
|
|
error = strprintf(_("Error: Got key that was not hex: %s"), key);
|
|
ret = false;
|
|
break;
|
|
}
|
|
if (!IsHex(value)) {
|
|
error = strprintf(_("Error: Got value that was not hex: %s"), value);
|
|
ret = false;
|
|
break;
|
|
}
|
|
|
|
std::vector<unsigned char> k = ParseHex(key);
|
|
std::vector<unsigned char> v = ParseHex(value);
|
|
if (!batch->Write(std::span{k}, std::span{v})) {
|
|
error = strprintf(_("Error: Unable to write record to new wallet"));
|
|
ret = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (ret) {
|
|
uint256 comp_checksum = hasher.GetHash();
|
|
if (checksum.IsNull()) {
|
|
error = _("Error: Missing checksum");
|
|
ret = false;
|
|
} else if (checksum != comp_checksum) {
|
|
error = strprintf(_("Error: Dumpfile checksum does not match. Computed %s, expected %s"), HexStr(comp_checksum), HexStr(checksum));
|
|
ret = false;
|
|
}
|
|
}
|
|
|
|
if (ret) {
|
|
batch->TxnCommit();
|
|
} else {
|
|
batch->TxnAbort();
|
|
}
|
|
|
|
batch.reset();
|
|
|
|
dump_file.close();
|
|
}
|
|
// On failure, gather the paths to remove
|
|
std::vector<fs::path> paths_to_remove = wallet->GetDatabase().Files();
|
|
if (!name.empty()) paths_to_remove.push_back(wallet_path);
|
|
|
|
wallet.reset(); // The pointer deleter will close the wallet for us.
|
|
|
|
// Remove the wallet dir if we have a failure
|
|
if (!ret) {
|
|
for (const auto& p : paths_to_remove) {
|
|
fs::remove(p);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
} // namespace wallet
|