mmgen-addrgen 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013-2014 by 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 list or range of addresses from a mmgen
  20. deterministic wallet.
  21. Call as 'btc-keygen' to allow key generation as well.
  22. """
  23. import sys
  24. import mmgen.config as g
  25. from mmgen.Opts import *
  26. from mmgen.license import *
  27. from mmgen.util import *
  28. from mmgen.addr import *
  29. gen_what = "addresses" if sys.argv[0].split("-")[-1] == "addrgen" else "keys"
  30. help_data = {
  31. 'prog_name': sys.argv[0].split("/")[-1],
  32. 'desc': """Generate a list or range of {} from an {g.proj_name} wallet,
  33. mnemonic, seed or password""".format(gen_what,g=g),
  34. 'usage':"[opts] [infile] <address list>",
  35. 'options': """
  36. -h, --help Print this help message{}
  37. -d, --outdir= d Specify an alternate directory 'd' for output
  38. -e, --echo-passphrase Echo passphrase or mnemonic to screen upon entry
  39. -H, --show-hash-presets Show information on available hash presets
  40. -K, --no-keyconv Use internal libraries for address generation
  41. instead of 'keyconv'
  42. -l, --seed-len= N Length of seed. Options: {seed_lens}
  43. (default: {g.seed_len})
  44. -p, --hash-preset= p Use scrypt.hash() parameters from preset 'p' when
  45. hashing password (default: '{g.hash_preset}')
  46. -P, --passwd-file= f Get passphrase from file 'f'
  47. -q, --quiet Suppress warnings; overwrite files without
  48. prompting
  49. -S, --stdout Print {what} to stdout
  50. -v, --verbose Produce more verbose output{}
  51. -b, --from-brain= l,p Generate {what} from a user-created password,
  52. i.e. a "brainwallet", using seed length 'l' and
  53. hash preset 'p' (comma-separated)
  54. -g, --from-incog Generate {what} from an incognito wallet
  55. -X, --from-incog-hex Generate {what} from incognito hexadecimal wallet
  56. -G, --from-incog-hidden= f,o,l Generate {what} from incognito data in file
  57. 'f' at offset 'o', with seed length of 'l'
  58. -m, --from-mnemonic Generate {what} from an electrum-like mnemonic
  59. -s, --from-seed Generate {what} from a seed in .{g.seed_ext} format
  60. """.format(
  61. *(
  62. ("\n-A, --no-addresses Print only secret keys, no addresses",
  63. "\n-x, --b16 Print secret keys in hexadecimal too")
  64. if gen_what == "keys" else ("","")),
  65. seed_lens=", ".join([str(i) for i in g.seed_lens]),
  66. what=gen_what, g=g
  67. ),
  68. 'notes': """
  69. Addresses are given in a comma-separated list. Hyphen-separated ranges are
  70. also allowed.{}
  71. If available, the external 'keyconv' program will be used for address
  72. generation.
  73. Data for the --from-<what> options will be taken from <infile> if <infile>
  74. is specified. Otherwise, the user will be prompted to enter the data.
  75. For passphrases all combinations of whitespace are equal, and leading and
  76. trailing space are ignored. This permits reading passphrase data from a
  77. multi-line file with free spacing and indentation. This is particularly
  78. convenient for long brainwallet passphrases, for example.
  79. BRAINWALLET NOTE:
  80. As brainwallets require especially strong hashing to thwart dictionary
  81. attacks, the brainwallet hash preset must be specified by the user, using
  82. the 'p' parameter of the '--from-brain' option
  83. The '--from-brain' option also requires the user to specify a seed length
  84. (the 'l' parameter)
  85. For a brainwallet passphrase to always generate the same keys and addresses,
  86. the same 'l' and 'p' parameters to '--from-brain' must be used in all future
  87. invocations with that passphrase
  88. """.format("\n\nBy default, both addresses and secret keys are generated."
  89. if gen_what == "keys" else "")
  90. }
  91. opts,cmd_args = parse_opts(sys.argv,help_data)
  92. if 'show_hash_presets' in opts: show_hash_presets()
  93. if 'verbose' in opts: g.verbose = True
  94. if 'quiet' in opts: g.quiet = True
  95. if 'from_incog_hex' in opts or 'from_incog_hidden' in opts:
  96. opts['from_incog'] = True
  97. opts['gen_what'] = gen_what
  98. if g.debug: show_opts_and_cmd_args(opts,cmd_args)
  99. if len(cmd_args) == 1 and (
  100. 'from_mnemonic' in opts
  101. or 'from_brain' in opts
  102. or 'from_seed' in opts
  103. or 'from_incog_hidden' in opts
  104. ):
  105. infile,addr_idx_arg = "",cmd_args[0]
  106. elif len(cmd_args) == 2:
  107. infile,addr_idx_arg = cmd_args
  108. check_infile(infile)
  109. else: usage(help_data)
  110. addr_idxs = parse_address_list(addr_idx_arg)
  111. if not addr_idxs: sys.exit(2)
  112. do_license_msg()
  113. # Interact with user:
  114. if gen_what == "keys" and not g.quiet:
  115. confirm_or_exit(cmessages['unencrypted_secret_keys'], 'continue')
  116. # Generate data:
  117. if gen_what == "addresses":
  118. opts['print_addresses_only'] = True
  119. else:
  120. if not 'no_addresses' in opts: opts['print_secret'] = True
  121. seed = get_seed_retry(infile,opts)
  122. seed_id = make_chksum_8(seed)
  123. addr_data = generate_addrs(seed, addr_idxs, opts)
  124. addr_data_chksum = make_chksum_8(
  125. " ".join(["{} {}".format(a['num'],a['addr']) for a in addr_data]),
  126. sep=True
  127. )
  128. addr_data_str = format_addr_data(
  129. addr_data, addr_data_chksum, seed_id, addr_idxs, opts)
  130. # Output data:
  131. if 'stdout' in opts:
  132. if gen_what == "keys" and not g.quiet:
  133. confirm = True
  134. else: confirm = False
  135. write_to_stdout(addr_data_str,"secret keys",confirm)
  136. elif not sys.stdout.isatty():
  137. write_to_stdout(addr_data_str,"secret keys",confirm=False)
  138. else:
  139. write_addr_data_to_file(seed, addr_data_str, addr_idxs, opts)
  140. msg("""
  141. Checksum for address data {}[{}]: {}
  142. Remember this checksum or save it to a secure location
  143. """.format(seed_id, fmt_addr_idxs(addr_idxs), addr_data_chksum).strip())