main_addrgen.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 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. if g.prog_name == 'mmgen-keygen':
  27. gen_what = 'keys'
  28. gen_desc = 'secret keys'
  29. opt_filter = None
  30. note_addrkey = 'By default, both addresses and secret keys are generated.\n\n'
  31. else:
  32. gen_what = 'addresses'
  33. gen_desc = 'addresses'
  34. opt_filter = 'hbcdeEiHOkKlpzPqrStUv-'
  35. note_addrkey = ''
  36. opts_data = {
  37. 'sets': [('print_checksum',True,'quiet',True)],
  38. 'text': {
  39. 'desc': """
  40. Generate a range or list of {desc} from an {pnm} wallet,
  41. mnemonic, seed or brainwallet
  42. """.format(desc=gen_desc,pnm=g.proj_name),
  43. 'usage':'[opts] [seed source] <index list or range(s)>',
  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. -E, --use-old-ed25519 Use original (and slow) ed25519 module for Monero
  52. address generation instead of ed25519ll_djbec
  53. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  54. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  55. 'f' at offset 'o' (comma-separated)
  56. -O, --old-incog-fmt Specify old-format incognito input
  57. -k, --use-internal-keccak-module Force use of the internal keccak module
  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. -U, --subwallet= U Generate {what} for subwallet 'U' (see SUBWALLETS
  74. below)
  75. -v, --verbose Produce more verbose output
  76. -x, --b16 Print secret keys in hexadecimal too
  77. """,
  78. 'notes': """
  79. NOTES FOR THIS COMMAND
  80. Address indexes are given as a comma-separated list and/or hyphen-separated
  81. range(s).
  82. {n_addrkey}If available, the secp256k1 library will be used for address generation.
  83. ADDRESS TYPES:
  84. {n_at}
  85. NOTES FOR ALL GENERATOR COMMANDS
  86. {n_sw}{n_pw}{n_bw}
  87. FMT CODES:
  88. {n_fmt}
  89. """
  90. },
  91. 'code': {
  92. 'options': lambda s: s.format(
  93. seed_lens=', '.join(map(str,g.seed_lens)),
  94. dmat="'{}' or '{}'".format(g.proto.dfl_mmtype,MMGenAddrType.mmtypes[g.proto.dfl_mmtype]['name']),
  95. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)]),
  96. kg=g.key_generator,
  97. pnm=g.proj_name,
  98. what=gen_what,
  99. g=g,
  100. ),
  101. 'notes': lambda s: s.format(
  102. n_addrkey=note_addrkey,
  103. n_sw=help_notes('subwallet')+'\n\n',
  104. n_pw=help_notes('passwd')+'\n\n',
  105. n_bw=help_notes('brainwallet'),
  106. n_fmt='\n '.join(SeedSource.format_fmt_codes().splitlines()),
  107. n_at='\n '.join(["'{}','{:<12} - {}".format(
  108. k,v['name']+"'",v['desc']) for k,v in list(MMGenAddrType.mmtypes.items())])
  109. )
  110. }
  111. }
  112. cmd_args = opts.init(opts_data,add_opts=['b16'],opt_filter=opt_filter)
  113. errmsg = "'{}': invalid parameter for --type option".format(opt.type)
  114. addr_type = MMGenAddrType(opt.type or g.proto.dfl_mmtype,errmsg=errmsg)
  115. if len(cmd_args) < 1: opts.usage()
  116. if opt.use_old_ed25519:
  117. msg('Using old (slow) ed25519 module by user request')
  118. idxs = AddrIdxList(fmt_str=cmd_args.pop())
  119. sf = get_seed_file(cmd_args,1)
  120. do_license_msg()
  121. ss = SeedSource(sf)
  122. ss_seed = ss.seed if opt.subwallet is None else ss.seed.subseed(opt.subwallet,print_msg=True)
  123. i = (gen_what=='addresses') or bool(opt.no_addresses)*2
  124. al = (KeyAddrList,AddrList,KeyList)[i](seed=ss_seed,addr_idxs=idxs,mmtype=addr_type)
  125. al.format()
  126. if al.gen_addrs and opt.print_checksum:
  127. Die(0,al.checksum)
  128. if al.gen_keys and keypress_confirm('Encrypt key list?'):
  129. al.encrypt()
  130. al.write_to_file(binary=True,desc='encrypted '+al.file_desc)
  131. else:
  132. al.write_to_file()