xmr_autosign.py 31 KB

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