main_autosign.py 9.7 KB

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