ct_xmr_autosign.py 14 KB

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