xmr_autosign.py 24 KB

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