ct_ref.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test.cmdtest_d.ct_ref: Reference file tests for the cmdtest.py test suite
  20. """
  21. import os
  22. from mmgen.util import capfirst
  23. from mmgen.wallet import get_wallet_cls
  24. from ..include.common import (
  25. cfg,
  26. imsg_r,
  27. ok,
  28. joinpath,
  29. cmp_or_die,
  30. ref_kafile_pass,
  31. read_from_file,
  32. sample_text
  33. )
  34. from .common import (
  35. dfl_words_file,
  36. ref_dir,
  37. chksum_pat,
  38. pwfile,
  39. ref_bw_file_spc,
  40. ref_enc_fn,
  41. get_env_without_debug_vars,
  42. tool_enc_passwd,
  43. skip
  44. )
  45. from .ct_base import CmdTestBase
  46. from .ct_shared import CmdTestShared
  47. wpasswd = 'reference password'
  48. class CmdTestRef(CmdTestBase, CmdTestShared):
  49. 'saved reference address, password and transaction files'
  50. tmpdir_nums = [8]
  51. networks = ('btc', 'btc_tn', 'ltc', 'ltc_tn')
  52. passthru_opts = ('daemon_data_dir', 'rpc_port', 'coin', 'testnet')
  53. need_daemon = True
  54. sources = {
  55. 'ref_addrfile': '98831F3A{}[1,31-33,500-501,1010-1011]{}.addrs',
  56. 'ref_segwitaddrfile':'98831F3A{}-S[1,31-33,500-501,1010-1011]{}.addrs',
  57. 'ref_bech32addrfile':'98831F3A{}-B[1,31-33,500-501,1010-1011]{}.addrs',
  58. 'ref_keyaddrfile': '98831F3A{}[1,31-33,500-501,1010-1011]{}.akeys.mmenc',
  59. 'ref_viewkeyaddrfile': '98831F3A-XMR-M[1-3].vkeys',
  60. 'ref_passwdfile_b32_24': '98831F3A-фубар@crypto.org-b32-24[1,4,1100].pws',
  61. 'ref_passwdfile_b32_12': '98831F3A-фубар@crypto.org-b32-12[1,4,1100].pws',
  62. 'ref_passwdfile_b58_10': '98831F3A-фубар@crypto.org-b58-10[1,4,1100].pws',
  63. 'ref_passwdfile_b58_20': '98831F3A-фубар@crypto.org-b58-20[1,4,1100].pws',
  64. 'ref_passwdfile_hex_32': '98831F3A-фубар@crypto.org-hex-32[1,4,1100].pws',
  65. 'ref_passwdfile_hex_48': '98831F3A-фубар@crypto.org-hex-48[1,4,1100].pws',
  66. 'ref_passwdfile_hex_64': '98831F3A-фубар@crypto.org-hex-64[1,4,1100].pws',
  67. 'ref_passwdfile_bip39_12': '98831F3A-фубар@crypto.org-bip39-12[1,4,1100].pws',
  68. 'ref_passwdfile_bip39_18': '98831F3A-фубар@crypto.org-bip39-18[1,4,1100].pws',
  69. 'ref_passwdfile_bip39_24': '98831F3A-фубар@crypto.org-bip39-24[1,4,1100].pws',
  70. 'ref_passwdfile_xmrseed_25': '98831F3A-фубар@crypto.org-xmrseed-25[1,4,1100].pws',
  71. 'ref_passwdfile_hex2bip39_12': '98831F3A-фубар@crypto.org-hex2bip39-12[1,4,1100].pws',
  72. 'ref_tx_file': { # data shared with ref_altcoin, autosign
  73. 'btc': (
  74. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  75. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx'
  76. ),
  77. 'ltc': (
  78. 'AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  79. 'A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx'
  80. ),
  81. 'bch': (
  82. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  83. '359FD5-BCH[6.68868,tl=1320969600].testnet.rawtx'
  84. ),
  85. 'eth': (
  86. '88FEFD-ETH[23.45495,40000].rawtx',
  87. 'B472BD-ETH[23.45495,40000].testnet.rawtx'
  88. ),
  89. 'mm1': (
  90. '5881D2-MM1[1.23456,50000].rawtx',
  91. '6BDB25-MM1[1.23456,50000].testnet.rawtx'
  92. ),
  93. 'etc': (
  94. 'ED3848-ETC[1.2345,40000].rawtx',
  95. ''
  96. )
  97. },
  98. }
  99. chk_data = {
  100. 'ref_subwallet_sid': {
  101. '98831F3A:32L':'D66B4885',
  102. '98831F3A:1S':'20D95B09',
  103. },
  104. 'ref_addrfile_chksum': {
  105. 'btc': ('6FEF 6FB9 7B13 5D91', '424E 4326 CFFE 5F51'),
  106. 'ltc': ('AD52 C3FE 8924 AAF0', '4EBE 2E85 E969 1B30'),
  107. },
  108. 'ref_segwitaddrfile_chksum': {
  109. 'btc': ('06C1 9C87 F25C 4EE6', '072C 8B07 2730 CB7A'),
  110. 'ltc': ('63DF E42A 0827 21C3', '5DD1 D186 DBE1 59F2'),
  111. },
  112. 'ref_bech32addrfile_chksum': {
  113. 'btc': ('9D2A D4B6 5117 F02E', '0527 9C39 6C1B E39A'),
  114. 'ltc': ('FF1C 7939 5967 AB82', 'ED3D 8AA4 BED4 0B40'),
  115. },
  116. 'ref_keyaddrfile_chksum': {
  117. 'btc': ('9F2D D781 1812 8BAD', '88CC 5120 9A91 22C2'),
  118. 'ltc': ('B804 978A 8796 3ED4', '98B5 AC35 F334 0398'),
  119. },
  120. 'ref_passwdfile_b32_12_chksum': '7252 CD8D EF0D 3DB1',
  121. 'ref_passwdfile_b32_24_chksum': '8D56 3845 A072 A5B9',
  122. 'ref_passwdfile_b58_10_chksum': '534F CC1A 6701 9FED',
  123. 'ref_passwdfile_b58_20_chksum': 'DDD9 44B0 CA28 183F',
  124. 'ref_passwdfile_hex_32_chksum': '05C7 3678 E25E BC32',
  125. 'ref_passwdfile_hex_48_chksum': '7DBB FFD0 633E DE6F',
  126. 'ref_passwdfile_hex_64_chksum': 'F11D CB0A 8AE3 4D21',
  127. 'ref_passwdfile_bip39_12_chksum': 'BF57 02A3 5229 CF18',
  128. 'ref_passwdfile_bip39_18_chksum': '31D3 1656 B7DC 27CF',
  129. 'ref_passwdfile_bip39_24_chksum': 'E565 3A59 7D91 4671',
  130. 'ref_passwdfile_xmrseed_25_chksum': 'B488 21D3 4539 968D',
  131. 'ref_passwdfile_hex2bip39_12_chksum': '93AD 4AE2 03D1 8A0A',
  132. }
  133. cmd_group = ( # TODO: move to tooltest2
  134. ('ref_words_to_subwallet_chk1', 'subwallet generation from reference words file (long subseed)'),
  135. ('ref_words_to_subwallet_chk2', 'subwallet generation from reference words file (short subseed)'),
  136. ('ref_subwallet_addrgen1', 'subwallet address file generation (long subseed)'),
  137. ('ref_subwallet_addrgen2', 'subwallet address file generation (short subseed)'),
  138. ('ref_subwallet_keygen1', 'subwallet key-address file generation (long subseed)'),
  139. ('ref_subwallet_keygen2', 'subwallet key-address file generation (short subseed)'),
  140. ('ref_addrfile_chk', 'saved reference address file'),
  141. ('ref_segwitaddrfile_chk', 'saved reference address file (segwit)'),
  142. ('ref_bech32addrfile_chk', 'saved reference address file (bech32)'),
  143. ('ref_keyaddrfile_chk', 'saved reference key-address file'),
  144. ('ref_passwdfile_chk_b58_20', 'saved reference password file (base58, 20 chars)'),
  145. ('ref_passwdfile_chk_b58_10', 'saved reference password file (base58, 10 chars)'),
  146. ('ref_passwdfile_chk_b32_24', 'saved reference password file (base32, 24 chars)'),
  147. ('ref_passwdfile_chk_b32_12', 'saved reference password file (base32, 12 chars)'),
  148. ('ref_passwdfile_chk_hex_32', 'saved reference password file (hexadecimal, 32 chars)'),
  149. ('ref_passwdfile_chk_hex_48', 'saved reference password file (hexadecimal, 48 chars)'),
  150. ('ref_passwdfile_chk_hex_64', 'saved reference password file (hexadecimal, 64 chars)'),
  151. ('ref_passwdfile_chk_bip39_12', 'saved reference password file (BIP39, 12 words)'),
  152. ('ref_passwdfile_chk_bip39_18', 'saved reference password file (BIP39, 18 words)'),
  153. ('ref_passwdfile_chk_bip39_24', 'saved reference password file (BIP39, 24 words)'),
  154. ('ref_passwdfile_chk_xmrseed_25', 'saved reference password file (Monero new-style mnemonic, 25 words)'),
  155. ('ref_passwdfile_chk_hex2bip39_12', 'saved reference password file (hex-to-BIP39, 12 words)'),
  156. # Create the fake inputs:
  157. # ('txcreate8', 'transaction creation (8)'),
  158. ('ref_tx_chk', 'signing saved reference tx file'),
  159. ('ref_brain_chk_spc3', 'saved brainwallet (non-standard spacing)'),
  160. ('ref_dieroll_chk_seedtruncate', 'saved dieroll wallet with extra entropy bits'),
  161. ('ref_tool_decrypt', 'decryption of saved MMGen-encrypted file'),
  162. )
  163. @property
  164. def nw_desc(self):
  165. return '{} {}'.format(
  166. self.proto.coin,
  167. ('Mainnet','Testnet')[self.proto.testnet])
  168. def _get_ref_subdir_by_coin(self,coin):
  169. return {
  170. 'btc': '',
  171. 'bch': '',
  172. 'ltc': 'litecoin',
  173. 'eth': 'ethereum',
  174. 'etc': 'ethereum_classic',
  175. 'xmr': 'monero',
  176. 'zec': 'zcash',
  177. 'dash': 'dash'
  178. }[coin.lower()]
  179. @property
  180. def ref_subdir(self):
  181. return self._get_ref_subdir_by_coin(self.proto.coin)
  182. def ref_words_to_subwallet_chk1(self):
  183. return self.ref_words_to_subwallet_chk('32L')
  184. def ref_words_to_subwallet_chk2(self):
  185. return self.ref_words_to_subwallet_chk('1S')
  186. def ref_words_to_subwallet_chk(self, ss_idx):
  187. wf = dfl_words_file
  188. ocls = get_wallet_cls('words')
  189. args = ['-d', self.tr.trash_dir, '-o', ocls.fmt_codes[-1], wf, ss_idx]
  190. t = self.spawn(
  191. 'mmgen-subwalletgen',
  192. self.testnet_opt + args,
  193. extra_desc = '(generate subwallet)',
  194. no_passthru_opts = True)
  195. t.expect(f'Generating subseed {ss_idx}')
  196. chk_sid = self.chk_data['ref_subwallet_sid'][f'98831F3A:{ss_idx}']
  197. fn = t.written_to_file(capfirst(ocls.desc))
  198. assert chk_sid in fn, f'incorrect filename: {fn} (does not contain {chk_sid})'
  199. ok()
  200. t = self.spawn(
  201. 'mmgen-walletchk',
  202. self.testnet_opt + [fn],
  203. extra_desc = '(check subwallet)',
  204. no_passthru_opts = True)
  205. t.expect(r'Valid MMGen native mnemonic data for Seed ID ([0-9A-F]*)\b', regex=True)
  206. sid = t.p.match.group(1)
  207. assert sid == chk_sid, f'subseed ID {sid} does not match expected value {chk_sid}'
  208. return t
  209. def ref_subwallet_addrgen(self, ss_idx, target='addr'):
  210. wf = dfl_words_file
  211. args = ['-d', self.tr.trash_dir, '--subwallet='+ss_idx, wf, '1-10']
  212. t = self.spawn(f'mmgen-{target}gen', args)
  213. t.expect(f'Generating subseed {ss_idx}')
  214. chk_sid = self.chk_data['ref_subwallet_sid'][f'98831F3A:{ss_idx}']
  215. assert chk_sid == t.expect_getend('Checksum for .* data ', regex=True)[:8]
  216. if target == 'key':
  217. t.expect('Encrypt key list? (y/N): ', 'n')
  218. fn = t.written_to_file(('Addresses', 'Secret keys')[target=='key'])
  219. assert chk_sid in fn, f'incorrect filename: {fn} (does not contain {chk_sid})'
  220. return t
  221. def ref_subwallet_addrgen1(self):
  222. return self.ref_subwallet_addrgen('32L')
  223. def ref_subwallet_addrgen2(self):
  224. return self.ref_subwallet_addrgen('1S')
  225. def ref_subwallet_keygen1(self):
  226. return self.ref_subwallet_addrgen('32L', target='key')
  227. def ref_subwallet_keygen2(self):
  228. return self.ref_subwallet_addrgen('1S', target='key')
  229. def ref_addrfile_chk(
  230. self,
  231. ftype = 'addr',
  232. coin = None,
  233. subdir = None,
  234. pfx = None,
  235. mmtype = None,
  236. id_key = None,
  237. pat = None):
  238. pat = pat or f'{self.nw_desc}.*Legacy'
  239. af_key = f'ref_{ftype}file' + ('_' + id_key if id_key else '')
  240. af_fn = CmdTestRef.sources[af_key].format(pfx or self.altcoin_pfx, '' if coin else self.tn_ext)
  241. af = joinpath(ref_dir, (subdir or self.ref_subdir, '')[ftype=='passwd'], af_fn)
  242. coin_arg = [] if coin is None else ['--coin='+coin]
  243. tool_cmd = ftype.replace('segwit', '').replace('bech32', '')+'file_chksum'
  244. t = self.spawn('mmgen-tool', coin_arg + ['--verbose', '-p1', tool_cmd, af])
  245. if ftype == 'keyaddr':
  246. t.do_decrypt_ka_data(pw=ref_kafile_pass, have_yes_opt=True)
  247. chksum_key = '_'.join([af_key, 'chksum'] + ([coin.lower()] if coin else []) + ([mmtype] if mmtype else []))
  248. rc = self.chk_data[chksum_key]
  249. ref_chksum = rc if (ftype == 'passwd' or coin) else rc[self.proto.base_coin.lower()][self.proto.testnet]
  250. if pat:
  251. t.expect(pat, regex=True)
  252. t.expect(chksum_pat, regex=True)
  253. m = t.p.match.group(0)
  254. cmp_or_die(ref_chksum, m)
  255. return t
  256. def ref_segwitaddrfile_chk(self):
  257. if not 'S' in self.proto.mmtypes:
  258. return skip(f'not supported by {self.proto.cls_name} protocol')
  259. return self.ref_addrfile_chk(ftype='segwitaddr', pat=f'{self.nw_desc}.*Segwit')
  260. def ref_bech32addrfile_chk(self):
  261. if not 'B' in self.proto.mmtypes:
  262. return skip(f'not supported by {self.proto.cls_name} protocol')
  263. return self.ref_addrfile_chk(ftype='bech32addr', pat=f'{self.nw_desc}.*Bech32')
  264. def ref_keyaddrfile_chk(self):
  265. return self.ref_addrfile_chk(ftype='keyaddr')
  266. def ref_passwdfile_chk(self, key, pat):
  267. return self.ref_addrfile_chk(ftype='passwd', id_key=key, pat=pat)
  268. def ref_passwdfile_chk_b58_20(self):
  269. return self.ref_passwdfile_chk(key='b58_20', pat=r'Base58.*len.* 20\b')
  270. def ref_passwdfile_chk_b58_10(self):
  271. return self.ref_passwdfile_chk(key='b58_10', pat=r'Base58.*len.* 10\b')
  272. def ref_passwdfile_chk_b32_24(self):
  273. return self.ref_passwdfile_chk(key='b32_24', pat=r'Base32.*len.* 24\b')
  274. def ref_passwdfile_chk_b32_12(self):
  275. return self.ref_passwdfile_chk(key='b32_12', pat=r'Base32.*len.* 12\b')
  276. def ref_passwdfile_chk_hex_32(self):
  277. return self.ref_passwdfile_chk(key='hex_32', pat=r'Hexadec.*len.* 32\b')
  278. def ref_passwdfile_chk_hex_48(self):
  279. return self.ref_passwdfile_chk(key='hex_48', pat=r'Hexadec.*len.* 48\b')
  280. def ref_passwdfile_chk_hex_64(self):
  281. return self.ref_passwdfile_chk(key='hex_64', pat=r'Hexadec.*len.* 64\b')
  282. def ref_passwdfile_chk_bip39_12(self):
  283. return self.ref_passwdfile_chk(key='bip39_12', pat=r'BIP39.*len.* 12\b')
  284. def ref_passwdfile_chk_bip39_18(self):
  285. return self.ref_passwdfile_chk(key='bip39_18', pat=r'BIP39.*len.* 18\b')
  286. def ref_passwdfile_chk_bip39_24(self):
  287. return self.ref_passwdfile_chk(key='bip39_24', pat=r'BIP39.*len.* 24\b')
  288. def ref_passwdfile_chk_xmrseed_25(self):
  289. return self.ref_passwdfile_chk(key='xmrseed_25', pat=r'Mon.*len.* 25\b')
  290. def ref_passwdfile_chk_hex2bip39_12(self):
  291. return self.ref_passwdfile_chk(key='hex2bip39_12', pat=r'BIP39.*len.* 12\b')
  292. def ref_tx_chk(self):
  293. fn = self.sources['ref_tx_file'][self.coin][bool(self.tn_ext)]
  294. if not fn:
  295. return
  296. tf = joinpath(ref_dir, self.ref_subdir, fn)
  297. wf = dfl_words_file
  298. self.write_to_tmpfile(pwfile, wpasswd)
  299. return self.txsign(wf, tf, save=False, has_label=True, view='y')
  300. def ref_brain_chk_spc3(self):
  301. return self.ref_brain_chk(bw_file=ref_bw_file_spc)
  302. def ref_dieroll_chk_seedtruncate(self):
  303. wf = joinpath(ref_dir, 'overflow128.b6d')
  304. return self.walletchk(wf, sid='8EC6D4A2')
  305. def ref_tool_decrypt(self):
  306. f = joinpath(ref_dir, ref_enc_fn)
  307. dec_file = joinpath(self.tmpdir, 'famous.txt')
  308. t = self.spawn(
  309. 'mmgen-tool',
  310. ['-q', 'decrypt', f, 'outfile='+dec_file, 'hash_preset=1'],
  311. env = os.environ if cfg.debug_utf8 else get_env_without_debug_vars())
  312. t.passphrase('data', tool_enc_passwd)
  313. t.written_to_file('Decrypted data')
  314. dec_txt = read_from_file(dec_file)
  315. imsg_r(dec_txt)
  316. cmp_or_die(sample_text+'\n', dec_txt) # file adds a newline to sample_text
  317. return t