main_autosign.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 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. -C, --clean Remove unneeded files on the removable device
  33. -I, --no-insert-check Don’t check for device insertion
  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. gen_key - generate the wallet encryption key and copy it to the mountpoint
  54. {asi.mountpoint!r} (as currently configured)
  55. setup - generate both wallet encryption key and temporary signing wallet
  56. wait - start in loop mode: wait-mount-sign-unmount-wait
  57. USAGE NOTES
  58. If no operation is specified, this program mounts a removable device
  59. (typically a USB flash drive) containing unsigned MMGen transactions, message
  60. files, and/or XMR wallet output files, signs them, unmounts the removable
  61. device and exits.
  62. If invoked with ‘wait’, the program waits in a loop, mounting the removable
  63. device, performing signing operations and unmounting the device every time it
  64. is inserted.
  65. On supported platforms (currently Orange Pi, Rock Pi and Raspberry Pi boards),
  66. the status LED indicates whether the program is busy or in standby mode, i.e.
  67. ready for device insertion or removal.
  68. The removable device must have a partition labeled MMGEN_TX with a user-
  69. writable root directory and a directory named ‘/tx’, where unsigned MMGen
  70. transactions are placed. Optionally, the directory ‘/msg’ may be created
  71. and unsigned message files produced by ‘mmgen-msg’ placed there.
  72. On both the signing and online machines the mountpoint ‘{asi.mountpoint}’
  73. (as currently configured) must exist and ‘/etc/fstab’ must contain the
  74. following entry:
  75. LABEL=MMGEN_TX {asi.mountpoint} auto noauto,user 0 0
  76. Signing is performed with a temporary wallet created in volatile memory in
  77. the directory ‘{asi.wallet_dir}’ (as currently configured). The wallet is
  78. encrypted with a 32-byte password saved in the file ‘autosign.key’ in the
  79. root of the removable device’s filesystem.
  80. The password and temporary wallet may be created in one operation by invoking
  81. ‘mmgen-autosign setup’ with the removable device inserted. In this case, the
  82. temporary wallet is created from the user’s default wallet, if it exists and
  83. the user so desires. If not, the user is prompted to enter a seed phrase.
  84. Alternatively, the password and temporary wallet may be created separately by
  85. first invoking ‘mmgen-autosign gen_key’ and then creating and encrypting the
  86. wallet using the -P (--passwd-file) option:
  87. $ mmgen-walletconv -iwords -d{asi.wallet_dir} -p1 -N -P{asi.mountpoint}/autosign.key -Lfoo
  88. Note that the hash preset must be ‘1’. To use a wallet file as the source
  89. instead of an MMGen seed phrase, omit the ‘-i’ option and add the wallet
  90. file path to the end of the command line. Multiple temporary wallets may
  91. be created in this way and used for signing (note, however, that for XMR
  92. operations only one wallet is supported).
  93. Autosigning is currently available only on Linux-based platforms.
  94. SECURITY NOTE
  95. By placing wallet and password on separate devices, this program creates
  96. a two-factor authentication setup whereby an attacker must gain physical
  97. control of both the removable device and signing machine in order to sign
  98. transactions. It’s therefore recommended to always keep the removable device
  99. secure, separated from the signing machine and hidden (in your pocket, for
  100. example) when not transacting. In addition, since login access on the
  101. signing machine is required to steal the user’s seed, it’s good practice
  102. to lock the signing machine’s screen once the setup process is complete.
  103. As a last resort, cutting power to the signing machine will destroy the
  104. volatile memory where the temporary wallet resides and foil any attack,
  105. even if you’ve lost control of the removable device.
  106. Always remember to power off the signing machine when your signing session
  107. is over.
  108. """
  109. },
  110. 'code': {
  111. 'options': lambda s: s.format(
  112. asi = asi,
  113. mn_fmts = fmt_list( asi.mn_fmts, fmt='no_spc' ),
  114. ),
  115. 'notes': lambda s: s.format(asi=asi)
  116. }
  117. }
  118. def main(do_loop):
  119. asi.init_led()
  120. asi.init_exit_handler()
  121. async def do():
  122. await asi.check_daemons_running()
  123. if do_loop:
  124. await asi.do_loop()
  125. else:
  126. ret = await asi.do_sign()
  127. asi.at_exit(not ret)
  128. async_run(do())
  129. from .autosign import Autosign,AutosignConfig
  130. cfg = AutosignConfig(
  131. opts_data = opts_data,
  132. init_opts = {
  133. 'quiet': True,
  134. 'out_fmt': 'wallet',
  135. 'usr_randchars': 0,
  136. 'hash_preset': '1',
  137. 'label': 'Autosign Wallet',
  138. },
  139. do_post_init = True )
  140. cmd_args = cfg._args
  141. asi = Autosign(cfg)
  142. cfg._post_init()
  143. if len(cmd_args) not in (0,1):
  144. cfg._opts.usage()
  145. if cmd_args:
  146. cmd = cmd_args[0]
  147. if cmd == 'gen_key':
  148. asi.gen_key()
  149. elif cmd == 'setup':
  150. asi.setup()
  151. from .ui import keypress_confirm
  152. if cfg.xmrwallets and keypress_confirm( cfg, '\nContinue with Monero setup?', default_yes=True ):
  153. msg('')
  154. asi.xmr_setup()
  155. elif cmd == 'wait':
  156. main(do_loop=True)
  157. elif cmd == 'clean':
  158. asi.do_mount()
  159. asi.clean_old_files()
  160. asi.do_umount()
  161. else:
  162. die(1,f'{cmd!r}: unrecognized command')
  163. else:
  164. main(do_loop=False)