ct_xmr_autosign.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 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.ct_xmr_autosign: xmr autosigning tests for the cmdtest.py test suite
  12. """
  13. import re
  14. from mmgen.color import blue, cyan, brown
  15. from mmgen.util import async_run
  16. from ..include.common import cfg, imsg, silence, end_silence
  17. from .common import get_file_with_ext
  18. from .ct_xmrwallet import CmdTestXMRWallet
  19. from .ct_autosign import CmdTestAutosignThreaded
  20. def make_burn_addr():
  21. from mmgen.tool.coin import tool_cmd
  22. return tool_cmd(
  23. cfg = cfg,
  24. cmdname = 'privhex2addr',
  25. proto = cfg._proto,
  26. mmtype = 'monero').privhex2addr('beadcafe'*8)
  27. class CmdTestXMRAutosign(CmdTestXMRWallet, CmdTestAutosignThreaded):
  28. """
  29. Monero autosigning operations
  30. """
  31. tmpdir_nums = [39]
  32. # ct_xmrwallet attrs:
  33. tx_relay_user = 'miner'
  34. # user sid autosign shift kal_range add_coind_args
  35. user_data = (
  36. ('miner', '98831F3A', False, 130, '1', []),
  37. ('alice', 'FE3C6545', True, 150, '1-2', []),
  38. )
  39. # ct_autosign attrs:
  40. coins = ['xmr']
  41. cmd_group = (
  42. ('daemon_version', 'checking daemon version'),
  43. ('gen_kafile_miner', 'generating key-address file for Miner'),
  44. ('create_wallet_miner', 'creating Monero wallet for Miner'),
  45. ('mine_initial_coins', 'mining initial coins'),
  46. ('create_tmp_wallets', 'creating temporary online wallets for Alice'),
  47. ('new_account_alice', 'adding an account to Alice’s tmp wallet'),
  48. ('new_address_alice', 'adding an address to Alice’s tmp wallet'),
  49. ('new_address_alice_label', 'adding an address to Alice’s tmp wallet (with label)'),
  50. ('dump_tmp_wallets', 'dumping Alice’s tmp wallets'),
  51. ('delete_tmp_wallets', 'deleting Alice’s tmp wallets'),
  52. ('autosign_setup', 'autosign setup with Alice’s seed'),
  53. ('autosign_xmr_setup', 'autosign setup (creation of Monero signing wallets)'),
  54. ('create_watchonly_wallets', 'creating watch-only wallets from Alice’s wallet dumps'),
  55. ('delete_tmp_dump_files', 'deleting Alice’s dump files'),
  56. ('fund_alice1', 'sending funds to Alice (wallet #1)'),
  57. ('check_bal_alice1', 'mining, checking balance (wallet #1)'),
  58. ('fund_alice2', 'sending funds to Alice (wallet #2)'),
  59. ('check_bal_alice2', 'mining, checking balance (wallet #2)'),
  60. ('wait_loop_start', 'starting autosign wait loop'),
  61. ('export_outputs1', 'exporting outputs from Alice’s watch-only wallet #1'),
  62. ('create_transfer_tx1', 'creating a transfer TX'),
  63. ('submit_transfer_tx1', 'submitting the transfer TX'),
  64. ('resubmit_transfer_tx1', 'resubmitting the transfer TX'),
  65. ('export_outputs2', 'exporting outputs from Alice’s watch-only wallet #1'),
  66. ('import_key_images1', 'importing signed key images into Alice’s online wallets'),
  67. ('sync_chkbal1', 'syncing Alice’s wallet #1'),
  68. ('abort_tx1', 'aborting the current transaction (error)'),
  69. ('create_transfer_tx2', 'creating a transfer TX (for relaying via proxy)'),
  70. ('abort_tx2', 'aborting the current transaction (OK, unsigned)'),
  71. ('create_transfer_tx2a', 'creating the transfer TX again'),
  72. ('submit_transfer_tx2', 'submitting the transfer TX (relaying via proxy)'),
  73. ('sync_chkbal2', 'syncing Alice’s wallets and checking balance'),
  74. ('dump_wallets', 'dumping Alice’s wallets'),
  75. ('delete_wallets', 'deleting Alice’s wallets'),
  76. ('restore_wallets', 'creating online (watch-only) wallets for Alice'),
  77. ('delete_dump_files', 'deleting Alice’s dump files'),
  78. ('export_outputs3', 'exporting outputs from Alice’s watch-only wallets'),
  79. ('import_key_images2', 'importing signed key images into Alice’s online wallets'),
  80. ('sync_chkbal3', 'syncing Alice’s wallets and checking balance'),
  81. ('wait_loop_kill', 'stopping autosign wait loop'),
  82. ('stop_daemons', 'stopping all wallet and coin daemons'),
  83. ('view', 'viewing Alice’s wallet in offline mode (wallet #1)'),
  84. ('listview', 'list-viewing Alice’s wallet in offline mode (wallet #2)'),
  85. ('txlist', 'listing Alice’s submitted transactions'),
  86. ('txview', 'viewing Alice’s submitted transactions'),
  87. ('txview_all', 'viewing all raw, signed and submitted transactions'),
  88. ('check_tx_dirs', 'cleaning and checking signable file directories'),
  89. )
  90. def __init__(self, trunner, cfgs, spawn):
  91. CmdTestAutosignThreaded.__init__(self, trunner, cfgs, spawn)
  92. CmdTestXMRWallet.__init__(self, trunner, cfgs, spawn)
  93. if trunner is None:
  94. return
  95. from mmgen.cfg import Config
  96. self.cfg = Config({
  97. 'coin': 'XMR',
  98. 'outdir': self.users['alice'].udir,
  99. 'wallet_dir': self.users['alice'].udir,
  100. 'wallet_rpc_password': 'passwOrd',
  101. 'test_suite': True,
  102. })
  103. self.burn_addr = make_burn_addr()
  104. self.opts.append('--xmrwallets={}'.format(self.users['alice'].kal_range)) # mmgen-autosign opts
  105. self.autosign_opts = ['--autosign'] # mmgen-xmrwallet opts
  106. self.spawn_env['MMGEN_TEST_SUITE_XMR_AUTOSIGN'] = '1'
  107. def create_tmp_wallets(self):
  108. self.spawn('', msg_only=True)
  109. data = self.users['alice']
  110. from mmgen.wallet import Wallet
  111. from mmgen.xmrwallet import op
  112. from mmgen.addrlist import KeyAddrList
  113. silence()
  114. kal = KeyAddrList(
  115. cfg = self.cfg,
  116. proto = self.proto,
  117. addr_idxs = '1-2',
  118. seed = Wallet(cfg, data.mmwords).seed,
  119. skip_chksum_msg = True,
  120. key_address_validity_check = False)
  121. kal.file.write(ask_overwrite=False)
  122. fn = get_file_with_ext(data.udir, 'akeys')
  123. m = op('create', self.cfg, fn, '1-2')
  124. async_run(m.main())
  125. async_run(m.stop_wallet_daemon())
  126. end_silence()
  127. return 'ok'
  128. def _new_addr_alice(self, *args):
  129. data = self.users['alice']
  130. return self.new_addr_alice(
  131. *args,
  132. kafile = get_file_with_ext(data.udir, 'akeys'))
  133. def new_account_alice(self):
  134. return self._new_addr_alice(
  135. '2',
  136. 'start',
  137. r'Creating new account for wallet .*2.* with label .*‘xmrwallet new account .*y/N\): ')
  138. def new_address_alice(self):
  139. return self._new_addr_alice(
  140. '2:1',
  141. 'continue',
  142. r'Creating new address for wallet .*2.*, account .*#1.* with label .*‘xmrwallet new address .*y/N\): ')
  143. def new_address_alice_label(self):
  144. return self._new_addr_alice(
  145. '2:1,Alice’s new address',
  146. 'stop',
  147. r'Creating new address for wallet .*2.*, account .*#1.* with label .*‘Alice’s new address .*y/N\): ')
  148. def dump_tmp_wallets(self):
  149. return self._dump_wallets(autosign=False)
  150. def dump_wallets(self):
  151. return self._dump_wallets(autosign=True)
  152. def _dump_wallets(self, autosign):
  153. data = self.users['alice']
  154. self.insert_device_online()
  155. t = self.spawn(
  156. 'mmgen-xmrwallet',
  157. self.extra_opts
  158. + [f'--wallet-dir={data.udir}', f'--daemon=localhost:{data.md.rpc_port}']
  159. + (self.autosign_opts if autosign else [])
  160. + ['dump']
  161. + ([] if autosign else [get_file_with_ext(data.udir, 'akeys')]))
  162. t.expect('2 wallets dumped')
  163. t.read()
  164. self.remove_device_online()
  165. return t
  166. def _delete_files(self, *ext_list):
  167. data = self.users['alice']
  168. self.spawn('', msg_only=True)
  169. for ext in ext_list:
  170. get_file_with_ext(data.udir, ext, no_dot=True, delete_all=True)
  171. return 'ok'
  172. def delete_tmp_wallets(self):
  173. return self._delete_files('MoneroWallet', 'MoneroWallet.keys', '.akeys')
  174. def delete_wallets(self):
  175. return self._delete_files('MoneroWatchOnlyWallet', '.keys', '.address.txt')
  176. def delete_tmp_dump_files(self):
  177. return self._delete_files('.dump')
  178. def gen_kafile_miner(self):
  179. return self.gen_kafiles(['miner'])
  180. def create_wallet_miner(self):
  181. return self.create_wallets_miner()
  182. def delete_dump_files(self):
  183. return self._delete_files('.dump')
  184. def fund_alice1(self):
  185. return self.fund_alice(wallet=1)
  186. def check_bal_alice1(self):
  187. return self.check_bal_alice(wallet=1)
  188. def fund_alice2(self):
  189. return self.fund_alice(wallet=2)
  190. def check_bal_alice2(self):
  191. return self.check_bal_alice(wallet=2)
  192. def autosign_setup(self):
  193. return self.run_setup(
  194. mn_type = 'mmgen',
  195. mn_file = self.users['alice'].mmwords,
  196. use_dfl_wallet = None,
  197. expect_args = ['Continue with Monero setup? (Y/n): ', 'n'])
  198. def autosign_xmr_setup(self):
  199. self.insert_device_online()
  200. self.do_mount_online()
  201. self.asi_online.xmr_dir.mkdir(exist_ok=True)
  202. (self.asi_online.xmr_dir / 'old.vkeys').touch()
  203. self.do_umount_online()
  204. self.remove_device_online()
  205. self.insert_device()
  206. t = self.spawn('mmgen-autosign', self.opts + ['xmr_setup'], no_passthru_opts=True)
  207. t.written_to_file('View keys')
  208. t.read()
  209. self.remove_device()
  210. return t
  211. def create_watchonly_wallets(self):
  212. return self.restore_wallets()
  213. def restore_wallets(self):
  214. self.insert_device_online()
  215. t = self.create_wallets('alice', op='restore')
  216. t.read() # required!
  217. self.remove_device_online()
  218. return t
  219. def _create_transfer_tx(self, amt, add_opts=[]):
  220. self.insert_device_online()
  221. t = self.do_op(
  222. 'transfer',
  223. 'alice',
  224. f'1:0:{self.burn_addr},{amt}',
  225. no_relay = True,
  226. do_ret = True,
  227. add_opts = add_opts)
  228. t.read() # required!
  229. self.remove_device_online()
  230. return t
  231. def create_transfer_tx1(self):
  232. return self._create_transfer_tx('0.124', add_opts=['--priority=2'])
  233. def create_transfer_tx2(self):
  234. return self._create_transfer_tx('0.257')
  235. create_transfer_tx2a = create_transfer_tx2
  236. def _abort_tx(self, expect, send=None, exit_val=None):
  237. self.insert_device_online()
  238. t = self.spawn('mmgen-xmrwallet', ['--autosign', 'abort'], exit_val=exit_val)
  239. t.expect(expect)
  240. if send:
  241. t.send(send)
  242. t.read() # required!
  243. self.remove_device_online()
  244. return t
  245. def abort_tx1(self):
  246. return self._abort_tx('No unsent transactions present', exit_val=2)
  247. def abort_tx2(self):
  248. return self._abort_tx('(y/N): ', 'y')
  249. def _xmr_autosign_op(
  250. self,
  251. op,
  252. desc = None,
  253. signable_desc = None,
  254. ext = None,
  255. wallet_arg = None,
  256. add_opts = [],
  257. wait_signed = False):
  258. if wait_signed:
  259. self._wait_signed(signable_desc)
  260. data = self.users['alice']
  261. args = (
  262. self.extra_opts
  263. + self.autosign_opts
  264. + [f'--wallet-dir={data.udir}', f'--daemon=localhost:{data.md.rpc_port}']
  265. + add_opts
  266. + [op]
  267. + ([get_file_with_ext(self.asi.xmr_tx_dir, ext)] if ext else [])
  268. + ([wallet_arg] if wallet_arg else [])
  269. )
  270. desc_pfx = f'{desc}, ' if desc else ''
  271. self.insert_device_online() # device must be removed by calling method
  272. return self.spawn('mmgen-xmrwallet', args, extra_desc=f'({desc_pfx}Alice)')
  273. def _sync_chkbal(self, wallet_arg, bal_chk_func):
  274. return self.sync_wallets(
  275. 'alice',
  276. op = 'sync',
  277. wallets = wallet_arg,
  278. bal_chk_func = bal_chk_func)
  279. def sync_chkbal1(self):
  280. return self._sync_chkbal('1', lambda n, b, ub: b == ub and 1 < b < 1.12)
  281. # 1.234567891234 - 0.124 = 1.110567891234 (minus fees)
  282. def sync_chkbal2(self):
  283. return self._sync_chkbal('1', lambda n, b, ub: b == ub and 0.8 < b < 0.86)
  284. # 1.234567891234 - 0.124 - 0.257 = 0.853567891234 (minus fees)
  285. def sync_chkbal3(self):
  286. return self._sync_chkbal(
  287. '1-2',
  288. lambda n, b, ub: b == ub and ((n == 1 and 0.8 < b < 0.86) or (n == 2 and b > 1.23)))
  289. def _mine_chk(self, desc):
  290. bal_type = {'locked':'b', 'unlocked':'ub'}[desc]
  291. return self.mine_chk(
  292. 'alice', 1, 0,
  293. lambda x: 0 < getattr(x, bal_type) < 1.234567891234,
  294. f'{desc} balance 0 < 1.234567891234')
  295. def submit_transfer_tx1(self):
  296. return self._submit_transfer_tx()
  297. def resubmit_transfer_tx1(self):
  298. return self._submit_transfer_tx(
  299. relay_parm = self.tx_relay_daemon_proxy_parm,
  300. op = 'resubmit',
  301. check_bal = False)
  302. def submit_transfer_tx2(self):
  303. return self._submit_transfer_tx(relay_parm=self.tx_relay_daemon_parm)
  304. def _submit_transfer_tx(self, relay_parm=None, ext=None, op='submit', check_bal=True):
  305. t = self._xmr_autosign_op(
  306. op = op,
  307. add_opts = [f'--tx-relay-daemon={relay_parm}'] if relay_parm else [],
  308. ext = ext,
  309. signable_desc = 'transaction',
  310. wait_signed = op == 'submit')
  311. t.expect(f'{op.capitalize()} transaction? (y/N): ', 'y')
  312. t.written_to_file('Submitted transaction')
  313. t.read()
  314. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  315. if check_bal:
  316. t.ok()
  317. return self._mine_chk('unlocked')
  318. else:
  319. return t
  320. def _export_outputs(self, wallet_arg, op, add_opts=[]):
  321. t = self._xmr_autosign_op(
  322. op = op,
  323. wallet_arg = wallet_arg,
  324. add_opts = add_opts)
  325. t.written_to_file('Wallet outputs')
  326. t.read()
  327. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  328. return t
  329. def export_outputs1(self):
  330. return self._export_outputs('1', op='export-outputs')
  331. def export_outputs2(self): # NB: --rescan-spent does not work with testnet/stagenet
  332. return self._export_outputs('1', op='export-outputs-sign', add_opts=['--rescan-blockchain'])
  333. def export_outputs3(self):
  334. return self._export_outputs('1-2', op='export-outputs-sign')
  335. def _import_key_images(self, wallet_arg):
  336. t = self._xmr_autosign_op(
  337. op = 'import-key-images',
  338. wallet_arg = wallet_arg,
  339. signable_desc = 'wallet outputs',
  340. wait_signed = True)
  341. t.read()
  342. self.remove_device_online() # device was inserted by _xmr_autosign_op()
  343. return t
  344. def import_key_images1(self):
  345. return self._import_key_images(None)
  346. def import_key_images2(self):
  347. return self._import_key_images(None)
  348. def txlist(self):
  349. self.insert_device_online()
  350. t = self.spawn('mmgen-xmrwallet', self.autosign_opts + ['txlist'])
  351. t.match_expect_list([
  352. 'SUBMITTED',
  353. 'Network', 'Submitted',
  354. 'transfer 1:0', '-> ext',
  355. 'transfer 1:0', '-> ext'
  356. ])
  357. t.read()
  358. self.remove_device_online()
  359. return t
  360. def txview(self):
  361. self.insert_device_online()
  362. t = self.spawn('mmgen-xmrwallet', self.autosign_opts + ['txview'])
  363. t.read()
  364. self.remove_device_online()
  365. return t
  366. def txview_all(self):
  367. self.spawn('', msg_only=True)
  368. self.insert_device()
  369. self.do_mount()
  370. imsg(blue('Opening transaction directory: ') + cyan(f'{self.asi.xmr_tx_dir}'))
  371. for fn in self.asi.xmr_tx_dir.iterdir():
  372. imsg('\n' + brown(f'Viewing ‘{fn.name}’'))
  373. self.spawn('mmgen-xmrwallet', ['txview', str(fn)], no_msg=True).read()
  374. imsg('')
  375. self.do_umount()
  376. self.remove_device()
  377. return 'ok'
  378. def check_tx_dirs(self):
  379. self.insert_device()
  380. self.do_mount()
  381. before = '\n'.join(self._gen_listing())
  382. self.do_umount()
  383. self.remove_device()
  384. self.insert_device()
  385. t = self.spawn('mmgen-autosign', self.opts + ['clean'])
  386. t.read()
  387. self.remove_device()
  388. self.insert_device()
  389. self.do_mount()
  390. after = '\n'.join(self._gen_listing())
  391. self.do_umount()
  392. self.remove_device()
  393. imsg(f'\nBefore cleaning:\n{before}')
  394. imsg(f'\nAfter cleaning:\n{after}')
  395. pat = r'xmr/tx: \s*\S+\.subtx \S+\.subtx\s+xmr/outputs:\s*$'
  396. assert re.search(pat, after, re.DOTALL), f'regex search for {pat} failed'
  397. return t
  398. def view(self):
  399. return self.sync_wallets('alice', op='view', wallets='1')
  400. def listview(self):
  401. return self.sync_wallets('alice', op='listview', wallets='2')