mmgen-walletchk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-walletchk: Check integrity of a mmgen deterministic wallet, display
  20. information about it and export seed and mnemonic data
  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 get_seed_from_wallet,wallet_to_incog_data
  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 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. -m, --export-mnemonic Export the wallet's mnemonic to file
  48. -s, --export-seed Export the wallet's seed to file
  49. """.format(g=g),
  50. 'notes': """
  51. Since good randomness is particularly important for incognito wallets,
  52. the '--usr-randchars' option is turned on by default to gather additional
  53. entropy from the user when one of the '--export-incog*' options is
  54. selected. If you fully trust your OS's random number generator and wish
  55. to disable this option, then specify '-r0' on the command line.
  56. """
  57. }
  58. opts,cmd_args = parse_opts(sys.argv,help_data)
  59. if 'quiet' in opts: g.quiet = True
  60. if 'verbose' in opts: g.verbose = True
  61. if 'export_incog_hidden' in opts or 'export_incog_hex' in opts:
  62. opts['export_incog'] = True
  63. if len(cmd_args) != 1: usage(help_data)
  64. check_infile(cmd_args[0])
  65. if 'export_mnemonic' in opts:
  66. qmsg("Exporting mnemonic data to file by user request")
  67. elif 'export_seed' in opts:
  68. qmsg("Exporting seed data to file by user request")
  69. elif 'export_incog' in opts:
  70. if opts['usr_randchars'] == -1: opts['usr_randchars'] = g.usr_randchars_dfl
  71. qmsg("Exporting wallet to incognito format by user request")
  72. incog_enc,seed_id,key_id,iv_id,preset = \
  73. wallet_to_incog_data(cmd_args[0],opts)
  74. if "export_incog_hidden" in opts:
  75. export_to_hidden_incog(incog_enc,opts)
  76. else:
  77. seed_len = (len(incog_enc)-g.salt_len-g.aesctr_iv_len)*8
  78. fn = "%s-%s-%s[%s,%s].%s" % (
  79. seed_id, key_id, iv_id, seed_len, preset,
  80. g.incog_hex_ext if "export_incog_hex" in opts else g.incog_ext
  81. )
  82. data = pretty_hexdump(incog_enc,2,8,line_nums=False) \
  83. if "export_incog_hex" in opts else incog_enc
  84. export_to_file(fn, data, opts, "incognito wallet data")
  85. sys.exit()
  86. seed = get_seed_from_wallet(cmd_args[0], opts)
  87. if seed: qmsg("Wallet is OK")
  88. else:
  89. msg("Error opening wallet")
  90. sys.exit(2)
  91. if 'export_mnemonic' in opts:
  92. wl = get_default_wordlist()
  93. from mmgen.mnemonic import get_mnemonic_from_seed
  94. p = True if g.debug else False
  95. mn = get_mnemonic_from_seed(seed, wl, g.default_wl, print_info=p)
  96. fn = "%s.%s" % (make_chksum_8(seed).upper(), g.mn_ext)
  97. export_to_file(fn, " ".join(mn)+"\n", opts, "mnemonic data")
  98. elif 'export_seed' in opts:
  99. from mmgen.bitcoin import b58encode_pad
  100. data = col4(b58encode_pad(seed))
  101. chk = make_chksum_6(b58encode_pad(seed))
  102. fn = "%s.%s" % (make_chksum_8(seed).upper(), g.seed_ext)
  103. export_to_file(fn, "%s %s\n" % (chk,data), opts, "seed data")