ts_seedsplit.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 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_seedsplit.py: Seed split/join tests for the test.py test suite
  20. """
  21. from mmgen.globalvars import g
  22. from mmgen.opts import opt
  23. from mmgen.seed import SeedSource,Wallet,IncogWallet,IncogWalletHex,IncogWalletHidden,SeedSourceEnc
  24. from test.test_py_d.ts_base import *
  25. ref_wf = 'test/ref/98831F3A.bip39'
  26. ref_sid = '98831F3A'
  27. wpasswd = 'abc'
  28. sh1_passwd = 'xyz'
  29. dfl_wcls = Wallet
  30. class TestSuiteSeedSplit(TestSuiteBase):
  31. 'splitting and joining seeds'
  32. networks = ('btc',)
  33. tmpdir_nums = [23]
  34. cmd_group = (
  35. ('ss_walletgen', 'wallet generation'),
  36. ('ss_2way_A_dfl1', '2-way seed split (share A)'),
  37. ('ss_2way_B_dfl1', '2-way seed split (share B)'),
  38. ('ss_2way_join_dfl1', '2-way seed join'),
  39. ('ss_2way_A_dfl2', "2-way seed split 'default' (share A)"),
  40. ('ss_2way_B_dfl2', "2-way seed split 'default' (share B)"),
  41. ('ss_2way_join_dfl2', "2-way seed join 'default'"),
  42. ('ss_2way_A_alice', "2-way seed split 'alice' (share A)"),
  43. ('ss_2way_B_alice', "2-way seed split 'alice' (share B)"),
  44. ('ss_2way_join_alice', "2-way seed join 'alice'"),
  45. ('ss_2way_join_alice_mix', "2-way seed join 'alice' (out of order)"),
  46. ('ss_2way_A_dfl_master3', '2-way seed split with master share #3 (share A)'),
  47. ('ss_2way_B_dfl_master3', '2-way seed split with master share #3 (share B)'),
  48. ('ss_2way_join_dfl_master3', '2-way seed join with master share #3'),
  49. ('ss_2way_A_dfl_usw', '2-way seed split of user-specified wallet (share A)'),
  50. ('ss_2way_B_dfl_usw', '2-way seed split of user-specified wallet (share B)'),
  51. ('ss_2way_join_dfl_usw', '2-way seed join of user-specified wallet'),
  52. ('ss_3way_A_dfl', '3-way seed split (share A)'),
  53. ('ss_3way_B_dfl', '3-way seed split (share B)'),
  54. ('ss_3way_C_dfl', '3-way seed split (share C)'),
  55. ('ss_3way_join_dfl', '3-way seed join'),
  56. ('ss_3way_join_dfl_mix', '3-way seed join (out of order)'),
  57. ('ss_3way_A_foobar_master7', "3-way seed split 'φυβαρ' with master share #7 (share A)"),
  58. ('ss_3way_B_foobar_master7', "3-way seed split 'φυβαρ' with master share #7 (share B)"),
  59. ('ss_3way_C_foobar_master7', "3-way seed split 'φυβαρ' with master share #7 (share C)"),
  60. ('ss_3way_join_foobar_master7', "3-way seed join 'φυβαρ' with master share #7"),
  61. ('ss_3way_join_foobar_master7_mix',"3-way seed join 'φυβαρ' with master share #7 (out of order)"),
  62. ('ss_3way_join_dfl_bad_invocation',"bad invocation of 'mmgen-seedjoin' - --id-str with non-master join"),
  63. ('ss_bad_invocation1', "bad invocation of 'mmgen-seedsplit' - no arguments"),
  64. ('ss_bad_invocation2', "bad invocation of 'mmgen-seedsplit' - master share with split specifier"),
  65. ('ss_bad_invocation3', "bad invocation of 'mmgen-seedsplit' - nonexistent file"),
  66. ('ss_bad_invocation4', "bad invocation of 'mmgen-seedsplit' - invalid file extension"),
  67. ('ss_bad_invocation5', "bad invocation of 'mmgen-seedjoin' - no arguments"),
  68. ('ss_bad_invocation6', "bad invocation of 'mmgen-seedjoin' - one file argument"),
  69. ('ss_bad_invocation7', "bad invocation of 'mmgen-seedjoin' - invalid file extension"),
  70. ('ss_bad_invocation8', "bad invocation of 'mmgen-seedjoin' - nonexistent file"),
  71. ('ss_bad_invocation9', "bad invocation of 'mmgen-seedsplit' - bad specifier"),
  72. ('ss_bad_invocation10', "bad invocation of 'mmgen-seedsplit' - nonexistent file"),
  73. ('ss_bad_invocation11', "bad invocation of 'mmgen-seedsplit' - invalid file extension"),
  74. )
  75. def get_tmp_subdir(self,subdir):
  76. return os.path.join(self.tmpdir,subdir)
  77. def ss_walletgen(self):
  78. t = self.spawn('mmgen-walletgen', ['-r0','-p1'])
  79. t.passphrase_new('new '+dfl_wcls.desc,wpasswd)
  80. t.label()
  81. self.write_to_tmpfile('dfl.sid',t.expect_getend('Seed ID: '))
  82. t.expect('move it to the data directory? (Y/n): ','y')
  83. t.written_to_file(capfirst(dfl_wcls.desc))
  84. return t
  85. def ss_splt(self,tdir,ofmt,spec,add_args=[],wf=None,master=None):
  86. try: os.mkdir(self.get_tmp_subdir(tdir))
  87. except: pass
  88. t = self.spawn('mmgen-seedsplit',
  89. ['-q','-d',self.get_tmp_subdir(tdir),'-r0','-o',ofmt]
  90. + (['-L',(spec or 'label')] if ofmt == 'w' else [])
  91. + add_args
  92. + (['--master-share={}'.format(master)] if master else [])
  93. + ([wf] if wf else [])
  94. + ([spec] if spec else []))
  95. if not wf:
  96. t.passphrase(dfl_wcls.desc,wpasswd)
  97. if spec:
  98. from mmgen.obj import SeedSplitSpecifier
  99. sss = SeedSplitSpecifier(spec)
  100. pat = "Processing .* {} of {} of .* id '{}'".format(sss.idx,sss.count,sss.id)
  101. else:
  102. pat = "master share #{}".format(master)
  103. t.expect(pat,regex=True)
  104. ocls = SeedSource.fmt_code_to_type(ofmt)
  105. pw = issubclass(ocls,SeedSourceEnc)
  106. if pw:
  107. t.hash_preset('new '+ocls.desc,'1')
  108. t.passphrase_new('new '+ocls.desc,sh1_passwd)
  109. if ocls == IncogWalletHidden:
  110. t.hincog_create(1234)
  111. t.written_to_file(capfirst(ocls.desc))
  112. return t
  113. def ss_join(self,tdir,ofmt,in_exts,add_args=[],sid=None,bad_invocation=False,master=None,id_str=None):
  114. td = self.get_tmp_subdir(tdir)
  115. shares = [get_file_with_ext(td,f) for f in in_exts]
  116. if not sid:
  117. sid = self.read_from_tmpfile('dfl.sid')
  118. t = self.spawn('mmgen-seedjoin',
  119. add_args
  120. + (['--master-share={}'.format(master)] if master else [])
  121. + (['--id-str={}'.format(id_str)] if id_str else [])
  122. + ['-d',td,'-o',ofmt]
  123. + (['--label','Joined Wallet Label','-r0'] if ofmt == 'w' else [])
  124. + shares)
  125. if bad_invocation:
  126. t.read()
  127. return t
  128. icls = ( Wallet if 'mmdat' in in_exts
  129. else IncogWallet if 'mmincog' in in_exts
  130. else IncogWalletHex if 'mmincox' in in_exts
  131. else IncogWalletHidden if '-H' in add_args
  132. else None )
  133. if icls in (IncogWallet,IncogWalletHex,IncogWalletHidden):
  134. t.hash_preset(icls.desc,'1')
  135. if icls:
  136. t.passphrase(icls.desc,sh1_passwd)
  137. if master:
  138. fs = "master share #{}, split id '{}', share count {}"
  139. pat = fs.format(master,id_str or 'default',len(shares)+(icls==IncogWalletHidden))
  140. t.expect(pat,regex=True)
  141. sid_cmp = t.expect_getend('Joined Seed ID: ')
  142. cmp_or_die(sid,sid_cmp)
  143. ocls = SeedSource.fmt_code_to_type(ofmt)
  144. if ocls == Wallet:
  145. t.hash_preset('new '+ocls.desc,'1')
  146. t.passphrase_new('new '+ocls.desc,wpasswd)
  147. t.written_to_file(capfirst(ocls.desc))
  148. return t
  149. def get_hincog_arg(self,tdir,suf='-default-2of2'):
  150. sid = self.read_from_tmpfile('dfl.sid')
  151. return os.path.join(self.tmpdir,tdir,sid+suf+'.hincog') + ',123'
  152. def ss_2way_A_dfl1(self): return self.ss_splt('2way_dfl1','w','1:2')
  153. def ss_2way_B_dfl1(self): return self.ss_splt('2way_dfl1','bip39','2:2')
  154. def ss_2way_join_dfl1(self): return self.ss_join('2way_dfl1','w',['mmdat','bip39'])
  155. def ss_2way_A_dfl2(self): return self.ss_splt('2way_dfl2','seed','default:1:2')
  156. def ss_2way_B_dfl2(self):
  157. return self.ss_splt('2way_dfl2','hincog','default:2:2',['-J',self.get_hincog_arg('2way_dfl2')])
  158. def ss_2way_join_dfl2(self):
  159. return self.ss_join('2way_dfl2','mmhex',['mmseed'],['-H',self.get_hincog_arg('2way_dfl2')])
  160. def ss_2way_A_alice(self): return self.ss_splt('2way_alice','w','alice:1:2')
  161. def ss_2way_B_alice(self): return self.ss_splt('2way_alice','mmhex','alice:2:2')
  162. def ss_2way_join_alice(self): return self.ss_join('2way_alice','seed',['mmdat','mmhex'])
  163. def ss_2way_join_alice_mix(self): return self.ss_join('2way_alice','seed',['mmhex','mmdat'])
  164. def ss_2way_A_dfl_usw(self): return self.ss_splt('2way_dfl_usw','words','1:2',[],wf=ref_wf)
  165. def ss_2way_B_dfl_usw(self): return self.ss_splt('2way_dfl_usw','incog','2:2',[],wf=ref_wf)
  166. def ss_2way_join_dfl_usw(self): return self.ss_join('2way_dfl_usw','mmhex',['mmwords','mmincog'],sid=ref_sid)
  167. def ss_3way_A_dfl(self): return self.ss_splt('3way_dfl','words','1:3')
  168. def ss_3way_B_dfl(self): return self.ss_splt('3way_dfl','incog_hex','2:3')
  169. def ss_3way_C_dfl(self): return self.ss_splt('3way_dfl','bip39','3:3')
  170. def ss_3way_join_dfl(self): return self.ss_join('3way_dfl','mmhex',['mmwords','mmincox','bip39'])
  171. def ss_3way_join_dfl_mix(self): return self.ss_join('3way_dfl','mmhex',['bip39','mmwords','mmincox'])
  172. def ss_2way_A_dfl_master3(self):
  173. return self.ss_splt('2way_dfl_master3','w','',master=3)
  174. def ss_2way_B_dfl_master3(self):
  175. return self.ss_splt('2way_dfl_master3','bip39','2:2',master=3)
  176. def ss_2way_join_dfl_master3(self):
  177. return self.ss_join('2way_dfl_master3','mmhex',['mmdat','bip39'],master=3)
  178. tdir2 = '3way_foobar_master7'
  179. def ss_3way_C_foobar_master7(self):
  180. return self.ss_splt(self.tdir2,'hincog','',
  181. ['-J',self.get_hincog_arg(self.tdir2,'-master7')],master=7)
  182. def ss_3way_B_foobar_master7(self):
  183. return self.ss_splt(self.tdir2,'bip39','φυβαρ:2:3',master=7)
  184. def ss_3way_A_foobar_master7(self):
  185. return self.ss_splt(self.tdir2,'mmhex','φυβαρ:3:3',master=7)
  186. def ss_3way_join_foobar_master7(self):
  187. return self.ss_join(self.tdir2,'seed', ['bip39','mmhex'],
  188. ['-H',self.get_hincog_arg(self.tdir2,'-master7')],master=7,id_str='φυβαρ')
  189. def ss_3way_join_foobar_master7_mix(self):
  190. return self.ss_join(self.tdir2,'seed', ['mmhex','bip39'],
  191. ['-H',self.get_hincog_arg(self.tdir2,'-master7')],master=7,id_str='φυβαρ')
  192. def ss_bad_invocation(self,cmd,args,exit_val):
  193. t = self.spawn(cmd,args)
  194. t.read()
  195. t.req_exit_val = exit_val
  196. return t
  197. def ss_3way_join_dfl_bad_invocation(self):
  198. t = self.ss_join('3way_dfl','mmhex',
  199. ['mmwords','mmincox','bip39'],
  200. id_str='foo',
  201. bad_invocation=True)
  202. t.req_exit_val = 1
  203. return t
  204. def ss_bad_invocation1(self):
  205. return self.ss_bad_invocation('mmgen-seedsplit',[],1)
  206. def ss_bad_invocation2(self):
  207. return self.ss_bad_invocation('mmgen-seedsplit',['-M1','1:9'],1)
  208. def ss_bad_invocation3(self):
  209. return self.ss_bad_invocation('mmgen-seedsplit',[self.tmpdir+'/no.mmdat','1:9'],1)
  210. def ss_bad_invocation4(self):
  211. return self.ss_bad_invocation('mmgen-seedsplit',[self.tmpdir+'/dfl.sid','1:9'],1)
  212. def ss_bad_invocation5(self):
  213. return self.ss_bad_invocation('mmgen-seedjoin',[],1)
  214. def ss_bad_invocation6(self):
  215. return self.ss_bad_invocation('mmgen-seedjoin',[self.tmpdir+'/a'],1)
  216. def ss_bad_invocation7(self):
  217. return self.ss_bad_invocation('mmgen-seedjoin',[self.tmpdir+'/a',self.tmpdir+'/b'],1)
  218. def ss_bad_invocation8(self):
  219. return self.ss_bad_invocation('mmgen-seedjoin',[self.tmpdir+'/a.mmdat',self.tmpdir+'/b.mmdat'],1)
  220. def ss_bad_invocation9(self):
  221. return self.ss_bad_invocation('mmgen-seedsplit',['x'],1)
  222. def ss_bad_invocation10(self):
  223. return self.ss_bad_invocation('mmgen-seedsplit',[self.tmpdir+'/a.mmdat','1:2'],1)
  224. def ss_bad_invocation11(self):
  225. return self.ss_bad_invocation('mmgen-seedsplit',[self.tmpdir+'/dfl.sid','1:2'],1)