main_autosign.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 with a filesystem labeled MMGEN_TX
  79. and a user-writable root directory. For interoperability between OS-es, it’s
  80. recommended to use the exFAT file system.
  81. On both the signing and online machines the mountpoint ‘{asi.mountpoint}’
  82. (as currently configured) must exist. Linux (not macOS) machines must have
  83. an ‘/etc/fstab’ with the following entry:
  84. LABEL=MMGEN_TX {asi.mountpoint} auto noauto,user 0 0
  85. Signing is performed with a temporary wallet created in volatile memory in
  86. the directory ‘{asi.wallet_dir}’ (as currently configured). The wallet is
  87. encrypted with a 32-byte password saved in the file ‘autosign.key’ in the
  88. root of the removable device’s filesystem.
  89. The password and temporary wallet may be created in one operation by invoking
  90. ‘mmgen-autosign setup’ with the removable device inserted. In this case, the
  91. temporary wallet is created from the user’s default wallet, if it exists and
  92. the user so desires. If not, the user is prompted to enter a seed phrase.
  93. Alternatively, the password and temporary wallet may be created separately by
  94. first invoking ‘mmgen-autosign gen_key’ and then creating and encrypting the
  95. wallet using the -P (--passwd-file) option:
  96. $ mmgen-walletconv -iwords -d{asi.wallet_dir} -p1 -N -P{asi.mountpoint}/autosign.key -Lfoo
  97. Note that the hash preset must be ‘1’. To use a wallet file as the source
  98. instead of an MMGen seed phrase, omit the ‘-i’ option and add the wallet
  99. file path to the end of the command line. Multiple temporary wallets may
  100. be created in this way and used for signing (note, however, that for XMR
  101. operations only one wallet is supported).
  102. Autosigning is currently supported on Linux and macOS only.
  103. SECURITY NOTE
  104. By placing wallet and password on separate devices, this program creates
  105. a two-factor authentication setup whereby an attacker must gain physical
  106. control of both the removable device and signing machine in order to sign
  107. transactions. It’s therefore recommended to always keep the removable device
  108. secure, separated from the signing machine and hidden (in your pocket, for
  109. example) when not transacting. In addition, since login access on the
  110. signing machine is required to steal the user’s seed, it’s good practice
  111. to lock the signing machine’s screen once the setup process is complete.
  112. As a last resort, cutting power to the signing machine will destroy the
  113. volatile memory where the temporary wallet resides and foil any attack,
  114. even if you’ve lost control of the removable device.
  115. Always remember to power off the signing machine when your signing session
  116. is over.
  117. """
  118. },
  119. 'code': {
  120. 'options': lambda s: s.format(
  121. asi = asi,
  122. mn_fmts = fmt_list( asi.mn_fmts, fmt='no_spc' ),
  123. ),
  124. 'notes': lambda s: s.format(asi=asi)
  125. }
  126. }
  127. def main(do_loop):
  128. asi.init_led()
  129. asi.init_exit_handler()
  130. async def do():
  131. await asi.check_daemons_running()
  132. if do_loop:
  133. await asi.main_loop()
  134. else:
  135. ret = await asi.do_sign()
  136. asi.at_exit(not ret)
  137. async_run(do())
  138. from .cfg import Config
  139. from .autosign import Autosign
  140. cfg = Config(
  141. opts_data = opts_data,
  142. init_opts = {
  143. 'quiet': True,
  144. 'out_fmt': 'wallet',
  145. 'usr_randchars': 0,
  146. 'hash_preset': '1',
  147. 'label': 'Autosign Wallet',
  148. },
  149. do_post_init = True )
  150. cmd = cfg._args[0] if len(cfg._args) == 1 else 'sign' if not cfg._args else cfg._opts.usage()
  151. valid_cmds = ('gen_key', 'setup', 'xmr_setup', 'sign', 'wait', 'clean', 'wipe_key')
  152. if cmd not in valid_cmds:
  153. die(1,f'‘{cmd}’: unrecognized command')
  154. if cmd != 'setup':
  155. for opt in ('seed_len', 'mnemonic_fmt'):
  156. if getattr(cfg,opt):
  157. die(1, f'--{opt.replace("_","-")} makes sense only for the ‘setup’ operation')
  158. if cmd not in ('sign', 'wait'):
  159. for opt in ('no_summary', 'led', 'stealth_led', 'full_summary'):
  160. if getattr(cfg,opt):
  161. die(1, f'--{opt.replace("_","-")} makes no sense for the ‘{cmd}’ operation')
  162. asi = Autosign(cfg,cmd)
  163. cfg._post_init()
  164. if cmd == 'gen_key':
  165. asi.gen_key()
  166. elif cmd == 'setup':
  167. asi.setup()
  168. from .ui import keypress_confirm
  169. if cfg.xmrwallets and keypress_confirm( cfg, '\nContinue with Monero setup?', default_yes=True ):
  170. msg('')
  171. asi.xmr_setup()
  172. asi.do_umount()
  173. elif cmd == 'xmr_setup':
  174. if not cfg.xmrwallets:
  175. die(1,'Please specify a wallet or range of wallets with the --xmrwallets option')
  176. asi.do_mount()
  177. asi.xmr_setup()
  178. asi.do_umount()
  179. elif cmd == 'sign':
  180. main(do_loop=False)
  181. elif cmd == 'wait':
  182. main(do_loop=True)
  183. elif cmd == 'clean':
  184. asi.do_mount()
  185. asi.clean_old_files()
  186. asi.do_umount()
  187. elif cmd == 'wipe_key':
  188. asi.do_mount()
  189. asi.wipe_encryption_key()
  190. asi.do_umount()