main_xmrwallet.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. mmgen-xmrwallet: Perform various Monero wallet and transacting operations for
  12. addresses in an MMGen XMR key-address file
  13. """
  14. import asyncio
  15. from .cfg import gc, Config
  16. from .util import die, fmt_dict
  17. from . import xmrwallet
  18. opts_data = {
  19. 'sets': [
  20. ('autosign', True, 'watch_only', True),
  21. ('autosign_mountpoint', bool, 'autosign', True),
  22. ('autosign_mountpoint', bool, 'watch_only', True),
  23. ],
  24. 'text': {
  25. 'desc': """Perform various Monero wallet and transacting operations for
  26. addresses in an MMGen XMR key-address file""",
  27. 'usage2': [
  28. '[opts] create | sync | list | view | listview | dump | restore [xmr_keyaddrfile] [wallets]',
  29. '[opts] label [xmr_keyaddrfile] LABEL_SPEC',
  30. '[opts] new [xmr_keyaddrfile] NEW_ADDRESS_SPEC',
  31. '[opts] transfer [xmr_keyaddrfile] TRANSFER_SPEC',
  32. '[opts] sweep | sweep_all [xmr_keyaddrfile] SWEEP_SPEC',
  33. '[opts] submit [TX_file]',
  34. '[opts] relay <TX_file>',
  35. '[opts] resubmit | abort (for use with --autosign only)',
  36. '[opts] txview | txlist [TX_file] ...',
  37. '[opts] export-outputs | export-outputs-sign | import-key-images [wallets]',
  38. ],
  39. 'options': """
  40. -h, --help Print this help message
  41. --, --longhelp Print help message for long (global) options
  42. -a, --autosign Use appropriate outdir and other params for
  43. autosigning operations (implies --watch-only).
  44. When this option is in effect, filename argu-
  45. ments must be omitted, as files are located
  46. automatically.
  47. -f, --priority=N Specify an integer priority ‘N’ for inclusion
  48. of a transaction in the blockchain (higher
  49. number means higher fee). Valid parameters:
  50. {tp}. If option
  51. is omitted, the default priority will be used
  52. -F, --full-address Print addresses in full instead of truncating
  53. -m, --autosign-mountpoint=P Specify the autosign mountpoint (defaults to
  54. ‘/mnt/mmgen_autosign’, implies --autosign)
  55. -b, --rescan-blockchain Rescan the blockchain if wallet fails to sync
  56. -d, --outdir=D Save transaction files to directory 'D'
  57. instead of the working directory
  58. -D, --daemon=H:P Connect to the monerod at {D}
  59. -e, --skip-empty-accounts Skip display of empty accounts in wallets
  60. where applicable
  61. -E, --skip-empty-addresses Skip display of used empty addresses in
  62. wallets where applicable
  63. -k, --use-internal-keccak-module Force use of the internal keccak module
  64. -p, --hash-preset=P Use scrypt hash preset 'P' for password
  65. hashing (default: '{gc.dfl_hash_preset}')
  66. -P, --rescan-spent Perform a rescan of spent outputs. Used only
  67. with the ‘export-outputs-sign’ operation
  68. -R, --tx-relay-daemon=H:P[:H:P] Relay transactions via a monerod specified by
  69. {R}
  70. -r, --restore-height=H Scan from height 'H' when creating wallets.
  71. Use special value ‘current’ to create empty
  72. wallet at current blockchain height.
  73. -R, --no-relay Save transaction to file instead of relaying
  74. -s, --no-start-wallet-daemon Don’t start the wallet daemon at startup
  75. -S, --no-stop-wallet-daemon Don’t stop the wallet daemon at exit
  76. -W, --watch-only Create or operate on watch-only wallets
  77. -w, --wallet-dir=D Output or operate on wallets in directory 'D'
  78. instead of the working directory
  79. -U, --wallet-rpc-user=user Wallet RPC username (currently: {cfg.monero_wallet_rpc_user!r})
  80. -P, --wallet-rpc-password=pass Wallet RPC password (currently: [scrubbed])
  81. """,
  82. 'notes': """
  83. {xmrwallet_help}
  84. """
  85. },
  86. 'code': {
  87. 'options': lambda cfg, s: s.format(
  88. D=xmrwallet.uarg_info['daemon'].annot,
  89. R=xmrwallet.uarg_info['tx_relay_daemon'].annot,
  90. cfg=cfg,
  91. gc=gc,
  92. tp=fmt_dict(xmrwallet.tx_priorities, fmt='equal_compact')
  93. ),
  94. 'notes': lambda help_mod, s: s.format(
  95. xmrwallet_help = help_mod('xmrwallet')
  96. )
  97. }
  98. }
  99. cfg = Config(opts_data=opts_data, init_opts={'coin':'xmr'})
  100. cmd_args = cfg._args
  101. if cmd_args and cfg.autosign and (
  102. cmd_args[0] in (
  103. xmrwallet.kafile_arg_ops
  104. + ('export-outputs', 'export-outputs-sign', 'import-key-images', 'txview', 'txlist')
  105. )
  106. or len(cmd_args) == 1 and cmd_args[0] in ('submit', 'resubmit', 'abort')
  107. ):
  108. cmd_args.insert(1, None)
  109. if len(cmd_args) < 2:
  110. cfg._usage()
  111. op = cmd_args.pop(0)
  112. infile = cmd_args.pop(0)
  113. wallets = spec = None
  114. if op in ('relay', 'submit', 'resubmit', 'abort'):
  115. if len(cmd_args) != 0:
  116. cfg._usage()
  117. elif op in ('txview', 'txlist'):
  118. infile = [infile] + cmd_args
  119. elif op in ('create', 'sync', 'list', 'view', 'listview', 'dump', 'restore'): # kafile_arg_ops
  120. if len(cmd_args) > 1:
  121. cfg._usage()
  122. wallets = cmd_args.pop(0) if cmd_args else None
  123. elif op in ('new', 'transfer', 'sweep', 'sweep_all', 'label'):
  124. if len(cmd_args) != 1:
  125. cfg._usage()
  126. spec = cmd_args[0]
  127. elif op in ('export-outputs', 'export-outputs-sign', 'import-key-images'):
  128. if not cfg.autosign:
  129. die(1, f'--autosign must be used with command {op!r}')
  130. if len(cmd_args) > 1:
  131. cfg._usage()
  132. wallets = cmd_args.pop(0) if cmd_args else None
  133. else:
  134. die(1, f'{op!r}: unrecognized operation')
  135. m = xmrwallet.op(op, cfg, infile, wallets, spec)
  136. if asyncio.run(m.main()):
  137. m.post_main_success()
  138. else:
  139. m.post_main_failure()