ref.py 14 KB

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