main_tool.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2017 Philemon <mmgen-py@yandex.com>
  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-tool: Perform various MMGen- and cryptocoin-related operations.
  20. Part of the MMGen suite
  21. """
  22. from mmgen.common import *
  23. stdin_msg = """
  24. To force a command to read from STDIN in place of its first argument (for
  25. supported commands), use '-' as the first argument.
  26. """.strip()
  27. cmd_help = """
  28. Cryptocoin address/key operations (compressed public keys supported):
  29. addr2hexaddr - convert coin address from base58 to hex format
  30. hex2wif - convert a private key from hex to WIF format (use 'pubkey_type=zcash_z' for zcash-z key)
  31. pubhash2addr - convert public key hash to address
  32. privhex2addr - generate coin address from private key in hex format
  33. privhex2pubhex - generate a hex public key from a hex private key
  34. pubhex2addr - convert a hex pubkey to an address
  35. pubhex2redeem_script - convert a hex pubkey to a witness redeem script
  36. wif2redeem_script - convert a WIF private key to a witness redeem script
  37. wif2segwit_pair - generate both a Segwit redeem script and address from WIF
  38. pubkey2addr - convert coin public key to address
  39. randpair - generate a random private key/address pair
  40. randwif - generate a random private key in WIF format
  41. wif2addr - generate a coin address from a key in WIF format
  42. wif2hex - convert a private key from WIF to hex format
  43. Wallet/TX operations (coin daemon must be running):
  44. getbalance - like '{pn}-cli getbalance' but shows confirmed/unconfirmed,
  45. spendable/unspendable balances for individual {pnm} wallets
  46. listaddress - list the specified {pnm} address and its balance
  47. listaddresses - list {pnm} addresses and their balances
  48. txview - show raw/signed {pnm} transaction in human-readable form
  49. twview - view tracking wallet
  50. General utilities:
  51. hexdump - encode data into formatted hexadecimal form (file or stdin)
  52. unhexdump - decode formatted hexadecimal data (file or stdin)
  53. bytespec - convert a byte specifier such as '1GB' into an integer
  54. hexlify - display string in hexadecimal format
  55. hexreverse - reverse bytes of a hexadecimal string
  56. rand2file - write 'n' bytes of random data to specified file
  57. randhex - print 'n' bytes (default 32) of random data in hex format
  58. hash256 - compute sha256(sha256(data)) (double sha256)
  59. hash160 - compute ripemd160(sha256(data)) (converts hexpubkey to hexaddr)
  60. b58randenc - generate a random 32-byte number and convert it to base 58
  61. b58tostr - convert a base 58 number to a string
  62. strtob58 - convert a string to base 58
  63. b58tohex - convert a base 58 number to hexadecimal
  64. hextob58 - convert a hexadecimal number to base 58
  65. b32tohex - convert a base 32 number to hexadecimal
  66. hextob32 - convert a hexadecimal number to base 32
  67. File encryption:
  68. encrypt - encrypt a file
  69. decrypt - decrypt a file
  70. {pnm} encryption suite:
  71. * Key: Scrypt (user-configurable hash parameters, 32-byte salt)
  72. * Enc: AES256_CTR, 16-byte rand IV, sha256 hash + 32-byte nonce + data
  73. * The encrypted file is indistinguishable from random data
  74. {pnm}-specific operations:
  75. add_label - add descriptive label for {pnm} address in tracking wallet
  76. remove_label - remove descriptive label for {pnm} address in tracking wallet
  77. addrfile_chksum - compute checksum for {pnm} address file
  78. keyaddrfile_chksum - compute checksum for {pnm} key-address file
  79. passwdfile_chksum - compute checksum for {pnm} password file
  80. find_incog_data - Use an Incog ID to find hidden incognito wallet data
  81. id6 - generate 6-character {pnm} ID for a file (or stdin)
  82. id8 - generate 8-character {pnm} ID for a file (or stdin)
  83. str2id6 - generate 6-character {pnm} ID for a string, ignoring spaces
  84. Mnemonic operations (choose 'electrum' (default), 'tirosh' or 'all'
  85. wordlists):
  86. mn_rand128 - generate random 128-bit mnemonic
  87. mn_rand192 - generate random 192-bit mnemonic
  88. mn_rand256 - generate random 256-bit mnemonic
  89. mn_stats - show stats for mnemonic wordlist
  90. mn_printlist - print mnemonic wordlist
  91. hex2mn - convert a 16, 24 or 32-byte number in hex format to a mnemonic
  92. mn2hex - convert a 12, 18 or 24-word mnemonic to a number in hex format
  93. IMPORTANT NOTE: Though {pnm} mnemonics use the Electrum wordlist, they're
  94. computed using a different algorithm and are NOT Electrum-compatible!
  95. {sm}
  96. """
  97. opts_data = lambda: {
  98. 'desc': 'Perform various {pnm}- and cryptocoin-related operations'.format(pnm=g.proj_name),
  99. 'usage': '[opts] <command> <command args>',
  100. 'options': """
  101. -d, --outdir= d Specify an alternate directory 'd' for output
  102. -h, --help Print this help message
  103. --, --longhelp Print help message for long options (common options)
  104. -P, --passwd-file= f Get passphrase from file 'f'.
  105. -q, --quiet Produce quieter output
  106. -r, --usr-randchars=n Get 'n' characters of additional randomness from
  107. user (min={g.min_urandchars}, max={g.max_urandchars})
  108. -t, --type=t Specify address type (valid options: 'compressed','segwit','zcash_z')
  109. -v, --verbose Produce more verbose output
  110. """.format(g=g),
  111. 'notes': """
  112. COMMANDS
  113. {ch}
  114. Type '{pn} help <command> for help on a particular command
  115. """.format( pn=g.prog_name,
  116. ch=cmd_help.format(
  117. pn=g.proto.name,
  118. pnm=g.proj_name,
  119. sm='\n '.join(stdin_msg.split('\n')))
  120. )
  121. }
  122. cmd_args = opts.init(opts_data,add_opts=['hidden_incog_input_params','in_fmt'])
  123. if len(cmd_args) < 1: opts.usage()
  124. Command = cmd_args.pop(0).capitalize()
  125. import mmgen.tool as tool
  126. if Command == 'Help' and not cmd_args: tool.usage(None)
  127. if Command not in tool.cmd_data:
  128. die(1,"'%s': no such command" % Command.lower())
  129. args,kwargs = tool.process_args(Command,cmd_args)
  130. try:
  131. ret = tool.__dict__[Command](*args,**kwargs)
  132. except Exception as e:
  133. die(1,'{}'.format(e))
  134. sys.exit(0 if ret in (None,True) else 1) # some commands die, some return False on failure