main_autosign.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2024 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. autosign: Auto-sign MMGen transactions, message files and XMR wallet output files
  20. """
  21. from .util import msg,die,fmt_list,exit_if_mswin,async_run
  22. exit_if_mswin('autosigning')
  23. opts_data = {
  24. 'sets': [('stealth_led', True, 'led', True)],
  25. 'text': {
  26. 'desc': 'Auto-sign MMGen transactions, message files and XMR wallet output files',
  27. 'usage':'[opts] [operation]',
  28. 'options': """
  29. -h, --help Print this help message
  30. --, --longhelp Print help message for long options (common options)
  31. -c, --coins=c Coins to sign for (comma-separated list)
  32. -I, --no-insert-check Don’t check for device insertion
  33. -l, --seed-len=N Specify wallet seed length of ‘N’ bits (for setup only)
  34. -L, --led Use status LED to signal standby, busy and error
  35. -m, --mountpoint=M Specify an alternate mountpoint 'M'
  36. (default: {asi.dfl_mountpoint!r})
  37. -M, --mnemonic-fmt=F During setup, prompt for mnemonic seed phrase of format
  38. 'F' (choices: {mn_fmts}; default: {asi.dfl_mn_fmt!r})
  39. -n, --no-summary Don’t print a transaction summary
  40. -s, --stealth-led Stealth LED mode - signal busy and error only, and only
  41. after successful authorization.
  42. -S, --full-summary Print a full summary of each signed transaction after
  43. each autosign run. The default list of non-MMGen outputs
  44. will not be printed.
  45. -q, --quiet Produce quieter output
  46. -v, --verbose Produce more verbose output
  47. -w, --wallet-dir=D Specify an alternate wallet dir
  48. (default: {asi.dfl_wallet_dir!r})
  49. -x, --xmrwallets=L Range or list of wallets to be used for XMR autosigning
  50. """,
  51. 'notes': """
  52. OPERATIONS
  53. clean - clean the removable device of unneeded files, removing only non-
  54. essential data
  55. gen_key - generate the wallet encryption key and copy it to the removable
  56. device mounted at mountpoint ‘{asi.mountpoint}’ (as currently
  57. configured)
  58. setup - generate both wallet encryption key and temporary signing wallet
  59. xmr_setup - set up temporary Monero signing wallets. This operation needn’t
  60. be performed by the user directly in most cases, as Monero setup
  61. is done by the ‘setup’ command when --xmrwallets is specified
  62. wait - start in loop mode: wait-mount-sign-unmount-wait
  63. wipe_key - wipe the wallet encryption key on the removable device, making
  64. signing transactions or stealing the user’s seed impossible.
  65. The operation is intended as a ‘kill switch’ and thus performed
  66. without prompting
  67. USAGE NOTES
  68. If no operation is specified, this program mounts a removable device
  69. (typically a USB flash drive) containing unsigned MMGen transactions, message
  70. files, and/or XMR wallet output files, signs them, unmounts the removable
  71. device and exits.
  72. If invoked with ‘wait’, the program waits in a loop, mounting the removable
  73. device, performing signing operations and unmounting the device every time it
  74. is inserted.
  75. On supported platforms (currently Orange Pi, Rock Pi and Raspberry Pi boards),
  76. the status LED indicates whether the program is busy or in standby mode, i.e.
  77. ready for device insertion or removal.
  78. The removable device must have a partition labeled MMGEN_TX with a user-
  79. writable root directory.
  80. On both the signing and online machines the mountpoint ‘{asi.mountpoint}’
  81. (as currently configured) must exist and ‘/etc/fstab’ must contain the
  82. following entry:
  83. LABEL=MMGEN_TX {asi.mountpoint} auto noauto,user 0 0
  84. Signing is performed with a temporary wallet created in volatile memory in
  85. the directory ‘{asi.wallet_dir}’ (as currently configured). The wallet is
  86. encrypted with a 32-byte password saved in the file ‘autosign.key’ in the
  87. root of the removable device’s filesystem.
  88. The password and temporary wallet may be created in one operation by invoking
  89. ‘mmgen-autosign setup’ with the removable device inserted. In this case, the
  90. temporary wallet is created from the user’s default wallet, if it exists and
  91. the user so desires. If not, the user is prompted to enter a seed phrase.
  92. Alternatively, the password and temporary wallet may be created separately by
  93. first invoking ‘mmgen-autosign gen_key’ and then creating and encrypting the
  94. wallet using the -P (--passwd-file) option:
  95. $ mmgen-walletconv -iwords -d{asi.wallet_dir} -p1 -N -P{asi.mountpoint}/autosign.key -Lfoo
  96. Note that the hash preset must be ‘1’. To use a wallet file as the source
  97. instead of an MMGen seed phrase, omit the ‘-i’ option and add the wallet
  98. file path to the end of the command line. Multiple temporary wallets may
  99. be created in this way and used for signing (note, however, that for XMR
  100. operations only one wallet is supported).
  101. Autosigning is currently available only on Linux-based platforms.
  102. SECURITY NOTE
  103. By placing wallet and password on separate devices, this program creates
  104. a two-factor authentication setup whereby an attacker must gain physical
  105. control of both the removable device and signing machine in order to sign
  106. transactions. It’s therefore recommended to always keep the removable device
  107. secure, separated from the signing machine and hidden (in your pocket, for
  108. example) when not transacting. In addition, since login access on the
  109. signing machine is required to steal the user’s seed, it’s good practice
  110. to lock the signing machine’s screen once the setup process is complete.
  111. As a last resort, cutting power to the signing machine will destroy the
  112. volatile memory where the temporary wallet resides and foil any attack,
  113. even if you’ve lost control of the removable device.
  114. Always remember to power off the signing machine when your signing session
  115. is over.
  116. """
  117. },
  118. 'code': {
  119. 'options': lambda s: s.format(
  120. asi = asi,
  121. mn_fmts = fmt_list( asi.mn_fmts, fmt='no_spc' ),
  122. ),
  123. 'notes': lambda s: s.format(asi=asi)
  124. }
  125. }
  126. def main(do_loop):
  127. asi.init_led()
  128. asi.init_exit_handler()
  129. async def do():
  130. await asi.check_daemons_running()
  131. if do_loop:
  132. await asi.main_loop()
  133. else:
  134. ret = await asi.do_sign()
  135. asi.at_exit(not ret)
  136. async_run(do())
  137. from .cfg import Config
  138. from .autosign import Autosign
  139. cfg = Config(
  140. opts_data = opts_data,
  141. init_opts = {
  142. 'quiet': True,
  143. 'out_fmt': 'wallet',
  144. 'usr_randchars': 0,
  145. 'hash_preset': '1',
  146. 'label': 'Autosign Wallet',
  147. },
  148. do_post_init = True )
  149. cmd = cfg._args[0] if len(cfg._args) == 1 else 'sign' if not cfg._args else cfg._opts.usage()
  150. valid_cmds = ('gen_key', 'setup', 'xmr_setup', 'sign', 'wait', 'clean', 'wipe_key')
  151. if cmd not in valid_cmds:
  152. die(1,f'‘{cmd}’: unrecognized command')
  153. if cmd != 'setup':
  154. for opt in ('seed_len', 'mnemonic_fmt'):
  155. if getattr(cfg,opt):
  156. die(1, f'--{opt.replace("_","-")} makes sense only for the ‘setup’ operation')
  157. if cmd not in ('sign', 'wait'):
  158. for opt in ('no_summary', 'led', 'stealth_led', 'full_summary'):
  159. if getattr(cfg,opt):
  160. die(1, f'--{opt.replace("_","-")} makes no sense for the ‘{cmd}’ operation')
  161. asi = Autosign(cfg,cmd)
  162. cfg._post_init()
  163. if cmd == 'gen_key':
  164. asi.gen_key()
  165. elif cmd == 'setup':
  166. asi.setup()
  167. from .ui import keypress_confirm
  168. if cfg.xmrwallets and keypress_confirm( cfg, '\nContinue with Monero setup?', default_yes=True ):
  169. msg('')
  170. asi.xmr_setup()
  171. asi.do_umount()
  172. elif cmd == 'xmr_setup':
  173. if not cfg.xmrwallets:
  174. die(1,'Please specify a wallet or range of wallets with the --xmrwallets option')
  175. asi.do_mount()
  176. asi.xmr_setup()
  177. asi.do_umount()
  178. elif cmd == 'sign':
  179. main(do_loop=False)
  180. elif cmd == 'wait':
  181. main(do_loop=True)
  182. elif cmd == 'clean':
  183. asi.do_mount()
  184. asi.clean_old_files()
  185. asi.do_umount()
  186. elif cmd == 'wipe_key':
  187. asi.do_mount()
  188. asi.wipe_encryption_key()
  189. asi.do_umount()