autosign.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test.cmdtest_d.autosign: Autosign tests for the cmdtest.py test suite
  20. """
  21. import sys, os, time, shutil, atexit
  22. from subprocess import run, DEVNULL
  23. from pathlib import Path
  24. from mmgen.cfg import Config
  25. from mmgen.color import red, blue, yellow, cyan, orange, purple, gray
  26. from mmgen.util import msg, suf, die, indent, fmt
  27. from mmgen.led import LEDControl
  28. from mmgen.autosign import Autosign, Signable
  29. from ..include.common import (
  30. omsg,
  31. omsg_r,
  32. oqmsg,
  33. oqmsg_r,
  34. start_test_daemons,
  35. stop_test_daemons,
  36. joinpath,
  37. imsg,
  38. read_from_file,
  39. silence,
  40. end_silence,
  41. VirtBlockDevice,
  42. )
  43. from .include.common import ref_dir, dfl_words_file, dfl_bip39_file
  44. from .base import CmdTestBase
  45. from .include.input import stealth_mnemonic_entry
  46. class CmdTestAutosignBase(CmdTestBase):
  47. networks = ('btc',)
  48. tmpdir_nums = [18]
  49. color = True
  50. platform_skip = ('win32',)
  51. threaded = False
  52. daemon_coins = []
  53. def __init__(self, cfg, trunner, cfgs, spawn):
  54. CmdTestBase.__init__(self, cfg, trunner, cfgs, spawn)
  55. if trunner is None:
  56. return
  57. self.silent_mount = self.live or self.tr.quiet
  58. self.network_ids = [c+'_tn' for c in self.daemon_coins] + self.daemon_coins
  59. self._create_autosign_instances(create_dirs=not cfg.skipping_deps)
  60. self.fs_image_path = Path(self.tmpdir).absolute() / 'removable_device_image'
  61. if sys.platform == 'linux':
  62. self.txdev = VirtBlockDevice(self.fs_image_path, '10M')
  63. if not (cfg.skipping_deps or self.live):
  64. self._create_removable_device()
  65. if sys.platform == 'darwin' and not cfg.no_daemon_stop:
  66. atexit.register(self._macOS_eject_disk, self.asi.dev_label)
  67. self.opts = ['--coins='+','.join(self.coins)]
  68. self.txhex_file = f'{self.tmpdir}/tx_dump.hex'
  69. if not self.live:
  70. self.spawn_env['MMGEN_TEST_SUITE_ROOT_PFX'] = self.tmpdir
  71. if self.threaded:
  72. self.spawn_env['MMGEN_TEST_SUITE_AUTOSIGN_THREADED'] = '1'
  73. def __del__(self):
  74. if hasattr(self, 'have_dummy_control_files'):
  75. db = LEDControl.boards['dummy']
  76. for fn in (db.control, db.trigger):
  77. run(f'sudo rm -f {fn}'.split(), check=True)
  78. if hasattr(self, 'txdev'):
  79. del self.txdev
  80. if not self.cfg.no_daemon_stop:
  81. if sys.platform == 'darwin':
  82. for label in (self.asi.dev_label, self.asi.ramdisk.label):
  83. self._macOS_eject_disk(label)
  84. def _create_autosign_instances(self, create_dirs):
  85. d = {'offline': {'name':'asi'}}
  86. if self.have_online:
  87. d['online'] = {'name':'asi_online'}
  88. for subdir, data in d.items():
  89. asi = Autosign(
  90. Config({
  91. 'coins': ','.join(self.coins),
  92. 'test_suite': True,
  93. 'test_suite_xmr_autosign': self.name == 'CmdTestXMRAutosign',
  94. 'test_suite_autosign_threaded': self.threaded,
  95. 'test_suite_root_pfx': None if self.live else self.tmpdir,
  96. 'online': subdir == 'online',
  97. }))
  98. if create_dirs and not self.live:
  99. for k in ('mountpoint', 'shm_dir', 'wallet_dir'):
  100. if subdir == 'online' and k in ('shm_dir', 'wallet_dir'):
  101. continue
  102. if sys.platform == 'darwin' and k != 'mountpoint':
  103. continue
  104. getattr(asi, k).mkdir(parents=True, exist_ok=True)
  105. if sys.platform == 'darwin' and k == 'mountpoint':
  106. asi.mountpoint.rmdir()
  107. continue
  108. setattr(self, data['name'], asi)
  109. def _set_e2label(self, label):
  110. imsg(f'Setting label to {label}')
  111. for cmd in ('/sbin/e2label', 'e2label'):
  112. try:
  113. run([cmd, str(self.txdev.img_path), label], check=True)
  114. break
  115. except:
  116. if cmd == 'e2label':
  117. raise
  118. def _create_removable_device(self):
  119. match sys.platform:
  120. case 'linux':
  121. self.txdev.create()
  122. self.txdev.attach(silent=True)
  123. args = [
  124. '-E', 'root_owner={}:{}'.format(os.getuid(), os.getgid()),
  125. '-L', self.asi.dev_label,
  126. str(self.txdev.img_path)]
  127. redir = DEVNULL
  128. for cmd in ('/sbin/mkfs.ext2', 'mkfs.ext2'):
  129. try:
  130. run([cmd] + args, stdout=redir, stderr=redir, check=True)
  131. break
  132. except:
  133. if cmd == 'mkfs.ext2':
  134. raise
  135. self.txdev.detach(silent=True)
  136. case 'darwin':
  137. cmd = [
  138. 'hdiutil', 'create', '-size', '10M', '-fs', 'exFAT',
  139. '-volname', self.asi.dev_label,
  140. str(self.fs_image_path)]
  141. redir = DEVNULL if self.tr.quiet else None
  142. run(cmd, stdout=redir, check=True)
  143. def _macOS_mount_fs_image(self, loc):
  144. time.sleep(0.2)
  145. run(
  146. ['hdiutil', 'attach', '-mountpoint', str(loc.mountpoint), f'{self.fs_image_path}.dmg'],
  147. stdout=DEVNULL, check=True)
  148. def _macOS_eject_disk(self, label):
  149. try:
  150. run(['diskutil' , 'eject', label], stdout=DEVNULL, stderr=DEVNULL)
  151. except:
  152. pass
  153. def start_daemons(self):
  154. self.spawn(msg_only=True)
  155. start_test_daemons(*self.network_ids)
  156. return 'ok'
  157. def stop_daemons(self):
  158. self.spawn(msg_only=True)
  159. stop_test_daemons(*self.network_ids, remove_datadir=True)
  160. return 'ok'
  161. def run_setup(
  162. self,
  163. mn_type = None,
  164. mn_file = None,
  165. use_dfl_wallet = False,
  166. seed_len = None,
  167. usr_entry_modes = False,
  168. wallet_passwd = None,
  169. keylist_passwd = None,
  170. add_opts = [],
  171. expect_args = []):
  172. mn_desc = mn_type or 'default'
  173. mn_type = mn_type or 'mmgen'
  174. if sys.platform == 'darwin' and not self.cfg.no_daemon_stop:
  175. self._macOS_eject_disk(self.asi.ramdisk.label)
  176. self.insert_device()
  177. t = self.spawn(
  178. 'mmgen-autosign',
  179. self.opts + add_opts
  180. + ([] if mn_desc == 'default' else [f'--mnemonic-fmt={mn_type}'])
  181. + ([f'--seed-len={seed_len}'] if seed_len else [])
  182. + ['setup'],
  183. no_passthru_opts = True)
  184. if use_dfl_wallet:
  185. t.expect('Use default wallet for autosigning? (Y/n): ', 'y')
  186. t.passphrase('MMGen wallet', wallet_passwd)
  187. else:
  188. if use_dfl_wallet is not None: # None => no dfl wallet present
  189. t.expect('Use default wallet for autosigning? (Y/n): ', 'n')
  190. mn_file = mn_file or {'mmgen': dfl_words_file, 'bip39': dfl_bip39_file}[mn_type]
  191. mn = read_from_file(mn_file).strip().split()
  192. if not seed_len:
  193. t.expect('words: ', {12:'1', 18:'2', 24:'3'}[len(mn)])
  194. t.expect('OK? (Y/n): ', '\n')
  195. from mmgen.mn_entry import mn_entry
  196. entry_mode = 'full'
  197. mne = mn_entry(self.cfg, mn_type, entry_mode=entry_mode)
  198. if usr_entry_modes:
  199. t.expect('user-configured')
  200. else:
  201. t.expect(
  202. 'Type a number.*: ',
  203. str(mne.entry_modes.index(entry_mode) + 1),
  204. regex = True)
  205. stealth_mnemonic_entry(t, mne, mn, entry_mode)
  206. t.written_to_file('Autosign wallet')
  207. if expect_args:
  208. t.expect(*expect_args)
  209. if keylist_passwd:
  210. t.passphrase('keylist data', keylist_passwd)
  211. t.expect('(y/N): ', 'y')
  212. t.read()
  213. self.remove_device()
  214. if sys.platform == 'darwin' and not self.cfg.no_daemon_stop:
  215. atexit.register(self._macOS_eject_disk, self.asi.ramdisk.label)
  216. return t
  217. def insert_device(self, asi='asi'):
  218. if self.live:
  219. return
  220. loc = getattr(self, asi)
  221. match sys.platform:
  222. case 'linux':
  223. self._set_e2label(loc.dev_label)
  224. self.txdev.attach()
  225. for _ in range(20):
  226. if loc.device_inserted:
  227. break
  228. time.sleep(0.1)
  229. else:
  230. die(2, f'device insert timeout exceeded {loc.dev_label}')
  231. case 'darwin':
  232. self._macOS_mount_fs_image(loc)
  233. def remove_device(self, asi='asi'):
  234. if self.live:
  235. return
  236. loc = getattr(self, asi)
  237. match sys.platform:
  238. case 'linux':
  239. self.txdev.detach()
  240. for _ in range(20):
  241. if not loc.device_inserted:
  242. break
  243. time.sleep(0.1)
  244. else:
  245. die(2, f'device remove timeout exceeded {loc.dev_label}')
  246. case 'darwin':
  247. self._macOS_eject_disk(loc.dev_label)
  248. def _mount_ops(self, loc, cmd, *args, **kwargs):
  249. return getattr(getattr(self, loc), cmd)(*args, silent=self.silent_mount, **kwargs)
  250. def do_mount(self, *args, **kwargs):
  251. return self._mount_ops('asi', 'do_mount', *args, **kwargs)
  252. def do_umount(self, *args, **kwargs):
  253. return self._mount_ops('asi', 'do_umount', *args, **kwargs)
  254. def _gen_listing(self):
  255. for k in self.asi.dirs:
  256. d = getattr(self.asi, k)
  257. if d.is_dir():
  258. yield '{:12} {}'.format(
  259. str(Path(*d.parts[6:])) + ':',
  260. ' '.join(sorted(i.name for i in d.iterdir()))).strip()
  261. class CmdTestAutosignClean(CmdTestAutosignBase):
  262. 'autosign directory cleaning operations'
  263. have_online = False
  264. live = False
  265. simulate_led = True
  266. no_insert_check = False
  267. coins = ['btc']
  268. tmpdir_nums = [38]
  269. cmd_group = (
  270. ('clean_no_xmr', 'cleaning signable file directories (no XMR)'),
  271. ('clean_xmr_only', 'cleaning signable file directories (XMR-only)'),
  272. ('clean_all', 'cleaning signable file directories (with XMR)'),
  273. )
  274. def create_fake_tx_files(self):
  275. imsg('Creating fake transaction files')
  276. if not self.asi.xmr_only:
  277. for fn in (
  278. 'a.rawtx', 'a.sigtx',
  279. 'b.rawtx', 'b.sigtx',
  280. 'c.rawtx',
  281. 'd.sigtx',
  282. ):
  283. (self.asi.tx_dir / fn).touch()
  284. for fn in (
  285. 'a.arawtx', 'a.asigtx', 'a.asubtx',
  286. 'b.arawtx', 'b.asigtx',
  287. 'c.asubtx',
  288. 'd.arawtx', 'd.asubtx',
  289. 'e.arawtx',
  290. 'f.asigtx', 'f.asubtx',
  291. ):
  292. (self.asi.txauto_dir / fn).touch()
  293. for fn in (
  294. 'a.rawmsg.json', 'a.sigmsg.json',
  295. 'b.rawmsg.json',
  296. 'c.sigmsg.json',
  297. 'd.rawmsg.json', 'd.sigmsg.json',
  298. ):
  299. (self.asi.msg_dir / fn).touch()
  300. if self.asi.have_xmr:
  301. for fn in (
  302. 'a.rawtx', 'a.sigtx', 'a.subtx',
  303. 'b.rawtx', 'b.sigtx',
  304. 'c.subtx',
  305. 'd.rawtx', 'd.subtx',
  306. 'e.rawtx',
  307. 'f.sigtx', 'f.subtx',
  308. ):
  309. (self.asi.xmr_tx_dir / fn).touch()
  310. for fn in (
  311. 'a.raw', 'a.sig',
  312. 'b.raw',
  313. 'c.sig',
  314. ):
  315. (self.asi.xmr_outputs_dir / fn).touch()
  316. return 'ok'
  317. def clean_no_xmr(self):
  318. return self._clean('btc,ltc,eth')
  319. def clean_xmr_only(self):
  320. self.asi = Autosign(Config({'_clone': self.asi.cfg, 'coins': 'xmr'}))
  321. return self._clean('xmr')
  322. def clean_all(self):
  323. self.asi = Autosign(Config({'_clone': self.asi.cfg, 'coins': 'xmr,btc,bch,eth'}))
  324. return self._clean('xmr,btc,bch,eth')
  325. def _clean(self, coins):
  326. self.spawn(msg_only=True)
  327. self.insert_device()
  328. silence()
  329. self.do_mount()
  330. end_silence()
  331. self.create_fake_tx_files()
  332. before = '\n'.join(self._gen_listing())
  333. t = self.spawn('mmgen-autosign', [f'--coins={coins}', 'clean'], no_msg=True)
  334. out = t.read()
  335. if sys.platform == 'darwin':
  336. self.insert_device()
  337. silence()
  338. self.do_mount()
  339. end_silence()
  340. after = '\n'.join(self._gen_listing())
  341. chk_non_xmr = """
  342. tx: a.sigtx b.sigtx c.rawtx d.sigtx
  343. txauto: a.asubtx b.asigtx c.asubtx d.asubtx e.arawtx f.asubtx
  344. msg: a.sigmsg.json b.rawmsg.json c.sigmsg.json d.sigmsg.json
  345. """
  346. chk_xmr = """
  347. xmr: outputs tx
  348. xmr/tx: a.subtx b.sigtx c.subtx d.subtx e.rawtx f.subtx
  349. xmr/outputs:
  350. """
  351. chk = ''
  352. shred_count = 0
  353. if not self.asi.xmr_only:
  354. for k in ('tx_dir', 'txauto_dir', 'msg_dir'):
  355. shutil.rmtree(getattr(self.asi, k))
  356. chk += chk_non_xmr.rstrip()
  357. shred_count += 9
  358. if self.asi.have_xmr:
  359. shutil.rmtree(self.asi.xmr_dir)
  360. chk += chk_xmr.rstrip()
  361. shred_count += 9
  362. self.do_umount()
  363. self.remove_device()
  364. imsg(f'\nBefore cleaning:\n{before}')
  365. imsg(f'\nAfter cleaning:\n{after}')
  366. assert f'{shred_count} files shredded' in out
  367. assert after + '\n' == fmt(chk), f'\n{after}\n!=\n{fmt(chk)}'
  368. return t
  369. class CmdTestAutosignThreaded(CmdTestAutosignBase):
  370. have_online = True
  371. live = False
  372. no_insert_check = False
  373. threaded = True
  374. def _user_txcreate(
  375. self,
  376. user,
  377. progname = 'txcreate',
  378. ui_handler = None,
  379. chg_addr = None,
  380. opts = [],
  381. output_args = [],
  382. exit_val = 0,
  383. inputs = '1',
  384. tweaks = [],
  385. expect_str = None,
  386. data_arg = None,
  387. need_rbf = False):
  388. if output_args:
  389. assert not chg_addr
  390. if chg_addr:
  391. assert not output_args
  392. if need_rbf and not self.proto.cap('rbf'):
  393. return 'skip'
  394. def do_return():
  395. if expect_str:
  396. t.expect(expect_str)
  397. t.read()
  398. self.remove_device_online()
  399. return t
  400. self.insert_device_online()
  401. sid = self._user_sid(user)
  402. t = self.spawn(
  403. f'mmgen-{progname}',
  404. opts
  405. + [f'--{user}', '--autosign']
  406. + ([data_arg] if data_arg else [])
  407. + (output_args or [f'{self.burn_addr},1.23456', f'{sid}:{chg_addr}']),
  408. exit_val = exit_val or None)
  409. if exit_val:
  410. return do_return()
  411. t = (ui_handler or self.txcreate_ui_common)(
  412. t,
  413. inputs = inputs,
  414. interactive_fee = '32s',
  415. tweaks = tweaks,
  416. file_desc = 'Unsigned automount transaction')
  417. return do_return()
  418. def _user_txsend(
  419. self,
  420. user,
  421. *,
  422. comment = None,
  423. no_wait = False,
  424. need_rbf = False,
  425. dump_hex = False,
  426. mark_sent = False):
  427. if need_rbf and not self.proto.cap('rbf'):
  428. return 'skip'
  429. if not no_wait:
  430. self._wait_signed('transaction')
  431. extra_opt = (
  432. [f'--dump-hex={self.txhex_file}'] if dump_hex
  433. else ['--mark-sent'] if mark_sent
  434. else [])
  435. self.insert_device_online()
  436. t = self.spawn('mmgen-txsend',
  437. [f'--{user}', '--quiet', '--autosign'] + extra_opt, no_passthru_opts=['coin'])
  438. if mark_sent:
  439. t.written_to_file('Sent automount transaction')
  440. else:
  441. t.view_tx('t')
  442. t.do_comment(comment)
  443. if dump_hex:
  444. t.written_to_file('Serialized transaction hex data')
  445. else:
  446. self._do_confirm_send(t, quiet=True)
  447. t.written_to_file('Sent automount transaction')
  448. t.read()
  449. self.remove_device_online()
  450. return t
  451. def _wait_loop_start(self, add_opts=[]):
  452. t = self.spawn(
  453. 'mmgen-autosign',
  454. self.opts + add_opts + ['--full-summary', 'wait'],
  455. direct_exec = True,
  456. no_passthru_opts = True,
  457. spawn_env_override = self.spawn_env | {'EXEC_WRAPPER_DO_RUNTIME_MSG': ''})
  458. self.write_to_tmpfile('autosign_thread_pid', str(t.ep.pid))
  459. return t
  460. def wait_loop_start(self, add_opts=[]):
  461. import threading
  462. threading.Thread(
  463. target = self._wait_loop_start,
  464. kwargs = {'add_opts': add_opts},
  465. name = 'Autosign wait loop').start()
  466. time.sleep(0.1) # try to ensure test output is displayed before next test starts
  467. return 'silent'
  468. def wait_loop_kill(self):
  469. return self._kill_process_from_pid_file('autosign_thread_pid', 'autosign wait loop')
  470. def _wait_signed(self, desc):
  471. oqmsg_r(gray(f'→ offline wallet{"s" if desc.endswith("s") else ""} waiting for {desc}'))
  472. assert not self.asi.device_inserted, f'‘{self.asi.dev_label}’ is inserted!'
  473. assert not self.asi.mountpoint.is_mount(), f'‘{self.asi.mountpoint}’ is mounted!'
  474. self.insert_device()
  475. while True:
  476. oqmsg_r(gray('.'))
  477. if self.asi.mountpoint.is_mount():
  478. oqmsg_r(gray(' signing '))
  479. break
  480. time.sleep(0.1)
  481. while True:
  482. oqmsg_r(gray('>'))
  483. if not self.asi.mountpoint.is_mount():
  484. oqmsg(gray(' done'))
  485. break
  486. time.sleep(0.1)
  487. imsg('')
  488. self.remove_device()
  489. return 'ok'
  490. def insert_device_online(self):
  491. return self.insert_device(asi='asi_online')
  492. def remove_device_online(self):
  493. return self.remove_device(asi='asi_online')
  494. def do_mount_online(self, *args, **kwargs):
  495. return self._mount_ops('asi_online', 'do_mount', *args, **kwargs)
  496. def do_umount_online(self, *args, **kwargs):
  497. return self._mount_ops('asi_online', 'do_umount', *args, **kwargs)
  498. async def txview(self):
  499. self.spawn(msg_only=True)
  500. self.insert_device()
  501. self.do_mount()
  502. src = Path(self.asi.txauto_dir)
  503. from mmgen.tx import CompletedTX
  504. txs = sorted(
  505. [await CompletedTX(cfg=self.cfg, filename=path, quiet_open=True) for path in sorted(src.iterdir())],
  506. key = lambda x: x.timestamp)
  507. for tx in txs:
  508. imsg(blue(f'\nViewing ‘{tx.infile.name}’:'))
  509. out = tx.info.format(terse=True)
  510. imsg(indent(out, indent=' '))
  511. self.do_umount()
  512. self.remove_device()
  513. return 'ok'
  514. class CmdTestAutosign(CmdTestAutosignBase):
  515. 'autosigning transactions for all supported coins'
  516. coins = ['btc', 'bch', 'ltc', 'eth']
  517. daemon_coins = ['btc', 'bch', 'ltc']
  518. txfile_coins = ['btc', 'bch', 'ltc', 'eth', 'mm1', 'etc']
  519. have_online = False
  520. live = False
  521. simulate_led = True
  522. no_insert_check = True
  523. wallet_passwd = 'abc'
  524. filedir_map = (
  525. ('btc', ''),
  526. ('bch', ''),
  527. ('ltc', 'litecoin'),
  528. ('eth', 'ethereum'),
  529. ('mm1', 'ethereum'),
  530. ('etc', 'ethereum_classic'),
  531. )
  532. cmd_group = (
  533. ('start_daemons', 'starting daemons'),
  534. ('copy_tx_files', 'copying transaction files'),
  535. ('gen_key', 'generating key'),
  536. ('create_dfl_wallet', 'creating default MMGen wallet'),
  537. ('bad_opt1', 'running ‘mmgen-autosign’ with --seed-len in invalid context'),
  538. ('bad_opt2', 'running ‘mmgen-autosign’ with --mnemonic-fmt in invalid context'),
  539. ('bad_opt3', 'running ‘mmgen-autosign’ with --led in invalid context'),
  540. ('run_setup_dfl_wallet', 'running ‘autosign setup’ (with default wallet)'),
  541. ('sign_quiet', 'signing transactions (--quiet)'),
  542. ('remove_signed_txfiles', 'removing signed transaction files'),
  543. ('run_setup_bip39', 'running ‘autosign setup’ (BIP39 mnemonic)'),
  544. ('create_bad_txfiles', 'creating bad transaction files'),
  545. ('sign_full_summary', 'signing transactions (--full-summary)'),
  546. ('remove_signed_txfiles_btc', 'removing transaction files (BTC only)'),
  547. ('remove_bad_txfiles', 'removing bad transaction files'),
  548. ('sign_led', 'signing transactions (--led - BTC files only)'),
  549. ('remove_signed_txfiles', 'removing signed transaction files'),
  550. ('sign_stealth_led', 'signing transactions (--stealth-led)'),
  551. ('remove_signed_txfiles', 'removing signed transaction files'),
  552. ('copy_msgfiles', 'copying message files'),
  553. ('sign_quiet_msg', 'signing transactions and messages (--quiet)'),
  554. ('remove_signed_txfiles', 'removing signed transaction files'),
  555. ('create_bad_txfiles2', 'creating bad transaction files'),
  556. ('remove_signed_msgfiles', 'removing signed message files'),
  557. ('create_invalid_msgfile', 'creating invalid message file'),
  558. ('sign_full_summary_msg', 'signing transactions and messages (--full-summary)'),
  559. ('remove_invalid_msgfile', 'removing invalid message file'),
  560. ('remove_bad_txfiles2', 'removing bad transaction files'),
  561. ('sign_no_unsigned', 'signing transactions and messages (nothing to sign)'),
  562. ('sign_no_unsigned_xmr', 'signing transactions and messages (nothing to sign, with XMR)'),
  563. ('sign_no_unsigned_xmronly', 'signing transactions and messages (nothing to sign, XMR-only)'),
  564. ('stop_daemons', 'stopping daemons'),
  565. ('sign_bad_no_daemon', 'signing transactions (error, no daemons running)'),
  566. ('wipe_key', 'wiping the wallet encryption key'),
  567. )
  568. def __init__(self, cfg, trunner, cfgs, spawn):
  569. super().__init__(cfg, trunner, cfgs, spawn)
  570. if trunner is None:
  571. return
  572. if self.live and not self.cfg.exact_output:
  573. die(1, red('autosign_live tests must be run with --exact-output enabled!'))
  574. if self.no_insert_check:
  575. self.opts.append('--no-insert-check')
  576. self.tx_file_ops('set_count') # initialize self.tx_count here so we can resume anywhere
  577. self.bad_tx_count = 0
  578. def gen_msg_fns():
  579. fmap = dict(self.filedir_map)
  580. for coin in self.coins:
  581. if coin == 'xmr':
  582. continue
  583. sdir = os.path.join('test', 'ref', fmap[coin])
  584. for fn in os.listdir(sdir):
  585. if fn.endswith(f'[{coin.upper()}].rawmsg.json'):
  586. yield os.path.join(sdir, fn)
  587. self.ref_msgfiles = tuple(gen_msg_fns())
  588. self.good_msg_count = 0
  589. self.bad_msg_count = 0
  590. if self.simulate_led:
  591. db = LEDControl.boards['dummy']
  592. for fn in (db.control, db.trigger):
  593. run(f'sudo rm -f {fn}'.split(), check=True)
  594. LEDControl.create_dummy_control_files()
  595. usrgrp = {'linux': 'root:root', 'darwin': 'root:wheel'}[sys.platform]
  596. for fn in (db.control, db.trigger): # trigger the auto-chmod feature
  597. run(f'sudo chmod 644 {fn}'.split(), check=True)
  598. run(f'sudo chown {usrgrp} {fn}'.split(), check=True)
  599. self.have_dummy_control_files = True
  600. self.spawn_env['MMGEN_TEST_SUITE_AUTOSIGN_LED_SIMULATE'] = '1'
  601. def gen_key(self):
  602. self.insert_device()
  603. t = self.spawn('mmgen-autosign', self.opts + ['gen_key'])
  604. t.expect_getend('Wrote key file ')
  605. t.read()
  606. self.remove_device()
  607. return t
  608. def create_dfl_wallet(self):
  609. t = self.spawn('mmgen-walletconv', [
  610. f'--outdir={self.cfg.data_dir}',
  611. '--usr-randchars=0', '--quiet', '--hash-preset=1', '--label=foo',
  612. 'test/ref/98831F3A.hex'
  613. ]
  614. )
  615. t.passphrase_new('new MMGen wallet', self.wallet_passwd)
  616. t.written_to_file('MMGen wallet')
  617. return t
  618. def _bad_opt(self, cmdline, expect):
  619. t = self.spawn('mmgen-autosign', ['--coins=btc'] + cmdline, exit_val=1)
  620. t.expect(expect)
  621. return t
  622. def bad_opt1(self):
  623. return self._bad_opt(['--seed-len=128'], 'is valid')
  624. def bad_opt2(self):
  625. return self._bad_opt(['--mnemonic-fmt=bip39', 'wait'], 'is valid')
  626. def bad_opt3(self):
  627. return self._bad_opt(['--led', 'gen_key'], 'is not valid')
  628. def run_setup_dfl_wallet(self):
  629. return self.run_setup(mn_type='default', use_dfl_wallet=True, wallet_passwd=self.wallet_passwd)
  630. def run_setup_bip39(self):
  631. from mmgen.cfgfile import mmgen_cfg_file
  632. fn = mmgen_cfg_file(self.cfg, 'usr').fn
  633. old_data = mmgen_cfg_file(self.cfg, 'usr').get_data(fn)
  634. new_data = [d.replace('bip39:fixed', 'bip39:full')[2:]
  635. if d.startswith('# mnemonic_entry_modes') else d for d in old_data]
  636. with open(fn, 'w') as fh:
  637. fh.write('\n'.join(new_data) + '\n')
  638. t = self.run_setup(
  639. mn_type = 'bip39',
  640. seed_len = 256,
  641. usr_entry_modes = True)
  642. with open(fn, 'w') as fh:
  643. fh.write('\n'.join(old_data) + '\n')
  644. return t
  645. def copy_tx_files(self):
  646. self.spawn(msg_only=True)
  647. return self.tx_file_ops('copy')
  648. def remove_signed_txfiles(self):
  649. self.tx_file_ops('remove_signed')
  650. return 'skip'
  651. def remove_signed_txfiles_btc(self):
  652. self.tx_file_ops('remove_signed', txfile_coins=['btc'])
  653. return 'skip'
  654. def tx_file_ops(self, op, txfile_coins=[]):
  655. assert op in ('copy', 'set_count', 'remove_signed')
  656. from .ref import CmdTestRef
  657. def gen():
  658. d = CmdTestRef.sources['ref_tx_file']
  659. dirmap = [e for e in self.filedir_map if e[0] in (txfile_coins or self.txfile_coins)]
  660. for coin, coindir in dirmap:
  661. for network in (0, 1):
  662. fn = d[coin][network]
  663. if fn:
  664. yield (coindir, fn)
  665. data = list(gen()) + [('', '25EFA3[2.34].testnet.rawtx')] # TX with 2 non-MMGen outputs
  666. self.tx_count = len(data)
  667. if op == 'set_count':
  668. return
  669. self.insert_device()
  670. silence()
  671. self.do_mount(verbose=not self.tr.quiet)
  672. end_silence()
  673. for coindir, fn in data:
  674. src = joinpath(ref_dir, coindir, fn)
  675. if self.cfg.debug_utf8:
  676. ext = '.testnet.rawtx' if fn.endswith('.testnet.rawtx') else '.rawtx'
  677. fn = fn[:-len(ext)] + '-α' + ext
  678. target = joinpath(self.asi.tx_dir, fn)
  679. if not op == 'remove_signed':
  680. shutil.copyfile(src, target)
  681. try:
  682. os.unlink(target.replace('.rawtx', '.sigtx'))
  683. except:
  684. pass
  685. self.do_umount()
  686. self.remove_device()
  687. return 'ok'
  688. def create_bad_txfiles(self):
  689. return self.bad_txfiles('create')
  690. def remove_bad_txfiles(self):
  691. return self.bad_txfiles('remove')
  692. create_bad_txfiles2 = create_bad_txfiles
  693. remove_bad_txfiles2 = remove_bad_txfiles
  694. def bad_txfiles(self, op):
  695. self.insert_device()
  696. self.do_mount()
  697. # create or delete 2 bad tx files
  698. self.spawn(msg_only=True)
  699. fns = [joinpath(self.asi.tx_dir, f'bad{n}.rawtx') for n in (1, 2)]
  700. match op:
  701. case 'create':
  702. for fn in fns:
  703. with open(fn, 'w') as fp:
  704. fp.write('bad tx data\n')
  705. self.bad_tx_count = 2
  706. case 'remove':
  707. for fn in fns:
  708. try:
  709. os.unlink(fn)
  710. except:
  711. pass
  712. self.bad_tx_count = 0
  713. self.do_umount()
  714. self.remove_device()
  715. return 'ok'
  716. def copy_msgfiles(self):
  717. return self.msgfile_ops('copy')
  718. def remove_signed_msgfiles(self):
  719. return self.msgfile_ops('remove_signed')
  720. def create_invalid_msgfile(self):
  721. return self.msgfile_ops('create_invalid')
  722. def remove_invalid_msgfile(self):
  723. return self.msgfile_ops('remove_invalid')
  724. def msgfile_ops(self, op):
  725. self.spawn(msg_only=True)
  726. destdir = joinpath(self.asi.mountpoint, 'msg')
  727. self.insert_device()
  728. self.do_mount()
  729. os.makedirs(destdir, exist_ok=True)
  730. match op:
  731. case 'create_invalid' | 'remove_invalid':
  732. fn = os.path.join(destdir, 'DEADBE[BTC].rawmsg.json')
  733. if op == 'create_invalid':
  734. with open(fn, 'w') as fp:
  735. fp.write('bad data\n')
  736. self.bad_msg_count += 1
  737. else:
  738. os.unlink(fn)
  739. self.bad_msg_count -= 1
  740. case 'copy':
  741. for fn in self.ref_msgfiles:
  742. if os.path.basename(fn) == 'ED405C[BTC].rawmsg.json': # contains bad Seed ID
  743. self.bad_msg_count += 1
  744. else:
  745. self.good_msg_count += 1
  746. imsg(f'Copying: {fn} -> {destdir}')
  747. shutil.copy2(fn, destdir)
  748. case 'remove_signed':
  749. for fn in self.ref_msgfiles:
  750. os.unlink(os.path.join(destdir, os.path.basename(fn).replace('rawmsg', 'sigmsg')))
  751. self.do_umount()
  752. self.remove_device()
  753. return 'ok'
  754. def do_sign(self, args=[], have_msg=False, exc_exit_val=None, expect_str=None):
  755. tx_desc = Signable.transaction.desc
  756. self.insert_device()
  757. def do_return():
  758. if expect_str:
  759. t.expect(expect_str)
  760. t.read()
  761. self.remove_device()
  762. imsg('')
  763. return t
  764. t = self.spawn(
  765. 'mmgen-autosign',
  766. self.opts + args,
  767. exit_val = exc_exit_val or (1 if self.bad_tx_count or (have_msg and self.bad_msg_count) else None))
  768. if exc_exit_val:
  769. return do_return()
  770. t.expect(
  771. f'{self.tx_count} {tx_desc}{suf(self.tx_count)} signed' if self.tx_count else
  772. f'No unsigned {tx_desc}s')
  773. if self.bad_tx_count:
  774. t.expect(f'{self.bad_tx_count} {tx_desc}{suf(self.bad_tx_count)} failed to sign')
  775. if have_msg:
  776. t.expect(
  777. f'{self.good_msg_count} message file{suf(self.good_msg_count)}{{0,1}} signed'
  778. if self.good_msg_count else
  779. 'No unsigned message files', regex=True)
  780. if self.bad_msg_count:
  781. t.expect(
  782. f'{self.bad_msg_count} message file{suf(self.bad_msg_count)}{{0,1}} failed to sign',
  783. regex = True)
  784. return do_return()
  785. def sign_quiet(self):
  786. return self.do_sign(['--quiet'])
  787. def sign_full_summary(self):
  788. return self.do_sign(['--full-summary'])
  789. def sign_led(self):
  790. return self.do_sign(['--quiet', '--led'])
  791. def sign_stealth_led(self):
  792. return self.do_sign(['--quiet', '--stealth-led'])
  793. def sign_quiet_msg(self):
  794. return self.do_sign(['--quiet'], have_msg=True)
  795. def sign_full_summary_msg(self):
  796. return self.do_sign(['--full-summary'], have_msg=True)
  797. def sign_bad_no_daemon(self):
  798. return self.do_sign(exc_exit_val=2, expect_str='listening on the correct port')
  799. def sign_no_unsigned(self):
  800. return self._sign_no_unsigned(
  801. coins = 'BTC',
  802. present = ['non_xmr_signables'],
  803. absent = ['xmr_signables'])
  804. def sign_no_unsigned_xmr(self):
  805. if self.coins == ['btc']:
  806. return 'skip'
  807. return self._sign_no_unsigned(
  808. coins = 'XMR,BTC',
  809. present = ['xmr_signables', 'non_xmr_signables'])
  810. def sign_no_unsigned_xmronly(self):
  811. if self.coins == ['btc']:
  812. return 'skip'
  813. return self._sign_no_unsigned(
  814. coins = 'XMR',
  815. present = ['xmr_signables'],
  816. absent = ['non_xmr_signables'])
  817. def _sign_no_unsigned(self, coins, present=[], absent=[]):
  818. self.insert_device()
  819. t = self.spawn('mmgen-autosign', ['--quiet', '--no-insert-check', f'--coins={coins}'])
  820. res = t.read()
  821. self.remove_device()
  822. for signable_list in present:
  823. for signable_clsname in getattr(Signable, signable_list):
  824. desc = getattr(Signable, signable_clsname).desc
  825. assert f'No unsigned {desc}s' in res, f'‘No unsigned {desc}s’ missing in output'
  826. for signable_list in absent:
  827. for signable_clsname in getattr(Signable, signable_list):
  828. desc = getattr(Signable, signable_clsname).desc
  829. assert not f'No unsigned {desc}s' in res, f'‘No unsigned {desc}s’ should be absent in output'
  830. return t
  831. def wipe_key(self):
  832. self.insert_device()
  833. t = self.spawn('mmgen-autosign', ['--quiet', '--no-insert-check', 'wipe_key'])
  834. t.expect('Shredding')
  835. t.read()
  836. self.remove_device()
  837. return t
  838. class CmdTestAutosignBTC(CmdTestAutosign):
  839. 'autosigning BTC transactions'
  840. coins = ['btc']
  841. daemon_coins = ['btc']
  842. txfile_coins = ['btc']
  843. class CmdTestAutosignLive(CmdTestAutosignBTC):
  844. 'live autosigning BTC transactions'
  845. live = True
  846. simulate_led = False
  847. no_insert_check = False
  848. cmd_group = (
  849. ('start_daemons', 'starting daemons'),
  850. ('copy_tx_files', 'copying transaction files'),
  851. ('gen_key', 'generating key'),
  852. ('run_setup_mmgen', 'running ‘autosign setup’ (MMGen native mnemonic)'),
  853. ('sign_live', 'signing transactions'),
  854. ('create_bad_txfiles', 'creating bad transaction files'),
  855. ('sign_live_led', 'signing transactions (--led)'),
  856. ('remove_bad_txfiles', 'removing bad transaction files'),
  857. ('sign_live_stealth_led', 'signing transactions (--stealth-led)'),
  858. ('stop_daemons', 'stopping daemons'),
  859. )
  860. def __init__(self, cfg, trunner, cfgs, spawn):
  861. super().__init__(cfg, trunner, cfgs, spawn)
  862. if trunner is None:
  863. return
  864. try:
  865. led = LEDControl(enabled=True, simulate=self.simulate_led)
  866. except Exception as e:
  867. msg(str(e))
  868. die(2, 'LEDControl initialization failed')
  869. self.color = led.board.color
  870. def run_setup_mmgen(self):
  871. return self.run_setup(mn_type='mmgen', use_dfl_wallet=None)
  872. def sign_live(self):
  873. return self.do_sign_live()
  874. def sign_live_led(self):
  875. return self.do_sign_live(['--led'], f'The {self.color} LED should start blinking slowly now')
  876. def sign_live_stealth_led(self):
  877. return self.do_sign_live(['--stealth-led'], 'You should see no LED activity now')
  878. def do_sign_live(self, led_opts=None, led_msg=None):
  879. def prompt_remove():
  880. omsg_r(orange('\nExtract removable device and then hit ENTER '))
  881. input()
  882. def prompt_insert_sign(t):
  883. omsg(orange(insert_msg))
  884. t.expect(f'{self.tx_count} non-automount transactions signed')
  885. if self.bad_tx_count:
  886. t.expect(f'{self.bad_tx_count} non-automount transactions failed to sign')
  887. t.expect('Waiting')
  888. if led_opts:
  889. opts_msg = '‘' + ' '.join(led_opts) + '’'
  890. info_msg = 'Running ‘mmgen-autosign wait’ with {}. {}'.format(opts_msg, led_msg)
  891. insert_msg = f'Insert removable device and watch for fast {self.color} LED activity during signing'
  892. else:
  893. opts_msg = 'no LED'
  894. info_msg = 'Running ‘mmgen-autosign wait’'
  895. insert_msg = 'Insert removable device '
  896. self.spawn(msg_only=True)
  897. self.do_umount()
  898. prompt_remove()
  899. omsg('\n' + cyan(indent(info_msg)))
  900. t = self.spawn(
  901. 'mmgen-autosign',
  902. self.opts + (led_opts or []) + ['--quiet', '--no-summary', 'wait'],
  903. no_msg = True,
  904. exit_val = 1)
  905. if self.tr.quiet:
  906. omsg('')
  907. prompt_insert_sign(t)
  908. self.do_mount() # race condition due to device insertion detection
  909. self.remove_signed_txfiles()
  910. self.do_umount()
  911. imsg(purple('\nKilling wait loop!'))
  912. t.kill(2) # 2 = SIGINT
  913. if self.simulate_led and led_opts:
  914. t.expect(f'Resetting {self.color} LED')
  915. return t
  916. class CmdTestAutosignLiveSimulate(CmdTestAutosignLive):
  917. 'live autosigning BTC transactions with simulated LED support'
  918. simulate_led = True