xmr_autosign.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.cmdtest_d.xmr_autosign: xmr autosigning tests for the cmdtest.py test suite
  12. """
  13. import os, re, asyncio, json
  14. from mmgen.color import blue, cyan, brown
  15. from ..include.common import imsg, silence, end_silence, strip_ansi_escapes, read_from_file
  16. from .include.common import get_file_with_ext, cleanup_env
  17. from .xmrwallet import CmdTestXMRWallet
  18. from .autosign import CmdTestAutosignThreaded
  19. def make_burn_addr(cfg):
  20. from mmgen.tool.coin import tool_cmd
  21. return tool_cmd(
  22. cfg = cfg,
  23. cmdname = 'privhex2addr',
  24. proto = cfg._proto,
  25. mmtype = 'monero').privhex2addr('beadcafe'*8)
  26. class CmdTestXMRAutosign(CmdTestXMRWallet, CmdTestAutosignThreaded):
  27. """
  28. Monero autosigning operations (xmrwallet compat mode)
  29. """
  30. tmpdir_nums = [39]
  31. # xmrwallet attrs:
  32. tx_relay_user = 'miner'
  33. # user sid autosign port_shift kal_range add_coind_args
  34. user_data = (
  35. ('miner', '98831F3A', False, 130, '1', []),
  36. ('alice', 'FE3C6545', True, 150, '1-2', []))
  37. # autosign attrs:
  38. coins = ['xmr']
  39. compat = True
  40. cmd_group = (
  41. ('daemon_version', 'checking daemon version'),
  42. ('create_tmp_wallets', 'creating temporary online wallets for Alice'),
  43. ('new_account_alice', 'adding an account to Alice’s tmp wallet'),
  44. ('new_address_alice', 'adding an address to Alice’s tmp wallet'),
  45. ('new_address_alice_label', 'adding an address to Alice’s tmp wallet (with label)'),
  46. ('dump_tmp_wallets', 'dumping Alice’s tmp wallets'),
  47. ('dump_tmp_wallets_json', 'dumping Alice’s tmp wallets to JSON format'),
  48. ('delete_tmp_wallets', 'deleting Alice’s tmp wallets'),
  49. ('gen_kafile_miner', 'generating key-address file for Miner'),
  50. ('create_wallet_miner', 'creating Monero wallet for Miner'),
  51. ('mine_initial_coins', 'mining initial coins'),
  52. ('autosign_setup', 'autosign setup with Alice’s seed'),
  53. ('autosign_xmr_setup', 'autosign setup (creation of Monero signing wallets)'),
  54. ('restore_watchonly_wallets', 'creating watch-only wallets from Alice’s wallet dumps'),
  55. ('delete_tmp_dump_files', 'deleting Alice’s dump files'),
  56. ('fund_alice1', 'sending funds to Alice (wallet #1)'),
  57. ('check_bal_alice1', 'mining, checking balance (wallet #1)'),
  58. ('fund_alice2', 'sending funds to Alice (wallet #2)'),
  59. ('check_bal_alice2', 'mining, checking balance (wallet #2)'),
  60. ('wait_loop_start', 'starting autosign wait loop'),
  61. ('export_outputs1', 'exporting outputs from Alice’s watch-only wallet #1'),
  62. ('create_transfer_tx1', 'creating a transfer TX'),
  63. ('submit_transfer_tx1', 'submitting the transfer TX'),
  64. ('resubmit_transfer_tx1', 'resubmitting the transfer TX'),
  65. ('export_outputs2', 'exporting outputs from Alice’s watch-only wallet #1'),
  66. ('import_key_images1', 'importing signed key images into Alice’s online wallets'),
  67. ('sync_chkbal1', 'syncing Alice’s wallet #1'),
  68. ('abort_tx1', 'aborting the current transaction (error)'),
  69. ('create_transfer_tx2', 'creating a transfer TX (for relaying via proxy)'),
  70. ('abort_tx2', 'aborting the current transaction (OK, unsigned)'),
  71. ('create_transfer_tx2a', 'creating the transfer TX again'),
  72. ('submit_transfer_tx2', 'submitting the transfer TX (relaying via proxy)'),
  73. ('sync_chkbal2', 'syncing Alice’s wallets and checking balance'),
  74. ('dump_wallets', 'dumping Alice’s wallets'),
  75. ('delete_wallets', 'deleting Alice’s wallets'),
  76. ('restore_wallets', 'creating online (watch-only) wallets for Alice'),
  77. ('delete_dump_files', 'deleting Alice’s dump files'),
  78. ('export_outputs3', 'exporting outputs from Alice’s watch-only wallets'),
  79. ('import_key_images2', 'importing signed key images into Alice’s online wallets'),
  80. ('sync_chkbal3', 'syncing Alice’s wallets and checking balance'),
  81. ('wait_loop_kill', 'stopping autosign wait loop'),
  82. ('stop_daemons', 'stopping all wallet and coin daemons'),
  83. ('view', 'viewing Alice’s wallet in offline mode (wallet #1)'),
  84. ('listview', 'list-viewing Alice’s wallet in offline mode (wallet #2)'),
  85. ('txlist', 'listing Alice’s submitted transactions'),
  86. ('txview', 'viewing Alice’s submitted transactions'),
  87. ('txview_all', 'viewing all raw, signed and submitted transactions'),
  88. ('check_tx_dirs', 'cleaning and checking signable file directories'),
  89. )
  90. def __init__(self, cfg, trunner, cfgs, spawn):
  91. CmdTestAutosignThreaded.__init__(self, cfg, trunner, cfgs, spawn)
  92. CmdTestXMRWallet.__init__(self, cfg, trunner, cfgs, spawn)
  93. if trunner is None:
  94. return
  95. from mmgen.cfg import Config
  96. self.alice_cfg = Config({
  97. 'coin': 'XMR',
  98. 'outdir': self.users['alice'].udir,
  99. 'wallet_rpc_password': 'passwOrd',
  100. 'test_suite': True,
  101. } | ({
  102. 'alice': True,
  103. 'compat': True
  104. } if self.compat else {
  105. 'wallet_dir': self.users['alice'].udir
  106. }))
  107. self.burn_addr = make_burn_addr(cfg)
  108. self.opts.append('--xmrwallets={}'.format(self.users['alice'].kal_range)) # mmgen-autosign opts
  109. self.autosign_opts = ['--autosign'] # mmgen-xmrwallet opts
  110. self.spawn_env['MMGEN_TEST_SUITE_XMR_AUTOSIGN'] = '1'
  111. def create_tmp_wallets(self):
  112. self.spawn(msg_only=True)
  113. data = self.users['alice']
  114. from mmgen.wallet import Wallet
  115. from mmgen.xmrwallet import op
  116. from mmgen.addrlist import KeyAddrList
  117. silence()
  118. kal = KeyAddrList(
  119. cfg = self.alice_cfg,
  120. proto = self.proto,
  121. addr_idxs = '1-2',
  122. seed = Wallet(self.alice_cfg, fn=data.mmwords).seed,
  123. skip_chksum_msg = True,
  124. key_address_validity_check = False)
  125. kal.file.write(ask_overwrite=False)
  126. fn = get_file_with_ext(data.udir, 'akeys')
  127. m = op('create', self.alice_cfg, fn, '1-2')
  128. asyncio.run(m.main())
  129. asyncio.run(m.stop_wallet_daemon())
  130. end_silence()
  131. return 'ok'
  132. def _new_addr_alice(self, *args):
  133. data = self.users['alice']
  134. return self.new_addr_alice(
  135. *args,
  136. kafile = get_file_with_ext(data.udir, 'akeys'))
  137. def new_account_alice(self):
  138. return self._new_addr_alice(
  139. '2',
  140. 'start',
  141. r'Creating new account for wallet .*2.* with label '
  142. r'.*‘xmrwallet new account .*y/N\): ')
  143. def new_address_alice(self):
  144. return self._new_addr_alice(
  145. '2:1',
  146. 'continue',
  147. r'Creating new address for wallet .*2.*, account .*#1.* with label '
  148. r'.*‘xmrwallet new address .*y/N\): ')
  149. def new_address_alice_label(self):
  150. return self._new_addr_alice(
  151. '2:1,Alice’s new address',
  152. 'stop',
  153. r'Creating new address for wallet .*2.*, account .*#1.* with label '
  154. r'.*‘Alice’s new address .*y/N\): ')
  155. def dump_tmp_wallets(self):
  156. return self._dump_wallets(autosign=False)
  157. def dump_tmp_wallets_json(self):
  158. return self._dump_wallets(autosign=False, op='dump_json')
  159. def dump_wallets(self):
  160. return self._dump_wallets(autosign=True)
  161. def _dump_wallets(self, autosign, op='dump'):
  162. data = self.users['alice']
  163. self.insert_device_online()
  164. t = self.spawn(
  165. 'mmgen-xmrwallet',
  166. self.extra_opts
  167. + (['--alice', '--compat'] if self.compat else [f'--wallet-dir={data.udir}'])
  168. + [f'--daemon=localhost:{data.md.rpc_port}']
  169. + (self.autosign_opts if autosign else [])
  170. + [op]
  171. + ([] if autosign else [get_file_with_ext(data.udir, 'akeys')]),
  172. env = cleanup_env(self.cfg))
  173. t.expect('2 wallets dumped')
  174. res = t.read()
  175. if op == 'dump_json':
  176. data = json.loads(re.sub('Stopping.*', '', strip_ansi_escapes(res)).strip())
  177. self.remove_device_online()
  178. return t
  179. def _delete_files(self, *ext_list):
  180. data = self.users['alice']
  181. self.spawn(msg_only=True)
  182. wdir = data.wd.wallet_dir if self.compat else data.udir
  183. for ext in ext_list:
  184. get_file_with_ext(wdir, ext, no_dot=True, delete_all=True)
  185. return 'ok'
  186. def delete_tmp_wallets(self):
  187. return self._delete_files('MoneroWallet', 'MoneroWallet.keys', '.akeys')
  188. def delete_wallets(self):
  189. return self._delete_files('MoneroWatchOnlyWallet', '.keys', '.address.txt')
  190. def delete_tmp_dump_files(self):
  191. return self._delete_files('.dump')
  192. def gen_kafile_miner(self):
  193. return self.gen_kafiles(['miner'])
  194. def create_wallet_miner(self):
  195. return self.create_wallets_miner()
  196. def delete_dump_files(self):
  197. return self._delete_files('.dump')
  198. async def fund_alice1(self):
  199. return await self.fund_alice(wallet=1)
  200. fund_alice1b = fund_alice1
  201. async def check_bal_alice1(self):
  202. return await self.check_bal_alice(wallet=1)
  203. async def fund_alice2(self):
  204. return await self.fund_alice(wallet=2)
  205. async def check_bal_alice2(self):
  206. return await self.check_bal_alice(wallet=2)
  207. def autosign_setup(self):
  208. return self.run_setup(
  209. mn_type = 'mmgen',
  210. mn_file = self.users['alice'].mmwords,
  211. use_dfl_wallet = None,
  212. expect_args = ['Continue with Monero setup? (Y/n): ', 'n'])
  213. def autosign_xmr_setup(self):
  214. self.insert_device_online()
  215. self.do_mount_online()
  216. self.asi_online.xmr_dir.mkdir(exist_ok=True)
  217. (self.asi_online.xmr_dir / 'old.vkeys').touch()
  218. self.do_umount_online()
  219. self.remove_device_online()
  220. self.insert_device()
  221. t = self.spawn('mmgen-autosign', self.opts + ['xmr_setup'], no_passthru_opts=True)
  222. t.written_to_file('View keys')
  223. t.read()
  224. self.remove_device()
  225. return t
  226. def restore_watchonly_wallets(self):
  227. return self._create_wallets('restore')
  228. def restore_wallets(self):
  229. return self._create_wallets('restore')
  230. def _create_wallets(self, op='create'):
  231. self.insert_device_online()
  232. t = self.create_wallets('alice', op=op)
  233. t.read() # required!
  234. self.remove_device_online()
  235. return t
  236. def _create_transfer_tx(self, amt, add_opts=[]):
  237. self.insert_device_online()
  238. t = self.do_op(
  239. 'transfer',
  240. 'alice',
  241. f'1:0:{self.burn_addr},{amt}',
  242. no_relay = True,
  243. do_ret = True,
  244. add_opts = add_opts)
  245. t.read() # required!
  246. self.remove_device_online()
  247. return t
  248. def create_transfer_tx1(self):
  249. return self._create_transfer_tx('0.124', add_opts=['--priority=2'])
  250. def create_transfer_tx2(self):
  251. return self._create_transfer_tx('0.257')
  252. create_transfer_tx2a = create_transfer_tx2
  253. def _abort_tx(self, expect, send=None, exit_val=None):
  254. self.insert_device_online()
  255. t = self.spawn('mmgen-xmrwallet', ['--autosign', 'abort'], exit_val=exit_val)
  256. t.expect(expect)
  257. if send:
  258. t.send(send)
  259. t.read() # required!
  260. self.remove_device_online()
  261. return t
  262. def abort_tx1(self):
  263. return self._abort_tx('No unsent transactions present', exit_val=2)
  264. def abort_tx2(self):
  265. return self._abort_tx('(y/N): ', 'y')
  266. def _xmr_autosign_op(
  267. self,
  268. op,
  269. desc = None,
  270. signable_desc = None,
  271. ext = None,
  272. wallet_arg = None,
  273. add_opts = [],
  274. wait_signed = False):
  275. if wait_signed:
  276. self._wait_signed(signable_desc)
  277. data = self.users['alice']
  278. args = (
  279. self.extra_opts
  280. + self.autosign_opts
  281. + (['--alice', '--compat'] if self.compat else [f'--wallet-dir={data.udir}'])
  282. + [f'--daemon=localhost:{data.md.rpc_port}']
  283. + add_opts
  284. + [op]
  285. + ([get_file_with_ext(self.asi.xmr_tx_dir, ext)] if ext else [])
  286. + ([wallet_arg] if wallet_arg else []))
  287. desc_pfx = f'{desc}, ' if desc else ''
  288. self.insert_device_online() # device must be removed by calling method
  289. return self.spawn('mmgen-xmrwallet', args, extra_desc=f'({desc_pfx}Alice)')
  290. def _sync_chkbal(self, wallet_arg, bal_chk_func):
  291. return self.sync_wallets(
  292. 'alice',
  293. op = 'sync',
  294. wallets = wallet_arg,
  295. bal_chk_func = bal_chk_func)
  296. def sync_chkbal1(self):
  297. return self._sync_chkbal('1', lambda n, b, ub: b == ub and 1 < b < 1.12)
  298. # 1.234567891234 - 0.124 = 1.110567891234 (minus fees)
  299. def sync_chkbal2(self):
  300. return self._sync_chkbal('1', lambda n, b, ub: b == ub and 0.8 < b < 0.86)
  301. # 1.234567891234 - 0.124 - 0.257 = 0.853567891234 (minus fees)
  302. def sync_chkbal3(self):
  303. return self._sync_chkbal(
  304. '1-2',
  305. lambda n, b, ub: b == ub and ((n == 1 and 0.8 < b < 0.86) or (n == 2 and b > 1.23)))
  306. async def submit_transfer_tx1(self):
  307. return await self._submit_transfer_tx()
  308. async def resubmit_transfer_tx1(self):
  309. return await self._submit_transfer_tx(
  310. relay_parm = self.tx_relay_daemon_proxy_parm,
  311. op = 'resubmit',
  312. check_bal = False)
  313. async def submit_transfer_tx2(self):
  314. return await self._submit_transfer_tx(relay_parm=self.tx_relay_daemon_parm)
  315. async def _submit_transfer_tx(self, relay_parm=None, ext=None, op='submit', check_bal=True):
  316. t = self._xmr_autosign_op(
  317. op = op,
  318. add_opts = [f'--tx-relay-daemon={relay_parm}'] if relay_parm else [],
  319. ext = ext,
  320. signable_desc = 'transaction',
  321. wait_signed = op == 'submit')
  322. t.expect(f'{op.capitalize()} transaction? (y/N): ', 'y')
  323. t.written_to_file('Submitted transaction')
  324. t.read()
  325. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  326. if check_bal:
  327. t.ok()
  328. return await self.mine_chk(
  329. 'alice', 1, 0,
  330. lambda x: 0 < x.ub < 1.234567891234,
  331. 'unlocked balance 0 < 1.234567891234')
  332. else:
  333. return t
  334. def _export_outputs(self, wallet_arg, op, add_opts=[]):
  335. t = self._xmr_autosign_op(
  336. op = op,
  337. wallet_arg = wallet_arg,
  338. add_opts = add_opts)
  339. t.written_to_file('Wallet outputs')
  340. t.read()
  341. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  342. return t
  343. def export_outputs1(self):
  344. return self._export_outputs('1', op='export-outputs')
  345. def export_outputs2(self): # NB: --rescan-spent does not work with testnet/stagenet
  346. return self._export_outputs('1', op='export-outputs-sign', add_opts=['--rescan-blockchain'])
  347. def export_outputs3(self):
  348. return self._export_outputs('1-2', op='export-outputs-sign')
  349. def _import_key_images(self, wallet_arg):
  350. t = self._xmr_autosign_op(
  351. op = 'import-key-images',
  352. wallet_arg = wallet_arg,
  353. signable_desc = 'wallet outputs',
  354. wait_signed = True)
  355. t.read()
  356. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  357. return t
  358. def import_key_images1(self):
  359. return self._import_key_images(None)
  360. def import_key_images2(self):
  361. return self._import_key_images(None)
  362. def txlist(self):
  363. self.insert_device_online()
  364. t = self.spawn('mmgen-xmrwallet', self.autosign_opts + ['txlist'])
  365. t.match_expect_list([
  366. 'SUBMITTED',
  367. 'Network', 'Submitted',
  368. 'transfer 1:0', '-> ext',
  369. 'transfer 1:0', '-> ext'
  370. ])
  371. t.read()
  372. self.remove_device_online()
  373. return t
  374. def txview(self):
  375. self.insert_device_online()
  376. t = self.spawn('mmgen-xmrwallet', self.autosign_opts + ['txview'])
  377. t.read()
  378. self.remove_device_online()
  379. return t
  380. def txview_all(self):
  381. self.spawn(msg_only=True)
  382. self.insert_device()
  383. self.do_mount()
  384. imsg(blue('Opening transaction directory: ') + cyan(f'{self.asi.xmr_tx_dir}'))
  385. for fn in self.asi.xmr_tx_dir.iterdir():
  386. imsg('\n' + brown(f'Viewing ‘{fn.name}’'))
  387. self.spawn('mmgen-xmrwallet', ['txview', str(fn)], no_msg=True).read()
  388. imsg('')
  389. self.do_umount()
  390. self.remove_device()
  391. return 'ok'
  392. def check_tx_dirs(self):
  393. self.insert_device()
  394. self.do_mount()
  395. before = '\n'.join(self._gen_listing())
  396. self.do_umount()
  397. self.remove_device()
  398. self.insert_device()
  399. t = self.spawn('mmgen-autosign', self.opts + ['clean'])
  400. t.read()
  401. self.remove_device()
  402. self.insert_device()
  403. self.do_mount()
  404. after = '\n'.join(self._gen_listing())
  405. self.do_umount()
  406. self.remove_device()
  407. imsg(f'\nBefore cleaning:\n{before}')
  408. imsg(f'\nAfter cleaning:\n{after}')
  409. pat = r'xmr/tx: \s*\S+\.subtx \S+\.subtx\s+xmr/outputs:\s*$'
  410. assert re.search(pat, after, re.DOTALL), f'regex search for {pat} failed'
  411. return t
  412. def view(self):
  413. return self.sync_wallets('alice', op='view', wallets='1')
  414. def listview(self):
  415. return self.sync_wallets('alice', op='listview', wallets='2')
  416. class CmdTestXMRAutosignNoCompat(CmdTestXMRAutosign):
  417. """
  418. Monero autosigning operations (non-xmrwallet compat mode)
  419. """
  420. compat = False
  421. class CmdTestXMRCompat(CmdTestXMRAutosign):
  422. """
  423. Monero autosigning operations (compat mode)
  424. """
  425. menu_prompt = 'efresh balances:\b'
  426. cmd_group = (
  427. ('autosign_setup', 'autosign setup with Alice’s seed'),
  428. ('autosign_xmr_setup', 'autosign setup (creation of Monero signing wallets)'),
  429. ('create_watchonly_wallets', 'creating Alice’s watch-only wallets'),
  430. ('gen_kafile_miner', 'generating key-address file for Miner'),
  431. ('create_wallet_miner', 'creating Monero wallet for Miner'),
  432. ('mine_initial_coins', 'mining initial coins'),
  433. ('fund_alice2', 'sending funds to Alice (wallet #2)'),
  434. ('check_bal_alice2', 'mining, checking balance (wallet #2)'),
  435. ('fund_alice1', 'sending funds to Alice (wallet #1)'),
  436. ('mine_blocks_10', 'mining some blocks'),
  437. ('alice_listaddresses1', 'adding label to Alice’s tracking wallets (listaddresses)'),
  438. ('fund_alice1b', 'sending funds to Alice (wallet #1)'),
  439. ('mine_blocks_10', 'mining some blocks'),
  440. ('alice_twview1', 'adding label to Alice’s tracking wallets (twview)'),
  441. )
  442. def __init__(self, cfg, trunner, cfgs, spawn):
  443. super().__init__(cfg, trunner, cfgs, spawn)
  444. if trunner is None:
  445. return
  446. self.alice_tw_dir = os.path.join(self.tr.data_dir, 'alice', 'altcoins', 'xmr', 'tracking-wallets')
  447. self.alice_dump_file = os.path.join(
  448. self.alice_tw_dir,
  449. '{}-2-MoneroWatchOnlyWallet.dump'.format(self.users['alice'].sid))
  450. self.alice_opts = [
  451. '--alice',
  452. '--coin=xmr',
  453. '--monero-wallet-rpc-password=passwOrd',
  454. f'--monero-daemon=localhost:{self.users["alice"].md.rpc_port}']
  455. def create_watchonly_wallets(self):
  456. return self._create_wallets()
  457. async def mine_blocks_10(self):
  458. return await self._mine_blocks(10)
  459. async def _mine_blocks(self, n):
  460. self.spawn(msg_only=True)
  461. return await self.mine(n)
  462. def alice_listaddresses1(self):
  463. return self._alice_twops(
  464. 'listaddresses',
  465. lbl_addr_num = 2,
  466. lbl_add_timestr = True,
  467. menu = 'R',
  468. expect_str = r'Primary account.*1\.234567891234')
  469. def alice_twview(self):
  470. return self._alice_twops('twview')
  471. def alice_twview1(self):
  472. return self._alice_twops(
  473. 'twview',
  474. lbl_addr_num = 1,
  475. lbl_addr_idx_num = 0,
  476. menu = 'R',
  477. expect_str = r'New Label.*2\.469135782468')
  478. def _alice_twops(
  479. self,
  480. op,
  481. *,
  482. lbl_addr_num = None,
  483. lbl_addr_idx_num = None,
  484. lbl_add_timestr = False,
  485. menu = '',
  486. expect_str = '',
  487. expect_arr = []):
  488. interactive = not expect_arr
  489. self.insert_device_online()
  490. t = self.spawn(
  491. 'mmgen-tool',
  492. self.alice_opts
  493. + self.autosign_opts
  494. + [op]
  495. + (['interactive=1'] if interactive else []))
  496. if interactive:
  497. if lbl_addr_num:
  498. t.expect(self.menu_prompt, 'l')
  499. t.expect('main menu): ', str(lbl_addr_num))
  500. if lbl_addr_idx_num is not None:
  501. t.expect('main menu): ', str(lbl_addr_idx_num))
  502. t.expect(': ', 'New Label\n')
  503. t.expect('(y/N): ', 'y' if lbl_add_timestr else 'n')
  504. for ch in menu:
  505. t.expect(self.menu_prompt, ch)
  506. if expect_str:
  507. t.expect(expect_str, regex=True)
  508. t.expect(self.menu_prompt, 'q')
  509. elif expect_arr:
  510. text = strip_ansi_escapes(t.read())
  511. for s in expect_arr:
  512. assert s in text
  513. self.remove_device_online()
  514. return t