tool_wallet.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2018-present The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test bitcoin-wallet."""
6
7 import os
8 import stat
9 import subprocess
10 import textwrap
11
12 from collections import OrderedDict
13
14 from test_framework.test_framework import BitcoinTestFramework
15 from test_framework.util import (
16 assert_equal,
17 assert_greater_than,
18 sha256sum_file,
19 )
20
21
22 class ToolWalletTest(BitcoinTestFramework):
23 def set_test_params(self):
24 self.num_nodes = 1
25 self.setup_clean_chain = True
26 self.rpc_timeout = 120
27
28 def skip_test_if_missing_module(self):
29 self.skip_if_no_wallet()
30 self.skip_if_no_wallet_tool()
31
32 def bitcoin_wallet_process(self, *args):
33 default_args = ['-datadir={}'.format(self.nodes[0].datadir_path), '-chain=%s' % self.chain]
34
35 return subprocess.Popen(self.get_binaries().wallet_argv() + default_args + list(args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
36
37 def assert_raises_tool_error(self, error, *args):
38 p = self.bitcoin_wallet_process(*args)
39 stdout, stderr = p.communicate()
40 assert_equal(stdout, '')
41 if isinstance(error, tuple):
42 assert_equal(p.poll(), error[0])
43 assert error[1] in stderr.strip()
44 else:
45 assert_equal(p.poll(), 1)
46 assert error in stderr.strip()
47
48 def assert_tool_output(self, output, *args):
49 p = self.bitcoin_wallet_process(*args)
50 stdout, stderr = p.communicate()
51 assert_equal(stderr, '')
52 assert_equal(stdout, output)
53 assert_equal(p.poll(), 0)
54
55 def wallet_shasum(self):
56 return sha256sum_file(self.wallet_path).hex()
57
58 def wallet_timestamp(self):
59 return os.path.getmtime(self.wallet_path)
60
61 def wallet_permissions(self):
62 return oct(os.lstat(self.wallet_path).st_mode)[-3:]
63
64 def log_wallet_timestamp_comparison(self, old, new):
65 result = 'unchanged' if new == old else 'increased!'
66 self.log.debug('Wallet file timestamp {}'.format(result))
67
68 def get_expected_info_output(self, name="", transactions=0, keypool=2, address=0, imported_privs=0):
69 wallet_name = self.default_wallet_name if name == "" else name
70 output_types = 4 # p2pkh, p2sh, segwit, bech32m
71 return textwrap.dedent('''\
72 Wallet info
73 ===========
74 Name: %s
75 Format: sqlite
76 Descriptors: yes
77 Encrypted: no
78 HD (hd seed available): yes
79 Keypool Size: %d
80 Transactions: %d
81 Address Book: %d
82 ''' % (wallet_name, keypool * output_types, transactions, imported_privs * 3 + address))
83
84 def read_dump(self, filename):
85 dump = OrderedDict()
86 with open(filename, "r") as f:
87 for row in f:
88 row = row.strip()
89 key, value = row.split(',')
90 dump[key] = value
91 return dump
92
93 def assert_is_sqlite(self, filename):
94 with open(filename, 'rb') as f:
95 file_magic = f.read(16)
96 assert_equal(file_magic, b'SQLite format 3\x00')
97
98 def write_dump(self, dump, filename, magic=None, skip_checksum=False):
99 if magic is None:
100 magic = "BITCOIN_CORE_WALLET_DUMP"
101 with open(filename, "w") as f:
102 row = ",".join([magic, dump[magic]]) + "\n"
103 f.write(row)
104 for k, v in dump.items():
105 if k == magic or k == "checksum":
106 continue
107 row = ",".join([k, v]) + "\n"
108 f.write(row)
109 if not skip_checksum:
110 row = ",".join(["checksum", dump["checksum"]]) + "\n"
111 f.write(row)
112
113 def do_tool_createfromdump(self, wallet_name, dumpfile):
114 dumppath = self.nodes[0].datadir_path / dumpfile
115 rt_dumppath = self.nodes[0].datadir_path / "rt-{}.dump".format(wallet_name)
116
117 args = ["-wallet={}".format(wallet_name),
118 "-dumpfile={}".format(dumppath)]
119 args.append("createfromdump")
120
121 load_output = ""
122 self.assert_tool_output(load_output, *args)
123 assert (self.nodes[0].wallets_path / wallet_name).is_dir()
124
125 self.assert_tool_output("The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n", '-wallet={}'.format(wallet_name), '-dumpfile={}'.format(rt_dumppath), 'dump')
126
127 wallet_dat = self.nodes[0].wallets_path / wallet_name / "wallet.dat"
128 self.assert_is_sqlite(wallet_dat)
129
130 def test_invalid_tool_commands_and_args(self):
131 self.log.info('Testing that various invalid commands raise with specific error messages')
132 self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'foo'", 'foo')
133 # `bitcoin-wallet help` raises an error. Use `bitcoin-wallet -help`.
134 self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'help'", 'help')
135 self.assert_raises_tool_error('Error: Additional arguments provided (create). Methods do not take arguments. Please refer to `-help`.', 'info', 'create')
136 self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo')
137 self.assert_raises_tool_error('No method provided. Run `bitcoin-wallet -help` for valid methods.')
138 self.assert_raises_tool_error('Wallet name must be provided when creating a new wallet.', 'create')
139 self.assert_raises_tool_error('Wallet name must be provided when creating a new wallet.', 'createfromdump')
140 error = f"SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of {self.config['environment']['CLIENT_NAME']}?"
141 self.assert_raises_tool_error(
142 error,
143 '-wallet=' + self.default_wallet_name,
144 'info',
145 )
146 path = self.nodes[0].wallets_path / "nonexistent.dat"
147 self.assert_raises_tool_error("Failed to load database path '{}'. Path does not exist.".format(path), '-wallet=nonexistent.dat', 'info')
148
149 def test_tool_wallet_info(self):
150 # Stop the node to close the wallet to call the info command.
151 self.stop_node(0)
152 self.log.info('Calling wallet tool info, testing output')
153 #
154 # TODO: Wallet tool info should work with wallet file permissions set to
155 # read-only without raising:
156 # "Error loading wallet.dat. Is wallet being used by another process?"
157 # The following lines should be uncommented and the tests still succeed:
158 #
159 # self.log.debug('Setting wallet file permissions to 400 (read-only)')
160 # os.chmod(self.wallet_path, stat.S_IRUSR)
161 # assert self.wallet_permissions() in ['400', '666'] # Sanity check. 666 on Windows.
162 # shasum_before = self.wallet_shasum()
163 timestamp_before = self.wallet_timestamp()
164 self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before))
165 out = self.get_expected_info_output(imported_privs=1)
166 self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info')
167 timestamp_after = self.wallet_timestamp()
168 self.log.debug('Wallet file timestamp after calling info: {}'.format(timestamp_after))
169 self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
170 self.log.debug('Setting wallet file permissions back to 600 (read/write)')
171 os.chmod(self.wallet_path, stat.S_IRUSR | stat.S_IWUSR)
172 assert self.wallet_permissions() in ['600', '666'] # Sanity check. 666 on Windows.
173 #
174 # TODO: Wallet tool info should not write to the wallet file.
175 # The following lines should be uncommented and the tests still succeed:
176 #
177 # assert_equal(timestamp_before, timestamp_after)
178 # shasum_after = self.wallet_shasum()
179 # assert_equal(shasum_before, shasum_after)
180 # self.log.debug('Wallet file shasum unchanged\n')
181
182 def test_tool_wallet_info_after_transaction(self):
183 """
184 Mutate the wallet with a transaction to verify that the info command
185 output changes accordingly.
186 """
187 self.start_node(0)
188 self.log.info('Generating transaction to mutate wallet')
189 self.generate(self.nodes[0], 1)
190 self.stop_node(0)
191
192 self.log.info('Calling wallet tool info after generating a transaction, testing output')
193 shasum_before = self.wallet_shasum()
194 timestamp_before = self.wallet_timestamp()
195 self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before))
196 out = self.get_expected_info_output(transactions=1, imported_privs=1)
197 self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info')
198 shasum_after = self.wallet_shasum()
199 timestamp_after = self.wallet_timestamp()
200 self.log.debug('Wallet file timestamp after calling info: {}'.format(timestamp_after))
201 self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
202 #
203 # TODO: Wallet tool info should not write to the wallet file.
204 # This assertion should be uncommented and succeed:
205 # assert_equal(timestamp_before, timestamp_after)
206 assert_equal(shasum_before, shasum_after)
207 self.log.debug('Wallet file shasum unchanged\n')
208
209 def test_tool_wallet_create_on_existing_wallet(self):
210 self.log.info('Calling wallet tool create on an existing wallet, testing output')
211 shasum_before = self.wallet_shasum()
212 timestamp_before = self.wallet_timestamp()
213 self.log.debug('Wallet file timestamp before calling create: {}'.format(timestamp_before))
214 out = "Topping up keypool...\n" + self.get_expected_info_output(name="foo", keypool=2000)
215 self.assert_tool_output(out, '-wallet=foo', 'create')
216 shasum_after = self.wallet_shasum()
217 timestamp_after = self.wallet_timestamp()
218 self.log.debug('Wallet file timestamp after calling create: {}'.format(timestamp_after))
219 self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
220 assert_equal(timestamp_before, timestamp_after)
221 assert_equal(shasum_before, shasum_after)
222 self.log.debug('Wallet file shasum unchanged\n')
223
224 def test_getwalletinfo_on_different_wallet(self):
225 self.log.info('Starting node with arg -wallet=foo')
226 self.start_node(0, ['-nowallet', '-wallet=foo'])
227
228 self.log.info('Calling getwalletinfo on a different wallet ("foo"), testing output')
229 shasum_before = self.wallet_shasum()
230 timestamp_before = self.wallet_timestamp()
231 self.log.debug('Wallet file timestamp before calling getwalletinfo: {}'.format(timestamp_before))
232 out = self.nodes[0].getwalletinfo()
233 self.stop_node(0)
234
235 shasum_after = self.wallet_shasum()
236 timestamp_after = self.wallet_timestamp()
237 self.log.debug('Wallet file timestamp after calling getwalletinfo: {}'.format(timestamp_after))
238
239 assert_equal(0, out['txcount'])
240 assert_equal(4000, out['keypoolsize'])
241 assert_equal(4000, out['keypoolsize_hd_internal'])
242
243 self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
244 assert_equal(timestamp_before, timestamp_after)
245 assert_equal(shasum_after, shasum_before)
246 self.log.debug('Wallet file shasum unchanged\n')
247
248 def test_dump_createfromdump(self):
249 self.start_node(0)
250 self.nodes[0].createwallet("todump")
251 file_format = self.nodes[0].get_wallet_rpc("todump").getwalletinfo()["format"]
252 self.nodes[0].createwallet("todump2")
253 self.stop_node(0)
254
255 self.log.info('Checking dump arguments')
256 self.assert_raises_tool_error('No dump file provided. To use dump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'dump')
257
258 self.log.info('Checking basic dump')
259 wallet_dump = self.nodes[0].datadir_path / "wallet.dump"
260 self.assert_tool_output('The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n', '-wallet=todump', '-dumpfile={}'.format(wallet_dump), 'dump')
261
262 dump_data = self.read_dump(wallet_dump)
263 orig_dump = dump_data.copy()
264 # Check the dump magic
265 assert_equal(dump_data['BITCOIN_CORE_WALLET_DUMP'], '1')
266 # Check the file format
267 assert_equal(dump_data["format"], file_format)
268
269 self.log.info('Checking that a dumpfile cannot be overwritten')
270 self.assert_raises_tool_error('File {} already exists. If you are sure this is what you want, move it out of the way first.'.format(wallet_dump), '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'dump')
271
272 self.log.info('Checking createfromdump arguments')
273 self.assert_raises_tool_error('No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'createfromdump')
274 non_exist_dump = self.nodes[0].datadir_path / "wallet.nodump"
275 self.assert_raises_tool_error('Dump file {} does not exist.'.format(non_exist_dump), '-wallet=todump', '-dumpfile={}'.format(non_exist_dump), 'createfromdump')
276 wallet_path = self.nodes[0].wallets_path / "todump2"
277 self.assert_raises_tool_error('Failed to create database path \'{}\'. Database already exists.'.format(wallet_path), '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
278 self.assert_raises_tool_error("Invalid parameter -descriptors", '-descriptors', '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
279
280 self.log.info('Checking createfromdump')
281 self.do_tool_createfromdump("load", "wallet.dump")
282
283 self.log.info('Checking createfromdump handling of magic and versions')
284 bad_ver_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_ver1.dump"
285 dump_data["BITCOIN_CORE_WALLET_DUMP"] = "0"
286 self.write_dump(dump_data, bad_ver_wallet_dump)
287 self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version 0', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump')
288 assert not (self.nodes[0].wallets_path / "badload").is_dir()
289 bad_ver_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_ver2.dump"
290 dump_data["BITCOIN_CORE_WALLET_DUMP"] = "2"
291 self.write_dump(dump_data, bad_ver_wallet_dump)
292 self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version 2', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump')
293 assert not (self.nodes[0].wallets_path / "badload").is_dir()
294 bad_magic_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_magic.dump"
295 del dump_data["BITCOIN_CORE_WALLET_DUMP"]
296 dump_data["not_the_right_magic"] = "1"
297 self.write_dump(dump_data, bad_magic_wallet_dump, "not_the_right_magic")
298 self.assert_raises_tool_error('Error: Dumpfile identifier record is incorrect. Got "not_the_right_magic", expected "BITCOIN_CORE_WALLET_DUMP".', '-wallet=badload', '-dumpfile={}'.format(bad_magic_wallet_dump), 'createfromdump')
299 assert not (self.nodes[0].wallets_path / "badload").is_dir()
300
301 self.log.info('Checking createfromdump handling of checksums')
302 bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum1.dump"
303 dump_data = orig_dump.copy()
304 checksum = dump_data["checksum"]
305 dump_data["checksum"] = "1" * 64
306 self.write_dump(dump_data, bad_sum_wallet_dump)
307 self.assert_raises_tool_error('Error: Dumpfile checksum does not match. Computed {}, expected {}'.format(checksum, "1" * 64), '-wallet=bad', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
308 assert not (self.nodes[0].wallets_path / "badload").is_dir()
309 bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum2.dump"
310 del dump_data["checksum"]
311 self.write_dump(dump_data, bad_sum_wallet_dump, skip_checksum=True)
312 self.assert_raises_tool_error('Error: Missing checksum', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
313 assert not (self.nodes[0].wallets_path / "badload").is_dir()
314 bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum3.dump"
315 dump_data["checksum"] = "2" * 10
316 self.write_dump(dump_data, bad_sum_wallet_dump)
317 self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
318 assert not (self.nodes[0].wallets_path / "badload").is_dir()
319 dump_data["checksum"] = "3" * 66
320 self.write_dump(dump_data, bad_sum_wallet_dump)
321 self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
322 assert not (self.nodes[0].wallets_path / "badload").is_dir()
323
324 def test_chainless_conflicts(self):
325 self.log.info("Test wallet tool when wallet contains conflicting transactions")
326 self.restart_node(0)
327 self.generate(self.nodes[0], 101)
328
329 def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
330
331 self.nodes[0].createwallet("conflicts")
332 wallet = self.nodes[0].get_wallet_rpc("conflicts")
333 def_wallet.sendtoaddress(wallet.getnewaddress(), 10)
334 self.generate(self.nodes[0], 1)
335
336 # parent tx
337 parent_txid = wallet.sendtoaddress(wallet.getnewaddress(), 9)
338 parent_txid_bytes = bytes.fromhex(parent_txid)[::-1]
339 conflict_utxo = wallet.gettransaction(txid=parent_txid, verbose=True)["decoded"]["vin"][0]
340
341 # The specific assertion in MarkConflicted being tested requires that the parent tx is already loaded
342 # by the time the child tx is loaded. Since transactions end up being loaded in txid order due to how both
343 # and sqlite store things, we can just grind the child tx until it has a txid that is greater than the parent's.
344 locktime = 500000000 # Use locktime as nonce, starting at unix timestamp minimum
345 addr = wallet.getnewaddress()
346 while True:
347 child_send_res = wallet.send(outputs=[{addr: 8}], add_to_wallet=False, locktime=locktime)
348 child_txid = child_send_res["txid"]
349 child_txid_bytes = bytes.fromhex(child_txid)[::-1]
350 if (child_txid_bytes > parent_txid_bytes):
351 wallet.sendrawtransaction(child_send_res["hex"])
352 break
353 locktime += 1
354
355 # conflict with parent
356 conflict_unsigned = self.nodes[0].createrawtransaction(inputs=[conflict_utxo], outputs=[{wallet.getnewaddress(): 9.9999}])
357 conflict_signed = wallet.signrawtransactionwithwallet(conflict_unsigned)["hex"]
358 conflict_txid = self.nodes[0].sendrawtransaction(conflict_signed)
359 self.generate(self.nodes[0], 1)
360 assert_equal(wallet.gettransaction(txid=parent_txid)["confirmations"], -1)
361 assert_equal(wallet.gettransaction(txid=child_txid)["confirmations"], -1)
362 assert_equal(wallet.gettransaction(txid=conflict_txid)["confirmations"], 1)
363
364 self.stop_node(0)
365
366 # Wallet tool should successfully give info for this wallet
367 expected_output = textwrap.dedent('''\
368 Wallet info
369 ===========
370 Name: conflicts
371 Format: sqlite
372 Descriptors: yes
373 Encrypted: no
374 HD (hd seed available): yes
375 Keypool Size: 8
376 Transactions: 4
377 Address Book: 4
378 ''')
379 self.assert_tool_output(expected_output, "-wallet=conflicts", "info")
380
381 def test_dump_very_large_records(self):
382 self.log.info("Test that wallets with large records are successfully dumped")
383
384 self.start_node(0)
385 self.nodes[0].createwallet("bigrecords")
386 wallet = self.nodes[0].get_wallet_rpc("bigrecords")
387
388 # Both BDB and sqlite have maximum page sizes of 65536 bytes, with defaults of 4096
389 # When a record exceeds some size threshold, both BDB and SQLite will store the data
390 # in one or more overflow pages. We want to make sure that our tooling can dump such
391 # records, even when they span multiple pages. To make a large record, we just need
392 # to make a very big transaction.
393 self.generate(self.nodes[0], 101)
394 def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
395 outputs = {}
396 for i in range(500):
397 outputs[wallet.getnewaddress(address_type="p2sh-segwit")] = 0.01
398 def_wallet.sendmany(amounts=outputs)
399 self.generate(self.nodes[0], 1)
400 send_res = wallet.sendall([def_wallet.getnewaddress()])
401 self.generate(self.nodes[0], 1)
402 assert_equal(send_res["complete"], True)
403 tx = wallet.gettransaction(txid=send_res["txid"], verbose=True)
404 assert_greater_than(tx["decoded"]["size"], 70000)
405
406 self.stop_node(0)
407
408 wallet_dump = self.nodes[0].datadir_path / "bigrecords.dump"
409 self.assert_tool_output("The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n", "-wallet=bigrecords", f"-dumpfile={wallet_dump}", "dump")
410 dump = self.read_dump(wallet_dump)
411 for k,v in dump.items():
412 if tx["hex"] in v:
413 break
414 else:
415 assert False, "Big transaction was not found in wallet dump"
416
417 def test_no_create_legacy(self):
418 self.log.info("Test that legacy wallets cannot be created")
419
420 self.assert_raises_tool_error("Invalid parameter -legacy", "-wallet=legacy", "-legacy", "create")
421 assert not (self.nodes[0].wallets_path / "legacy").exists()
422 self.assert_raises_tool_error("Invalid parameter -descriptors", "-wallet=legacy", "-descriptors=false", "create")
423 assert not (self.nodes[0].wallets_path / "legacy").exists()
424 self.assert_raises_tool_error("The -dumpfile option cannot be used with the 'create' command.", "-wallet=legacy", "-dumpfile=wallet.dump", "create")
425
426 def test_no_create_unnamed(self):
427 self.log.info("Test that unnamed (default) wallets cannot be created")
428
429 self.assert_raises_tool_error("Wallet name cannot be empty", "-wallet=", "create")
430 assert not (self.nodes[0].wallets_path / "wallet.dat").exists()
431
432 self.assert_raises_tool_error("Wallet name cannot be empty", "-wallet=", "-dumpfile=wallet.dump", "createfromdump")
433 assert not (self.nodes[0].wallets_path / "wallet.dat").exists()
434
435 def run_test(self):
436 self.wallet_path = self.nodes[0].wallets_path / self.default_wallet_name / self.wallet_data_filename
437 self.test_invalid_tool_commands_and_args()
438 # Warning: The following tests are order-dependent.
439 self.test_tool_wallet_info()
440 self.test_tool_wallet_info_after_transaction()
441 self.test_tool_wallet_create_on_existing_wallet()
442 self.test_getwalletinfo_on_different_wallet()
443 self.test_dump_createfromdump()
444 self.test_chainless_conflicts()
445 self.test_dump_very_large_records()
446 self.test_no_create_legacy()
447 self.test_no_create_unnamed()
448
449
450 if __name__ == '__main__':
451 ToolWalletTest(__file__).main()
452