ts_wallet.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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.test_py_d.ts_wallet: Wallet conversion tests for the test.py test suite
  20. """
  21. import os
  22. from mmgen.util import msg,capfirst,get_extension
  23. from mmgen.wallet import get_wallet_cls
  24. from ..include.common import cfg,joinpath,imsg
  25. from .common import ref_dir,ref_wallet_brainpass,ref_wallet_incog_offset,hincog_fn,hincog_bytes
  26. from .ts_base import TestSuiteBase
  27. from .ts_shared import TestSuiteShared
  28. class TestSuiteWalletConv(TestSuiteBase,TestSuiteShared):
  29. 'wallet conversion to and from reference data'
  30. networks = ('btc',)
  31. tmpdir_nums = [11,12,13]
  32. sources = { '128': {
  33. 'ref_wallet': 'FE3C6545-D782B529[128,1].mmdat',
  34. 'ic_wallet': 'FE3C6545-E29303EA-5E229E30[128,1].mmincog',
  35. 'ic_wallet_hex': 'FE3C6545-BC4BE3F2-32586837[128,1].mmincox',
  36. 'hic_wallet': 'FE3C6545-161E495F-BEB7548E[128,1].incog-offset123',
  37. 'hic_wallet_old': 'FE3C6545-161E495F-9860A85B[128,1].incog-old.offset123',
  38. },
  39. '192': {
  40. 'ref_wallet': '1378FC64-6F0F9BB4[192,1].mmdat',
  41. 'ic_wallet': '1378FC64-2907DE97-F980D21F[192,1].mmincog',
  42. 'ic_wallet_hex': '1378FC64-4DCB5174-872806A7[192,1].mmincox',
  43. 'hic_wallet': '1378FC64-B55E9958-77256FC1[192,1].incog.offset123',
  44. 'hic_wallet_old': '1378FC64-B55E9958-D85FF20C[192,1].incog-old.offset123',
  45. },
  46. '256': {
  47. 'ref_wallet': '98831F3A-27F2BF93[256,1].mmdat',
  48. 'ic_wallet': '98831F3A-5482381C-18460FB1[256,1].mmincog',
  49. 'ic_wallet_hex': '98831F3A-1630A9F2-870376A9[256,1].mmincox',
  50. 'hic_wallet': '98831F3A-F59B07A0-559CEF19[256,1].incog.offset123',
  51. 'hic_wallet_old': '98831F3A-F59B07A0-848535F3[256,1].incog-old.offset123',
  52. },
  53. }
  54. cmd_group = (
  55. # reading
  56. ('ref_wallet_conv', 'conversion of saved reference wallet'),
  57. ('ref_mn_conv', 'conversion of saved MMGen native mnemonic'),
  58. ('ref_bip39_conv', 'conversion of saved BIP39 mnemonic'),
  59. ('ref_seed_conv', 'conversion of saved seed file'),
  60. ('ref_hex_conv', 'conversion of saved MMGen hexadecimal seed file'),
  61. ('ref_plainhex_conv', 'conversion of saved plain hexadecimal seed file'),
  62. ('ref_dieroll_conv', 'conversion of saved dieroll (b6d) seed file'),
  63. ('ref_brain_conv', 'conversion of ref brainwallet'),
  64. ('ref_incog_conv', 'conversion of saved incog wallet'),
  65. ('ref_incox_conv', 'conversion of saved hex incog wallet'),
  66. ('ref_hincog_conv', 'conversion of saved hidden incog wallet'),
  67. ('ref_hincog_conv_old','conversion of saved hidden incog wallet (old format)'),
  68. # writing
  69. ('ref_wallet_conv_out', 'ref seed conversion to wallet'),
  70. ('ref_mn_conv_out', 'ref seed conversion to MMGen native mnemonic'),
  71. ('ref_bip39_conv_out', 'ref seed conversion to BIP39 mnemonic'),
  72. ('ref_hex_conv_out', 'ref seed conversion to MMGen hex seed'),
  73. ('ref_plainhex_conv_out','ref seed conversion to plain hex seed'),
  74. ('ref_dieroll_conv_out','ref seed conversion to dieroll (b6d) seed'),
  75. ('ref_seed_conv_out', 'ref seed conversion to seed'),
  76. ('ref_incog_conv_out', 'ref seed conversion to incog data'),
  77. ('ref_incox_conv_out', 'ref seed conversion to hex incog data'),
  78. ('ref_hincog_conv_out', 'ref seed conversion to hidden incog data'),
  79. ('ref_hincog_blkdev_conv_out', 'ref seed conversion to hidden incog data on block device')
  80. )
  81. def __init__(self,trunner,cfgs,spawn):
  82. for k,j in self.cmd_group:
  83. for n in (1,2,3):
  84. setattr(self,f'{k}_{n}',getattr(self,k))
  85. return TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  86. def ref_wallet_conv(self):
  87. wf = joinpath(ref_dir,self.sources[str(self.seed_len)]['ref_wallet'])
  88. return self.walletconv_in(wf,oo=True)
  89. def ref_mn_conv(self,ext='mmwords'):
  90. wf = joinpath(ref_dir,self.seed_id+'.'+ext)
  91. return self.walletconv_in(wf,oo=True)
  92. def ref_bip39_conv(self): return self.ref_mn_conv(ext='bip39')
  93. def ref_seed_conv(self): return self.ref_mn_conv(ext='mmseed')
  94. def ref_hex_conv(self): return self.ref_mn_conv(ext='mmhex')
  95. def ref_plainhex_conv(self): return self.ref_mn_conv(ext='hex')
  96. def ref_dieroll_conv(self): return self.ref_mn_conv(ext='b6d')
  97. def ref_brain_conv(self):
  98. uopts = ['-i','bw','-p','1','-l',str(self.seed_len)]
  99. return self.walletconv_in(None,uopts,oo=True,icls=get_wallet_cls('brain'))
  100. def ref_incog_conv(self,wfk='ic_wallet',in_fmt='i'):
  101. uopts = ['-i',in_fmt,'-p','1','-l',str(self.seed_len)]
  102. wf = joinpath(ref_dir,self.sources[str(self.seed_len)][wfk])
  103. return self.walletconv_in(wf,uopts,oo=True)
  104. def ref_incox_conv(self):
  105. return self.ref_incog_conv(in_fmt='xi',wfk='ic_wallet_hex')
  106. def ref_hincog_conv(self,wfk='hic_wallet',add_uopts=[]):
  107. ic_f = joinpath(ref_dir,self.sources[str(self.seed_len)][wfk])
  108. uopts = ['-i','hi','-p','1','-l',str(self.seed_len)] + add_uopts
  109. hi_opt = ['-H',f'{ic_f},{ref_wallet_incog_offset}']
  110. return self.walletconv_in(
  111. None,
  112. uopts + hi_opt,
  113. oo = True,
  114. icls = get_wallet_cls('incog_hidden') )
  115. def ref_hincog_conv_old(self):
  116. return self.ref_hincog_conv(wfk='hic_wallet_old',add_uopts=['-O'])
  117. def ref_wallet_conv_out(self): return self.walletconv_out('w')
  118. def ref_mn_conv_out(self): return self.walletconv_out('mn')
  119. def ref_bip39_conv_out(self): return self.walletconv_out('bip39')
  120. def ref_seed_conv_out(self): return self.walletconv_out('seed')
  121. def ref_hex_conv_out(self): return self.walletconv_out('hexseed')
  122. def ref_plainhex_conv_out(self): return self.walletconv_out('hex')
  123. def ref_dieroll_conv_out(self): return self.walletconv_out('dieroll')
  124. def ref_incog_conv_out(self): return self.walletconv_out('i')
  125. def ref_incox_conv_out(self): return self.walletconv_out('xi')
  126. def ref_hincog_conv_out(self,ic_f=None):
  127. if not ic_f:
  128. ic_f = joinpath(self.tmpdir,hincog_fn)
  129. hi_parms = f'{ic_f},{ref_wallet_incog_offset}'
  130. sl_parm = '-l' + str(self.seed_len)
  131. return self.walletconv_out('hi',
  132. uopts = ['-J',hi_parms,sl_parm],
  133. uopts_chk = ['-H',hi_parms,sl_parm] )
  134. def ref_hincog_blkdev_conv_out(self):
  135. def do_run(cmd):
  136. from subprocess import run,PIPE,DEVNULL
  137. return run(cmd,stdout=PIPE,stderr=DEVNULL,check=True)
  138. if self.skip_for_win():
  139. return 'skip'
  140. imsg('Creating block device image file')
  141. ic_img = joinpath(self.tmpdir,'hincog_blkdev_img')
  142. do_run(['dd','if=/dev/zero','of='+ic_img,'bs=1K','count=1'])
  143. ic_dev = do_run(['sudo','/sbin/losetup','-f']).stdout.strip().decode()
  144. ic_dev_mode_orig = '{:o}'.format( os.stat(ic_dev).st_mode & 0xfff )
  145. ic_dev_mode = '0666'
  146. imsg(f'Changing permissions on loop device to {ic_dev_mode!r}')
  147. do_run(['sudo','chmod',ic_dev_mode,ic_dev])
  148. imsg(f'Attaching loop device {ic_dev!r}')
  149. do_run(['sudo','/sbin/losetup',ic_dev,ic_img])
  150. self.ref_hincog_conv_out(ic_f=ic_dev)
  151. imsg(f'Detaching loop device {ic_dev!r}')
  152. do_run(['sudo','/sbin/losetup','-d',ic_dev])
  153. imsg(f'Resetting permissions on loop device to {ic_dev_mode_orig!r}')
  154. do_run(['sudo','chmod',ic_dev_mode_orig,ic_dev])
  155. return 'ok'
  156. # wallet conversion tests
  157. def walletconv_in(self,infile,uopts=[],oo=False,icls=None):
  158. ocls = get_wallet_cls('words')
  159. opts = ['-d',self.tmpdir,'-o',ocls.fmt_codes[0],self.usr_rand_arg]
  160. if_arg = [infile] if infile else []
  161. d = '(convert)'
  162. t = self.spawn('mmgen-walletconv',opts+uopts+if_arg,extra_desc=d)
  163. t.license()
  164. icls = icls or get_wallet_cls(ext=get_extension(infile))
  165. if icls.type == 'brain':
  166. t.expect('Enter brainwallet: ',ref_wallet_brainpass+'\n')
  167. if icls.enc and icls.type != 'brain':
  168. t.passphrase(icls.desc,self.wpasswd)
  169. if self.test_name[:19] == 'ref_hincog_conv_old':
  170. t.expect('Is the Seed ID correct? (Y/n): ','\n')
  171. else:
  172. t.expect(['Passphrase is OK',' are correct'])
  173. wf = t.written_to_file(capfirst(ocls.desc),oo=oo)
  174. t.p.wait()
  175. # back check of result
  176. msg('' if cfg.profile else ' OK')
  177. return self.walletchk( wf,
  178. pf = None,
  179. extra_desc = '(check)',
  180. sid = self.seed_id )
  181. def walletconv_out(self,out_fmt='w',uopts=[],uopts_chk=[]):
  182. wcls = get_wallet_cls(fmt_code=out_fmt)
  183. opts = ['-d',self.tmpdir,'-p1','-o',out_fmt] + uopts
  184. infile = joinpath(ref_dir,self.seed_id+'.mmwords')
  185. t = self.spawn('mmgen-walletconv',[self.usr_rand_arg]+opts+[infile],extra_desc='(convert)')
  186. add_args = [f'-l{self.seed_len}']
  187. t.license()
  188. if wcls.enc and wcls.type != 'brain':
  189. t.passphrase_new('new '+wcls.desc,self.wpasswd)
  190. t.usr_rand(self.usr_rand_chars)
  191. if wcls.type.startswith('incog'):
  192. for i in (1,2,3):
  193. t.expect('Encrypting random data from your operating system with ephemeral key')
  194. if wcls.type == 'incog_hidden':
  195. t.hincog_create(hincog_bytes)
  196. if out_fmt == 'w':
  197. t.label()
  198. wf = t.written_to_file(capfirst(wcls.desc),oo=True)
  199. pf = None
  200. if wcls.type == 'incog_hidden':
  201. add_args += uopts_chk
  202. wf = None
  203. msg('' if cfg.profile else ' OK')
  204. return self.walletchk( wf,
  205. pf = pf,
  206. wcls = wcls,
  207. extra_desc = '(check)',
  208. sid = self.seed_id,
  209. add_args = add_args )