main_addrgen.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2018 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-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. MAT = MMGenAddrType
  27. if sys.argv[0].split('-')[-1] == 'keygen':
  28. gen_what = 'keys'
  29. gen_desc = 'secret keys'
  30. opt_filter = None
  31. note_addrkey = 'By default, both addresses and secret keys are generated.\n\n'
  32. else:
  33. gen_what = 'addresses'
  34. gen_desc = 'addresses'
  35. opt_filter = 'hbcdeEiHOKlpzPqrStv-'
  36. note_addrkey = ''
  37. note_secp256k1 = """
  38. If available, the secp256k1 library will be used for address generation.
  39. """.strip()
  40. opts_data = lambda: {
  41. 'sets': [('print_checksum',True,'quiet',True)],
  42. 'desc': """Generate a range or list of {desc} from an {pnm} wallet,
  43. mnemonic, seed or brainwallet""".format(desc=gen_desc,pnm=g.proj_name),
  44. 'usage':'[opts] [seed source] <index list or range(s)>',
  45. 'options': """
  46. -h, --help Print this help message
  47. --, --longhelp Print help message for long options (common options)
  48. -A, --no-addresses Print only secret keys, no addresses
  49. -c, --print-checksum Print address list checksum and exit
  50. -d, --outdir= d Output files to directory 'd' instead of working dir
  51. -e, --echo-passphrase Echo passphrase or mnemonic to screen upon entry
  52. -E, --use-internal-ed25519-mod Use (slow) internal ed25519 module for Monero
  53. address generation, even if ed25519ll is installed
  54. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  55. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  56. 'f' at offset 'o' (comma-separated)
  57. -O, --old-incog-fmt Specify old-format incognito input
  58. -K, --key-generator=m Use method 'm' for public key generation
  59. Options: {kgs} (default: {kg})
  60. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  61. is required only for brainwallet and incognito inputs
  62. with non-standard (< {g.seed_len}-bit) seed lengths
  63. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  64. for password hashing (default: '{g.hash_preset}')
  65. -z, --show-hash-presets Show information on available hash presets
  66. -P, --passwd-file= f Get wallet passphrase from file 'f'
  67. -q, --quiet Produce quieter output; suppress some warnings
  68. -r, --usr-randchars=n Get 'n' characters of additional randomness from user
  69. (min={g.min_urandchars}, max={g.max_urandchars}, default={g.usr_randchars})
  70. -S, --stdout Print {what} to stdout
  71. -t, --type=t Choose address type. Options: see ADDRESS TYPES below
  72. (default: {dmat})
  73. -v, --verbose Produce more verbose output
  74. -x, --b16 Print secret keys in hexadecimal too
  75. """.format(
  76. seed_lens=', '.join(map(str,g.seed_lens)),
  77. pnm=g.proj_name,
  78. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)]),
  79. kg=g.key_generator,
  80. what=gen_what,g=g,
  81. dmat="'{}' or '{}'".format(g.proto.dfl_mmtype,MAT.mmtypes[g.proto.dfl_mmtype]['name'])
  82. ),
  83. 'notes': u"""
  84. NOTES FOR THIS COMMAND
  85. Address indexes are given as a comma-separated list and/or hyphen-separated
  86. range(s).
  87. {n_addrkey}{n_secp}
  88. ADDRESS TYPES:
  89. {n_at}
  90. NOTES FOR ALL GENERATOR COMMANDS
  91. {n_pw}
  92. {n_bw}
  93. FMT CODES:
  94. {n_fmt}
  95. """.format(
  96. n_secp=note_secp256k1,
  97. n_addrkey=note_addrkey,
  98. n_pw=help_notes('passwd'),
  99. n_bw=help_notes('brainwallet'),
  100. n_fmt='\n '.join(SeedSource.format_fmt_codes().splitlines()),
  101. n_at='\n '.join(["'{}','{:<12} - {}".format(k,v['name']+"'",v['desc']) for k,v in MAT.mmtypes.items()]),
  102. o=opts
  103. )
  104. }
  105. cmd_args = opts.init(opts_data,add_opts=['b16'],opt_filter=opt_filter)
  106. errmsg = "'{}': invalid parameter for --type option".format(opt.type)
  107. addr_type = MAT(opt.type or g.proto.dfl_mmtype,errmsg=errmsg)
  108. if len(cmd_args) < 1: opts.usage()
  109. if opt.use_internal_ed25519_mod:
  110. msg('Using (slow) internal ed25519 module by user request')
  111. idxs = AddrIdxList(fmt_str=cmd_args.pop())
  112. sf = get_seed_file(cmd_args,1)
  113. do_license_msg()
  114. ss = SeedSource(sf)
  115. i = (gen_what=='addresses') or bool(opt.no_addresses)*2
  116. al = (KeyAddrList,AddrList,KeyList)[i](seed=ss.seed,addr_idxs=idxs,mmtype=addr_type)
  117. al.format()
  118. if al.gen_addrs and opt.print_checksum:
  119. Die(0,al.checksum)
  120. if al.gen_keys and keypress_confirm('Encrypt key list?'):
  121. al.encrypt()
  122. al.write_to_file(binary=True,desc='encrypted '+al.file_desc)
  123. else:
  124. al.write_to_file()