10static const std::string
DUMP_MAGIC =
"BITCOIN_CORE_WALLET_DUMP";
16 std::string dump_filename =
gArgs.
GetArg(
"-dumpfile",
"");
17 if (dump_filename.empty()) {
18 error =
_(
"No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
23 path = fs::absolute(path);
30 if (dump_file.fail()) {
38 std::unique_ptr<DatabaseBatch> batch = db.
MakeBatch();
41 if (!batch->StartCursor()) {
42 error =
_(
"Error: Couldn't create cursor into database");
48 dump_file.write(line.data(), line.size());
49 hasher.
write(line.data(), line.size());
53 dump_file.write(line.data(), line.size());
54 hasher.
write(line.data(), line.size());
63 ret = batch->ReadAtCursor(ss_key, ss_value, complete);
68 error =
_(
"Error reading next record from wallet database");
71 std::string key_str =
HexStr(ss_key);
72 std::string value_str =
HexStr(ss_value);
73 line =
strprintf(
"%s,%s\n", key_str, value_str);
74 dump_file.write(line.data(), line.size());
75 hasher.
write(line.data(), line.size());
103 wallet->WalletLogPrintf(
"Releasing wallet\n");
111 std::string dump_filename =
gArgs.
GetArg(
"-dumpfile",
"");
112 if (dump_filename.empty()) {
113 error =
_(
"No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.");
118 dump_path = fs::absolute(dump_path);
130 std::string magic_key;
131 std::getline(dump_file, magic_key,
',');
132 std::string version_value;
133 std::getline(dump_file, version_value,
'\n');
135 error =
strprintf(
_(
"Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key,
DUMP_MAGIC);
142 error =
strprintf(
_(
"Error: Unable to parse version %u as a uint32_t"), version_value);
147 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);
151 std::string magic_hasher_line =
strprintf(
"%s,%s\n", magic_key, version_value);
152 hasher.
write(magic_hasher_line.data(), magic_hasher_line.size());
155 std::string format_key;
156 std::getline(dump_file, format_key,
',');
157 std::string format_value;
158 std::getline(dump_file, format_value,
'\n');
159 if (format_key !=
"format") {
160 error =
strprintf(
_(
"Error: Dumpfile format record is incorrect. Got \"%s\", expected \"format\"."), format_key);
165 std::string file_format =
gArgs.
GetArg(
"-format", format_value);
166 if (file_format.empty()) {
167 error =
_(
"No wallet file format provided. To use createfromdump, -format=<format> must be provided.");
171 if (file_format ==
"bdb") {
173 }
else if (file_format ==
"sqlite") {
176 error =
strprintf(
_(
"Unknown wallet file format \"%s\" provided. Please provide one of \"bdb\" or \"sqlite\"."), file_format);
179 if (file_format != format_value) {
180 warnings.push_back(
strprintf(
_(
"Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format));
182 std::string format_hasher_line =
strprintf(
"%s,%s\n", format_key, format_value);
183 hasher.
write(format_hasher_line.data(), format_hasher_line.size());
189 std::unique_ptr<WalletDatabase> database =
MakeDatabase(wallet_path, options, status,
error);
190 if (!database)
return false;
205 std::unique_ptr<DatabaseBatch> batch = db.
MakeBatch();
209 while (dump_file.good()) {
211 std::getline(dump_file, key,
',');
213 std::getline(dump_file, value,
'\n');
215 if (key ==
"checksum") {
216 std::vector<unsigned char> parsed_checksum =
ParseHex(value);
217 std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.
begin());
221 std::string line =
strprintf(
"%s,%s\n", key, value);
222 hasher.
write(line.data(), line.size());
224 if (key.empty() || value.empty()) {
239 std::vector<unsigned char> k =
ParseHex(key);
240 std::vector<unsigned char> v =
ParseHex(value);
245 if (!batch->Write(ss_key, ss_value)) {
255 error =
_(
"Error: Missing checksum");
257 }
else if (checksum != comp_checksum) {
258 error =
strprintf(
_(
"Error: Dumpfile checksum does not match. Computed %s, expected %s"),
HexStr(comp_checksum),
HexStr(checksum));
277 fs::remove_all(wallet_path);
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Double ended buffer combining vector and stream-like interfaces.
A writer stream (for serialization) that computes a 256-bit hash.
void write(const char *pch, size_t size)
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
An instance of this class represents one 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
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
bool CreateFromDump(const std::string &name, const fs::path &wallet_path, bilingual_str &error, std::vector< bilingual_str > &warnings)
bool DumpWallet(CWallet &wallet, bilingual_str &error)
static const std::string DUMP_MAGIC
static void WalletToolReleaseWallet(CWallet *wallet)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< unsigned char > ParseHex(const char *psz)
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool IsHex(const std::string &str)
std::optional< DatabaseFormat > require_format
bool error(const char *fmt, const Args &... args)
bilingual_str _(const char *psz)
Translation function.
DBErrors
Error statuses for the wallet database.