main_addrgen.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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-addrgen: Generate a series or range of addresses from an MMGen
  20. deterministic wallet
  21. """
  22. from mmgen.common import *
  23. from mmgen.crypto import *
  24. from mmgen.addr import *
  25. from mmgen.seed import SeedSource
  26. if sys.argv[0].split('-')[-1] == 'keygen':
  27. gen_what = 'keys'
  28. opt_filter = None
  29. note1 = """
  30. By default, both addresses and secret keys are generated.
  31. """.strip()
  32. else:
  33. gen_what = 'addresses'
  34. opt_filter = 'hbcdeiHOKlpzPqrSv-'
  35. note1 = """
  36. If available, the external 'keyconv' program will be used for address
  37. generation.
  38. """.strip()
  39. opts_data = {
  40. 'sets': [('print_checksum',True,'quiet',True)],
  41. 'desc': """Generate a range or list of {what} from an {pnm} wallet,
  42. mnemonic, seed or password""".format(what=gen_what,pnm=g.proj_name),
  43. 'usage':'[opts] [infile] <range or list of address indexes>',
  44. 'options': """
  45. -h, --help Print this help message
  46. --, --longhelp Print help message for long options (common options)
  47. -A, --no-addresses Print only secret keys, no addresses
  48. -c, --print-checksum Print address list checksum and exit
  49. -d, --outdir= d Output files to directory 'd' instead of working dir
  50. -e, --echo-passphrase Echo passphrase or mnemonic to screen upon entry
  51. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  52. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  53. 'f' at offset 'o' (comma-separated)
  54. -O, --old-incog-fmt Specify old-format incognito input
  55. -K, --key-generator=m Use method 'm' for public key generation
  56. Options: {kgs} (default: {kg})
  57. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  58. is required only for brainwallet and incognito inputs
  59. with non-standard (< {g.seed_len}-bit) seed lengths
  60. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  61. for password hashing (default: '{g.hash_preset}')
  62. -z, --show-hash-presets Show information on available hash presets
  63. -P, --passwd-file= f Get wallet passphrase from file 'f'
  64. -q, --quiet Produce quieter output; suppress some warnings
  65. -r, --usr-randchars=n Get 'n' characters of additional randomness from user
  66. (min={g.min_urandchars}, max={g.max_urandchars}, default={g.usr_randchars})
  67. -S, --stdout Print {what} to stdout
  68. -v, --verbose Produce more verbose output
  69. -x, --b16 Print secret keys in hexadecimal too
  70. """.format(
  71. seed_lens=', '.join([str(i) for i in g.seed_lens]),
  72. pnm=g.proj_name,
  73. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)]),
  74. kg=g.key_generator,
  75. what=gen_what,g=g
  76. ),
  77. 'notes': """
  78. Address indexes are given in a comma-separated list and/or hyphen-separated ranges.
  79. {n}
  80. {o.pw_note}
  81. {o.bw_note}
  82. FMT CODES:
  83. {f}
  84. """.format(
  85. n=note1,
  86. f='\n '.join(SeedSource.format_fmt_codes().splitlines()),
  87. o=opts
  88. )
  89. }
  90. cmd_args = opts.init(opts_data,add_opts=['b16'],opt_filter=opt_filter)
  91. if len(cmd_args) < 1: opts.usage()
  92. idxs = AddrIdxList(fmt_str=cmd_args.pop())
  93. sf = get_seed_file(cmd_args,1)
  94. do_license_msg()
  95. ss = SeedSource(sf)
  96. i = (gen_what=='addresses') or bool(opt.no_addresses)*2
  97. al = (KeyAddrList,AddrList,KeyList)[i](seed=ss.seed,addr_idxs=idxs)
  98. al.format()
  99. if al.gen_addrs and opt.print_checksum:
  100. Die(0,al.checksum)
  101. if al.gen_keys and keypress_confirm('Encrypt key list?'):
  102. al.encrypt()
  103. al.write_to_file(binary=True)
  104. else:
  105. al.write_to_file()