20 static const std::string
DUMP_MAGIC =
"BITCOIN_CORE_WALLET_DUMP";
26 std::string dump_filename =
args.
GetArg(
"-dumpfile",
"");
27 if (dump_filename.empty()) {
28 error =
_(
"No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
38 std::ofstream dump_file;
40 if (dump_file.fail()) {
48 std::unique_ptr<DatabaseBatch> batch = db.
MakeBatch();
51 std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
53 error =
_(
"Error: Couldn't create cursor into database");
59 dump_file.write(line.data(), line.size());
64 dump_file.write(line.data(), line.size());
78 error =
_(
"Error reading next record from wallet database");
82 std::string key_str =
HexStr(ss_key);
83 std::string value_str =
HexStr(ss_value);
84 line =
strprintf(
"%s,%s\n", key_str, value_str);
85 dump_file.write(line.data(), line.size());
114 wallet->WalletLogPrintf(
"Releasing wallet\n");
122 std::string dump_filename =
args.
GetArg(
"-dumpfile",
"");
123 if (dump_filename.empty()) {
124 error =
_(
"No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.");
134 std::ifstream dump_file{dump_path};
141 std::string magic_key;
142 std::getline(dump_file, magic_key,
',');
143 std::string version_value;
144 std::getline(dump_file, version_value,
'\n');
146 error =
strprintf(
_(
"Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key,
DUMP_MAGIC);
153 error =
strprintf(
_(
"Error: Unable to parse version %u as a uint32_t"), version_value);
158 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);
162 std::string magic_hasher_line =
strprintf(
"%s,%s\n", magic_key, version_value);
166 std::string format_key;
167 std::getline(dump_file, format_key,
',');
168 std::string format_value;
169 std::getline(dump_file, format_value,
'\n');
170 if (format_key !=
"format") {
171 error =
strprintf(
_(
"Error: Dumpfile format record is incorrect. Got \"%s\", expected \"format\"."), format_key);
176 std::string file_format =
args.
GetArg(
"-format", format_value);
177 if (file_format.empty()) {
178 error =
_(
"No wallet file format provided. To use createfromdump, -format=<format> must be provided.");
182 if (file_format ==
"bdb") {
184 }
else if (file_format ==
"sqlite") {
187 error =
strprintf(
_(
"Unknown wallet file format \"%s\" provided. Please provide one of \"bdb\" or \"sqlite\"."), file_format);
190 if (file_format != format_value) {
191 warnings.push_back(
strprintf(
_(
"Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format));
193 std::string format_hasher_line =
strprintf(
"%s,%s\n", format_key, format_value);
201 std::unique_ptr<WalletDatabase> database =
MakeDatabase(wallet_path, options, status,
error);
202 if (!database)
return false;
217 std::unique_ptr<DatabaseBatch> batch = db.
MakeBatch();
221 while (dump_file.good()) {
223 std::getline(dump_file, key,
',');
225 std::getline(dump_file, value,
'\n');
227 if (key ==
"checksum") {
228 std::vector<unsigned char> parsed_checksum =
ParseHex(value);
229 if (parsed_checksum.size() != checksum.
size()) {
234 std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.
begin());
238 std::string line =
strprintf(
"%s,%s\n", key, value);
241 if (key.empty() || value.empty()) {
256 std::vector<unsigned char>
k =
ParseHex(key);
257 std::vector<unsigned char> v =
ParseHex(value);
262 if (!batch->Write(ss_key, ss_value)) {
270 uint256 comp_checksum = hasher.GetHash();
272 error =
_(
"Error: Missing checksum");
274 }
else if (checksum != comp_checksum) {
275 error =
strprintf(
_(
"Error: Dumpfile checksum does not match. Computed %s, expected %s"),
HexStr(comp_checksum),
HexStr(checksum));
294 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.
constexpr bool IsNull() const
static constexpr unsigned int size()
constexpr unsigned char * begin()
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
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::string Format()=0
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
bool error(const char *fmt, const Args &... args)
static path absolute(const path &p)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
DBErrors
Error statuses for the wallet database.
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
static void WalletToolReleaseWallet(CWallet *wallet)
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
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)
static const std::string DUMP_MAGIC
std::shared_ptr< CWallet > wallet
Span< const std::byte > MakeByteSpan(V &&v) noexcept
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::optional< DatabaseFormat > require_format
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool IsHex(std::string_view str)
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.