main_cli.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 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-cli: Communicate with a coin daemon via its JSON-RPC interface
  12. """
  13. import asyncio, json
  14. from .util2 import cliargs_convert
  15. from .cfg import gc, Config
  16. from .rpc import rpc_init
  17. from .rpc.util import json_encoder
  18. opts_data = {
  19. 'text': {
  20. 'desc': 'Communicate with a coin daemon via its JSON-RPC interface',
  21. 'usage': '[opts] <command> <command args>',
  22. 'options': """
  23. -h, --help Print this help message
  24. --, --longhelp Print help message for long (global) options
  25. -a, --ascii-output Ensure that output is ASCII encoded
  26. -w, --wallet=NAME Use tracking wallet with name NAME
  27. """,
  28. 'notes': """
  29. The utility accepts all {pn} global configuration options and sources the user
  30. config file, allowing users to preconfigure hosts, ports, passwords, datadirs,
  31. tracking wallets and so forth, thus saving a great deal of typing at the
  32. command line. This behavior may be overridden with the --skip-cfg-file option.
  33. Arguments are given in JSON format, with lowercase ‘true’, ‘false’ and ‘null’
  34. for booleans and None, and double-quoted strings in dicts and lists.
  35. EXAMPLES
  36. $ mmgen-cli --wallet=wallet2 listreceivedbyaddress 0 true
  37. $ mmgen-cli --coin=ltc --rpc-host=orion getblockcount
  38. $ mmgen-cli --regtest=1 --wallet=bob getbalance
  39. $ mmgen-cli --coin=eth eth_getBalance 0x00000000219ab540356cBB839Cbe05303d7705Fa latest
  40. $ mmgen-cli createrawtransaction \\
  41. '[{{"txid":"832f5aa9af55dc453314e26869c8f96db1f2a9acac9f23ae18d396903971e0c6","vout":0}}]' \\
  42. '[{{"1111111111111111111114oLvT2":0.001}}]'
  43. """
  44. },
  45. 'code': {
  46. 'notes': lambda cfg, s, help_notes: s.format(
  47. pn = gc.proj_name)
  48. }
  49. }
  50. cfg = Config(opts_data=opts_data)
  51. cmd, *args = cfg._args
  52. async def main():
  53. c = await rpc_init(cfg)
  54. ret = await c.call(cmd, *cliargs_convert(args), wallet=cfg.wallet)
  55. print(
  56. (ascii(ret) if cfg.ascii_output else ret) if isinstance(ret, str) else
  57. json.dumps(ret, cls=json_encoder, indent=4, ensure_ascii=cfg.ascii_output))
  58. asyncio.run(main())