ts_main.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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_main.py: Basic operations tests for the test.py test suite
  20. """
  21. from mmgen.globalvars import g
  22. from mmgen.opts import opt
  23. from test.common 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. def make_brainwallet_file(fn):
  28. # Print random words with random whitespace in between
  29. wl = rwords.split()
  30. nwords,ws_list,max_spaces = 10,' \n',5
  31. def rand_ws_seq():
  32. nchars = getrandnum(1) % max_spaces + 1
  33. return ''.join([ws_list[getrandnum_range(1,200) % len(ws_list)] for i in range(nchars)])
  34. rand_pairs = [wl[getrandnum_range(1,200) % len(wl)] + rand_ws_seq() for i in range(nwords)]
  35. d = ''.join(rand_pairs).rstrip() + '\n'
  36. if opt.verbose: msg_r('Brainwallet password:\n{}'.format(cyan(d)))
  37. write_data_to_file(fn,d,'brainwallet password',quiet=True,ignore_opt_outdir=True)
  38. def verify_checksum_or_exit(checksum,chk):
  39. if checksum != chk:
  40. raise TestSuiteFatalException('Checksum error: {}'.format(chk))
  41. vmsg(green('Checksums match: ') + cyan(chk))
  42. addrs_per_wallet = 8
  43. # 100 words chosen randomly from here:
  44. # https://github.com/bitcoin/bips/pull/432/files/6332230d63149a950d05db78964a03bfd344e6b0
  45. rwords = """
  46. алфавит алый амнезия амфора артист баян белый биатлон брат бульвар веревка вернуть весть возраст
  47. восток горло горный десяток дятел ежевика жест жизнь жрать заговор здание зона изделие итог кабина
  48. кавалер каждый канал керосин класс клятва князь кривой крыша крючок кузнец кукла ландшафт мальчик
  49. масса масштаб матрос мрак муравей мычать негодяй носок ночной нрав оборот оружие открытие оттенок
  50. палуба пароход период пехота печать письмо позор полтора понятие поцелуй почему приступ пруд пятно
  51. ранее режим речь роса рынок рябой седой сердце сквозь смех снимок сойти соперник спичка стон
  52. сувенир сугроб суть сцена театр тираж толк удивить улыбка фирма читатель эстония эстрада юность
  53. """
  54. class TestSuiteMain(TestSuiteBase,TestSuiteShared):
  55. 'basic operations with emulated tracking wallet'
  56. tmpdir_nums = [1,2,3,4,5,14,15,16,20,21]
  57. networks = ('btc','btc_tn','ltc','ltc_tn','bch','bch_tn')
  58. passthru_opts = ('coin','testnet')
  59. segwit_opts_ok = True
  60. cmd_group = (
  61. ('walletgen_dfl_wallet', (15,'wallet generation (default wallet)',[[[],15]])),
  62. ('subwalletgen_dfl_wallet', (15,'subwallet generation (default wallet)',[[[pwfile],15]])),
  63. ('export_seed_dfl_wallet',(15,'seed export to mmseed format (default wallet)',[[[pwfile],15]])),
  64. ('addrgen_dfl_wallet',(15,'address generation (default wallet)',[[[pwfile],15]])),
  65. ('txcreate_dfl_wallet',(15,'transaction creation (default wallet)',[[['addrs'],15]])),
  66. ('txsign_dfl_wallet',(15,'transaction signing (default wallet)',[[['rawtx',pwfile],15]])),
  67. ('passchg_dfl_wallet',(16,'password, label and hash preset change (default wallet)',[[[pwfile],15]])),
  68. ('walletchk_newpass_dfl_wallet',(16,'wallet check with new pw, label and hash preset',[[[pwfile],16]])),
  69. ('delete_dfl_wallet',(15,'delete default wallet',[[[pwfile],15]])),
  70. ('walletgen', (1,'wallet generation', [[['del_dw_run'],15]])),
  71. ('subwalletgen', (1,'subwallet generation', [[['mmdat'],1]])),
  72. ('subwalletgen_mnemonic',(1,'subwallet generation (to mnemonic format)',[[['mmdat'],1]])),
  73. # ('walletchk', (1,'wallet check', [[['mmdat'],1]])),
  74. ('passchg', (5,'password, label and hash preset change',[[['mmdat',pwfile],1]])),
  75. ('passchg_keeplabel',(5,'password, label and hash preset change (keep label)',[[['mmdat',pwfile],1]])),
  76. ('passchg_usrlabel',(5,'password, label and hash preset change (interactive label)',[[['mmdat',pwfile],1]])),
  77. ('walletchk_newpass',(5,'wallet check with new pw, label and hash preset',[[['mmdat',pwfile],5]])),
  78. ('addrgen', (1,'address generation', [[['mmdat',pwfile],1]])),
  79. ('txcreate', (1,'transaction creation', [[['addrs'],1]])),
  80. ('txbump', (1,'transaction fee bumping (no send)',[[['rawtx'],1]])),
  81. ('txsign', (1,'transaction signing', [[['mmdat','rawtx',pwfile,'txbump'],1]])),
  82. ('txsend', (1,'transaction sending', [[['sigtx'],1]])),
  83. # txdo must go after txsign
  84. ('txdo', (1,'online transaction', [[['sigtx','mmdat'],1]])),
  85. ('export_seed', (1,'seed export to mmseed format', [[['mmdat'],1]])),
  86. ('export_hex', (1,'seed export to hexadecimal format', [[['mmdat'],1]])),
  87. ('export_mnemonic', (1,'seed export to mmwords format', [[['mmdat'],1]])),
  88. ('export_incog', (1,'seed export to mmincog format', [[['mmdat'],1]])),
  89. ('export_incog_hex',(1,'seed export to mmincog hex format', [[['mmdat'],1]])),
  90. ('export_incog_hidden',(1,'seed export to hidden mmincog format', [[['mmdat'],1]])),
  91. ('addrgen_seed', (1,'address generation from mmseed file', [[['mmseed','addrs'],1]])),
  92. ('addrgen_hex', (1,'address generation from mmhex file', [[['mmhex','addrs'],1]])),
  93. ('addrgen_mnemonic',(1,'address generation from mmwords file',[[['mmwords','addrs'],1]])),
  94. ('addrgen_incog', (1,'address generation from mmincog file',[[['mmincog','addrs'],1]])),
  95. ('addrgen_incog_hex',(1,'address generation from mmincog hex file',[[['mmincox','addrs'],1]])),
  96. ('addrgen_incog_hidden',(1,'address generation from hidden mmincog file', [[[hincog_fn,'addrs'],1]])),
  97. ('keyaddrgen', (1,'key-address file generation', [[['mmdat',pwfile],1]])),
  98. ('txsign_keyaddr',(1,'transaction signing with key-address file', [[['akeys.mmenc','rawtx'],1]])),
  99. ('txcreate_ni', (1,'transaction creation (non-interactive)', [[['addrs'],1]])),
  100. ('walletgen2',(2,'wallet generation (2), 128-bit seed', [[['del_dw_run'],15]])),
  101. ('addrgen2', (2,'address generation (2)', [[['mmdat'],2]])),
  102. ('txcreate2', (2,'transaction creation (2)', [[['addrs'],2]])),
  103. ('txsign2', (2,'transaction signing, two transactions',[[['mmdat','rawtx'],1],[['mmdat','rawtx'],2]])),
  104. ('export_mnemonic2', (2,'seed export to mmwords format (2)',[[['mmdat'],2]])),
  105. ('walletgen3',(3,'wallet generation (3)', [[['del_dw_run'],15]])),
  106. ('addrgen3', (3,'address generation (3)', [[['mmdat'],3]])),
  107. ('txcreate3', (3,'tx creation with inputs and outputs from two wallets', [[['addrs'],1],[['addrs'],3]])),
  108. ('txsign3', (3,'tx signing with inputs and outputs from two wallets',[[['mmdat'],1],[['mmdat','rawtx'],3]])),
  109. ('walletgen14', (14,'wallet generation (14)', [[['del_dw_run'],15]],14)),
  110. ('addrgen14', (14,'address generation (14)', [[['mmdat'],14]])),
  111. ('keyaddrgen14',(14,'key-address file generation (14)', [[['mmdat'],14]],14)),
  112. ('walletgen4',(4,'wallet generation (4) (brainwallet)', [[['del_dw_run'],15]])),
  113. ('addrgen4', (4,'address generation (4)', [[['mmdat'],4]])),
  114. ('txcreate4', (4,'tx creation with inputs and outputs from four seed sources, key-address file and non-MMGen inputs and outputs', [[['addrs'],1],[['addrs'],2],[['addrs'],3],[['addrs'],4],[['addrs','akeys.mmenc'],14]])),
  115. ('txsign4', (4,'tx signing with inputs and outputs from incog file, mnemonic file, wallet, brainwallet, key-address file and non-MMGen inputs and outputs', [[['mmincog'],1],[['mmwords'],2],[['mmdat'],3],[['mmbrain','rawtx'],4],[['akeys.mmenc'],14]])),
  116. ('txdo4', (4,'tx creation,signing and sending with inputs and outputs from four seed sources, key-address file and non-MMGen inputs and outputs', [[['addrs'],1],[['addrs'],2],[['addrs'],3],[['addrs'],4],[['addrs','akeys.mmenc'],14],[['mmincog'],1],[['mmwords'],2],[['mmdat'],3],[['mmbrain','rawtx'],4],[['akeys.mmenc'],14]])), # must go after txsign4
  117. ('txbump4', (4,'tx fee bump + send with inputs and outputs from four seed sources, key-address file and non-MMGen inputs and outputs', [[['akeys.mmenc'],14],[['mmincog'],1],[['mmwords'],2],[['mmdat'],3],[['akeys.mmenc'],14],[['mmbrain','sigtx','mmdat','txdo'],4]])), # must go after txsign4
  118. ('walletgen5',(20,'wallet generation (5)', [[['del_dw_run'],15]],20)),
  119. ('addrgen5', (20,'address generation (5)', [[['mmdat'],20]])),
  120. ('txcreate5', (20,'transaction creation with bad vsize (5)', [[['addrs'],20]])),
  121. ('txsign5', (20,'transaction signing with bad vsize', [[['mmdat','rawtx'],20]])),
  122. ('walletgen6',(21,'wallet generation (6)', [[['del_dw_run'],15]],21)),
  123. ('addrgen6', (21,'address generation (6)', [[['mmdat'],21]])),
  124. ('txcreate6', (21,'transaction creation with corrected vsize (6)', [[['addrs'],21]])),
  125. ('txsign6', (21,'transaction signing with corrected vsize', [[['mmdat','rawtx'],21]])),
  126. )
  127. def __init__(self,trunner,cfgs,spawn):
  128. self.lbl_id = ('account','label')[g.coin=='BTC'] # update as other coins adopt Core's label API
  129. if g.coin in ('BTC','BCH','LTC'):
  130. self.tx_fee = {'btc':'0.0001','bch':'0.001','ltc':'0.01'}[g.coin.lower()]
  131. self.txbump_fee = {'btc':'123s','bch':'567s','ltc':'12345s'}[g.coin.lower()]
  132. return TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  133. def _get_addrfile_checksum(self,display=False):
  134. addrfile = self.get_file_with_ext('addrs')
  135. silence()
  136. from mmgen.addr import AddrList
  137. chk = AddrList(addrfile).chksum
  138. if opt.verbose and display: msg('Checksum: {}'.format(cyan(chk)))
  139. end_silence()
  140. return chk
  141. def walletgen_dfl_wallet(self,seed_len=None):
  142. return self.walletgen(seed_len=seed_len,gen_dfl_wallet=True)
  143. def subwalletgen_dfl_wallet(self,pf):
  144. return self.subwalletgen(wf='default')
  145. def export_seed_dfl_wallet(self,pf,desc='seed data',out_fmt='seed'):
  146. return self.export_seed(wf=None,desc=desc,out_fmt=out_fmt,pf=pf)
  147. def addrgen_dfl_wallet(self,pf=None,check_ref=False):
  148. return self.addrgen(wf=None,pf=pf,check_ref=check_ref)
  149. def txcreate_dfl_wallet(self,addrfile):
  150. return self.txcreate_common(sources=['15'])
  151. def txsign_dfl_wallet(self,txfile,pf='',save=True,has_label=False):
  152. return self.txsign(txfile,wf=None,pf=pf,save=save,has_label=has_label)
  153. def passchg_dfl_wallet(self,pf):
  154. return self.passchg(wf=None,pf=pf)
  155. def walletchk_newpass_dfl_wallet(self,pf):
  156. return self.walletchk_newpass(wf=None,pf=pf)
  157. def delete_dfl_wallet(self,pf):
  158. self.write_to_tmpfile('del_dw_run',b'',binary=True)
  159. if opt.no_dw_delete: return 'skip'
  160. for wf in [f for f in os.listdir(g.data_dir) if f[-6:]=='.mmdat']:
  161. os.unlink(joinpath(g.data_dir,wf))
  162. self.spawn('',msg_only=True)
  163. self.have_dfl_wallet = False
  164. return 'ok'
  165. def walletgen(self,del_dw_run='dummy',seed_len=None,gen_dfl_wallet=False):
  166. self.write_to_tmpfile(pwfile,self.wpasswd+'\n')
  167. args = ['-p1']
  168. if not gen_dfl_wallet: args += ['-d',self.tmpdir]
  169. if seed_len: args += ['-l',str(seed_len)]
  170. t = self.spawn('mmgen-walletgen', args + [self.usr_rand_arg])
  171. t.license()
  172. t.usr_rand(self.usr_rand_chars)
  173. t.expect('Generating')
  174. t.passphrase_new('new MMGen wallet',self.wpasswd)
  175. t.label()
  176. if not self.have_dfl_wallet and gen_dfl_wallet:
  177. t.expect('move it to the data directory? (Y/n): ','y')
  178. self.have_dfl_wallet = True
  179. t.written_to_file('MMGen wallet')
  180. return t
  181. def subwalletgen(self,wf):
  182. args = [self.usr_rand_arg,'-p1','-d',self.tr.trash_dir,'-L','Label']
  183. if wf != 'default': args += [wf]
  184. t = self.spawn('mmgen-subwalletgen', args + ['10s'])
  185. t.license()
  186. t.passphrase('MMGen wallet',self.cfgs['1']['wpasswd'])
  187. t.expect('Generating subseed 10S')
  188. t.passphrase_new('new MMGen wallet','foo')
  189. t.usr_rand(self.usr_rand_chars)
  190. fn = t.written_to_file('MMGen wallet')
  191. assert fn[-6:] == '.mmdat','incorrect file extension: {}'.format(fn[-6:])
  192. return t
  193. def subwalletgen_mnemonic(self,wf):
  194. args = [self.usr_rand_arg,'-p1','-d',self.tr.trash_dir,'-o','words',wf,'3L']
  195. t = self.spawn('mmgen-subwalletgen', args)
  196. t.license()
  197. t.passphrase('MMGen wallet',self.cfgs['1']['wpasswd'])
  198. t.expect('Generating subseed 3L')
  199. fn = t.written_to_file('Mnemonic data')
  200. assert fn[-8:] == '.mmwords','incorrect file extension: {}'.format(fn[-8:])
  201. return t
  202. def passchg(self,wf,pf,label_action='cmdline'):
  203. silence()
  204. self.write_to_tmpfile(pwfile,get_data_from_file(pf))
  205. end_silence()
  206. add_args = {'cmdline': ['-d',self.tmpdir,'-L','Changed label (UTF-8) α'],
  207. 'keep': ['-d',self.tr.trash_dir,'--keep-label'],
  208. 'user': ['-d',self.tr.trash_dir]
  209. }[label_action]
  210. t = self.spawn('mmgen-passchg', add_args + [self.usr_rand_arg, '-p2'] + ([],[wf])[bool(wf)])
  211. t.license()
  212. t.passphrase('MMGen wallet',self.cfgs['1']['wpasswd'],pwtype='old')
  213. t.expect_getend('Hash preset changed to ')
  214. t.passphrase('MMGen wallet',self.wpasswd,pwtype='new') # reuse passphrase?
  215. t.expect('Repeat passphrase: ',self.wpasswd+'\n')
  216. t.usr_rand(self.usr_rand_chars)
  217. if label_action == 'user':
  218. t.expect('Enter a wallet label.*: ','Interactive Label (UTF-8) α\n',regex=True)
  219. t.expect_getend(('Label changed to ','Reusing label ')[label_action=='keep'])
  220. # t.expect_getend('Key ID changed: ')
  221. if not wf:
  222. t.expect("Type uppercase 'YES' to confirm: ",'YES\n')
  223. t.written_to_file('New wallet')
  224. t.expect('Securely deleting old wallet')
  225. # t.expect('Okay to WIPE 1 regular file ? (Yes/No)','Yes\n')
  226. t.expect('Wallet passphrase has changed')
  227. t.expect_getend('has been changed to ')
  228. else:
  229. t.written_to_file('MMGen wallet')
  230. return t
  231. def passchg_keeplabel(self,wf,pf):
  232. return self.passchg(wf,pf,label_action='keep')
  233. def passchg_usrlabel(self,wf,pf):
  234. return self.passchg(wf,pf,label_action='user')
  235. def walletchk_newpass(self,wf,pf):
  236. return self.walletchk(wf,pf,pw=True)
  237. def _write_fake_data_to_file(self,d):
  238. unspent_data_file = joinpath(self.tmpdir,'unspent.json')
  239. write_data_to_file(unspent_data_file,d,'Unspent outputs',quiet=True,ignore_opt_outdir=True)
  240. os.environ['MMGEN_BOGUS_WALLET_DATA'] = unspent_data_file
  241. bwd_msg = 'MMGEN_BOGUS_WALLET_DATA={}'.format(unspent_data_file)
  242. if opt.print_cmdline: msg(bwd_msg)
  243. if opt.log: self.tr.log_fd.write(bwd_msg + ' ')
  244. if opt.verbose or opt.exact_output:
  245. sys.stderr.write("Fake transaction wallet data written to file {!r}\n".format(unspent_data_file))
  246. def _create_fake_unspent_entry(self,coinaddr,al_id=None,idx=None,lbl=None,non_mmgen=False,segwit=False):
  247. if 'S' not in g.proto.mmtypes: segwit = False
  248. if lbl: lbl = ' ' + lbl
  249. k = coinaddr.addr_fmt
  250. if not segwit and k == 'p2sh': k = 'p2pkh'
  251. s_beg,s_end = { 'p2pkh': ('76a914','88ac'),
  252. 'p2sh': ('a914','87'),
  253. 'bech32': (g.proto.witness_vernum_hex + '14','') }[k]
  254. amt1,amt2 = {'btc':(10,40),'bch':(10,40),'ltc':(1000,4000)}[g.coin.lower()]
  255. ret = {
  256. self.lbl_id: '{}:{}'.format(g.proto.base_coin.lower(),coinaddr) if non_mmgen \
  257. else ('{}:{}{}'.format(al_id,idx,lbl)),
  258. 'vout': int(getrandnum(4) % 8),
  259. 'txid': os.urandom(32).hex(),
  260. 'amount': g.proto.coin_amt('{}.{}'.format(amt1 + getrandnum(4) % amt2, getrandnum(4) % 100000000)),
  261. 'address': coinaddr,
  262. 'spendable': False,
  263. 'scriptPubKey': '{}{}{}'.format(s_beg,coinaddr.hex,s_end),
  264. 'confirmations': getrandnum(3) // 2 # max: 8388608 (7 digits)
  265. }
  266. return ret
  267. def _create_fake_unspent_data(self,adata,tx_data,non_mmgen_input='',non_mmgen_input_compressed=True):
  268. out = []
  269. for d in tx_data.values():
  270. al = adata.addrlist(al_id=d['al_id'])
  271. for n,(idx,coinaddr) in enumerate(al.addrpairs()):
  272. lbl = get_label(do_shuffle=True)
  273. out.append(self._create_fake_unspent_entry(coinaddr,d['al_id'],idx,lbl,segwit=d['segwit']))
  274. if n == 0: # create a duplicate address. This means addrs_per_wallet += 1
  275. out.append(self._create_fake_unspent_entry(coinaddr,d['al_id'],idx,lbl,segwit=d['segwit']))
  276. if non_mmgen_input:
  277. from mmgen.obj import PrivKey
  278. privkey = PrivKey(os.urandom(32),compressed=non_mmgen_input_compressed,pubkey_type='std')
  279. from mmgen.addr import AddrGenerator,KeyGenerator
  280. rand_coinaddr = AddrGenerator('p2pkh').to_addr(KeyGenerator('std').to_pubhex(privkey))
  281. of = joinpath(self.cfgs[non_mmgen_input]['tmpdir'],non_mmgen_fn)
  282. write_data_to_file(of, privkey.wif+'\n','compressed {} key'.format(g.proto.name),
  283. quiet=True,ignore_opt_outdir=True)
  284. out.append(self._create_fake_unspent_entry(rand_coinaddr,non_mmgen=True,segwit=False))
  285. return out
  286. def _create_tx_data(self,sources,addrs_per_wallet=addrs_per_wallet):
  287. from mmgen.addr import AddrData,AddrList
  288. from mmgen.obj import AddrIdxList
  289. tx_data,ad = {},AddrData()
  290. for s in sources:
  291. afile = get_file_with_ext(self.cfgs[s]['tmpdir'],'addrs')
  292. al = AddrList(afile)
  293. ad.add(al)
  294. aix = AddrIdxList(fmt_str=self.cfgs[s]['addr_idx_list'])
  295. if len(aix) != addrs_per_wallet:
  296. raise TestSuiteFatalException(
  297. 'Address index list length != {}: {}'.format(addrs_per_wallet,repr(aix)))
  298. tx_data[s] = {
  299. 'addrfile': afile,
  300. 'chk': al.chksum,
  301. 'al_id': al.al_id,
  302. 'addr_idxs': aix[-2:],
  303. 'segwit': self.cfgs[s]['segwit']
  304. }
  305. return ad,tx_data
  306. def _make_txcreate_cmdline(self,tx_data):
  307. from mmgen.obj import PrivKey
  308. privkey = PrivKey(os.urandom(32),compressed=True,pubkey_type='std')
  309. t = ('p2pkh','segwit')['S' in g.proto.mmtypes]
  310. from mmgen.addr import AddrGenerator,KeyGenerator
  311. rand_coinaddr = AddrGenerator(t).to_addr(KeyGenerator('std').to_pubhex(privkey))
  312. # total of two outputs must be < 10 BTC (<1000 LTC)
  313. mods = {'btc':(6,4),'bch':(6,4),'ltc':(600,400)}[g.coin.lower()]
  314. for k in self.cfgs:
  315. self.cfgs[k]['amts'] = [None,None]
  316. for idx,mod in enumerate(mods):
  317. self.cfgs[k]['amts'][idx] = '{}.{}'.format(getrandnum(4) % mod, str(getrandnum(4))[:5])
  318. cmd_args = ['--outdir='+self.tmpdir]
  319. for num in tx_data:
  320. s = tx_data[num]
  321. cmd_args += [
  322. '{}:{},{}'.format(s['al_id'],s['addr_idxs'][0],self.cfgs[num]['amts'][0]),
  323. ]
  324. # + one change address and one BTC address
  325. if num is list(tx_data.keys())[-1]:
  326. cmd_args += ['{}:{}'.format(s['al_id'],s['addr_idxs'][1])]
  327. cmd_args += ['{},{}'.format(rand_coinaddr,self.cfgs[num]['amts'][1])]
  328. return cmd_args + [tx_data[num]['addrfile'] for num in tx_data]
  329. def txcreate_common(self,
  330. sources=['1'],
  331. non_mmgen_input='',
  332. do_label=False,
  333. txdo_args=[],
  334. add_args=[],
  335. view='n',
  336. addrs_per_wallet=addrs_per_wallet,
  337. non_mmgen_input_compressed=True,
  338. cmdline_inputs=False):
  339. if opt.verbose or opt.exact_output:
  340. sys.stderr.write(green('Generating fake tracking wallet info\n'))
  341. silence()
  342. ad,tx_data = self._create_tx_data(sources,addrs_per_wallet)
  343. dfake = self._create_fake_unspent_data(ad,tx_data,non_mmgen_input,non_mmgen_input_compressed)
  344. self._write_fake_data_to_file(repr(dfake))
  345. cmd_args = self._make_txcreate_cmdline(tx_data)
  346. if cmdline_inputs:
  347. from mmgen.tx import TwLabel
  348. cmd_args = ['--inputs={},{},{},{},{},{}'.format(
  349. TwLabel(dfake[0][self.lbl_id]).mmid,dfake[1]['address'],
  350. TwLabel(dfake[2][self.lbl_id]).mmid,dfake[3]['address'],
  351. TwLabel(dfake[4][self.lbl_id]).mmid,dfake[5]['address']
  352. ),'--outdir='+self.tr.trash_dir] + cmd_args[1:]
  353. end_silence()
  354. if opt.verbose or opt.exact_output: sys.stderr.write('\n')
  355. t = self.spawn(
  356. 'mmgen-'+('txcreate','txdo')[bool(txdo_args)],
  357. ([],['--rbf'])[g.proto.cap('rbf')] +
  358. ['-f',self.tx_fee,'-B'] + add_args + cmd_args + txdo_args)
  359. if t.expect([('Get','Transac')[cmdline_inputs],'Unable to connect to \S+'],regex=True) == 1:
  360. raise TestSuiteException('\n'+t.p.after)
  361. if cmdline_inputs:
  362. t.written_to_file('tion')
  363. return t
  364. t.license()
  365. if txdo_args and add_args: # txdo4
  366. t.do_decrypt_ka_data(hp='1',pw=self.cfgs['14']['kapasswd'])
  367. for num in tx_data:
  368. t.expect_getend('ting address data from file ')
  369. chk=t.expect_getend(r'Checksum for address data .*?: ',regex=True)
  370. verify_checksum_or_exit(tx_data[num]['chk'],chk)
  371. # not in tracking wallet warning, (1 + num sources) times
  372. for num in range(len(tx_data) + 1):
  373. t.expect('Continue anyway? (y/N): ','y')
  374. outputs_list = [(addrs_per_wallet+1)*i + 1 for i in range(len(tx_data))]
  375. if non_mmgen_input: outputs_list.append(len(tx_data)*(addrs_per_wallet+1) + 1)
  376. self.txcreate_ui_common(t,
  377. menu=(['M'],['M','D','m','g'])[self.test_name=='txcreate'],
  378. inputs=' '.join(map(str,outputs_list)),
  379. add_comment=('',ref_tx_label_lat_cyr_gr)[do_label],
  380. non_mmgen_inputs=(0,1)[bool(non_mmgen_input and not txdo_args)],
  381. view=view)
  382. return t
  383. def txcreate(self,addrfile):
  384. return self.txcreate_common(sources=['1'],add_args=['--vsize-adj=1.01'])
  385. def txbump(self,txfile,prepend_args=[],seed_args=[]):
  386. if not g.proto.cap('rbf'):
  387. msg('Skipping RBF'); return 'skip'
  388. args = prepend_args + ['--quiet','--outdir='+self.tmpdir,txfile] + seed_args
  389. t = self.spawn('mmgen-txbump',args)
  390. if seed_args:
  391. t.do_decrypt_ka_data(hp='1',pw=self.cfgs['14']['kapasswd'])
  392. t.expect('deduct the fee from (Hit ENTER for the change output): ','1\n')
  393. # Fee must be > tx_fee + network relay fee (currently 0.00001)
  394. t.expect('OK? (Y/n): ','\n')
  395. t.expect('Enter transaction fee: ',self.txbump_fee+'\n')
  396. t.expect('OK? (Y/n): ','\n')
  397. if seed_args: # sign and send
  398. t.do_comment(False,has_label=True)
  399. for cnum,desc in (('1','incognito data'),('3','MMGen wallet'),('4','MMGen wallet')):
  400. t.passphrase(desc,self.cfgs[cnum]['wpasswd'])
  401. self._do_confirm_send(t,quiet=not g.debug,confirm_send=True)
  402. if g.debug:
  403. t.written_to_file('Transaction')
  404. else:
  405. t.do_comment(False)
  406. t.expect('Save transaction? (y/N): ','y')
  407. t.written_to_file('Transaction')
  408. os.unlink(txfile) # our tx file replaces the original
  409. cmd = 'touch ' + joinpath(self.tmpdir,'txbump')
  410. os.system(cmd)
  411. return t
  412. def txsend(self,sigfile,bogus_send=True,extra_opts=[]):
  413. if not bogus_send: os.environ['MMGEN_BOGUS_SEND'] = ''
  414. t = self.spawn('mmgen-txsend', extra_opts + ['-d',self.tmpdir,sigfile])
  415. if not bogus_send: os.environ['MMGEN_BOGUS_SEND'] = '1'
  416. self.txsend_ui_common(t,view='t',add_comment='')
  417. return t
  418. def txdo(self,addrfile,wallet):
  419. t = self.txcreate_common(sources=['1'],txdo_args=[wallet])
  420. self.txsign_ui_common(t,view='n',do_passwd=True)
  421. self.txsend_ui_common(t)
  422. return t
  423. def _walletconv_export(self,wf,desc,uargs=[],out_fmt='w',pf=None,out_pw=False):
  424. opts = ['-d',self.tmpdir,'-o',out_fmt] + uargs + \
  425. ([],[wf])[bool(wf)] + ([],['-P',pf])[bool(pf)]
  426. t = self.spawn('mmgen-walletconv',opts)
  427. t.license()
  428. if not pf:
  429. t.passphrase('MMGen wallet',self.wpasswd)
  430. if out_pw:
  431. t.passphrase_new('new '+desc,self.wpasswd)
  432. t.usr_rand(self.usr_rand_chars)
  433. if ' '.join(desc.split()[-2:]) == 'incognito data':
  434. m = 'Generating encryption key from OS random data '
  435. t.expect(m); t.expect(m)
  436. incog_id = t.expect_getend('New Incog Wallet ID: ')
  437. t.expect(m)
  438. if desc == 'hidden incognito data':
  439. self.write_to_tmpfile(incog_id_fn,incog_id)
  440. ret = t.expect(['Create? (Y/n): ',"'YES' to confirm: "])
  441. if ret == 0:
  442. t.send('\n')
  443. t.expect('Enter file size: ',str(hincog_bytes)+'\n')
  444. else:
  445. t.send('YES\n')
  446. if out_fmt == 'w': t.label()
  447. return t.written_to_file(capfirst(desc),oo=True),t
  448. def export_seed(self,wf,desc='seed data',out_fmt='seed',pf=None):
  449. f,t = self._walletconv_export(wf,desc=desc,out_fmt=out_fmt,pf=pf)
  450. silence()
  451. msg('{}: {}'.format(capfirst(desc),cyan(get_data_from_file(f,desc))))
  452. end_silence()
  453. return t
  454. def export_hex(self,wf,desc='hexadecimal seed data',out_fmt='hex',pf=None):
  455. return self.export_seed(wf,desc=desc,out_fmt=out_fmt,pf=pf)
  456. def export_mnemonic(self,wf):
  457. return self.export_seed(wf,desc='mnemonic data',out_fmt='words')
  458. def export_incog(self,wf,desc='incognito data',out_fmt='i',add_args=[]):
  459. uargs = ['-p1',self.usr_rand_arg] + add_args
  460. f,t = self._walletconv_export(wf,desc=desc,out_fmt=out_fmt,uargs=uargs,out_pw=True)
  461. return t
  462. def export_incog_hex(self,wf):
  463. return self.export_incog(wf,desc='hex incognito data',out_fmt='xi')
  464. # TODO: make outdir and hidden incog compatible (ignore --outdir and warn user?)
  465. def export_incog_hidden(self,wf):
  466. rf = joinpath(self.tmpdir,hincog_fn)
  467. add_args = ['-J','{},{}'.format(rf,hincog_offset)]
  468. return self.export_incog(
  469. wf,desc='hidden incognito data',out_fmt='hi',add_args=add_args)
  470. def addrgen_seed(self,wf,foo,desc='seed data',in_fmt='seed'):
  471. stdout = desc == 'seed data' # capture output to screen once
  472. add_args = ([],['-S'])[bool(stdout)] + self.segwit_arg
  473. t = self.spawn('mmgen-addrgen', add_args +
  474. ['-i'+in_fmt,'-d',self.tmpdir,wf,self.addr_idx_list])
  475. t.license()
  476. t.expect_getend('Valid {} for Seed ID '.format(desc))
  477. vmsg('Comparing generated checksum with checksum from previous address file')
  478. chk = t.expect_getend(r'Checksum for address data .*?: ',regex=True)
  479. if stdout: t.read()
  480. verify_checksum_or_exit(self._get_addrfile_checksum(),chk)
  481. if in_fmt != 'seed':
  482. t.no_overwrite()
  483. t.req_exit_val = 1
  484. return t
  485. def addrgen_hex(self,wf,foo,desc='hexadecimal seed data',in_fmt='hex'):
  486. return self.addrgen_seed(wf,foo,desc=desc,in_fmt=in_fmt)
  487. def addrgen_mnemonic(self,wf,foo):
  488. return self.addrgen_seed(wf,foo,desc='mnemonic data',in_fmt='words')
  489. def addrgen_incog(self,wf=[],foo='',in_fmt='i',desc='incognito data',args=[]):
  490. t = self.spawn('mmgen-addrgen', args + self.segwit_arg + ['-i'+in_fmt,'-d',self.tmpdir]+
  491. ([],[wf])[bool(wf)] + [self.addr_idx_list])
  492. t.license()
  493. t.expect_getend('Incog Wallet ID: ')
  494. t.hash_preset(desc,'1')
  495. t.passphrase('{} \w{{8}}'.format(desc),self.wpasswd)
  496. vmsg('Comparing generated checksum with checksum from address file')
  497. chk = t.expect_getend(r'Checksum for address data .*?: ',regex=True)
  498. verify_checksum_or_exit(self._get_addrfile_checksum(),chk)
  499. t.no_overwrite()
  500. t.req_exit_val = 1
  501. return t
  502. def addrgen_incog_hex(self,wf,foo):
  503. return self.addrgen_incog(wf,'',in_fmt='xi',desc='hex incognito data')
  504. def addrgen_incog_hidden(self,wf,foo):
  505. rf = joinpath(self.tmpdir,hincog_fn)
  506. return self.addrgen_incog([],'',in_fmt='hi',desc='hidden incognito data',
  507. args=['-H','{},{}'.format(rf,hincog_offset),'-l',str(hincog_seedlen)])
  508. def txsign_keyaddr(self,keyaddr_file,txfile):
  509. t = self.spawn('mmgen-txsign', ['-d',self.tmpdir,'-p1','-M',keyaddr_file,txfile])
  510. t.license()
  511. t.do_decrypt_ka_data(hp='1',pw=self.kapasswd)
  512. t.view_tx('n')
  513. self.txsign_end(t)
  514. return t
  515. def txcreate_ni(self,addrfile):
  516. return self.txcreate_common(sources=['1'],cmdline_inputs=True,add_args=['--yes'])
  517. def walletgen2(self,del_dw_run='dummy'):
  518. return self.walletgen(seed_len=128)
  519. def addrgen2(self,wf):
  520. return self.addrgen(wf,pf='')
  521. def txcreate2(self,addrfile):
  522. return self.txcreate_common(sources=['2'])
  523. def txsign2(self,txf1,wf1,txf2,wf2):
  524. t = self.spawn('mmgen-txsign', ['-d',self.tmpdir,txf1,wf1,txf2,wf2])
  525. t.license()
  526. for cnum in ('1','2'):
  527. t.view_tx('n')
  528. t.passphrase('MMGen wallet',self.cfgs[cnum]['wpasswd'])
  529. self.txsign_end(t,cnum)
  530. return t
  531. def export_mnemonic2(self,wf):
  532. return self.export_mnemonic(wf)
  533. def walletgen3(self,del_dw_run='dummy'):
  534. return self.walletgen()
  535. def addrgen3(self,wf):
  536. return self.addrgen(wf,pf='')
  537. def txcreate3(self,addrfile1,addrfile2):
  538. return self.txcreate_common(sources=['1','3'])
  539. def txsign3(self,wf1,wf2,txf2):
  540. t = self.spawn('mmgen-txsign', ['-d',self.tmpdir,wf1,wf2,txf2])
  541. t.license()
  542. t.view_tx('n')
  543. for cnum in ('1','3'):
  544. t.passphrase('MMGen wallet',self.cfgs[cnum]['wpasswd'])
  545. self.txsign_end(t)
  546. return t
  547. walletgen14 = walletgen
  548. addrgen14 = TestSuiteShared.addrgen
  549. keyaddrgen14 = TestSuiteShared.keyaddrgen
  550. def walletgen4(self,del_dw_run='dummy'):
  551. bwf = joinpath(self.tmpdir,self.bw_filename)
  552. make_brainwallet_file(bwf)
  553. seed_len = str(self.seed_len)
  554. args = ['-d',self.tmpdir,'-p1',self.usr_rand_arg,'-l'+seed_len,'-ib']
  555. t = self.spawn('mmgen-walletconv', args + [bwf])
  556. t.license()
  557. t.passphrase_new('new MMGen wallet',self.wpasswd)
  558. t.usr_rand(self.usr_rand_chars)
  559. t.label()
  560. t.written_to_file('MMGen wallet')
  561. return t
  562. def addrgen4(self,wf):
  563. return self.addrgen(wf,pf='')
  564. def txcreate4(self,f1,f2,f3,f4,f5,f6):
  565. return self.txcreate_common(sources=['1','2','3','4','14'],non_mmgen_input='4',do_label=True,view='y')
  566. def txsign4(self,f1,f2,f3,f4,f5,f6):
  567. non_mm_file = joinpath(self.tmpdir,non_mmgen_fn)
  568. a = ['-d',self.tmpdir,'-i','brain','-b'+self.bw_params,'-p1','-k',non_mm_file,'-M',f6,f1,f2,f3,f4,f5]
  569. t = self.spawn('mmgen-txsign',a)
  570. t.license()
  571. t.do_decrypt_ka_data(hp='1',pw=self.cfgs['14']['kapasswd'])
  572. t.view_tx('t')
  573. for cnum,desc in (('1','incognito data'),('3','MMGen wallet')):
  574. t.passphrase('{}'.format(desc),self.cfgs[cnum]['wpasswd'])
  575. self.txsign_end(t,has_label=True)
  576. return t
  577. def txdo4(self,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12):
  578. non_mm_file = joinpath(self.tmpdir,non_mmgen_fn)
  579. add_args = ['-d',self.tmpdir,'-i','brain','-b'+self.bw_params,'-p1','-k',non_mm_file,'-M',f12]
  580. self.get_file_with_ext('sigtx',delete_all=True) # delete tx signed by txsign4
  581. t = self.txcreate_common(sources=['1','2','3','4','14'],
  582. non_mmgen_input='4',do_label=True,txdo_args=[f7,f8,f9,f10],add_args=add_args)
  583. for cnum,desc in (('1','incognito data'),('3','MMGen wallet')):
  584. t.passphrase('{}'.format(desc),self.cfgs[cnum]['wpasswd'])
  585. self.txsign_ui_common(t)
  586. self.txsend_ui_common(t)
  587. cmd = 'touch ' + joinpath(self.tmpdir,'txdo')
  588. os.system(cmd)
  589. return t
  590. def txbump4(self,f1,f2,f3,f4,f5,f6,f7,f8,f9): # f7:txfile,f9:'txdo'
  591. non_mm_file = joinpath(self.tmpdir,non_mmgen_fn)
  592. return self.txbump(f7,prepend_args=['-p1','-k',non_mm_file,'-M',f1],seed_args=[f2,f3,f4,f5,f6,f8])
  593. def walletgen5(self,del_dw_run='dummy'):
  594. return self.walletgen()
  595. def addrgen5(self,wf):
  596. return self.addrgen(wf,pf='')
  597. def txcreate5(self,addrfile):
  598. return self.txcreate_common(sources=['20'],non_mmgen_input='20',non_mmgen_input_compressed=False)
  599. def txsign5(self,txf,wf,bad_vsize=True,add_args=[]):
  600. non_mm_file = joinpath(self.tmpdir,non_mmgen_fn)
  601. t = self.spawn('mmgen-txsign', add_args + ['-d',self.tmpdir,'-k',non_mm_file,txf,wf])
  602. t.license()
  603. t.view_tx('n')
  604. t.passphrase('MMGen wallet',self.cfgs['20']['wpasswd'])
  605. if bad_vsize:
  606. t.expect('Estimated transaction vsize')
  607. t.expect('1 transaction could not be signed')
  608. exit_val = 2
  609. else:
  610. t.do_comment(False)
  611. t.expect('Save signed transaction? (Y/n): ','y')
  612. exit_val = 0
  613. t.read()
  614. t.req_exit_val = exit_val
  615. return t
  616. def walletgen6(self,del_dw_run='dummy'):
  617. return self.walletgen()
  618. def addrgen6(self,wf):
  619. return self.addrgen(wf,pf='')
  620. def txcreate6(self,addrfile):
  621. return self.txcreate_common(
  622. sources=['21'],non_mmgen_input='21',non_mmgen_input_compressed=False,add_args=['--vsize-adj=1.08'])
  623. def txsign6(self,txf,wf):
  624. return self.txsign5(txf,wf,bad_vsize=False,add_args=['--vsize-adj=1.08'])