xmr_autosign.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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_redo(self):
  220. return self.autosign_xmr_setup(write_viewkeys=False)
  221. def autosign_xmr_setup(self, write_viewkeys=True):
  222. self.insert_device_online()
  223. self.do_mount_online()
  224. self.asi_online.xmr_dir.mkdir(exist_ok=True)
  225. (self.asi_online.xmr_dir / 'old.vkeys').touch()
  226. self.do_umount_online()
  227. self.remove_device_online()
  228. self.insert_device()
  229. t = self.spawn('mmgen-autosign', self.opts + ['xmr_setup'], no_passthru_opts=True)
  230. if write_viewkeys:
  231. t.written_to_file('View keys')
  232. else:
  233. t.expect('already exists, skipping')
  234. t.read()
  235. self.remove_device()
  236. return t
  237. def restore_watchonly_wallets(self):
  238. return self._create_wallets('restore')
  239. def restore_wallets(self):
  240. return self._create_wallets('restore')
  241. def _create_wallets(self, op='create'):
  242. self.insert_device_online()
  243. t = self.create_wallets('alice', op=op)
  244. t.read() # required!
  245. self.remove_device_online()
  246. return t
  247. def _create_transfer_tx(self, amt, add_opts=[]):
  248. self.insert_device_online()
  249. t = self.do_op(
  250. 'transfer',
  251. 'alice',
  252. f'1:0:{self.burn_addr},{amt}',
  253. no_relay = True,
  254. do_ret = True,
  255. add_opts = add_opts)
  256. t.read() # required!
  257. self.remove_device_online()
  258. return t
  259. def create_transfer_tx1(self):
  260. return self._create_transfer_tx('0.124', add_opts=['--priority=2'])
  261. def create_transfer_tx2(self):
  262. return self._create_transfer_tx('0.257')
  263. create_transfer_tx2a = create_transfer_tx2
  264. def _abort_tx(self, expect, send=None, exit_val=None):
  265. self.insert_device_online()
  266. t = self.spawn('mmgen-xmrwallet', ['--autosign', 'abort'], exit_val=exit_val)
  267. t.expect(expect)
  268. if send:
  269. t.send(send)
  270. t.read() # required!
  271. self.remove_device_online()
  272. return t
  273. def abort_tx1(self):
  274. return self._abort_tx('No unsent transactions present', exit_val=2)
  275. def abort_tx2(self):
  276. return self._abort_tx('(y/N): ', 'y')
  277. def _xmr_autosign_op(
  278. self,
  279. op,
  280. desc = None,
  281. signable_desc = None,
  282. ext = None,
  283. wallet_arg = None,
  284. add_opts = [],
  285. wait_signed = False):
  286. if wait_signed:
  287. self._wait_signed(signable_desc)
  288. data = self.users['alice']
  289. args = (
  290. self.extra_opts
  291. + self.autosign_opts
  292. + (['--alice', '--compat'] if self.compat else [f'--wallet-dir={data.udir}'])
  293. + [f'--daemon=localhost:{data.md.rpc_port}']
  294. + add_opts
  295. + [op]
  296. + ([get_file_with_ext(self.asi.xmr_tx_dir, ext)] if ext else [])
  297. + ([wallet_arg] if wallet_arg else []))
  298. desc_pfx = f'{desc}, ' if desc else ''
  299. self.insert_device_online() # device must be removed by calling method
  300. return self.spawn('mmgen-xmrwallet', args, extra_desc=f'({desc_pfx}Alice)')
  301. def _sync_chkbal(self, wallet_arg, bal_chk_func):
  302. return self.sync_wallets(
  303. 'alice',
  304. op = 'sync',
  305. wallets = wallet_arg,
  306. bal_chk_func = bal_chk_func)
  307. def sync_chkbal1(self):
  308. return self._sync_chkbal('1', lambda n, b, ub: b == ub and 1 < b < 1.12)
  309. # 1.234567891234 - 0.124 = 1.110567891234 (minus fees)
  310. def sync_chkbal2(self):
  311. return self._sync_chkbal('1', lambda n, b, ub: b == ub and 0.8 < b < 0.86)
  312. # 1.234567891234 - 0.124 - 0.257 = 0.853567891234 (minus fees)
  313. def sync_chkbal3(self):
  314. return self._sync_chkbal(
  315. '1-2',
  316. lambda n, b, ub: b == ub and ((n == 1 and 0.8 < b < 0.86) or (n == 2 and b > 1.23)))
  317. async def submit_transfer_tx1(self):
  318. return await self._submit_transfer_tx()
  319. async def resubmit_transfer_tx1(self):
  320. return await self._submit_transfer_tx(
  321. relay_parm = self.tx_relay_daemon_proxy_parm,
  322. op = 'resubmit',
  323. check_bal = False)
  324. async def submit_transfer_tx2(self):
  325. return await self._submit_transfer_tx(relay_parm=self.tx_relay_daemon_parm)
  326. async def _submit_transfer_tx(self, relay_parm=None, ext=None, op='submit', check_bal=True):
  327. t = self._xmr_autosign_op(
  328. op = op,
  329. add_opts = [f'--tx-relay-daemon={relay_parm}'] if relay_parm else [],
  330. ext = ext,
  331. signable_desc = 'transaction',
  332. wait_signed = op == 'submit')
  333. t.expect(f'{op.capitalize()} transaction? (y/N): ', 'y')
  334. t.written_to_file('Submitted transaction')
  335. t.read()
  336. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  337. if check_bal:
  338. t.ok()
  339. return await self.mine_chk(
  340. 'alice', 1, 0,
  341. lambda x: 0 < x.ub < 1.234567891234,
  342. 'unlocked balance 0 < 1.234567891234')
  343. else:
  344. return t
  345. def _export_outputs(self, wallet_arg, op, add_opts=[]):
  346. t = self._xmr_autosign_op(
  347. op = op,
  348. wallet_arg = wallet_arg,
  349. add_opts = add_opts)
  350. t.written_to_file('Wallet outputs')
  351. t.read()
  352. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  353. return t
  354. def export_outputs1(self):
  355. return self._export_outputs('1', op='export-outputs')
  356. def export_outputs2(self): # NB: --rescan-spent does not work with testnet/stagenet
  357. return self._export_outputs('1', op='export-outputs-sign', add_opts=['--rescan-blockchain'])
  358. def export_outputs3(self):
  359. return self._export_outputs('1-2', op='export-outputs-sign')
  360. def _import_key_images(self, wallet_arg):
  361. t = self._xmr_autosign_op(
  362. op = 'import-key-images',
  363. wallet_arg = wallet_arg,
  364. signable_desc = 'wallet outputs',
  365. wait_signed = True)
  366. t.read()
  367. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  368. return t
  369. def import_key_images1(self):
  370. return self._import_key_images(None)
  371. def import_key_images2(self):
  372. return self._import_key_images(None)
  373. def txlist(self):
  374. self.insert_device_online()
  375. t = self.spawn('mmgen-xmrwallet', self.autosign_opts + ['txlist'])
  376. t.match_expect_list([
  377. 'SUBMITTED',
  378. 'Network', 'Submitted',
  379. 'transfer 1:0', '-> ext',
  380. 'transfer 1:0', '-> ext'
  381. ])
  382. t.read()
  383. self.remove_device_online()
  384. return t
  385. def txview(self):
  386. self.insert_device_online()
  387. t = self.spawn('mmgen-xmrwallet', self.autosign_opts + ['txview'])
  388. t.read()
  389. self.remove_device_online()
  390. return t
  391. def txview_all(self):
  392. self.spawn(msg_only=True)
  393. self.insert_device()
  394. self.do_mount()
  395. imsg(blue('Opening transaction directory: ') + cyan(f'{self.asi.xmr_tx_dir}'))
  396. for fn in self.asi.xmr_tx_dir.iterdir():
  397. imsg('\n' + brown(f'Viewing ‘{fn.name}’'))
  398. self.spawn('mmgen-xmrwallet', ['txview', str(fn)], no_msg=True).read()
  399. imsg('')
  400. self.do_umount()
  401. self.remove_device()
  402. return 'ok'
  403. def check_tx_dirs(self):
  404. self.insert_device()
  405. self.do_mount()
  406. before = '\n'.join(self._gen_listing())
  407. self.do_umount()
  408. self.remove_device()
  409. self.insert_device()
  410. t = self.spawn('mmgen-autosign', self.opts + ['clean'])
  411. t.read()
  412. self.remove_device()
  413. self.insert_device()
  414. self.do_mount()
  415. after = '\n'.join(self._gen_listing())
  416. self.do_umount()
  417. self.remove_device()
  418. imsg(f'\nBefore cleaning:\n{before}')
  419. imsg(f'\nAfter cleaning:\n{after}')
  420. pat = r'xmr/tx: \s*\S+\.subtx \S+\.subtx\s+xmr/outputs:\s*$'
  421. assert re.search(pat, after, re.DOTALL), f'regex search for {pat} failed'
  422. return t
  423. def view(self):
  424. return self.sync_wallets('alice', op='view', wallets='1')
  425. def listview(self):
  426. return self.sync_wallets('alice', op='listview', wallets='2')
  427. class CmdTestXMRAutosignNoCompat(CmdTestXMRAutosign):
  428. """
  429. Monero autosigning operations (non-xmrwallet compat mode)
  430. """
  431. compat = False
  432. class CmdTestXMRCompat(CmdTestXMRAutosign):
  433. """
  434. Monero autosigning operations (compat mode)
  435. """
  436. menu_prompt = 'efresh balances:\b'
  437. listaddresses_menu_prompt = 'efresh bals:\b'
  438. extra_daemons = ['ltc']
  439. cmd_group = (
  440. ('autosign_setup', 'autosign setup with Alice’s seed'),
  441. ('autosign_xmr_setup', 'autosign setup (creation of Monero signing wallets)'),
  442. ('addrimport_alice', 'creating (importing) Alice’s watch-only wallets from key-address file'),
  443. ('addrimport_alice2', 'reimporting Alice’s watch-only wallets from key-address file'),
  444. ('gen_kafile_miner', 'generating key-address file for Miner'),
  445. ('create_wallet_miner', 'creating Monero wallet for Miner'),
  446. ('mine_initial_coins', 'mining initial coins'),
  447. ('fund_alice2', 'sending funds to Alice (wallet #2)'),
  448. ('check_bal_alice2', 'mining, checking balance (wallet #2)'),
  449. ('fund_alice1', 'sending funds to Alice (wallet #1)'),
  450. ('mine_blocks_10', 'mining some blocks'),
  451. ('alice_listaddresses_lbl', 'adding label to Alice’s tracking wallets (listaddresses)'),
  452. ('fund_alice1b', 'sending funds to Alice (wallet #1)'),
  453. ('mine_blocks_10', 'mining some blocks'),
  454. ('alice_twview1', 'adding label to Alice’s tracking wallets (twview)'),
  455. ('new_account_alice', 'adding an account to Alice’s wallet'),
  456. ('new_address_alice', 'adding an address to Alice’s wallet'),
  457. ('new_address_alice_label', 'adding an address to Alice’s wallet (with label)'),
  458. ('alice_dump', 'dumping alice’s wallets to JSON format'),
  459. ('fund_alice_sub1', 'sending funds to Alice’s subaddress #1 (wallet #2)'),
  460. ('mine_blocks_1', 'mining a block'),
  461. ('fund_alice_sub2', 'sending funds to Alice’s subaddress #2 (wallet #2)'),
  462. ('mine_blocks_1', 'mining a block'),
  463. ('fund_alice_sub3', 'sending funds to Alice’s subaddress #3 (wallet #2)'),
  464. ('alice_twview2', 'viewing Alice’s tracking wallets (reload, sort options)'),
  465. ('alice_twview_chk1', 'viewing Alice’s tracking wallets (check balances)'),
  466. ('alice_listaddresses_sort', 'listing Alice’s addresses (sort options)'),
  467. ('wait_loop_start_compat', 'starting autosign wait loop in XMR compat mode [--coins=xmr]'),
  468. ('alice_txcreate1', 'creating a transaction'),
  469. ('alice_txabort1', 'aborting the transaction'),
  470. ('alice_txcreate2', 'recreating the transaction'),
  471. ('wait_signed1', 'autosigning the transaction'),
  472. ('wait_loop_kill', 'stopping autosign wait loop'),
  473. ('alice_txabort2', 'aborting the raw and signed transactions'),
  474. ('alice_txcreate3', 'recreating the transaction'),
  475. ('wait_loop_start_ltc', 'starting autosign wait loop in XMR compat mode [--coins=ltc,xmr]'),
  476. ('alice_txsend1', 'sending the transaction'),
  477. ('alice_txstatus1', 'getting the transaction status (in mempool)'),
  478. ('mine_blocks_10', 'mining some blocks'),
  479. ('alice_txstatus2', 'getting the transaction status (confirmations)'),
  480. ('alice_txstatus3', 'getting the transaction status (verbose)'),
  481. ('alice_twview_chk2', 'viewing Alice’s tracking wallets (check balances)'),
  482. ('alice_txcreate_sweep1', 'creating a sweep transaction (account sweep)'),
  483. ('alice_txsend2', 'sending the transaction'),
  484. ('mine_blocks_10', 'mining some blocks'),
  485. ('alice_twview_chk3', 'viewing Alice’s tracking wallets (check balances)'),
  486. ('alice_txcreate_sweep2', 'creating a sweep transaction (address sweep)'),
  487. ('alice_txsend3', 'sending the transaction'),
  488. ('mine_blocks_10', 'mining some blocks'),
  489. ('alice_twview_chk4', 'viewing Alice’s tracking wallets (check balances)'),
  490. ('wait_loop_kill', 'stopping autosign wait loop'),
  491. ('delete_offline_shmdir', 'deleting offline autosign wallet dir'),
  492. ('autosign_setup', 'autosign setup with Alice’s seed'),
  493. ('autosign_xmr_setup_redo', 'autosign setup (creation of Monero signing wallets, redo)'),
  494. ('wait_loop_start_compat', 'starting autosign wait loop in XMR compat mode [--coins=xmr]'),
  495. ('alice_txcreate_sweep3', 'creating a sweep transaction (sweep to empty account)'),
  496. ('alice_txsend4', 'sending the transaction'),
  497. ('mine_blocks_10', 'mining some blocks'),
  498. ('alice_twview_chk5', 'viewing Alice’s tracking wallets (check balances)'),
  499. ('wait_loop_kill', 'stopping autosign wait loop'),
  500. ('alice_newacct1', 'adding account to Alice’s tracking wallet (dfl label)'),
  501. ('alice_newacct2', 'adding account to Alice’s tracking wallet (no timestr)'),
  502. ('alice_newacct3', 'adding account to Alice’s tracking wallet'),
  503. ('alice_newacct4', 'adding account to Alice’s tracking wallet (dfl label, no timestr)'),
  504. ('alice_newaddr1', 'adding address to Alice’s tracking wallet'),
  505. ('alice_newaddr2', 'adding address to Alice’s tracking wallet (no timestr)'),
  506. ('alice_newaddr3', 'adding address to Alice’s tracking wallet (dfl label)'),
  507. ('alice_newaddr4', 'adding address to Alice’s tracking wallet (dfl label, no timestr)'),
  508. ('stop_daemons', 'stopping all wallet and coin daemons'),
  509. )
  510. def __init__(self, cfg, trunner, cfgs, spawn):
  511. super().__init__(cfg, trunner, cfgs, spawn)
  512. if trunner is None:
  513. return
  514. self.alice_tw_dir = os.path.join(self.tr.data_dir, 'alice', 'altcoins', 'xmr', 'tracking-wallets')
  515. self.alice_dump_file = os.path.join(
  516. self.alice_tw_dir,
  517. '{}-2-MoneroWatchOnlyWallet.dump'.format(self.users['alice'].sid))
  518. self.alice_daemon_opts = [
  519. f'--monero-daemon=localhost:{self.users["alice"].md.rpc_port}',
  520. '--monero-wallet-rpc-password=passwOrd']
  521. self.alice_opts = ['--alice', '--coin=xmr'] + self.alice_daemon_opts
  522. def addrimport_alice(self):
  523. return self._addrimport_alice(create_address_files=True)
  524. def addrimport_alice2(self):
  525. return self._addrimport_alice(expect_str='Skipping.*Skipping')
  526. def _addrimport_alice(self, *, expect_str=None, create_address_files=False):
  527. self.insert_device_online()
  528. t = self.spawn('mmgen-addrimport', self.alice_opts + self.autosign_opts)
  529. if expect_str:
  530. t.expect(expect_str, regex=True)
  531. if create_address_files:
  532. self._create_address_files(t, 'alice')
  533. t.read() # required!
  534. self.remove_device_online()
  535. return t
  536. async def mine_blocks_1(self):
  537. return await self._mine_blocks(1)
  538. async def mine_blocks_10(self):
  539. return await self._mine_blocks(10)
  540. async def _mine_blocks(self, n):
  541. self.spawn(msg_only=True)
  542. return await self.mine(n)
  543. def _new_addr_alice(self, *args):
  544. return self.new_addr_alice(*args, do_autosign=True)
  545. async def alice_dump(self):
  546. t = self._xmr_autosign_op('dump')
  547. t.read()
  548. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  549. return t
  550. async def fund_alice_sub1(self):
  551. return await self._fund_alice(1, 9876543210)
  552. async def fund_alice_sub2(self):
  553. return await self._fund_alice(2, 8765432109)
  554. async def fund_alice_sub3(self):
  555. return await self._fund_alice(3, 7654321098)
  556. async def _fund_alice(self, addr_num, amt):
  557. data = json.loads(read_from_file(self.alice_dump_file))
  558. addr_data = data['MoneroMMGenWalletDumpFile']['data']['wallet_metadata'][1]['addresses']
  559. return await self.fund_alice(addr=addr_data[addr_num-1]['address'], amt=amt)
  560. def alice_newacct1(self):
  561. return self._alice_newacct(2, lbl_text='New Test Account', add_timestr=True)
  562. def alice_newacct2(self):
  563. return self._alice_newacct(1, lbl_text='New Test Account')
  564. def alice_newacct3(self):
  565. return self._alice_newacct(2, add_timestr=True)
  566. def alice_newacct4(self):
  567. return self._alice_newacct(2)
  568. def _alice_newacct(self, wallet_num, lbl_text='', add_timestr=False):
  569. return self._alice_twops(
  570. 'listaddresses',
  571. newacct_wallet_num = wallet_num,
  572. lbl_text = lbl_text,
  573. add_timestr = add_timestr,
  574. expect_str = (lbl_text or 'new account ') + (r'.*\[20.*\]' if add_timestr else ''))
  575. def alice_newaddr1(self):
  576. return self._alice_newaddr(1, lbl_text='New Test Address', add_timestr=True)
  577. def alice_newaddr2(self):
  578. return self._alice_newaddr(1, lbl_text='New Test Address')
  579. def alice_newaddr3(self):
  580. return self._alice_newaddr(2, add_timestr=True)
  581. def alice_newaddr4(self):
  582. return self._alice_newaddr(2)
  583. def _alice_newaddr(self, acct_num, lbl_text='', add_timestr=False):
  584. return self._alice_twops(
  585. 'listaddresses',
  586. newaddr_acct_num = acct_num,
  587. lbl_text = lbl_text,
  588. add_timestr = add_timestr,
  589. expect_str = (lbl_text or 'new address ') + (r'.*\[20.*\]' if add_timestr else ''))
  590. def alice_listaddresses(self):
  591. return self._alice_twops('listaddresses', menu='R')
  592. def alice_listaddresses_sort(self):
  593. return self._alice_twops('listaddresses', menu='aAdMELLuuuraAdMeEuu')
  594. def alice_listaddresses_lbl(self):
  595. return self._alice_twops(
  596. 'listaddresses',
  597. lbl_acct_num = 2,
  598. lbl_addr_idx = 0,
  599. lbl_text = 'New Test Label',
  600. add_timestr = True,
  601. menu = 'R',
  602. expect_str = r'Primary account.*1\.234567891234')
  603. def alice_twview(self):
  604. return self._alice_twops('twview')
  605. def alice_twview1(self):
  606. return self._alice_twops(
  607. 'twview',
  608. lbl_acct_num = 1,
  609. lbl_addr_idx = 0,
  610. lbl_text = 'New Test Label',
  611. menu = 'R',
  612. expect_str = r'New Test Label.*2\.469135782468')
  613. def alice_twview2(self):
  614. return self._alice_twops('twview', menu='RaAdMraAdMe')
  615. def alice_twview_chk1(self):
  616. return self._alice_twview_chk([
  617. 'Total XMR: 3.722345649021 [3.729999970119]',
  618. '1 0.026296296417',
  619. '0.007654321098'])
  620. def alice_twview_chk2(self):
  621. return self._alice_twview_chk(['Total XMR: 3.715053370119'], sync=True)
  622. def alice_twview_chk3(self):
  623. return self._alice_twview_chk(['Total XMR: 3.713242570119', '1.232757091234'], sync=True)
  624. def alice_twview_chk4(self):
  625. return self._alice_twview_chk(['Total XMR: 3.709050970119', '1.254861787651'], sync=True)
  626. def alice_twview_chk5(self):
  627. return self._alice_twview_chk(['Total XMR: 3.707234170119', '2.452375982468'], sync=True)
  628. def _alice_twview_chk(self, expect_arr, sync=False):
  629. return self._alice_twops(
  630. 'twview',
  631. interactive = sync,
  632. menu = 'R' if sync else '',
  633. expect_arr = expect_arr)
  634. def _alice_twops(
  635. self,
  636. op,
  637. *,
  638. lbl_acct_num = None,
  639. lbl_addr_idx = None,
  640. newacct_wallet_num = None,
  641. newaddr_acct_num = None,
  642. lbl_text = '',
  643. add_timestr = False,
  644. menu = '',
  645. interactive = True,
  646. expect_str = '',
  647. expect_arr = []):
  648. self.insert_device_online()
  649. t = self.spawn(
  650. 'mmgen-tool',
  651. self.alice_opts
  652. + self.autosign_opts
  653. + [op]
  654. + (['interactive=1'] if interactive else []))
  655. menu_prompt = self.listaddresses_menu_prompt if op == 'listaddresses' else self.menu_prompt
  656. have_lbl = lbl_acct_num or newacct_wallet_num or newaddr_acct_num
  657. have_new_addr = newacct_wallet_num or newaddr_acct_num
  658. if interactive:
  659. if lbl_acct_num:
  660. t.expect(menu_prompt, 'l')
  661. t.expect('main menu): ', str(lbl_acct_num))
  662. t.expect('main menu): ', str(lbl_addr_idx))
  663. elif newacct_wallet_num:
  664. t.expect(menu_prompt, 'N')
  665. t.expect('number, or ENTER to return to main menu> ', f'{newacct_wallet_num}\n')
  666. elif newaddr_acct_num:
  667. t.expect(menu_prompt, 'n')
  668. t.expect('main menu): ', str(newaddr_acct_num))
  669. if have_lbl:
  670. t.expect(': ', lbl_text + '\n') # add label
  671. t.expect('(y/N): ', ('y' if add_timestr else 'n')) # add timestr
  672. if have_new_addr:
  673. t.expect('(y/N): ', 'y')
  674. for ch in menu:
  675. t.expect(menu_prompt, ch)
  676. if expect_str:
  677. t.expect(expect_str, regex=True)
  678. t.expect(menu_prompt, 'q')
  679. elif expect_arr:
  680. text = strip_ansi_escapes(t.read())
  681. for s in expect_arr:
  682. assert s in text
  683. t.read()
  684. self.remove_device_online()
  685. return t
  686. def wait_loop_start_compat(self):
  687. return self.wait_loop_start(opts=['--xmrwallet-compat', '--coins=xmr'])
  688. def wait_loop_start_ltc(self):
  689. return self.wait_loop_start(opts=['--xmrwallet-compat', '--coins=ltc,xmr'])
  690. def alice_txcreate1(self):
  691. return self._alice_txops('txcreate', [f'{self.burn_addr},0.012345'], acct_num=1)
  692. def alice_txcreate_sweep1(self):
  693. return self._alice_txops('txcreate', menu='S', sweep_menu='23', sweep_type='sweep')
  694. def alice_txcreate_sweep2(self):
  695. return self._alice_txops('txcreate', menu='s', sweep_menu='3', sweep_type='sweep_all')
  696. def alice_txcreate_sweep3(self):
  697. return self._alice_txops('txcreate', menu='S', sweep_menu='12', sweep_type='sweep')
  698. alice_txcreate3 = alice_txcreate2 = alice_txcreate1
  699. def _alice_txabort(self):
  700. return self._alice_txops('txsend', opts=['--alice', '--abort'])
  701. alice_txabort1 = alice_txabort2 = _alice_txabort
  702. def _alice_txsend(self):
  703. return self._alice_txops(
  704. 'txsend',
  705. opts = ['--alice', '--quiet'],
  706. add_opts = self.alice_daemon_opts,
  707. wait_signed = True)
  708. alice_txsend1 = alice_txsend2 = alice_txsend3 = alice_txsend4 = _alice_txsend
  709. def alice_txstatus1(self):
  710. return self._alice_txstatus(expect_str='TxID: .* in mempool')
  711. def alice_txstatus2(self):
  712. return self._alice_txstatus(['--quiet'], expect_str='confirmations')
  713. def alice_txstatus3(self):
  714. return self._alice_txstatus(['--verbose'], expect_str='confirmations.*Info for transaction')
  715. def _alice_txstatus(self, add_opts=[], expect_str=None):
  716. return self._alice_txops(
  717. 'txsend',
  718. opts = ['--alice', '--status'],
  719. add_opts = self.alice_daemon_opts + add_opts,
  720. expect_str = expect_str)
  721. def wait_signed1(self):
  722. self.spawn(msg_only=True)
  723. oqmsg('')
  724. self._wait_signed('transaction')
  725. return 'silent'
  726. def _alice_txops(
  727. self,
  728. op,
  729. args = [],
  730. *,
  731. opts = [],
  732. add_opts = [],
  733. menu = '',
  734. acct_num = None,
  735. wait_signed = False,
  736. sweep_type = None,
  737. sweep_menu = '',
  738. signable_desc = 'transaction',
  739. expect_str = None):
  740. if wait_signed:
  741. self._wait_signed(signable_desc)
  742. self.insert_device_online()
  743. t = self.spawn(f'mmgen-{op}', (opts or self.alice_opts) + self.autosign_opts + add_opts + args)
  744. if '--abort' in opts:
  745. t.expect('(y/N): ', 'y')
  746. elif op == 'txcreate':
  747. if sweep_type:
  748. t.expect(self.menu_prompt, menu)
  749. for ch in sweep_menu:
  750. t.expect('main menu): ', ch)
  751. t.expect('number> ', {'sweep': '1', 'sweep_all': '2'}[sweep_type])
  752. t.expect('(y/N): ', 'y') # create new address?
  753. else:
  754. for ch in menu + 'q':
  755. t.expect(self.menu_prompt, ch)
  756. t.expect('to spend from: ', f'{acct_num}\n')
  757. t.expect('(y/N): ', 'y') # save?
  758. elif op == 'txsend' and not '--status' in opts:
  759. t.expect('(y/N): ', 'y') # view?
  760. if expect_str:
  761. t.expect(expect_str, regex=True)
  762. t.read() # required!
  763. self.remove_device_online()
  764. return t