main_addrgen.py 5.8 KB

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