#!/usr/bin/env python # # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution # Copyright (C) 2013 by philemon # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . """ mmgen-passchg: Change a mmgen deterministic wallet's passphrase, label or hash preset """ import sys from mmgen.Opts import * from mmgen.utils import * import mmgen.config as g help_data = { 'desc': """Change the passphrase, hash preset or label of a {} deterministic wallet""".format(g.proj_name), 'usage': "[opts] [filename]", 'options': """ -h, --help Print this help message -d, --outdir d Specify an alternate directory 'd' for output -H, --show-hash-presets Show information on available hash presets -k, --keep-old-passphrase Keep old passphrase (use when changing hash strength or label only) -L, --label l Change the wallet's label to 'l' -p, --hash-preset p Change scrypt.hash() parameters to preset 'p' (default: '{}') -P, --passwd-file f Get new passphrase from file 'f' -v, --verbose Produce more verbose output NOTE: The key ID will change if either the passphrase or hash preset are changed """.format(g.hash_preset) } short_opts = "hd:HkL:p:P:v" long_opts = "help","outdir=","show_hash_presets","keep_old_passphrase",\ "label=","hash_preset=","passwd_file=","verbose" opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts) if 'show_hash_presets' in opts: show_hash_presets() check_opts(opts,long_opts) if len(cmd_args) != 1: msg("One input file must be specified") sys.exit(2) infile = cmd_args[0] # Old key: label,metadata,hash_preset,salt,enc_seed = get_data_from_wallet(infile,opts) seed_id,key_id = metadata[:2] # Repeat on incorrect pw entry prompt = "Enter %spassphrase: " % ("" if 'keep_old_passphrase' in opts else "old ") while True: passwd = get_mmgen_passphrase(prompt,{}) key = make_key(passwd, salt, hash_preset) seed = decrypt_seed(enc_seed, key, seed_id, key_id) if seed: break changed = {} if 'label' in opts: if opts['label'] != label: msg("Label changed: '%s' -> '%s'" % (label, opts['label'])) changed['label'] = True else: msg("Label is unchanged: '%s'" % (label)) else: opts['label'] = label # Copy the old label if 'hash_preset' in opts: if hash_preset != opts['hash_preset']: msg("Hash preset has changed (%s -> %s)" % (hash_preset, opts['hash_preset'])) changed['preset'] = True else: msg("Hash preset is unchanged") else: opts['hash_preset'] = hash_preset if 'keep_old_passphrase' in opts: msg("Keeping old passphrase by user request") else: new_passwd = get_new_passphrase("new passphrase", opts) if new_passwd == passwd: msg("Passphrase is unchanged") else: msg("Passphrase has changed") passwd = new_passwd changed['passwd'] = True if 'preset' in changed or 'passwd' in changed: # Update key ID, salt msg("Will update salt and key ID") from hashlib import sha256 from Crypto import Random salt = sha256(salt + Random.new().read(128)).digest()[:g.salt_len] key = make_key(passwd, salt, opts['hash_preset']) new_key_id = make_chksum_8(key) msg("Key ID changed: %s -> %s" % (key_id,new_key_id)) key_id = new_key_id enc_seed = encrypt_seed(seed, key, opts) elif not 'label' in changed: msg("Data unchanged. No file will be written") sys.exit(2) write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts)