ts_wallet.py 9.0 KB

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