mmgen-walletchk 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-walletchk: Check integrity of a mmgen deterministic wallet, display
  20. information about it and export seed and mnemonic data
  21. """
  22. import sys
  23. from mmgen.Opts import *
  24. from mmgen.util import *
  25. help_data = {
  26. 'prog_name': sys.argv[0].split("/")[-1],
  27. 'desc': """Check integrity of a %s deterministic wallet, display
  28. its information and export seed and mnemonic data."""\
  29. % g.proj_name,
  30. 'usage': "[opts] [filename]",
  31. 'options': """
  32. -h, --help Print this help message
  33. -d, --outdir d Specify an alternate directory 'd' for output
  34. -e, --echo-passphrase Print passphrase to screen when typing it
  35. -P, --passwd-file f Get passphrase from file 'f'
  36. -q, --quiet Suppress warnings; overwrite files without prompting
  37. -S, --stdout Print seed or mnemonic data to standard output
  38. -v, --verbose Produce more verbose output
  39. -g, --export-incog Export wallet to incognito format
  40. -X, --export-incog-hex Export wallet to incognito hexadecimal format
  41. -G, --export-incog-hidden f,o Hide incognito data in existing file 'f'
  42. at offset 'o' (comma-separated)
  43. -m, --export-mnemonic Export the wallet's mnemonic to file
  44. -s, --export-seed Export the wallet's seed to file
  45. """
  46. }
  47. short_opts = "hd:eP:qSvgXG:ms"
  48. long_opts = "help","outdir=","echo_passphrase","passwd_file=","quiet",\
  49. "stdout","verbose",\
  50. "export_incog","export_incog_hex","export_incog_hidden=",\
  51. "export_mnemonic","export_seed"
  52. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  53. if 'quiet' in opts: g.quiet = True
  54. if 'verbose' in opts: g.verbose = True
  55. if 'export_incog_hidden' in opts or 'export_incog_hex' in opts:
  56. opts['export_incog'] = True
  57. # Argument sanity checks and processing:
  58. check_opts(opts,long_opts)
  59. if len(cmd_args) != 1: usage(help_data)
  60. check_infile(cmd_args[0])
  61. if 'export_mnemonic' in opts:
  62. qmsg("Exporting mnemonic data to file by user request")
  63. elif 'export_seed' in opts:
  64. qmsg("Exporting seed data to file by user request")
  65. elif 'export_incog' in opts:
  66. qmsg("Exporting wallet to incognito format by user request")
  67. d = get_data_from_wallet(cmd_args[0],silent=True)
  68. seed_id,key_id,preset,salt,enc_seed = \
  69. d[1][0], d[1][1], d[2].split(":")[0], d[3], d[4]
  70. passwd = get_mmgen_passphrase("Enter mmgen passphrase: ",opts)
  71. key = make_key(passwd, salt, preset, "main key")
  72. # We don't need the seed; just do this to verify password.
  73. if decrypt_seed(enc_seed, key, seed_id, key_id) == False:
  74. sys.exit(2)
  75. from Crypto import Random
  76. iv = Random.new().read(g.aesctr_iv_len)
  77. iv_id = make_chksum_8(iv)
  78. qmsg("IV ID: %s" % iv_id)
  79. from binascii import hexlify
  80. from hashlib import sha256
  81. # IV is used BOTH to initialize counter and to salt password!
  82. key = make_key(passwd, iv, preset, "wrapper key")
  83. incog_enc = encrypt_seed(salt + enc_seed, key, iv=int(hexlify(iv),16))
  84. if "export_incog_hidden" in opts:
  85. fname,offset = opts['export_incog_hidden'].split(",") #Already sanity-checked
  86. offset = int(offset)
  87. check_data_fits_file_at_offset(fname,offset,len(iv + incog_enc),"write")
  88. if not g.quiet: confirm_or_exit("","alter file '%s'" % fname)
  89. f = os.open(fname,os.O_RDWR)
  90. os.lseek(f, offset, os.SEEK_SET)
  91. os.write(f, iv + incog_enc)
  92. os.close(f)
  93. qmsg("Data written to file '%s' at offset %s" % (fname,offset),
  94. "Data written to file")
  95. else:
  96. fn = "%s-%s-%s[%s,%s].%s" % (
  97. seed_id, key_id, iv_id, len(enc_seed)*8, preset,
  98. g.incog_hex_ext if "export_incog_hex" in opts else g.incog_ext
  99. )
  100. data = iv + incog_enc
  101. if "export_incog_hex" in opts:
  102. data = "".join([hexlify(data[i*2:i*2+2]) + (" " if (i+1)%8 else "\n")
  103. for i in range(len(data)/2)])
  104. export_to_file(fn, data, "incognito wallet data", opts)
  105. sys.exit()
  106. seed = get_seed_from_wallet(cmd_args[0], opts)
  107. if seed: qmsg("Wallet is OK")
  108. else:
  109. msg("Error opening wallet")
  110. sys.exit(2)
  111. if 'export_mnemonic' in opts:
  112. wl = get_default_wordlist()
  113. from mmgen.mnemonic import get_mnemonic_from_seed
  114. p = True if g.debug else False
  115. mn = get_mnemonic_from_seed(seed, wl, g.default_wl, print_info=p)
  116. fn = "%s.%s" % (make_chksum_8(seed).upper(), g.mn_ext)
  117. export_to_file(fn, " ".join(mn)+"\n", "mnemonic data", opts)
  118. elif 'export_seed' in opts:
  119. from mmgen.bitcoin import b58encode_pad
  120. data = col4(b58encode_pad(seed))
  121. chk = make_chksum_6(b58encode_pad(seed))
  122. fn = "%s.%s" % (make_chksum_8(seed).upper(), g.seed_ext)
  123. export_to_file(fn, "%s %s\n" % (chk,data), "seed data", opts)