ts_ref.py 13 KB

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