mmgen-addrgen 6.8 KB

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