main_cli.py 2.3 KB

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