main_addrgen.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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 MMGenAddrType
  25. from .addrlist import AddrList,KeyAddrList,KeyList,AddrIdxList
  26. from .addrfile import AddrFile
  27. from .wallet import Wallet
  28. if g.prog_name == 'mmgen-keygen':
  29. gen_what = 'keys'
  30. gen_clsname = 'KeyAddrList'
  31. gen_desc = 'secret keys'
  32. opt_filter = None
  33. note_addrkey = 'By default, both addresses and secret keys are generated.\n\n'
  34. else:
  35. gen_what = 'addresses'
  36. gen_clsname = 'AddrList'
  37. gen_desc = 'addresses'
  38. opt_filter = 'hbcdeEiHOkKlpzPqrStUv-'
  39. note_addrkey = ''
  40. opts_data = {
  41. 'sets': [('print_checksum',True,'quiet',True)],
  42. 'text': {
  43. 'desc': """
  44. Generate a range or list of {desc} from an {pnm} wallet,
  45. mnemonic, seed or brainwallet
  46. """.format(desc=gen_desc,pnm=g.proj_name),
  47. 'usage':'[opts] [seed source] <index list or range(s)>',
  48. 'options': """
  49. -h, --help Print this help message
  50. --, --longhelp Print help message for long options (common options)
  51. -A, --no-addresses Print only secret keys, no addresses
  52. -c, --print-checksum Print address list checksum and exit
  53. -d, --outdir= d Output files to directory 'd' instead of working dir
  54. -e, --echo-passphrase Echo passphrase or mnemonic to screen upon entry
  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, --keygen-backend=n Use backend 'n' for public key generation. Options
  61. for {coin_id}: {kgs}
  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 (< {dsl}-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,help_notes,s: s.format(
  95. dmat=help_notes('dfl_mmtype'),
  96. kgs=help_notes('keygen_backends'),
  97. coin_id=help_notes('coin_id'),
  98. dsl=help_notes('dfl_seed_len'),
  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=help_notes('fmt_codes'),
  109. n_at=help_notes('address_types'),
  110. )
  111. }
  112. }
  113. cmd_args = opts.init(opts_data,opt_filter=opt_filter)
  114. errmsg = f'{opt.type!r}: invalid parameter for --type option'
  115. from .protocol import init_proto_from_opts
  116. proto = init_proto_from_opts()
  117. addr_type = MMGenAddrType(
  118. proto = proto,
  119. id_str = opt.type or proto.dfl_mmtype,
  120. errmsg = errmsg )
  121. if len(cmd_args) < 1:
  122. opts.usage()
  123. if opt.keygen_backend:
  124. from .keygen import check_backend
  125. check_backend( proto, opt.keygen_backend, opt.type )
  126. idxs = AddrIdxList(fmt_str=cmd_args.pop())
  127. from .fileutil import get_seed_file
  128. sf = get_seed_file(cmd_args,1)
  129. do_license_msg()
  130. ss = Wallet(sf)
  131. ss_seed = ss.seed if opt.subwallet is None else ss.seed.subseed(opt.subwallet,print_msg=True)
  132. if opt.no_addresses:
  133. gen_clsname = 'KeyList'
  134. al = globals()[gen_clsname](
  135. proto = proto,
  136. seed = ss_seed,
  137. addr_idxs = idxs,
  138. mmtype = addr_type )
  139. af = al.get_file()
  140. af.format()
  141. if al.gen_addrs and opt.print_checksum:
  142. Die(0,al.checksum)
  143. if al.gen_keys and keypress_confirm('Encrypt key list?'):
  144. af.encrypt()
  145. af.write(
  146. binary = True,
  147. desc = f'encrypted {af.desc}' )
  148. else:
  149. af.write()