ts_shared.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_shared.py: Shared methods for the test.py test suite
  20. """
  21. import os
  22. from mmgen.globalvars import g
  23. from mmgen.opts import opt
  24. from mmgen.util import ymsg
  25. from test.test_py_d.common import *
  26. from test.common import *
  27. class TestSuiteShared(object):
  28. 'shared methods for the test.py test suite'
  29. def txcreate_ui_common( self,t,
  30. caller = None,
  31. menu = [],
  32. inputs = '1',
  33. file_desc = 'Transaction',
  34. input_sels_prompt = 'to spend',
  35. bad_input_sels = False,
  36. non_mmgen_inputs = 0,
  37. interactive_fee = '',
  38. fee_desc = 'transaction fee',
  39. fee_res = None,
  40. eth_fee_res = None,
  41. add_comment = '',
  42. view = 't',
  43. save = True ):
  44. txdo = (caller or self.test_name)[:4] == 'txdo'
  45. for choice in menu + ['q']:
  46. t.expect(r'\[q\]uit view, .*?:.',choice,regex=True)
  47. if bad_input_sels:
  48. for r in ('x','3-1','9999'):
  49. t.expect(input_sels_prompt+': ',r+'\n')
  50. t.expect(input_sels_prompt+': ',inputs+'\n')
  51. if not txdo:
  52. for i in range(non_mmgen_inputs):
  53. t.expect('Accept? (y/N): ','y')
  54. have_est_fee = t.expect([fee_desc+': ','OK? (Y/n): ']) == 1
  55. if have_est_fee and not interactive_fee:
  56. t.send('y')
  57. else:
  58. if have_est_fee:
  59. t.send('n')
  60. if g.proto.base_coin == 'ETH': # TODO: pexpect race condition?
  61. time.sleep(0.05)
  62. if eth_fee_res:
  63. t.expect('or gas price: ',interactive_fee+'\n')
  64. else:
  65. t.send(interactive_fee+'\n')
  66. if fee_res: t.expect(fee_res)
  67. t.expect('OK? (Y/n): ','y')
  68. t.expect('(Y/n): ','\n') # chg amt OK?
  69. t.do_comment(add_comment)
  70. t.view_tx(view)
  71. if not txdo:
  72. t.expect('(y/N): ',('n','y')[save])
  73. t.written_to_file(file_desc)
  74. return t
  75. def txsign_ui_common( self,t,
  76. caller = None,
  77. view = 't',
  78. add_comment = '',
  79. file_desc = 'Signed transaction',
  80. ni = False,
  81. save = True,
  82. do_passwd = False,
  83. has_label = False ):
  84. txdo = (caller or self.test_name)[:4] == 'txdo'
  85. if do_passwd:
  86. t.passphrase('MMGen wallet',self.wpasswd)
  87. if not ni and not txdo:
  88. t.view_tx(view)
  89. t.do_comment(add_comment,has_label=has_label)
  90. t.expect('(Y/n): ',('n','y')[save])
  91. t.written_to_file(file_desc)
  92. return t
  93. def txsend_ui_common( self,t,
  94. caller = None,
  95. view = 'n',
  96. add_comment = '',
  97. file_desc = 'Sent transaction',
  98. confirm_send = True,
  99. bogus_send = True,
  100. quiet = False,
  101. has_label = False ):
  102. txdo = (caller or self.test_name)[:4] == 'txdo'
  103. if not txdo:
  104. t.license() # MMGEN_NO_LICENSE is set, so does nothing
  105. t.view_tx(view)
  106. t.do_comment(add_comment,has_label=has_label)
  107. self._do_confirm_send(t,quiet=quiet,confirm_send=confirm_send)
  108. if bogus_send:
  109. txid = ''
  110. t.expect('BOGUS transaction NOT sent')
  111. else:
  112. txid = t.expect_getend('Transaction sent: ')
  113. assert len(txid) == 64,"'{}': Incorrect txid length!".format(txid)
  114. t.written_to_file(file_desc)
  115. return txid
  116. def txsign_end(self,t,tnum=None,has_label=False):
  117. t.expect('Signing transaction')
  118. t.do_comment(False,has_label=has_label)
  119. t.expect('Save signed transaction.*?\? \(Y/n\): ','y',regex=True)
  120. t.written_to_file('Signed transaction' + (' #' + tnum if tnum else ''), oo=True)
  121. return t
  122. def txsign( self, txfile, wf,
  123. pf = '',
  124. bumpf = '',
  125. save = True,
  126. has_label = False,
  127. do_passwd = True,
  128. extra_opts = [],
  129. extra_desc = '' ):
  130. opts = extra_opts + ['-d',self.tmpdir,txfile] + ([wf] if wf else [])
  131. t = self.spawn('mmgen-txsign', opts, extra_desc)
  132. t.license()
  133. t.view_tx('n')
  134. if do_passwd: t.passphrase('MMGen wallet',self.wpasswd)
  135. if save:
  136. self.txsign_end(t,has_label=has_label)
  137. else:
  138. t.do_comment(False,has_label=has_label)
  139. t.expect('Save signed transaction? (Y/n): ','n')
  140. t.req_exit_val = 1
  141. return t
  142. def ref_brain_chk(self,bw_file=ref_bw_file):
  143. wf = joinpath(ref_dir,bw_file)
  144. add_args = ['-l{}'.format(self.seed_len), '-p'+ref_bw_hash_preset]
  145. return self.walletchk(wf,pf=None,add_args=add_args,
  146. desc='brainwallet',sid=self.ref_bw_seed_id)
  147. def walletchk(self,wf,pf,desc='MMGen wallet',add_args=[],sid=None,pw=False,extra_desc=''):
  148. args = []
  149. hp = self.hash_preset if hasattr(self,'hash_preset') else '1'
  150. wf_arg = [wf] if wf else []
  151. t = self.spawn('mmgen-walletchk',
  152. add_args+args+['-p',hp]+wf_arg,
  153. extra_desc=extra_desc)
  154. if desc != 'hidden incognito data':
  155. t.expect("Getting {} from file '".format(desc))
  156. if pw:
  157. t.passphrase(desc,self.wpasswd)
  158. t.expect(['Passphrase is OK', 'Passphrase.* are correct'],regex=True)
  159. chk = t.expect_getend('Valid {} for Seed ID '.format(desc))[:8]
  160. if sid: cmp_or_die(chk,sid)
  161. return t
  162. def addrgen(self,wf,pf=None,check_ref=False,ftype='addr',id_str=None,extra_args=[],mmtype=None):
  163. if not mmtype and ftype[:4] != 'pass':
  164. mmtype = self.segwit_mmtype
  165. cmd_pfx = (ftype,'pass')[ftype[:4]=='pass']
  166. t = self.spawn('mmgen-{}gen'.format(cmd_pfx),
  167. ['-d',self.tmpdir] + extra_args +
  168. ([],['--type='+str(mmtype)])[bool(mmtype)] +
  169. ([],[wf])[bool(wf)] +
  170. ([],[id_str])[bool(id_str)] +
  171. [getattr(self,'{}_idx_list'.format(cmd_pfx))],
  172. extra_desc='({})'.format(mmtype) if mmtype in ('segwit','bech32') else '')
  173. t.license()
  174. t.passphrase('MMGen wallet',self.wpasswd)
  175. t.expect('Passphrase is OK')
  176. desc = ('address','password')[ftype[:4]=='pass']
  177. chk = t.expect_getend(r'Checksum for {} data .*?: '.format(desc),regex=True)
  178. if ftype[:4] == 'pass':
  179. t.expect('Encrypt password list? (y/N): ','\n')
  180. t.written_to_file('Password list',oo=True)
  181. else:
  182. t.written_to_file('Addresses',oo=True)
  183. if check_ref:
  184. chk_ref = (self.chk_data[self.test_name] if ftype[:4] == 'pass' else
  185. self.chk_data[self.test_name][self.fork][g.testnet])
  186. cmp_or_die(chk,chk_ref,desc='{}list data checksum'.format(ftype))
  187. return t
  188. def keyaddrgen(self,wf,pf=None,check_ref=False,mmtype=None):
  189. if not mmtype:
  190. mmtype = self.segwit_mmtype
  191. args = ['-d',self.tmpdir,self.usr_rand_arg,wf,self.addr_idx_list]
  192. t = self.spawn('mmgen-keygen',
  193. ([],['--type='+str(mmtype)])[bool(mmtype)] + args,
  194. extra_desc='({})'.format(mmtype) if mmtype in ('segwit','bech32') else '')
  195. t.license()
  196. t.passphrase('MMGen wallet',self.wpasswd)
  197. chk = t.expect_getend(r'Checksum for key-address data .*?: ',regex=True)
  198. if check_ref:
  199. chk_ref = self.chk_data[self.test_name][self.fork][g.testnet]
  200. cmp_or_die(chk,chk_ref,desc='key-address list data checksum')
  201. t.expect('Encrypt key list? (y/N): ','y')
  202. t.usr_rand(self.usr_rand_chars)
  203. t.hash_preset('new key list','1')
  204. t.passphrase_new('new key list',self.kapasswd)
  205. t.written_to_file('Encrypted secret keys',oo=True)
  206. return t
  207. def _do_confirm_send(self,t,quiet=False,confirm_send=True):
  208. t.expect('Are you sure you want to broadcast this')
  209. m = ('YES, I REALLY WANT TO DO THIS','YES')[quiet]
  210. t.expect("'{}' to confirm: ".format(m),('',m)[confirm_send]+'\n')