ct_wallet.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  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_py_d.ct_wallet: Wallet conversion tests for the cmdtest.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 .ct_base import CmdTestBase
  27. from .ct_shared import CmdTestShared
  28. class CmdTestWalletConv(CmdTestBase,CmdTestShared):
  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,_ in self.cmd_group:
  83. for n in (1,2,3):
  84. setattr(self,f'{k}_{n}',getattr(self,k))
  85. CmdTestBase.__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)
  89. def ref_mn_conv(self,ext='mmwords'):
  90. wf = joinpath(ref_dir,self.seed_id+'.'+ext)
  91. return self.walletconv_in(wf)
  92. def ref_bip39_conv(self):
  93. return self.ref_mn_conv(ext='bip39')
  94. def ref_seed_conv(self):
  95. return self.ref_mn_conv(ext='mmseed')
  96. def ref_hex_conv(self):
  97. return self.ref_mn_conv(ext='mmhex')
  98. def ref_plainhex_conv(self):
  99. return self.ref_mn_conv(ext='hex')
  100. def ref_dieroll_conv(self):
  101. return self.ref_mn_conv(ext='b6d')
  102. def ref_brain_conv(self):
  103. uopts = ['-i','bw','-p','1','-l',str(self.seed_len)]
  104. return self.walletconv_in(None,uopts,icls=get_wallet_cls('brain'))
  105. def ref_incog_conv(self,wfk='ic_wallet',in_fmt='i'):
  106. uopts = ['-i',in_fmt,'-p','1','-l',str(self.seed_len)]
  107. wf = joinpath(ref_dir,self.sources[str(self.seed_len)][wfk])
  108. return self.walletconv_in(wf,uopts)
  109. def ref_incox_conv(self):
  110. return self.ref_incog_conv(in_fmt='xi',wfk='ic_wallet_hex')
  111. def ref_hincog_conv(self,wfk='hic_wallet',add_uopts=[]):
  112. ic_f = joinpath(ref_dir,self.sources[str(self.seed_len)][wfk])
  113. uopts = ['-i','hi','-p','1','-l',str(self.seed_len)] + add_uopts
  114. hi_opt = ['-H',f'{ic_f},{ref_wallet_incog_offset}']
  115. return self.walletconv_in(
  116. None,
  117. uopts + hi_opt,
  118. icls = get_wallet_cls('incog_hidden') )
  119. def ref_hincog_conv_old(self):
  120. return self.ref_hincog_conv(wfk='hic_wallet_old',add_uopts=['-O'])
  121. def ref_wallet_conv_out(self):
  122. return self.walletconv_out('w')
  123. def ref_mn_conv_out(self):
  124. return self.walletconv_out('mn')
  125. def ref_bip39_conv_out(self):
  126. return self.walletconv_out('bip39')
  127. def ref_seed_conv_out(self):
  128. return self.walletconv_out('seed')
  129. def ref_hex_conv_out(self):
  130. return self.walletconv_out('hexseed')
  131. def ref_plainhex_conv_out(self):
  132. return self.walletconv_out('hex')
  133. def ref_dieroll_conv_out(self):
  134. return self.walletconv_out('dieroll')
  135. def ref_incog_conv_out(self):
  136. return self.walletconv_out('i')
  137. def ref_incox_conv_out(self):
  138. return self.walletconv_out('xi')
  139. def ref_hincog_conv_out(self,ic_f=None):
  140. if not ic_f:
  141. ic_f = joinpath(self.tmpdir,hincog_fn)
  142. hi_parms = f'{ic_f},{ref_wallet_incog_offset}'
  143. sl_parm = '-l' + str(self.seed_len)
  144. return self.walletconv_out('hi',
  145. uopts = ['-J',hi_parms,sl_parm],
  146. uopts_chk = ['-H',hi_parms,sl_parm] )
  147. def ref_hincog_blkdev_conv_out(self):
  148. def do_run(cmd):
  149. from subprocess import run,PIPE,DEVNULL
  150. return run(cmd,stdout=PIPE,stderr=DEVNULL,check=True)
  151. if self.skip_for_win():
  152. return 'skip'
  153. imsg('Creating block device image file')
  154. ic_img = joinpath(self.tmpdir,'hincog_blkdev_img')
  155. do_run(['dd','if=/dev/zero','of='+ic_img,'bs=1K','count=1'])
  156. ic_dev = do_run(['sudo','/sbin/losetup','-f']).stdout.strip().decode()
  157. ic_dev_mode_orig = '{:o}'.format( os.stat(ic_dev).st_mode & 0xfff )
  158. ic_dev_mode = '0666'
  159. imsg(f'Changing permissions on loop device to {ic_dev_mode!r}')
  160. do_run(['sudo','chmod',ic_dev_mode,ic_dev])
  161. imsg(f'Attaching loop device {ic_dev!r}')
  162. do_run(['sudo','/sbin/losetup',ic_dev,ic_img])
  163. self.ref_hincog_conv_out(ic_f=ic_dev)
  164. imsg(f'Detaching loop device {ic_dev!r}')
  165. do_run(['sudo','/sbin/losetup','-d',ic_dev])
  166. imsg(f'Resetting permissions on loop device to {ic_dev_mode_orig!r}')
  167. do_run(['sudo','chmod',ic_dev_mode_orig,ic_dev])
  168. return 'ok'
  169. # wallet conversion tests
  170. def walletconv_in(self,infile,uopts=[],icls=None):
  171. ocls = get_wallet_cls('words')
  172. opts = ['-d',self.tmpdir,'-o',ocls.fmt_codes[0],self.usr_rand_arg]
  173. if_arg = [infile] if infile else []
  174. d = '(convert)'
  175. t = self.spawn('mmgen-walletconv',opts+uopts+if_arg,extra_desc=d)
  176. t.license()
  177. icls = icls or get_wallet_cls(ext=get_extension(infile))
  178. if icls.type == 'brain':
  179. t.expect('Enter brainwallet: ',ref_wallet_brainpass+'\n')
  180. if icls.enc and icls.type != 'brain':
  181. t.passphrase(icls.desc,self.wpasswd)
  182. if self.test_name[:19] == 'ref_hincog_conv_old':
  183. t.expect('Is the Seed ID correct? (Y/n): ','\n')
  184. else:
  185. t.expect(['Passphrase is OK',' are correct'])
  186. wf = t.written_to_file(capfirst(ocls.desc))
  187. t.p.wait()
  188. # back check of result
  189. msg('' if cfg.profile else ' OK')
  190. return self.walletchk(
  191. wf,
  192. extra_desc = '(check)',
  193. sid = self.seed_id)
  194. def walletconv_out(self,out_fmt='w',uopts=[],uopts_chk=[]):
  195. wcls = get_wallet_cls(fmt_code=out_fmt)
  196. opts = ['-d',self.tmpdir,'-p1','-o',out_fmt] + uopts
  197. infile = joinpath(ref_dir,self.seed_id+'.mmwords')
  198. t = self.spawn('mmgen-walletconv',[self.usr_rand_arg]+opts+[infile],extra_desc='(convert)')
  199. add_args = [f'-l{self.seed_len}']
  200. t.license()
  201. if wcls.enc and wcls.type != 'brain':
  202. t.passphrase_new('new '+wcls.desc,self.wpasswd)
  203. t.usr_rand(self.usr_rand_chars)
  204. if wcls.type.startswith('incog'):
  205. for _ in range(3):
  206. t.expect('Encrypting random data from your operating system with ephemeral key')
  207. if wcls.type == 'incog_hidden':
  208. t.hincog_create(hincog_bytes)
  209. if out_fmt == 'w':
  210. t.label()
  211. wf = t.written_to_file(capfirst(wcls.desc))
  212. if wcls.type == 'incog_hidden':
  213. add_args += uopts_chk
  214. wf = None
  215. msg('' if cfg.profile else ' OK')
  216. return self.walletchk(
  217. wf,
  218. wcls = wcls,
  219. extra_desc = '(check)',
  220. sid = self.seed_id,
  221. add_args = add_args)