ts_xmr_autosign.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2023 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
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. test.test_py_d.ts_xmr_autosign: xmr autosigning tests for the test.py test suite
  12. """
  13. from .ts_xmrwallet import *
  14. from pathlib import Path
  15. from .ts_autosign import TestSuiteAutosignBase
  16. def make_burn_addr():
  17. from mmgen.tool.coin import tool_cmd
  18. return tool_cmd(
  19. cfg = cfg,
  20. cmdname = 'privhex2addr',
  21. proto = cfg._proto,
  22. mmtype = 'monero' ).privhex2addr('beadcafe'*8)
  23. class TestSuiteXMRAutosign(TestSuiteXMRWallet,TestSuiteAutosignBase):
  24. """
  25. Monero autosigning operations
  26. """
  27. tmpdir_nums = [39]
  28. # ts_xmrwallet attrs:
  29. user_data = (
  30. ('miner', '98831F3A', False, 130, '1', []),
  31. ('alice', 'FE3C6545', True, 150, '1-2', []),
  32. )
  33. # ts_autosign attrs:
  34. coins = ['xmr']
  35. daemon_coins = []
  36. txfile_coins = []
  37. live = False
  38. simulate = False
  39. bad_tx_count = 0
  40. tx_relay_user = 'miner'
  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. ('clean', 'cleaning signable file directories'),
  50. ('autosign_setup', 'autosign setup with Alice’s seed'),
  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_alice', 'sending funds to Alice'),
  57. ('create_transfer_tx1', 'creating a transfer TX'),
  58. ('sign_transfer_tx1', 'signing the transfer TX'),
  59. ('submit_transfer_tx1', 'submitting the transfer TX'),
  60. ('create_transfer_tx2', 'creating a transfer TX (for relaying via proxy)'),
  61. ('sign_transfer_tx2', 'signing the transfer TX (for relaying via proxy)'),
  62. ('submit_transfer_tx2', 'submitting the transfer TX (relaying via proxy)'),
  63. ('list_wallets', 'listing Alice’s wallets and checking balance'),
  64. ('dump_wallets', 'dumping Alice’s wallets'),
  65. ('delete_wallets', 'deleting Alice’s wallets'),
  66. ('restore_wallets', 'creating online (watch-only) wallets for Alice'),
  67. ('delete_dump_files', 'deleting Alice’s dump files'),
  68. ('export_outputs', 'exporting outputs from Alice’s watch-only wallets'),
  69. ('export_key_images', 'exporting signed key images from Alice’s offline wallets'),
  70. ('import_key_images', 'importing signed key images into Alice’s online wallets'),
  71. ('list_wallets', 'listing Alice’s wallets and checking balance'),
  72. )
  73. def __init__(self,trunner,cfgs,spawn):
  74. TestSuiteXMRWallet.__init__(self,trunner,cfgs,spawn)
  75. TestSuiteAutosignBase.__init__(self,trunner,cfgs,spawn)
  76. if trunner == None:
  77. return
  78. from mmgen.cfg import Config
  79. self.cfg = Config({
  80. 'coin': 'XMR',
  81. 'outdir': self.users['alice'].udir,
  82. 'wallet_dir': self.users['alice'].udir,
  83. 'wallet_rpc_password': 'passwOrd',
  84. })
  85. self.burn_addr = make_burn_addr()
  86. self.opts.append('--xmrwallets={}'.format( self.users['alice'].kal_range )) # mmgen-autosign opts
  87. self.autosign_opts = [f'--autosign-mountpoint={self.mountpoint}'] # mmgen-xmrwallet opts
  88. self.tx_count = 1
  89. def create_tmp_wallets(self):
  90. self.spawn('',msg_only=True)
  91. data = self.users['alice']
  92. from mmgen.wallet import Wallet
  93. from mmgen.xmrwallet import MoneroWalletOps,xmrwallet_uargs
  94. silence()
  95. kal = KeyAddrList(
  96. cfg = self.cfg,
  97. proto = self.proto,
  98. addr_idxs = '1-2',
  99. seed = Wallet(cfg,data.mmwords).seed )
  100. kal.file.write(ask_overwrite=False)
  101. fn = get_file_with_ext(data.udir,'akeys')
  102. m = MoneroWalletOps.create(
  103. self.cfg,
  104. xmrwallet_uargs(fn, '1-2', None))
  105. async_run(m.main())
  106. async_run(m.stop_wallet_daemon())
  107. end_silence()
  108. return 'ok'
  109. def _new_addr_alice(self,*args):
  110. data = self.users['alice']
  111. return self.new_addr_alice(
  112. *args,
  113. kafile = get_file_with_ext(data.udir,'akeys') )
  114. def new_account_alice(self):
  115. return self._new_addr_alice(
  116. '2',
  117. 'start',
  118. fr'Creating new account.*Index:\s+{self.na_idx}\s')
  119. def new_address_alice(self):
  120. return self._new_addr_alice(
  121. '2:1',
  122. 'continue',
  123. fr'Account index:\s+1\s+Creating new address' )
  124. def new_address_alice_label(self):
  125. return self._new_addr_alice(
  126. '2:1,Alice’s new address',
  127. 'stop',
  128. fr'Account index:\s+1\s+Creating new address.*Alice’s new address' )
  129. def dump_tmp_wallets(self):
  130. return self._dump_wallets(autosign=False)
  131. def dump_wallets(self):
  132. return self._dump_wallets(autosign=True)
  133. def _dump_wallets(self,autosign):
  134. data = self.users['alice']
  135. t = self.spawn(
  136. 'mmgen-xmrwallet',
  137. self.extra_opts
  138. + [f'--wallet-dir={data.udir}', f'--daemon=localhost:{data.md.rpc_port}']
  139. + (self.autosign_opts if autosign else [])
  140. + ['dump']
  141. + ([] if autosign else [get_file_with_ext(data.udir,'akeys')]) )
  142. t.expect('2 wallets dumped')
  143. return t
  144. def _delete_files(self,*ext_list):
  145. data = self.users['alice']
  146. self.spawn('',msg_only=True)
  147. for ext in ext_list:
  148. get_file_with_ext(data.udir,ext,no_dot=True,delete_all=True)
  149. return 'ok'
  150. def delete_tmp_wallets(self):
  151. return self._delete_files( 'MoneroWallet', 'MoneroWallet.keys', '.akeys' )
  152. def delete_wallets(self):
  153. return self._delete_files( 'MoneroWatchOnlyWallet', '.keys', '.address.txt' )
  154. def delete_tmp_dump_files(self):
  155. return self._delete_files( '.dump' )
  156. def delete_dump_files(self):
  157. return self._delete_files( '.dump' )
  158. def autosign_setup(self):
  159. Path(self.autosign_xmr_dir).mkdir(parents=True,exist_ok=True)
  160. Path(self.autosign_xmr_dir,'old.vkeys').touch()
  161. t = self.run_setup(
  162. mn_type = 'mmgen',
  163. mn_file = self.users['alice'].mmwords,
  164. use_dfl_wallet = None )
  165. t.expect('Continue with Monero setup? (Y/n): ','y')
  166. t.written_to_file('View keys')
  167. return t
  168. def create_watchonly_wallets(self):
  169. return self.create_wallets( 'alice', op='restore' )
  170. def restore_wallets(self):
  171. return self.create_wallets( 'alice', op='restore' )
  172. def list_wallets(self):
  173. return self.sync_wallets(
  174. 'alice',
  175. op = 'list',
  176. bal_chk_func = lambda n,bal: (0.83 < bal < 0.8536) if n == 0 else True )
  177. # 1.234567891234 - 0.124 - 0.257 = 0.853567891234 (minus fees)
  178. def _create_transfer_tx(self,amt):
  179. return self.do_op('transfer','alice',f'1:0:{self.burn_addr},{amt}',no_relay=True,do_ret=True)
  180. def create_transfer_tx1(self):
  181. return self._create_transfer_tx('0.124')
  182. def create_transfer_tx2(self):
  183. get_file_with_ext(self.asi.xmr_tx_dir,'rawtx',delete_all=True)
  184. get_file_with_ext(self.asi.xmr_tx_dir,'sigtx',delete_all=True)
  185. return self._create_transfer_tx('0.257')
  186. def _sign_transfer_tx(self):
  187. return self.do_sign(['--full-summary'],tx_name='Monero transaction')
  188. def sign_transfer_tx1(self):
  189. return self._sign_transfer_tx()
  190. def sign_transfer_tx2(self):
  191. return self._sign_transfer_tx()
  192. def _xmr_autosign_op(self,op,desc,dtype=None,ext=None,wallet_arg=None,add_opts=[]):
  193. data = self.users['alice']
  194. args = (
  195. self.extra_opts
  196. + self.autosign_opts
  197. + [f'--wallet-dir={data.udir}']
  198. + ([f'--daemon=localhost:{data.md.rpc_port}'] if not op == 'submit' else [])
  199. + add_opts
  200. + [ op ]
  201. + ([get_file_with_ext(self.asi.xmr_tx_dir,ext)] if ext else [])
  202. + ([wallet_arg] if wallet_arg else [])
  203. )
  204. t = self.spawn( 'mmgen-xmrwallet', args, extra_desc=f'({desc}, Alice)' )
  205. if dtype:
  206. t.written_to_file(dtype.capitalize())
  207. return t
  208. def submit_transfer_tx1(self):
  209. return self._submit_transfer_tx( self.tx_relay_daemon_parm, ext='sigtx' )
  210. def submit_transfer_tx2(self):
  211. return self._submit_transfer_tx( self.tx_relay_daemon_proxy_parm, ext=None )
  212. def _submit_transfer_tx(self,relay_parm,ext):
  213. t = self._xmr_autosign_op(
  214. op = 'submit',
  215. desc = 'submitting TX',
  216. add_opts = [f'--tx-relay-daemon={relay_parm}'],
  217. ext = ext )
  218. t.expect( 'Submit transaction? (y/N): ', 'y' )
  219. t.written_to_file('Submitted transaction')
  220. t.ok()
  221. return self.mine_chk(
  222. 'alice', 1, 0,
  223. lambda x: 0 < x < 1.234567891234,
  224. 'unlocked balance 0 < 1.234567891234' )
  225. def export_outputs(self):
  226. return self._xmr_autosign_op(
  227. op = 'export-outputs',
  228. desc = 'exporting outputs',
  229. dtype = 'wallet outputs',
  230. wallet_arg = '1-2' )
  231. def export_key_images(self):
  232. self.tx_count = 2
  233. return self.do_sign(['--full-summary'],tx_name='Monero wallet outputs file')
  234. def import_key_images(self):
  235. return self._xmr_autosign_op(
  236. op = 'import-key-images',
  237. desc = 'importing key images' )
  238. def create_fake_tx_files(self):
  239. imsg('Creating fake transaction files')
  240. self.asi.msg_dir.mkdir(exist_ok=True)
  241. self.asi.xmr_dir.mkdir(exist_ok=True)
  242. self.asi.xmr_tx_dir.mkdir(exist_ok=True)
  243. self.asi.xmr_outputs_dir.mkdir(exist_ok=True)
  244. for fn in (
  245. 'a.rawtx', 'a.sigtx',
  246. 'b.rawtx', 'b.sigtx',
  247. 'c.rawtx',
  248. 'd.sigtx',
  249. ):
  250. (self.asi.tx_dir / fn).touch()
  251. for fn in (
  252. 'a.rawmsg.json', 'a.sigmsg.json',
  253. 'b.rawmsg.json',
  254. 'c.sigmsg.json',
  255. 'd.rawmsg.json', 'd.sigmsg.json',
  256. ):
  257. (self.asi.msg_dir / fn).touch()
  258. for fn in (
  259. 'a.rawtx', 'a.sigtx', 'a.subtx',
  260. 'b.rawtx', 'b.sigtx',
  261. 'c.subtx',
  262. 'd.rawtx', 'd.subtx',
  263. 'e.rawtx',
  264. 'f.sigtx','f.subtx',
  265. ):
  266. (self.asi.xmr_tx_dir / fn).touch()
  267. for fn in (
  268. 'a.raw', 'a.sig',
  269. 'b.raw',
  270. 'c.sig',
  271. ):
  272. (self.asi.xmr_outputs_dir / fn).touch()
  273. return 'ok'
  274. def clean(self):
  275. def gen_listing():
  276. for k in ('tx_dir','msg_dir','xmr_tx_dir','xmr_outputs_dir'):
  277. d = getattr(self.asi,k)
  278. if d.is_dir():
  279. yield '{:12} {}'.format(
  280. str(Path(*d.parts[4:])) + ':',
  281. ' '.join(sorted(i.name for i in d.iterdir()))).strip()
  282. self.create_fake_tx_files()
  283. before = '\n'.join(gen_listing())
  284. t = self.spawn( 'mmgen-autosign', [f'--mountpoint={self.mountpoint}','clean'] )
  285. out = t.read()
  286. after = '\n'.join(gen_listing())
  287. chk = """
  288. tx: a.sigtx b.sigtx c.rawtx d.sigtx
  289. msg: a.sigmsg.json b.rawmsg.json c.sigmsg.json d.sigmsg.json
  290. xmr/tx: a.subtx b.sigtx c.subtx d.subtx e.rawtx f.subtx
  291. xmr/outputs:
  292. """
  293. shutil.rmtree(self.asi.mountpoint)
  294. self.asi.tx_dir.mkdir(parents=True)
  295. imsg(f'\nBefore cleaning:\n{before}')
  296. imsg(f'\nAfter cleaning:\n{after}')
  297. assert '13 files shredded' in out
  298. assert after + '\n' == fmt(chk), f'\n{after}\n!=\n{fmt(chk)}'
  299. return t