xmr_autosign.py 22 KB

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