mmgen-walletgen 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 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-walletgen: Generate a mmgen deterministic wallet
  20. """
  21. import sys, os
  22. from hashlib import sha256
  23. from mmgen.Opts import *
  24. from mmgen.license import *
  25. import mmgen.config as g
  26. from mmgen.walletgen import *
  27. from mmgen.util import *
  28. prog_name = sys.argv[0].split("/")[-1]
  29. help_data = {
  30. 'prog_name': prog_name,
  31. 'desc': "Generate a {} deterministic wallet".format(g.proj_name),
  32. 'usage': "[opts] [infile]",
  33. 'options': """
  34. -h, --help Print this help message
  35. -d, --outdir d Specify an alternate directory 'd' for output
  36. -e, --echo-passphrase Print passphrase to screen when typing it
  37. -H, --show-hash-presets Show information on available hash presets
  38. -l, --seed-len n Create seed of length 'n'. Options: {}
  39. (default: {})
  40. -L, --label l Label to identify this wallet (32 chars max.
  41. Allowed symbols: A-Z, a-z, 0-9, " ", "_", ".")
  42. -p, --hash-preset p Use scrypt.hash() parameters from preset 'p'
  43. (default: '{}')
  44. -P, --passwd-file f Get passphrase from file 'f'
  45. -q, --quiet Produce quieter output; overwrite files without
  46. prompting
  47. -u, --usr-randlen n Get 'n' characters of randomness from the user
  48. (default: {})
  49. -v, --verbose Produce more verbose output
  50. -b, --from-brain l,p Generate wallet from a user-created passphrase,
  51. i.e. a "brainwallet", using seed length 'l' and
  52. hash preset 'p' (comma-separated)
  53. -g, --from-incognito Generate wallet from an incognito-format wallet
  54. -m, --from-mnemonic Generate wallet from an Electrum-like mnemonic
  55. -s, --from-seed Generate wallet from a seed in .{S} format
  56. By default (i.e. when invoked without any of the '--from-<what>' options),
  57. {} generates a wallet based on a random seed.
  58. Data for the --from-<what> options will be taken from <infile> if <infile>
  59. is specified. Otherwise, the user will be prompted to enter the data.
  60. For passphrases all combinations of whitespace are equal, and leading and
  61. trailing space are ignored. This permits reading passphrase data from a
  62. multi-line file with free spacing and indentation. This is particularly
  63. convenient for long brainwallet passphrases, for example.
  64. BRAINWALLET NOTE:
  65. As brainwallets require especially strong hashing to thwart dictionary
  66. attacks, the brainwallet hash preset must be specified by the user, using
  67. the 'p' parameter of the '--from-brain' option. This preset should be
  68. stronger than the one used for hashing the seed (i.e. the default value or
  69. the one specified in the '--hash-preset' option).
  70. The '--from-brain' option also requires the user to specify a seed length
  71. (the 'l' parameter), which overrides both the default and any one given in
  72. the '--seed-len' option.
  73. For a brainwallet passphrase to always generate the same keys and
  74. addresses, the same 'l' and 'p' parameters to '--from-brain' must be used
  75. in all future invocations with that passphrase.
  76. """.format(
  77. ",".join([str(i) for i in g.seed_lens]),
  78. g.seed_len,
  79. g.hash_preset,
  80. g.usr_randlen,
  81. prog_name,
  82. S=g.seed_ext,
  83. )
  84. }
  85. short_opts = "hd:eHl:L:p:P:qu:vb:gms"
  86. long_opts = "help","outdir=","echo_passphrase","show_hash_presets","seed_len=",\
  87. "label=","hash_preset=","passwd_file=","quiet","usr_randlen=","verbose",\
  88. "from_brain=","from_incognito","from_mnemonic","from_seed"
  89. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  90. if 'quiet' in opts: g.quiet = True
  91. if 'verbose' in opts: g.verbose = True
  92. if 'show_hash_presets' in opts: show_hash_presets()
  93. check_opts(opts,long_opts)
  94. if g.debug: show_opts_and_cmd_args(opts,cmd_args)
  95. if len(cmd_args) == 1:
  96. infile = cmd_args[0]
  97. check_infile(infile)
  98. ext = infile.split(".")[-1]
  99. ok_exts = g.seedfile_exts
  100. for e in ok_exts:
  101. if e == ext: break
  102. else:
  103. msg(
  104. "Input file must have one of the following extensions: .%s" % ", .".join(ok_exts))
  105. sys.exit(1)
  106. elif len(cmd_args) == 0:
  107. infile = ""
  108. else: usage(help_data)
  109. # Begin execution
  110. do_license_msg()
  111. qmsg_r("Acquiring random data from your computer...")
  112. from time import sleep
  113. sleep(0.25)
  114. try:
  115. from Crypto import Random
  116. r = Random.new()
  117. os_rand_data = (r.read(256),r.read(128))
  118. except:
  119. msg("FAILED\nUnable to generate random numbers. Exiting")
  120. sys.exit(2)
  121. qmsg("OK")
  122. if g.debug: display_os_random_data(os_rand_data)
  123. usr_keys,key_timings = get_random_data_from_user(opts)
  124. qmsg("")
  125. if g.debug: display_user_random_data(usr_keys,key_timings)
  126. usr_rand_data = sha256(usr_keys).digest() + \
  127. sha256("".join(key_timings)).digest()
  128. for i in 'from_mnemonic','from_brain','from_seed','from_incognito':
  129. if infile or (i in opts):
  130. seed = get_seed_retry(infile,opts)
  131. if "from_incognito" in opts or get_extension(infile) == g.incog_ext:
  132. qmsg(cmessages['incognito'] % make_chksum_8(seed))
  133. else: qmsg("")
  134. break
  135. else:
  136. # Truncate random data for smaller seed lengths
  137. seed = os_rand_data[0] + usr_rand_data
  138. seed = sha256(seed).digest()[:opts['seed_len']/8]
  139. salt = os_rand_data[1] + usr_rand_data
  140. salt = sha256(salt).digest()[:g.salt_len]
  141. qmsg("""Now you must choose a passphrase to encrypt the seed with. A key will be
  142. generated from your passphrase using a hash preset of '%s'. Please note that
  143. no strength checking of passphrases is performed. For an empty passphrase,
  144. just hit ENTER twice.
  145. """ % opts['hash_preset'])
  146. passwd = get_new_passphrase("{} wallet passphrase".format(g.proj_name), opts)
  147. key = make_key(passwd, salt, opts['hash_preset'])
  148. enc_seed = encrypt_seed(seed, key)
  149. write_wallet_to_file(seed,passwd,make_chksum_8(key),salt,enc_seed,opts)