ts_wallet.py 9.3 KB

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