main_xmrwallet.py 6.6 KB

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