mmgen-passchg 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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-passchg: Change a mmgen deterministic wallet's passphrase, label or
  20. hash preset
  21. """
  22. import sys
  23. import mmgen.Opts as Opts
  24. from mmgen.utils import *
  25. from mmgen.config import *
  26. help_data = {
  27. 'desc': """Change the passphrase, hash preset or label of a {}
  28. deterministic wallet""".format(proj_name),
  29. 'usage': "[opts] [filename]",
  30. 'options': """
  31. -h, --help Print this help message
  32. -d, --outdir d Specify an alternate directory 'd' for output
  33. -k, --keep-old-passphrase Keep old passphrase (use when changing hash
  34. strength or label only)
  35. -L, --label Change the wallet's label
  36. -p, --hash-preset p Change scrypt.hash() parameters to preset 'p'
  37. (default: '{}')
  38. -P, --show-hash-presets Show information on available hash presets
  39. -v, --verbose Produce more verbose output
  40. NOTE: The key ID will change if either the passphrase or hash preset
  41. are changed
  42. """.format(hash_preset)
  43. }
  44. short_opts = "hd:kL:p:Pv"
  45. long_opts = "help","outdir=","keep_old_passphrase","label=","hash_preset=",\
  46. "show_hash_presets","verbose"
  47. opts,cmd_args = Opts.process_opts(sys.argv,help_data,short_opts,long_opts)
  48. if 'show_hash_presets' in opts: show_hash_presets()
  49. set_if_unset_and_typeconvert(opts,(('hash_preset',hash_preset,'str'),))
  50. check_opts(opts,('hash_preset','outdir','label'))
  51. if len(cmd_args) != 1:
  52. msg("One input file must be specified")
  53. sys.exit(2)
  54. infile = cmd_args[0]
  55. # Old key:
  56. label,metadata,hash_preset,salt,enc_seed = get_data_from_wallet(infile,opts)
  57. seed_id,key_id = metadata[:2]
  58. passwd = " ".join(get_words("","","Enter old passphrase: ",opts))
  59. key = make_key(passwd, salt, hash_preset)
  60. seed = decrypt_seed(enc_seed, key, seed_id, key_id)
  61. changed = {}
  62. if 'label' in opts:
  63. if opts['label'] != label:
  64. msg("Label changed: '%s' -> '%s'" % (label, opts['label']))
  65. changed['label'] = True
  66. else:
  67. msg("Label is unchanged: '%s'" % (label))
  68. else: opts['label'] = label # Copy the old label
  69. if 'hash_preset' in opts:
  70. if hash_preset != opts['hash_preset']:
  71. msg("Hash preset has changed (%s -> %s)" %
  72. (hash_preset, opts['hash_preset']))
  73. changed['preset'] = True
  74. else:
  75. msg("Hash preset is unchanged")
  76. else:
  77. opts['hash_preset'] = hash_preset
  78. if 'keep_old_passphrase' in opts:
  79. msg("Keeping old passphrase by user request")
  80. else:
  81. new_passwd = get_first_passphrase_from_user(
  82. "new passphrase", {'quiet': True })
  83. if new_passwd == passwd:
  84. msg("Passphrase is unchanged")
  85. else:
  86. msg("Passphrase has changed")
  87. passwd = new_passwd
  88. changed['passwd'] = True
  89. if 'preset' in changed or 'passwd' in changed: # Update key ID, salt
  90. msg("Will update salt and key ID")
  91. from hashlib import sha256
  92. from Crypto import Random
  93. salt = sha256(salt + Random.new().read(128)).digest()[:salt_len]
  94. key = make_key(passwd, salt, opts['hash_preset'])
  95. new_key_id = make_chksum_8(key)
  96. msg("Key ID changed: %s -> %s" % (key_id,new_key_id))
  97. key_id = new_key_id
  98. enc_seed = encrypt_seed(seed, key, opts)
  99. elif not 'label' in changed:
  100. msg("Data unchanged. No file will be written")
  101. sys.exit(2)
  102. write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts)