main_autosign.py 8.0 KB

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