Skip to content

Commit a88c320

Browse files
committed
wallettool: Add createfromdump command
Creates a new wallet file using the dump file produced by the dump command
1 parent e1e7a90 commit a88c320

File tree

4 files changed

+208
-1
lines changed

4 files changed

+208
-1
lines changed

src/bitcoin-wallet.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
2727
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2828
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2929
argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
30-
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file.", ArgsManager::ALLOW_STRING, OptionsCategory::OPTIONS);
30+
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_STRING, OptionsCategory::OPTIONS);
3131
argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
3232
argsman.AddArg("-descriptors", "Create descriptors wallet. Only for create", ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);
33+
argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
3334
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
3435

3536
argsman.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
3637
argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
3738
argsman.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
3839
argsman.AddArg("dump", "Print out all of the wallet key-value records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
40+
argsman.AddArg("createfromdump", "Create new wallet file from dumped records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
3941
}
4042

4143
static bool WalletAppInit(int argc, char* argv[])

src/wallet/dump.cpp

+186
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,189 @@ bool DumpWallet(CWallet& wallet, bilingual_str& error)
9494

9595
return ret;
9696
}
97+
98+
// The standard wallet deleter function blocks on the validation interface
99+
// queue, which doesn't exist for the bitcoin-wallet. Define our own
100+
// deleter here.
101+
static void WalletToolReleaseWallet(CWallet* wallet)
102+
{
103+
wallet->WalletLogPrintf("Releasing wallet\n");
104+
wallet->Close();
105+
delete wallet;
106+
}
107+
108+
bool CreateFromDump(const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings)
109+
{
110+
// Get the dumpfile
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.");
114+
return false;
115+
}
116+
117+
fs::path dump_path = dump_filename;
118+
dump_path = fs::absolute(dump_path);
119+
if (!fs::exists(dump_path)) {
120+
error = strprintf(_("Dump file %s does not exist."), dump_path.string());
121+
return false;
122+
}
123+
fsbridge::ifstream dump_file(dump_path);
124+
125+
// Compute the checksum
126+
CHashWriter hasher(0, 0);
127+
uint256 checksum;
128+
129+
// Check the magic and version
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');
134+
if (magic_key != DUMP_MAGIC) {
135+
error = strprintf(_("Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key, DUMP_MAGIC);
136+
dump_file.close();
137+
return false;
138+
}
139+
// Check the version number (value of first record)
140+
uint32_t ver;
141+
if (!ParseUInt32(version_value, &ver)) {
142+
error =strprintf(_("Error: Unable to parse version %u as a uint32_t"), version_value);
143+
dump_file.close();
144+
return false;
145+
}
146+
if (ver != DUMP_VERSION) {
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);
148+
dump_file.close();
149+
return false;
150+
}
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());
153+
154+
// Get the stored file format
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);
161+
dump_file.close();
162+
return false;
163+
}
164+
// Get the data file format with format_value as the default
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.");
168+
return false;
169+
}
170+
DatabaseFormat data_format;
171+
if (file_format == "bdb") {
172+
data_format = DatabaseFormat::BERKELEY;
173+
} else if (file_format == "sqlite") {
174+
data_format = DatabaseFormat::SQLITE;
175+
} else {
176+
error = strprintf(_("Unknown wallet file format \"%s\" provided. Please provide one of \"bdb\" or \"sqlite\"."), file_format);
177+
return false;
178+
}
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));
181+
}
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());
184+
185+
DatabaseOptions options;
186+
DatabaseStatus status;
187+
options.require_create = true;
188+
options.require_format = data_format;
189+
std::unique_ptr<WalletDatabase> database = MakeDatabase(wallet_path, options, status, error);
190+
if (!database) return false;
191+
192+
// dummy chain interface
193+
bool ret = true;
194+
std::shared_ptr<CWallet> wallet(new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet);
195+
{
196+
LOCK(wallet->cs_wallet);
197+
bool first_run = true;
198+
DBErrors load_wallet_ret = wallet->LoadWallet(first_run);
199+
if (load_wallet_ret != DBErrors::LOAD_OK) {
200+
error = strprintf(_("Error creating %s"), name);
201+
return false;
202+
}
203+
204+
// Get the database handle
205+
WalletDatabase& db = wallet->GetDatabase();
206+
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
207+
batch->TxnBegin();
208+
209+
// Read the records from the dump file and write them to the database
210+
while (dump_file.good()) {
211+
std::string key;
212+
std::getline(dump_file, key, ',');
213+
std::string value;
214+
std::getline(dump_file, value, '\n');
215+
216+
if (key == "checksum") {
217+
std::vector<unsigned char> parsed_checksum = ParseHex(value);
218+
std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.begin());
219+
break;
220+
}
221+
222+
std::string line = strprintf("%s,%s\n", key, value);
223+
hasher.write(line.data(), line.size());
224+
225+
if (key.empty() || value.empty()) {
226+
continue;
227+
}
228+
229+
if (!IsHex(key)) {
230+
error = strprintf(_("Error: Got key that was not hex: %s"), key);
231+
ret = false;
232+
break;
233+
}
234+
if (!IsHex(value)) {
235+
error = strprintf(_("Error: Got value that was not hex: %s"), value);
236+
ret = false;
237+
break;
238+
}
239+
240+
std::vector<unsigned char> k = ParseHex(key);
241+
std::vector<unsigned char> v = ParseHex(value);
242+
243+
CDataStream ss_key(k, SER_DISK, CLIENT_VERSION);
244+
CDataStream ss_value(v, SER_DISK, CLIENT_VERSION);
245+
246+
if (!batch->Write(ss_key, ss_value)) {
247+
error = strprintf(_("Error: Unable to write record to new wallet"));
248+
ret = false;
249+
break;
250+
}
251+
}
252+
253+
if (ret) {
254+
uint256 comp_checksum = hasher.GetHash();
255+
if (checksum.IsNull()) {
256+
error = _("Error: Missing checksum");
257+
ret = false;
258+
} else if (checksum != comp_checksum) {
259+
error = strprintf(_("Error: Dumpfile checksum does not match. Computed %s, expected %s"), HexStr(comp_checksum), HexStr(checksum));
260+
ret = false;
261+
}
262+
}
263+
264+
if (ret) {
265+
batch->TxnCommit();
266+
} else {
267+
batch->TxnAbort();
268+
}
269+
270+
batch.reset();
271+
272+
dump_file.close();
273+
}
274+
wallet.reset(); // The pointer deleter will close the wallet for us.
275+
276+
// Remove the wallet dir if we have a failure
277+
if (!ret) {
278+
fs::remove_all(wallet_path);
279+
}
280+
281+
return ret;
282+
}

src/wallet/dump.h

+3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
#ifndef BITCOIN_WALLET_DUMP_H
66
#define BITCOIN_WALLET_DUMP_H
77

8+
#include <fs.h>
9+
810
class CWallet;
911

1012
struct bilingual_str;
1113

1214
bool DumpWallet(CWallet& wallet, bilingual_str& error);
15+
bool CreateFromDump(const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings);
1316

1417
#endif // BITCOIN_WALLET_DUMP_H

src/wallet/wallettool.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
107107
{
108108
fs::path path = fs::absolute(name, GetWalletDir());
109109

110+
// -format is only allowed with createfromdump. Disallow it for all other commands.
111+
if (gArgs.IsArgSet("-format") && command != "createfromdump") {
112+
tfm::format(std::cerr, "The -format option can only be used with the \"createfromdump\" command.\n");
113+
return false;
114+
}
110115
// -dumpfile is only allowed with dump and createfromdump. Disallow it for all other commands.
111116
if (gArgs.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
112117
tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
@@ -164,6 +169,17 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
164169
}
165170
tfm::format(std::cout, "The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n");
166171
return ret;
172+
} else if (command == "createfromdump") {
173+
bilingual_str error;
174+
std::vector<bilingual_str> warnings;
175+
bool ret = CreateFromDump(name, path, error, warnings);
176+
for (const auto& warning : warnings) {
177+
tfm::format(std::cout, "%s\n", warning.original);
178+
}
179+
if (!ret && !error.empty()) {
180+
tfm::format(std::cerr, "%s\n", error.original);
181+
}
182+
return ret;
167183
} else {
168184
tfm::format(std::cerr, "Invalid command: %s\n", command);
169185
return false;

0 commit comments

Comments
 (0)