PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/functional/tool_wallet.py

https://github.com/bitcoin/bitcoin
Python | 415 lines | 399 code | 11 blank | 5 comment | 9 complexity | 9be91afcdf59de32fb1c587fa2054fd0 MD5 | raw file
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2018-2021 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. import hashlib
  7. import os
  8. import stat
  9. import subprocess
  10. import textwrap
  11. from collections import OrderedDict
  12. from test_framework.test_framework import BitcoinTestFramework
  13. from test_framework.util import assert_equal
  14. BUFFER_SIZE = 16 * 1024
  15. class ToolWalletTest(BitcoinTestFramework):
  16. def set_test_params(self):
  17. self.num_nodes = 1
  18. self.setup_clean_chain = True
  19. self.rpc_timeout = 120
  20. def skip_test_if_missing_module(self):
  21. self.skip_if_no_wallet()
  22. self.skip_if_no_wallet_tool()
  23. def bitcoin_wallet_process(self, *args):
  24. binary = self.config["environment"]["BUILDDIR"] + '/src/bitcoin-wallet' + self.config["environment"]["EXEEXT"]
  25. default_args = ['-datadir={}'.format(self.nodes[0].datadir), '-chain=%s' % self.chain]
  26. if not self.options.descriptors and 'create' in args:
  27. default_args.append('-legacy')
  28. return subprocess.Popen([binary] + default_args + list(args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  29. def assert_raises_tool_error(self, error, *args):
  30. p = self.bitcoin_wallet_process(*args)
  31. stdout, stderr = p.communicate()
  32. assert_equal(p.poll(), 1)
  33. assert_equal(stdout, '')
  34. assert_equal(stderr.strip(), error)
  35. def assert_tool_output(self, output, *args):
  36. p = self.bitcoin_wallet_process(*args)
  37. stdout, stderr = p.communicate()
  38. assert_equal(stderr, '')
  39. assert_equal(stdout, output)
  40. assert_equal(p.poll(), 0)
  41. def wallet_shasum(self):
  42. h = hashlib.sha1()
  43. mv = memoryview(bytearray(BUFFER_SIZE))
  44. with open(self.wallet_path, 'rb', buffering=0) as f:
  45. for n in iter(lambda: f.readinto(mv), 0):
  46. h.update(mv[:n])
  47. return h.hexdigest()
  48. def wallet_timestamp(self):
  49. return os.path.getmtime(self.wallet_path)
  50. def wallet_permissions(self):
  51. return oct(os.lstat(self.wallet_path).st_mode)[-3:]
  52. def log_wallet_timestamp_comparison(self, old, new):
  53. result = 'unchanged' if new == old else 'increased!'
  54. self.log.debug('Wallet file timestamp {}'.format(result))
  55. def get_expected_info_output(self, name="", transactions=0, keypool=2, address=0):
  56. wallet_name = self.default_wallet_name if name == "" else name
  57. if self.options.descriptors:
  58. output_types = 4 # p2pkh, p2sh, segwit, bech32m
  59. return textwrap.dedent('''\
  60. Wallet info
  61. ===========
  62. Name: %s
  63. Format: sqlite
  64. Descriptors: yes
  65. Encrypted: no
  66. HD (hd seed available): yes
  67. Keypool Size: %d
  68. Transactions: %d
  69. Address Book: %d
  70. ''' % (wallet_name, keypool * output_types, transactions, address))
  71. else:
  72. output_types = 3 # p2pkh, p2sh, segwit. Legacy wallets do not support bech32m.
  73. return textwrap.dedent('''\
  74. Wallet info
  75. ===========
  76. Name: %s
  77. Format: bdb
  78. Descriptors: no
  79. Encrypted: no
  80. HD (hd seed available): yes
  81. Keypool Size: %d
  82. Transactions: %d
  83. Address Book: %d
  84. ''' % (wallet_name, keypool, transactions, address * output_types))
  85. def read_dump(self, filename):
  86. dump = OrderedDict()
  87. with open(filename, "r", encoding="utf8") as f:
  88. for row in f:
  89. row = row.strip()
  90. key, value = row.split(',')
  91. dump[key] = value
  92. return dump
  93. def assert_is_sqlite(self, filename):
  94. with open(filename, 'rb') as f:
  95. file_magic = f.read(16)
  96. assert file_magic == b'SQLite format 3\x00'
  97. def assert_is_bdb(self, filename):
  98. with open(filename, 'rb') as f:
  99. f.seek(12, 0)
  100. file_magic = f.read(4)
  101. assert file_magic == b'\x00\x05\x31\x62' or file_magic == b'\x62\x31\x05\x00'
  102. def write_dump(self, dump, filename, magic=None, skip_checksum=False):
  103. if magic is None:
  104. magic = "BITCOIN_CORE_WALLET_DUMP"
  105. with open(filename, "w", encoding="utf8") as f:
  106. row = ",".join([magic, dump[magic]]) + "\n"
  107. f.write(row)
  108. for k, v in dump.items():
  109. if k == magic or k == "checksum":
  110. continue
  111. row = ",".join([k, v]) + "\n"
  112. f.write(row)
  113. if not skip_checksum:
  114. row = ",".join(["checksum", dump["checksum"]]) + "\n"
  115. f.write(row)
  116. def assert_dump(self, expected, received):
  117. e = expected.copy()
  118. r = received.copy()
  119. # BDB will add a "version" record that is not present in sqlite
  120. # In that case, we should ignore this record in both
  121. # But because this also effects the checksum, we also need to drop that.
  122. v_key = "0776657273696f6e" # Version key
  123. if v_key in e and v_key not in r:
  124. del e[v_key]
  125. del e["checksum"]
  126. del r["checksum"]
  127. if v_key not in e and v_key in r:
  128. del r[v_key]
  129. del e["checksum"]
  130. del r["checksum"]
  131. assert_equal(len(e), len(r))
  132. for k, v in e.items():
  133. assert_equal(v, r[k])
  134. def do_tool_createfromdump(self, wallet_name, dumpfile, file_format=None):
  135. dumppath = os.path.join(self.nodes[0].datadir, dumpfile)
  136. rt_dumppath = os.path.join(self.nodes[0].datadir, "rt-{}.dump".format(wallet_name))
  137. dump_data = self.read_dump(dumppath)
  138. args = ["-wallet={}".format(wallet_name),
  139. "-dumpfile={}".format(dumppath)]
  140. if file_format is not None:
  141. args.append("-format={}".format(file_format))
  142. args.append("createfromdump")
  143. load_output = ""
  144. if file_format is not None and file_format != dump_data["format"]:
  145. load_output += "Warning: Dumpfile wallet format \"{}\" does not match command line specified format \"{}\".\n".format(dump_data["format"], file_format)
  146. self.assert_tool_output(load_output, *args)
  147. assert os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", wallet_name))
  148. 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')
  149. rt_dump_data = self.read_dump(rt_dumppath)
  150. wallet_dat = os.path.join(self.nodes[0].datadir, "regtest/wallets/", wallet_name, "wallet.dat")
  151. if rt_dump_data["format"] == "bdb":
  152. self.assert_is_bdb(wallet_dat)
  153. else:
  154. self.assert_is_sqlite(wallet_dat)
  155. def test_invalid_tool_commands_and_args(self):
  156. self.log.info('Testing that various invalid commands raise with specific error messages')
  157. self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'foo'", 'foo')
  158. # `bitcoin-wallet help` raises an error. Use `bitcoin-wallet -help`.
  159. self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'help'", 'help')
  160. self.assert_raises_tool_error('Error: Additional arguments provided (create). Methods do not take arguments. Please refer to `-help`.', 'info', 'create')
  161. self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo')
  162. self.assert_raises_tool_error('No method provided. Run `bitcoin-wallet -help` for valid methods.')
  163. self.assert_raises_tool_error('Wallet name must be provided when creating a new wallet.', 'create')
  164. locked_dir = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets")
  165. error = 'Error initializing wallet database environment "{}"!'.format(locked_dir)
  166. if self.options.descriptors:
  167. error = f"SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of {self.config['environment']['PACKAGE_NAME']}?"
  168. self.assert_raises_tool_error(
  169. error,
  170. '-wallet=' + self.default_wallet_name,
  171. 'info',
  172. )
  173. path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "nonexistent.dat")
  174. self.assert_raises_tool_error("Failed to load database path '{}'. Path does not exist.".format(path), '-wallet=nonexistent.dat', 'info')
  175. def test_tool_wallet_info(self):
  176. # Stop the node to close the wallet to call the info command.
  177. self.stop_node(0)
  178. self.log.info('Calling wallet tool info, testing output')
  179. #
  180. # TODO: Wallet tool info should work with wallet file permissions set to
  181. # read-only without raising:
  182. # "Error loading wallet.dat. Is wallet being used by another process?"
  183. # The following lines should be uncommented and the tests still succeed:
  184. #
  185. # self.log.debug('Setting wallet file permissions to 400 (read-only)')
  186. # os.chmod(self.wallet_path, stat.S_IRUSR)
  187. # assert self.wallet_permissions() in ['400', '666'] # Sanity check. 666 because Appveyor.
  188. # shasum_before = self.wallet_shasum()
  189. timestamp_before = self.wallet_timestamp()
  190. self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before))
  191. out = self.get_expected_info_output(address=1)
  192. self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info')
  193. timestamp_after = self.wallet_timestamp()
  194. self.log.debug('Wallet file timestamp after calling info: {}'.format(timestamp_after))
  195. self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
  196. self.log.debug('Setting wallet file permissions back to 600 (read/write)')
  197. os.chmod(self.wallet_path, stat.S_IRUSR | stat.S_IWUSR)
  198. assert self.wallet_permissions() in ['600', '666'] # Sanity check. 666 because Appveyor.
  199. #
  200. # TODO: Wallet tool info should not write to the wallet file.
  201. # The following lines should be uncommented and the tests still succeed:
  202. #
  203. # assert_equal(timestamp_before, timestamp_after)
  204. # shasum_after = self.wallet_shasum()
  205. # assert_equal(shasum_before, shasum_after)
  206. # self.log.debug('Wallet file shasum unchanged\n')
  207. def test_tool_wallet_info_after_transaction(self):
  208. """
  209. Mutate the wallet with a transaction to verify that the info command
  210. output changes accordingly.
  211. """
  212. self.start_node(0)
  213. self.log.info('Generating transaction to mutate wallet')
  214. self.generate(self.nodes[0], 1)
  215. self.stop_node(0)
  216. self.log.info('Calling wallet tool info after generating a transaction, testing output')
  217. shasum_before = self.wallet_shasum()
  218. timestamp_before = self.wallet_timestamp()
  219. self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before))
  220. out = self.get_expected_info_output(transactions=1, address=1)
  221. self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info')
  222. shasum_after = self.wallet_shasum()
  223. timestamp_after = self.wallet_timestamp()
  224. self.log.debug('Wallet file timestamp after calling info: {}'.format(timestamp_after))
  225. self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
  226. #
  227. # TODO: Wallet tool info should not write to the wallet file.
  228. # This assertion should be uncommented and succeed:
  229. # assert_equal(timestamp_before, timestamp_after)
  230. assert_equal(shasum_before, shasum_after)
  231. self.log.debug('Wallet file shasum unchanged\n')
  232. def test_tool_wallet_create_on_existing_wallet(self):
  233. self.log.info('Calling wallet tool create on an existing wallet, testing output')
  234. shasum_before = self.wallet_shasum()
  235. timestamp_before = self.wallet_timestamp()
  236. self.log.debug('Wallet file timestamp before calling create: {}'.format(timestamp_before))
  237. out = "Topping up keypool...\n" + self.get_expected_info_output(name="foo", keypool=2000)
  238. self.assert_tool_output(out, '-wallet=foo', 'create')
  239. shasum_after = self.wallet_shasum()
  240. timestamp_after = self.wallet_timestamp()
  241. self.log.debug('Wallet file timestamp after calling create: {}'.format(timestamp_after))
  242. self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
  243. assert_equal(timestamp_before, timestamp_after)
  244. assert_equal(shasum_before, shasum_after)
  245. self.log.debug('Wallet file shasum unchanged\n')
  246. def test_getwalletinfo_on_different_wallet(self):
  247. self.log.info('Starting node with arg -wallet=foo')
  248. self.start_node(0, ['-nowallet', '-wallet=foo'])
  249. self.log.info('Calling getwalletinfo on a different wallet ("foo"), testing output')
  250. shasum_before = self.wallet_shasum()
  251. timestamp_before = self.wallet_timestamp()
  252. self.log.debug('Wallet file timestamp before calling getwalletinfo: {}'.format(timestamp_before))
  253. out = self.nodes[0].getwalletinfo()
  254. self.stop_node(0)
  255. shasum_after = self.wallet_shasum()
  256. timestamp_after = self.wallet_timestamp()
  257. self.log.debug('Wallet file timestamp after calling getwalletinfo: {}'.format(timestamp_after))
  258. assert_equal(0, out['txcount'])
  259. if not self.options.descriptors:
  260. assert_equal(1000, out['keypoolsize'])
  261. assert_equal(1000, out['keypoolsize_hd_internal'])
  262. assert_equal(True, 'hdseedid' in out)
  263. else:
  264. assert_equal(4000, out['keypoolsize'])
  265. assert_equal(4000, out['keypoolsize_hd_internal'])
  266. self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
  267. assert_equal(timestamp_before, timestamp_after)
  268. assert_equal(shasum_after, shasum_before)
  269. self.log.debug('Wallet file shasum unchanged\n')
  270. def test_salvage(self):
  271. # TODO: Check salvage actually salvages and doesn't break things. https://github.com/bitcoin/bitcoin/issues/7463
  272. self.log.info('Check salvage')
  273. self.start_node(0)
  274. self.nodes[0].createwallet("salvage")
  275. self.stop_node(0)
  276. self.assert_tool_output('', '-wallet=salvage', 'salvage')
  277. def test_dump_createfromdump(self):
  278. self.start_node(0)
  279. self.nodes[0].createwallet("todump")
  280. file_format = self.nodes[0].get_wallet_rpc("todump").getwalletinfo()["format"]
  281. self.nodes[0].createwallet("todump2")
  282. self.stop_node(0)
  283. self.log.info('Checking dump arguments')
  284. self.assert_raises_tool_error('No dump file provided. To use dump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'dump')
  285. self.log.info('Checking basic dump')
  286. wallet_dump = os.path.join(self.nodes[0].datadir, "wallet.dump")
  287. 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')
  288. dump_data = self.read_dump(wallet_dump)
  289. orig_dump = dump_data.copy()
  290. # Check the dump magic
  291. assert_equal(dump_data['BITCOIN_CORE_WALLET_DUMP'], '1')
  292. # Check the file format
  293. assert_equal(dump_data["format"], file_format)
  294. self.log.info('Checking that a dumpfile cannot be overwritten')
  295. 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')
  296. self.log.info('Checking createfromdump arguments')
  297. self.assert_raises_tool_error('No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'createfromdump')
  298. non_exist_dump = os.path.join(self.nodes[0].datadir, "wallet.nodump")
  299. self.assert_raises_tool_error('Unknown wallet file format "notaformat" provided. Please provide one of "bdb" or "sqlite".', '-wallet=todump', '-format=notaformat', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
  300. self.assert_raises_tool_error('Dump file {} does not exist.'.format(non_exist_dump), '-wallet=todump', '-dumpfile={}'.format(non_exist_dump), 'createfromdump')
  301. wallet_path = os.path.join(self.nodes[0].datadir, 'regtest', 'wallets', 'todump2')
  302. self.assert_raises_tool_error('Failed to create database path \'{}\'. Database already exists.'.format(wallet_path), '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
  303. self.assert_raises_tool_error("The -descriptors option can only be used with the 'create' command.", '-descriptors', '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
  304. self.log.info('Checking createfromdump')
  305. self.do_tool_createfromdump("load", "wallet.dump")
  306. if self.is_bdb_compiled():
  307. self.do_tool_createfromdump("load-bdb", "wallet.dump", "bdb")
  308. if self.is_sqlite_compiled():
  309. self.do_tool_createfromdump("load-sqlite", "wallet.dump", "sqlite")
  310. self.log.info('Checking createfromdump handling of magic and versions')
  311. bad_ver_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_ver1.dump")
  312. dump_data["BITCOIN_CORE_WALLET_DUMP"] = "0"
  313. self.write_dump(dump_data, bad_ver_wallet_dump)
  314. 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')
  315. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  316. bad_ver_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_ver2.dump")
  317. dump_data["BITCOIN_CORE_WALLET_DUMP"] = "2"
  318. self.write_dump(dump_data, bad_ver_wallet_dump)
  319. 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')
  320. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  321. bad_magic_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_magic.dump")
  322. del dump_data["BITCOIN_CORE_WALLET_DUMP"]
  323. dump_data["not_the_right_magic"] = "1"
  324. self.write_dump(dump_data, bad_magic_wallet_dump, "not_the_right_magic")
  325. 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')
  326. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  327. self.log.info('Checking createfromdump handling of checksums')
  328. bad_sum_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_sum1.dump")
  329. dump_data = orig_dump.copy()
  330. checksum = dump_data["checksum"]
  331. dump_data["checksum"] = "1" * 64
  332. self.write_dump(dump_data, bad_sum_wallet_dump)
  333. 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')
  334. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  335. bad_sum_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_sum2.dump")
  336. del dump_data["checksum"]
  337. self.write_dump(dump_data, bad_sum_wallet_dump, skip_checksum=True)
  338. self.assert_raises_tool_error('Error: Missing checksum', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
  339. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  340. bad_sum_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_sum3.dump")
  341. dump_data["checksum"] = "2" * 10
  342. self.write_dump(dump_data, bad_sum_wallet_dump)
  343. self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
  344. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  345. dump_data["checksum"] = "3" * 66
  346. self.write_dump(dump_data, bad_sum_wallet_dump)
  347. self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
  348. assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
  349. def run_test(self):
  350. self.wallet_path = os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename)
  351. self.test_invalid_tool_commands_and_args()
  352. # Warning: The following tests are order-dependent.
  353. self.test_tool_wallet_info()
  354. self.test_tool_wallet_info_after_transaction()
  355. self.test_tool_wallet_create_on_existing_wallet()
  356. self.test_getwalletinfo_on_different_wallet()
  357. if not self.options.descriptors:
  358. # Salvage is a legacy wallet only thing
  359. self.test_salvage()
  360. self.test_dump_createfromdump()
  361. if __name__ == '__main__':
  362. ToolWalletTest().main()