Bitcoin Core  22.99.0
P2P Digital Currency
dump.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <wallet/dump.h>
6 
7 #include <fs.h>
8 #include <util/translation.h>
9 #include <wallet/wallet.h>
10 
11 #include <algorithm>
12 #include <fstream>
13 #include <memory>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 namespace wallet {
19 static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
20 uint32_t DUMP_VERSION = 1;
21 
23 {
24  // Get the dumpfile
25  std::string dump_filename = gArgs.GetArg("-dumpfile", "");
26  if (dump_filename.empty()) {
27  error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
28  return false;
29  }
30 
31  fs::path path = fs::PathFromString(dump_filename);
32  path = fs::absolute(path);
33  if (fs::exists(path)) {
34  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));
35  return false;
36  }
37  std::ofstream dump_file;
38  dump_file.open(path);
39  if (dump_file.fail()) {
40  error = strprintf(_("Unable to open %s for writing"), fs::PathToString(path));
41  return false;
42  }
43 
44  CHashWriter hasher(0, 0);
45 
46  WalletDatabase& db = wallet.GetDatabase();
47  std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
48 
49  bool ret = true;
50  if (!batch->StartCursor()) {
51  error = _("Error: Couldn't create cursor into database");
52  ret = false;
53  }
54 
55  // Write out a magic string with version
56  std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION);
57  dump_file.write(line.data(), line.size());
58  hasher.write(MakeByteSpan(line));
59 
60  // Write out the file format
61  line = strprintf("%s,%s\n", "format", db.Format());
62  dump_file.write(line.data(), line.size());
63  hasher.write(MakeByteSpan(line));
64 
65  if (ret) {
66 
67  // Read the records
68  while (true) {
71  bool complete;
72  ret = batch->ReadAtCursor(ss_key, ss_value, complete);
73  if (complete) {
74  ret = true;
75  break;
76  } else if (!ret) {
77  error = _("Error reading next record from wallet database");
78  break;
79  }
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());
84  hasher.write(MakeByteSpan(line));
85  }
86  }
87 
88  batch->CloseCursor();
89  batch.reset();
90 
91  // Close the wallet after we're done with it. The caller won't be doing this
92  wallet.Close();
93 
94  if (ret) {
95  // Write the hash
96  tfm::format(dump_file, "checksum,%s\n", HexStr(hasher.GetHash()));
97  dump_file.close();
98  } else {
99  // Remove the dumpfile on failure
100  dump_file.close();
101  fs::remove(path);
102  }
103 
104  return ret;
105 }
106 
107 // The standard wallet deleter function blocks on the validation interface
108 // queue, which doesn't exist for the bitcoin-wallet. Define our own
109 // deleter here.
111 {
112  wallet->WalletLogPrintf("Releasing wallet\n");
113  wallet->Close();
114  delete wallet;
115 }
116 
117 bool CreateFromDump(const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings)
118 {
119  // Get the dumpfile
120  std::string dump_filename = gArgs.GetArg("-dumpfile", "");
121  if (dump_filename.empty()) {
122  error = _("No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.");
123  return false;
124  }
125 
126  fs::path dump_path = fs::PathFromString(dump_filename);
127  dump_path = fs::absolute(dump_path);
128  if (!fs::exists(dump_path)) {
129  error = strprintf(_("Dump file %s does not exist."), fs::PathToString(dump_path));
130  return false;
131  }
132  std::ifstream dump_file{dump_path};
133 
134  // Compute the checksum
135  CHashWriter hasher(0, 0);
136  uint256 checksum;
137 
138  // Check the magic and version
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');
143  if (magic_key != DUMP_MAGIC) {
144  error = strprintf(_("Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key, DUMP_MAGIC);
145  dump_file.close();
146  return false;
147  }
148  // Check the version number (value of first record)
149  uint32_t ver;
150  if (!ParseUInt32(version_value, &ver)) {
151  error =strprintf(_("Error: Unable to parse version %u as a uint32_t"), version_value);
152  dump_file.close();
153  return false;
154  }
155  if (ver != DUMP_VERSION) {
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);
157  dump_file.close();
158  return false;
159  }
160  std::string magic_hasher_line = strprintf("%s,%s\n", magic_key, version_value);
161  hasher.write(MakeByteSpan(magic_hasher_line));
162 
163  // Get the stored file format
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);
170  dump_file.close();
171  return false;
172  }
173  // Get the data file format with format_value as the default
174  std::string file_format = gArgs.GetArg("-format", format_value);
175  if (file_format.empty()) {
176  error = _("No wallet file format provided. To use createfromdump, -format=<format> must be provided.");
177  return false;
178  }
179  DatabaseFormat data_format;
180  if (file_format == "bdb") {
181  data_format = DatabaseFormat::BERKELEY;
182  } else if (file_format == "sqlite") {
183  data_format = DatabaseFormat::SQLITE;
184  } else {
185  error = strprintf(_("Unknown wallet file format \"%s\" provided. Please provide one of \"bdb\" or \"sqlite\"."), file_format);
186  return false;
187  }
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));
190  }
191  std::string format_hasher_line = strprintf("%s,%s\n", format_key, format_value);
192  hasher.write(MakeByteSpan(format_hasher_line));
193 
194  DatabaseOptions options;
195  DatabaseStatus status;
196  options.require_create = true;
197  options.require_format = data_format;
198  std::unique_ptr<WalletDatabase> database = MakeDatabase(wallet_path, options, status, error);
199  if (!database) return false;
200 
201  // dummy chain interface
202  bool ret = true;
203  std::shared_ptr<CWallet> wallet(new CWallet(nullptr /* chain */, name, gArgs, std::move(database)), WalletToolReleaseWallet);
204  {
205  LOCK(wallet->cs_wallet);
206  DBErrors load_wallet_ret = wallet->LoadWallet();
207  if (load_wallet_ret != DBErrors::LOAD_OK) {
208  error = strprintf(_("Error creating %s"), name);
209  return false;
210  }
211 
212  // Get the database handle
213  WalletDatabase& db = wallet->GetDatabase();
214  std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
215  batch->TxnBegin();
216 
217  // Read the records from the dump file and write them to the database
218  while (dump_file.good()) {
219  std::string key;
220  std::getline(dump_file, key, ',');
221  std::string value;
222  std::getline(dump_file, value, '\n');
223 
224  if (key == "checksum") {
225  std::vector<unsigned char> parsed_checksum = ParseHex(value);
226  if (parsed_checksum.size() != checksum.size()) {
227  error = Untranslated("Error: Checksum is not the correct size");
228  ret = false;
229  break;
230  }
231  std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.begin());
232  break;
233  }
234 
235  std::string line = strprintf("%s,%s\n", key, value);
236  hasher.write(MakeByteSpan(line));
237 
238  if (key.empty() || value.empty()) {
239  continue;
240  }
241 
242  if (!IsHex(key)) {
243  error = strprintf(_("Error: Got key that was not hex: %s"), key);
244  ret = false;
245  break;
246  }
247  if (!IsHex(value)) {
248  error = strprintf(_("Error: Got value that was not hex: %s"), value);
249  ret = false;
250  break;
251  }
252 
253  std::vector<unsigned char> k = ParseHex(key);
254  std::vector<unsigned char> v = ParseHex(value);
255 
257  CDataStream ss_value(v, SER_DISK, CLIENT_VERSION);
258 
259  if (!batch->Write(ss_key, ss_value)) {
260  error = strprintf(_("Error: Unable to write record to new wallet"));
261  ret = false;
262  break;
263  }
264  }
265 
266  if (ret) {
267  uint256 comp_checksum = hasher.GetHash();
268  if (checksum.IsNull()) {
269  error = _("Error: Missing checksum");
270  ret = false;
271  } else if (checksum != comp_checksum) {
272  error = strprintf(_("Error: Dumpfile checksum does not match. Computed %s, expected %s"), HexStr(comp_checksum), HexStr(checksum));
273  ret = false;
274  }
275  }
276 
277  if (ret) {
278  batch->TxnCommit();
279  } else {
280  batch->TxnAbort();
281  }
282 
283  batch.reset();
284 
285  dump_file.close();
286  }
287  wallet.reset(); // The pointer deleter will close the wallet for us.
288 
289  // Remove the wallet dir if we have a failure
290  if (!ret) {
291  fs::remove_all(wallet_path);
292  }
293 
294  return ret;
295 }
296 } // namespace wallet
wallet::WalletDatabase
An instance of this class represents one database.
Definition: db.h:104
MakeByteSpan
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
fs::exists
static bool exists(const path &p)
Definition: fs.h:69
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
tinyformat::format
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1062
SER_DISK
@ SER_DISK
Definition: serialize.h:132
wallet.h
fs.h
wallet::DatabaseStatus
DatabaseStatus
Definition: db.h:213
bilingual_str
Bilingual messages:
Definition: translation.h:16
IsHex
bool IsHex(const std::string &str)
Definition: strencodings.cpp:61
wallet::DatabaseFormat
DatabaseFormat
Definition: db.h:199
base_blob::size
static constexpr unsigned int size()
Definition: uint256.h:80
dump.h
wallet::DumpWallet
bool DumpWallet(CWallet &wallet, bilingual_str &error)
Definition: dump.cpp:22
wallet::DBErrors
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:45
wallet
Definition: node.h:38
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:112
wallet
std::shared_ptr< CWallet > wallet
Definition: notifications.cpp:38
wallet::DUMP_MAGIC
static const std::string DUMP_MAGIC
Definition: dump.cpp:19
wallet::MakeDatabase
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1104
wallet::DatabaseOptions::require_create
bool require_create
Definition: db.h:206
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
wallet::WalletDatabase::MakeBatch
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
wallet::CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:232
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
wallet::DBErrors::LOAD_OK
@ LOAD_OK
wallet::DatabaseOptions::require_format
std::optional< DatabaseFormat > require_format
Definition: db.h:207
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:588
fs::PathFromString
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:135
uint256
256-bit opaque blob.
Definition: uint256.h:126
gArgs
ArgsManager gArgs
Definition: system.cpp:87
wallet::WalletDatabase::Format
virtual std::string Format()=0
name
const char * name
Definition: rest.cpp:52
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
wallet::CreateFromDump
bool CreateFromDump(const std::string &name, const fs::path &wallet_path, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: dump.cpp:117
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:33
wallet::DatabaseFormat::BERKELEY
@ BERKELEY
translation.h
wallet::WalletToolReleaseWallet
static void WalletToolReleaseWallet(CWallet *wallet)
Definition: dump.cpp:110
LOCK
#define LOCK(cs)
Definition: sync.h:226
wallet::DatabaseFormat::SQLITE
@ SQLITE
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
fs::absolute
static path absolute(const path &p)
Definition: fs.h:62
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
CHashWriter::GetHash
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:123
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:511
wallet::DatabaseOptions
Definition: db.h:204
wallet::DUMP_VERSION
uint32_t DUMP_VERSION
Definition: dump.cpp:20
ByteUnit::k
@ k
ParseUInt32
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:319
base_blob::begin
unsigned char * begin()
Definition: uint256.h:60
CHashWriter::write
void write(Span< const std::byte > src)
Definition: hash.h:114