ts_autosign.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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.test_py_d.ts_autosign: Autosign tests for the test.py test suite
  20. """
  21. import os,shutil
  22. from subprocess import run
  23. from pathlib import Path
  24. from mmgen.cfg import gc
  25. from mmgen.color import red,green,blue,purple
  26. from mmgen.util import suf
  27. from ..include.common import *
  28. from .common import *
  29. from .ts_base import *
  30. from .ts_shared import *
  31. from .input import *
  32. from mmgen.led import LEDControl
  33. from mmgen.autosign import Autosign,AutosignConfig
  34. filedir_map = (
  35. ('btc',''),
  36. ('bch',''),
  37. ('ltc','litecoin'),
  38. ('eth','ethereum'),
  39. ('mm1','ethereum'),
  40. ('etc','ethereum_classic'),
  41. )
  42. def init_led(simulate):
  43. try:
  44. cf = LEDControl(enabled=True,simulate=simulate)
  45. except Exception as e:
  46. msg(str(e))
  47. die(2,'LEDControl initialization failed')
  48. for fn in (cf.board.status,cf.board.trigger):
  49. if fn:
  50. run(['sudo','chmod','0666',fn],check=True)
  51. def check_mountpoint(asi):
  52. if not asi.mountpoint.is_mount():
  53. try:
  54. run(['mount',asi.mountpoint],check=True)
  55. imsg(f'Mounted {asi.mountpoint}')
  56. except:
  57. die(2,f'Could not mount {asi.mountpoint}! Exiting')
  58. if not asi.tx_dir.is_dir():
  59. die(2,f'Directory {asi.tx_dir} does not exist! Exiting')
  60. def do_mount(mountpoint):
  61. if not os.path.ismount(mountpoint):
  62. try: run(['mount',mountpoint],check=True)
  63. except: pass
  64. def do_umount(mountpoint):
  65. if os.path.ismount(mountpoint):
  66. try: run(['umount',mountpoint],check=True)
  67. except: pass
  68. class TestSuiteAutosignBase(TestSuiteBase):
  69. networks = ('btc',)
  70. tmpdir_nums = [18]
  71. color = True
  72. mountpoint_basename = 'mmgen_autosign'
  73. no_insert_check = True
  74. win_skip = True
  75. def __init__(self,trunner,cfgs,spawn):
  76. super().__init__(trunner,cfgs,spawn)
  77. if trunner == None:
  78. return
  79. self.network_ids = [c+'_tn' for c in self.daemon_coins] + self.daemon_coins
  80. if not self.live:
  81. self.wallet_dir = Path( self.tmpdir, 'dev.shm.autosign' )
  82. self.asi = Autosign(
  83. AutosignConfig({
  84. 'coins': ','.join(self.coins),
  85. 'mountpoint': (
  86. None if self.live else
  87. os.path.join(self.tmpdir,self.mountpoint_basename)
  88. ),
  89. 'wallet_dir': None if self.live else self.wallet_dir,
  90. 'test_suite': True,
  91. 'test_suite_xmr_autosign': self.name == 'TestSuiteXMRAutosign',
  92. })
  93. )
  94. self.mountpoint = self.asi.mountpoint
  95. if self.simulate and not cfg.exact_output:
  96. die(1,red('This command must be run with --exact-output enabled!'))
  97. if self.simulate or not self.live:
  98. LEDControl.create_dummy_control_files()
  99. self.spawn_env['MMGEN_TEST_SUITE_AUTOSIGN_LED_SIMULATE'] = '1'
  100. self.opts = ['--coins='+','.join(self.coins)]
  101. if self.live:
  102. check_mountpoint(self.asi)
  103. init_led(self.simulate)
  104. else:
  105. self.asi.tx_dir.mkdir(parents=True,exist_ok=True) # creates mountpoint
  106. self.wallet_dir.mkdir(parents=True,exist_ok=True)
  107. self.opts.extend([
  108. f'--mountpoint={self.mountpoint}',
  109. f'--wallet-dir={self.wallet_dir}',
  110. ])
  111. if self.no_insert_check:
  112. self.opts.append('--no-insert-check')
  113. self.tx_file_ops('set_count') # initialize tx_count here so we can resume anywhere
  114. def gen_msg_fns():
  115. fmap = dict(filedir_map)
  116. for coin in self.coins:
  117. if coin == 'xmr':
  118. continue
  119. sdir = os.path.join('test','ref',fmap[coin])
  120. for fn in os.listdir(sdir):
  121. if fn.endswith(f'[{coin.upper()}].rawmsg.json'):
  122. yield os.path.join(sdir,fn)
  123. self.ref_msgfiles = tuple(gen_msg_fns())
  124. self.good_msg_count = 0
  125. self.bad_msg_count = 0
  126. def __del__(self):
  127. if gc.platform == 'win' or self.tr == None:
  128. return
  129. if self.simulate or not self.live:
  130. LEDControl.delete_dummy_control_files()
  131. def start_daemons(self):
  132. self.spawn('',msg_only=True)
  133. start_test_daemons(*self.network_ids)
  134. return 'ok'
  135. def stop_daemons(self):
  136. self.spawn('',msg_only=True)
  137. stop_test_daemons(*self.network_ids)
  138. return 'ok'
  139. def gen_key(self):
  140. t = self.spawn( 'mmgen-autosign', self.opts + ['gen_key'] )
  141. t.expect_getend('Wrote key file ')
  142. return t
  143. def create_dfl_wallet(self):
  144. t = self.spawn( 'mmgen-walletconv', [
  145. f'--outdir={cfg.data_dir}',
  146. '--usr-randchars=0', '--quiet', '--hash-preset=1', '--label=foo',
  147. 'test/ref/98831F3A.hex'
  148. ]
  149. )
  150. t.passphrase_new('new MMGen wallet','abc')
  151. t.written_to_file('MMGen wallet')
  152. return t
  153. def run_setup_dfl_wallet(self):
  154. return self.run_setup(mn_type='default',use_dfl_wallet=True)
  155. def run_setup_bip39(self):
  156. return self.run_setup(mn_type='bip39')
  157. def run_setup(self,mn_type=None,mn_file=None,use_dfl_wallet=False):
  158. mn_desc = mn_type or 'default'
  159. mn_type = mn_type or 'mmgen'
  160. t = self.spawn(
  161. 'mmgen-autosign',
  162. self.opts +
  163. ([] if mn_desc == 'default' else [f'--mnemonic-fmt={mn_type}']) +
  164. ['setup'] )
  165. if use_dfl_wallet:
  166. t.expect( 'Use default wallet for autosigning? (Y/n): ', 'y' )
  167. t.passphrase( 'MMGen wallet', 'abc' )
  168. else:
  169. if use_dfl_wallet is not None: # None => no dfl wallet present
  170. t.expect( 'Use default wallet for autosigning? (Y/n): ', 'n' )
  171. mn_file = mn_file or { 'mmgen': dfl_words_file, 'bip39': dfl_bip39_file }[mn_type]
  172. mn = read_from_file(mn_file).strip().split()
  173. from mmgen.mn_entry import mn_entry
  174. entry_mode = 'full'
  175. mne = mn_entry( cfg, mn_type, entry_mode )
  176. t.expect('words: ',{ 12:'1', 18:'2', 24:'3' }[len(mn)])
  177. t.expect('OK? (Y/n): ','\n')
  178. t.expect('Type a number.*: ',str(mne.entry_modes.index(entry_mode)+1),regex=True)
  179. stealth_mnemonic_entry(t,mne,mn,entry_mode)
  180. wf = t.written_to_file('Autosign wallet')
  181. return t
  182. def copy_tx_files(self):
  183. self.spawn('',msg_only=True)
  184. return self.tx_file_ops('copy')
  185. def remove_signed_txfiles(self):
  186. self.tx_file_ops('remove_signed')
  187. return 'skip'
  188. def remove_signed_txfiles_btc(self):
  189. self.tx_file_ops('remove_signed',txfile_coins=['btc'])
  190. return 'skip'
  191. def tx_file_ops(self,op,txfile_coins=[]):
  192. assert op in ('copy','set_count','remove_signed')
  193. fdata = [e for e in filedir_map if e[0] in (txfile_coins or self.txfile_coins)]
  194. from .ts_ref import TestSuiteRef
  195. tfns = [TestSuiteRef.sources['ref_tx_file'][c][1] for c,d in fdata] + \
  196. [TestSuiteRef.sources['ref_tx_file'][c][0] for c,d in fdata] + \
  197. ['25EFA3[2.34].testnet.rawtx'] # TX with 2 non-MMGen outputs
  198. self.tx_count = len([fn for fn in tfns if fn])
  199. if op == 'set_count':
  200. return
  201. tfs = [joinpath(ref_dir,d[1],fn) for d,fn in zip(fdata+fdata+[('btc','')],tfns)]
  202. for f,fn in zip(tfs,tfns):
  203. if fn: # use empty fn to skip file
  204. if cfg.debug_utf8:
  205. ext = '.testnet.rawtx' if fn.endswith('.testnet.rawtx') else '.rawtx'
  206. fn = fn[:-len(ext)] + '-α' + ext
  207. target = joinpath(self.mountpoint,'tx',fn)
  208. if not op == 'remove_signed':
  209. shutil.copyfile(f,target)
  210. try:
  211. os.unlink(target.replace('.rawtx','.sigtx'))
  212. except:
  213. pass
  214. return 'ok'
  215. def create_bad_txfiles(self):
  216. return self.bad_txfiles('create')
  217. def remove_bad_txfiles(self):
  218. return self.bad_txfiles('remove')
  219. def bad_txfiles(self,op):
  220. if self.live:
  221. do_mount(self.mountpoint)
  222. # create or delete 2 bad tx files
  223. self.spawn('',msg_only=True)
  224. fns = [joinpath(self.mountpoint,'tx',f'bad{n}.rawtx') for n in (1,2)]
  225. if op == 'create':
  226. for fn in fns:
  227. with open(fn,'w') as fp:
  228. fp.write('bad tx data\n')
  229. self.bad_tx_count = 2
  230. elif op == 'remove':
  231. for fn in fns:
  232. try: os.unlink(fn)
  233. except: pass
  234. self.bad_tx_count = 0
  235. return 'ok'
  236. def copy_msgfiles(self):
  237. return self.msgfile_ops('copy')
  238. def remove_signed_msgfiles(self):
  239. return self.msgfile_ops('remove_signed')
  240. def create_invalid_msgfile(self):
  241. return self.msgfile_ops('create_invalid')
  242. def remove_invalid_msgfile(self):
  243. return self.msgfile_ops('remove_invalid')
  244. def msgfile_ops(self,op):
  245. self.spawn('',msg_only=True)
  246. destdir = joinpath(self.mountpoint,'msg')
  247. os.makedirs(destdir,exist_ok=True)
  248. if op.endswith('_invalid'):
  249. fn = os.path.join(destdir,'DEADBE[BTC].rawmsg.json')
  250. if op == 'create_invalid':
  251. with open(fn,'w') as fp:
  252. fp.write('bad data\n')
  253. self.bad_msg_count += 1
  254. elif op == 'remove_invalid':
  255. os.unlink(fn)
  256. self.bad_msg_count -= 1
  257. else:
  258. for fn in self.ref_msgfiles:
  259. if op == 'copy':
  260. if os.path.basename(fn) == 'ED405C[BTC].rawmsg.json': # contains bad Seed ID
  261. self.bad_msg_count += 1
  262. else:
  263. self.good_msg_count += 1
  264. imsg(f'Copying: {fn} -> {destdir}')
  265. shutil.copy2(fn,destdir)
  266. elif op == 'remove_signed':
  267. os.unlink(os.path.join( destdir, os.path.basename(fn).replace('rawmsg','sigmsg') ))
  268. return 'ok'
  269. def do_sign(self,args,have_msg=False,tx_name='transaction'):
  270. t = self.spawn('mmgen-autosign', self.opts + args )
  271. t.expect(
  272. f'{self.tx_count} {tx_name}{suf(self.tx_count)} signed' if self.tx_count else
  273. 'No unsigned transactions' )
  274. if self.bad_tx_count:
  275. t.expect(f'{self.bad_tx_count} {tx_name}{suf(self.bad_tx_count)} failed to sign')
  276. t.req_exit_val = 1
  277. if have_msg:
  278. t.expect(
  279. f'{self.good_msg_count} message file{suf(self.good_msg_count)}{{0,1}} signed'
  280. if self.good_msg_count else
  281. 'No unsigned message files', regex=True )
  282. if self.bad_msg_count:
  283. t.expect(
  284. f'{self.bad_msg_count} message file{suf(self.bad_msg_count)}{{0,1}} failed to sign',
  285. regex = True )
  286. t.req_exit_val = 1
  287. if 'wait' in args:
  288. t.expect('Waiting')
  289. imsg(purple('\nKilling wait loop!'))
  290. t.kill(2)
  291. t.req_exit_val = 1
  292. else:
  293. t.read()
  294. imsg('')
  295. return t
  296. class TestSuiteAutosign(TestSuiteAutosignBase):
  297. 'autosigning transactions for all supported coins'
  298. coins = ['btc','bch','ltc','eth']
  299. daemon_coins = ['btc','bch','ltc']
  300. txfile_coins = ['btc','bch','ltc','eth','mm1','etc']
  301. live = False
  302. simulate = False
  303. bad_tx_count = 0
  304. cmd_group = (
  305. ('start_daemons', 'starting daemons'),
  306. ('copy_tx_files', 'copying transaction files'),
  307. ('gen_key', 'generating key'),
  308. ('create_dfl_wallet', 'creating default MMGen wallet'),
  309. ('run_setup_dfl_wallet', 'running ‘autosign setup’ (with default wallet)'),
  310. ('sign_quiet', 'signing transactions (--quiet)'),
  311. ('remove_signed_txfiles', 'removing signed transaction files'),
  312. ('run_setup_bip39', 'running ‘autosign setup’ (BIP39 mnemonic)'),
  313. ('create_bad_txfiles', 'creating bad transaction files'),
  314. ('sign_full_summary', 'signing transactions (--full-summary)'),
  315. ('remove_signed_txfiles_btc','removing transaction files (BTC only)'),
  316. ('remove_bad_txfiles', 'removing bad transaction files'),
  317. ('sign_led', 'signing transactions (--led - BTC files only)'),
  318. ('remove_signed_txfiles', 'removing signed transaction files'),
  319. ('sign_stealth_led', 'signing transactions (--stealth-led)'),
  320. ('remove_signed_txfiles', 'removing signed transaction files'),
  321. ('copy_msgfiles', 'copying message files'),
  322. ('sign_quiet_msg', 'signing transactions and messages (--quiet)'),
  323. ('remove_signed_txfiles', 'removing signed transaction files'),
  324. ('create_bad_txfiles', 'creating bad transaction files'),
  325. ('remove_signed_msgfiles', 'removing signed message files'),
  326. ('create_invalid_msgfile', 'creating invalid message file'),
  327. ('sign_full_summary_msg', 'signing transactions and messages (--full-summary)'),
  328. ('remove_invalid_msgfile', 'removing invalid message file'),
  329. ('remove_bad_txfiles', 'removing bad transaction files'),
  330. ('sign_no_unsigned_msg', 'signing transactions and messages (nothing to sign)'),
  331. ('stop_daemons', 'stopping daemons'),
  332. )
  333. def sign_quiet(self):
  334. return self.do_sign(['--quiet','wait'])
  335. def sign_full_summary(self):
  336. return self.do_sign(['--full-summary','wait'])
  337. def sign_led(self):
  338. return self.do_sign(['--quiet','--led'])
  339. def sign_stealth_led(self):
  340. return self.do_sign(['--quiet','--stealth-led','wait'])
  341. def sign_quiet_msg(self):
  342. return self.do_sign(['--quiet','wait'],have_msg=True)
  343. def sign_full_summary_msg(self):
  344. return self.do_sign(['--full-summary','wait'],have_msg=True)
  345. def sign_no_unsigned_msg(self):
  346. self.tx_count = 0
  347. self.good_msg_count = 0
  348. self.bad_msg_count = 0
  349. return self.do_sign(['--quiet','wait'],have_msg=True)
  350. class TestSuiteAutosignBTC(TestSuiteAutosign):
  351. 'autosigning BTC transactions'
  352. coins = ['btc']
  353. daemon_coins = ['btc']
  354. txfile_coins = ['btc']
  355. class TestSuiteAutosignLive(TestSuiteAutosignBTC):
  356. 'live autosigning BTC transactions'
  357. live = True
  358. cmd_group = (
  359. ('start_daemons', 'starting daemons'),
  360. ('copy_tx_files', 'copying transaction files'),
  361. ('gen_key', 'generating key'),
  362. ('run_setup_mmgen', 'running ‘autosign setup’ (MMGen native mnemonic)'),
  363. ('sign_live', 'signing transactions'),
  364. ('create_bad_txfiles', 'creating bad transaction files'),
  365. ('sign_live_led', 'signing transactions (--led)'),
  366. ('remove_bad_txfiles', 'removing bad transaction files'),
  367. ('sign_live_stealth_led','signing transactions (--stealth-led)'),
  368. ('stop_daemons', 'stopping daemons'),
  369. )
  370. def run_setup_mmgen(self):
  371. return self.run_setup(mn_type='mmgen',use_dfl_wallet=None)
  372. def sign_live(self):
  373. return self.do_sign_live([])
  374. def sign_live_led(self):
  375. return self.do_sign_live(['--led'])
  376. def sign_live_stealth_led(self):
  377. return self.do_sign_live(['--stealth-led'])
  378. def do_sign_live(self,led_opts):
  379. def prompt_remove():
  380. omsg_r(blue('\nRemove removable device and then hit ENTER '))
  381. input()
  382. def prompt_insert_sign(t):
  383. omsg(blue(insert_msg))
  384. t.expect(f'{self.tx_count} transactions signed')
  385. if self.bad_tx_count:
  386. t.expect(f'{self.bad_tx_count} transactions failed to sign')
  387. t.expect('Waiting')
  388. if led_opts:
  389. opts_msg = "'" + ' '.join(led_opts) + "'"
  390. info_msg = f"Running 'mmgen-autosign wait' with {led_opts[0]}. " + {
  391. '--led': "The LED should start blinking slowly now",
  392. '--stealth-led': "You should see no LED activity now"
  393. }[led_opts[0]]
  394. insert_msg = 'Insert removable device and watch for fast LED activity during signing'
  395. else:
  396. opts_msg = 'no LED'
  397. info_msg = "Running 'mmgen-autosign wait'"
  398. insert_msg = 'Insert removable device '
  399. omsg(purple(f'Running autosign test with {opts_msg}'))
  400. do_umount(self.mountpoint)
  401. prompt_remove()
  402. omsg(green(info_msg))
  403. t = self.spawn(
  404. 'mmgen-autosign',
  405. self.opts + led_opts + ['--quiet','--no-summary','wait'])
  406. if not cfg.exact_output:
  407. omsg('')
  408. prompt_insert_sign(t)
  409. do_mount(self.mountpoint) # race condition due to device insertion detection
  410. self.remove_signed_txfiles()
  411. do_umount(self.mountpoint)
  412. imsg(purple('\nKilling wait loop!'))
  413. t.kill(2) # 2 = SIGINT
  414. t.req_exit_val = 1
  415. if self.simulate and led_opts:
  416. t.expect("Stopping LED")
  417. return t
  418. class TestSuiteAutosignLiveSimulate(TestSuiteAutosignLive):
  419. 'live autosigning BTC transactions with simulated LED support'
  420. simulate = True