main_walletchk.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2014 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-walletchk: Check integrity of an MMGen deterministic wallet, display
  20. information about it and export it to various formats
  21. """
  22. import sys
  23. import mmgen.config as g
  24. from mmgen.Opts import *
  25. from mmgen.util import *
  26. from mmgen.crypto import *
  27. help_data = {
  28. 'prog_name': g.prog_name,
  29. 'desc': """Check integrity of an {} deterministic wallet, display
  30. its information, and export seed and mnemonic data.
  31. """.format(g.proj_name),
  32. 'usage': "[opts] [filename]",
  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. -P, --passwd-file= f Get MMGen wallet passphrase from file 'f'
  38. -q, --quiet Suppress warnings; overwrite files without prompting
  39. -r, --usr-randchars= n Get 'n' characters of additional randomness from
  40. user (min={g.min_urandchars}, max={g.max_urandchars})
  41. -S, --stdout Print seed or mnemonic data to standard output
  42. -v, --verbose Produce more verbose output
  43. -g, --export-incog Export wallet to incognito format
  44. -X, --export-incog-hex Export wallet to incognito hexadecimal format
  45. -G, --export-incog-hidden=f,o Hide incognito data in existing file 'f'
  46. at offset 'o' (comma-separated)
  47. -o, --old-incog-fmt Use old (pre-0.7.8) incog format
  48. -m, --export-mnemonic Export the wallet's mnemonic to file
  49. -s, --export-seed Export the wallet's seed to file
  50. """.format(g=g),
  51. 'notes': """
  52. Since good randomness is particularly important for incognito wallets,
  53. the '--usr-randchars' option is turned on by default to gather additional
  54. entropy from the user when one of the '--export-incog*' options is
  55. selected. If you fully trust your OS's random number generator and wish
  56. to disable this option, then specify '-r0' on the command line.
  57. """
  58. }
  59. def wallet_to_incog_data(infile,opts):
  60. d = get_data_from_wallet(infile,silent=True)
  61. seed_id,key_id,preset,salt,enc_seed = \
  62. d[1][0], d[1][1], d[2].split(":")[0], d[3], d[4]
  63. while True:
  64. passwd = get_mmgen_passphrase("{} wallet".format(g.proj_name),opts)
  65. key = make_key(passwd, salt, preset, "main key")
  66. seed = decrypt_seed(enc_seed, key, seed_id, key_id)
  67. if seed: break
  68. iv = get_random(g.aesctr_iv_len,opts)
  69. iv_id = make_iv_chksum(iv)
  70. msg("Incog ID: %s" % iv_id)
  71. if not 'old_incog_fmt' in opts:
  72. salt = get_random(g.salt_len,opts)
  73. key = make_key(passwd, salt, preset, "incog wallet key")
  74. key_id = make_chksum_8(key)
  75. from hashlib import sha256
  76. chk = sha256(seed).digest()[:8]
  77. enc_seed = encrypt_data(chk+seed, key, 1, "seed")
  78. # IV is used BOTH to initialize counter and to salt password!
  79. key = make_key(passwd, iv, preset, "incog wrapper key")
  80. m = "incog data"
  81. wrap_enc = encrypt_data(salt + enc_seed, key, int(hexlify(iv),16), m)
  82. return iv+wrap_enc,seed_id,key_id,iv_id,preset
  83. def export_to_hidden_incog(incog_enc,opts):
  84. outfile,offset = opts['export_incog_hidden'].split(",") #Already sanity-checked
  85. if 'outdir' in opts: outfile = make_full_path(opts['outdir'],outfile)
  86. check_data_fits_file_at_offset(outfile,int(offset),len(incog_enc),"write")
  87. if not g.quiet: confirm_or_exit("","alter file '%s'" % outfile)
  88. import os
  89. f = os.open(outfile,os.O_RDWR)
  90. os.lseek(f, int(offset), os.SEEK_SET)
  91. os.write(f, incog_enc)
  92. os.close(f)
  93. msg("Data written to file '%s' at offset %s" %
  94. (os.path.relpath(outfile),offset))
  95. opts,cmd_args = parse_opts(sys.argv,help_data)
  96. if 'export_incog_hidden' in opts or 'export_incog_hex' in opts:
  97. opts['export_incog'] = True
  98. if len(cmd_args) != 1: usage(help_data)
  99. check_infile(cmd_args[0])
  100. if 'export_mnemonic' in opts:
  101. qmsg("Exporting mnemonic data to file by user request")
  102. elif 'export_seed' in opts:
  103. qmsg("Exporting seed data to file by user request")
  104. elif 'export_incog' in opts:
  105. if opts['usr_randchars'] == -1: opts['usr_randchars'] = g.usr_randchars_dfl
  106. qmsg("Exporting wallet to incognito format by user request")
  107. incog_enc,seed_id,key_id,iv_id,preset = \
  108. wallet_to_incog_data(cmd_args[0],opts)
  109. if "export_incog_hidden" in opts:
  110. export_to_hidden_incog(incog_enc,opts)
  111. else:
  112. z = 0 if 'old_incog_fmt' in opts else 8
  113. seed_len = (len(incog_enc)-g.salt_len-g.aesctr_iv_len-z)*8
  114. fn = "%s-%s-%s[%s,%s].%s" % (
  115. seed_id, key_id, iv_id, seed_len, preset,
  116. g.incog_hex_ext if "export_incog_hex" in opts else g.incog_ext
  117. )
  118. data = pretty_hexdump(incog_enc,2,8,line_nums=False) \
  119. if "export_incog_hex" in opts else incog_enc
  120. write_to_file_or_stdout(fn, data, opts, "incognito wallet data")
  121. sys.exit()
  122. seed = get_seed_retry(cmd_args[0], opts)
  123. if seed: msg("Wallet is OK")
  124. else:
  125. msg("Error opening wallet")
  126. sys.exit(2)
  127. if 'export_mnemonic' in opts:
  128. wl = get_default_wordlist()
  129. from mmgen.mnemonic import get_mnemonic_from_seed
  130. mn = get_mnemonic_from_seed(seed, wl, g.default_wl, g.debug)
  131. fn = "%s.%s" % (make_chksum_8(seed).upper(), g.mn_ext)
  132. write_to_file_or_stdout(fn, " ".join(mn)+"\n", opts, "mnemonic data")
  133. elif 'export_seed' in opts:
  134. from mmgen.bitcoin import b58encode_pad
  135. data = col4(b58encode_pad(seed))
  136. chk = make_chksum_6(b58encode_pad(seed))
  137. fn = "%s.%s" % (make_chksum_8(seed).upper(), g.seed_ext)
  138. write_to_file_or_stdout(fn, "%s %s\n" % (chk,data), opts, "seed data")