19 static const std::string
DUMP_MAGIC =
"BITCOIN_CORE_WALLET_DUMP";
25 std::string dump_filename =
args.
GetArg(
"-dumpfile",
"");
26 if (dump_filename.empty()) {
27 error =
_(
"No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
37 std::ofstream dump_file;
39 if (dump_file.fail()) {
47 std::unique_ptr<DatabaseBatch> batch = db.
MakeBatch();
50 if (!batch->StartCursor()) {
51 error =
_(
"Error: Couldn't create cursor into database");
57 dump_file.write(line.data(), line.size());
62 dump_file.write(line.data(), line.size());
72 ret = batch->ReadAtCursor(ss_key, ss_value, complete);
77 error =
_(
"Error reading next record from wallet database");
80 std::string key_str =
HexStr(ss_key);
81 std::string value_str =
HexStr(ss_value);
82 line =
strprintf(
"%s,%s\n", key_str, value_str);
83 dump_file.write(line.data(), line.size());
112 wallet->WalletLogPrintf(
"Releasing wallet\n");
120 std::string dump_filename =
args.
GetArg(
"-dumpfile",
"");
121 if (dump_filename.empty()) {
122 error =
_(
"No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.");
132 std::ifstream dump_file{dump_path};
139 std::string magic_key;
140 std::getline(dump_file, magic_key,
',');
141 std::string version_value;
142 std::getline(dump_file, version_value,
'\n');
144 error =
strprintf(
_(
"Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key,
DUMP_MAGIC);
151 error =
strprintf(
_(
"Error: Unable to parse version %u as a uint32_t"), version_value);
156 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);
160 std::string magic_hasher_line =
strprintf(
"%s,%s\n", magic_key, version_value);
164 std::string format_key;
165 std::getline(dump_file, format_key,
',');
166 std::string format_value;
167 std::getline(dump_file, format_value,
'\n');
168 if (format_key !=
"format") {
169 error =
strprintf(
_(
"Error: Dumpfile format record is incorrect. Got \"%s\", expected \"format\"."), format_key);
174 std::string file_format =
args.
GetArg(
"-format", format_value);
175 if (file_format.empty()) {
176 error =
_(
"No wallet file format provided. To use createfromdump, -format=<format> must be provided.");
180 if (file_format ==
"bdb") {
182 }
else if (file_format ==
"sqlite") {
185 error =
strprintf(
_(
"Unknown wallet file format \"%s\" provided. Please provide one of \"bdb\" or \"sqlite\"."), file_format);
188 if (file_format != format_value) {
189 warnings.push_back(
strprintf(
_(
"Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format));
191 std::string format_hasher_line =
strprintf(
"%s,%s\n", format_key, format_value);
199 std::unique_ptr<WalletDatabase> database =
MakeDatabase(wallet_path, options, status,
error);
200 if (!database)
return false;
215 std::unique_ptr<DatabaseBatch> batch = db.
MakeBatch();
219 while (dump_file.good()) {
221 std::getline(dump_file, key,
',');
223 std::getline(dump_file, value,
'\n');
225 if (key ==
"checksum") {
226 std::vector<unsigned char> parsed_checksum =
ParseHex(value);
227 if (parsed_checksum.size() != checksum.
size()) {
232 std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.
begin());
236 std::string line =
strprintf(
"%s,%s\n", key, value);
239 if (key.empty() || value.empty()) {
254 std::vector<unsigned char>
k =
ParseHex(key);
255 std::vector<unsigned char> v =
ParseHex(value);
260 if (!batch->Write(ss_key, ss_value)) {
268 uint256 comp_checksum = hasher.GetHash();
270 error =
_(
"Error: Missing checksum");
272 }
else if (checksum != comp_checksum) {
273 error =
strprintf(
_(
"Error: Dumpfile checksum does not match. Computed %s, expected %s"),
HexStr(comp_checksum),
HexStr(checksum));
292 fs::remove_all(wallet_path);
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
std::optional< DatabaseFormat > require_format
static path PathFromString(const std::string &string)
Convert byte string to path object.
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
static constexpr unsigned int size()
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
bool IsHex(std::string_view str)
Double ended buffer combining vector and stream-like interfaces.
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
static std::string PathToString(const path &path)
Convert path object to a byte string.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bilingual_str _(const char *psz)
Translation function.
A writer stream (for serialization) that computes a 256-bit hash.
static void WalletToolReleaseWallet(CWallet *wallet)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
static const std::string DUMP_MAGIC
bool CreateFromDump(const ArgsManager &args, const std::string &name, const fs::path &wallet_path, bilingual_str &error, std::vector< bilingual_str > &warnings)
bool DumpWallet(const ArgsManager &args, CWallet &wallet, bilingual_str &error)
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Span< const std::byte > MakeByteSpan(V &&v) noexcept
DBErrors
Error statuses for the wallet database.
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
virtual std::string Format()=0
std::shared_ptr< CWallet > wallet
static bool exists(const path &p)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
static path absolute(const path &p)
bool error(const char *fmt, const Args &... args)
An instance of this class represents one database.