electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 63c7a27d7e35b4ae13a5eeb2b673a841a8c25242
parent 0e3500469e7c865f1db421e39a146ababba40118
Author: ThomasV <thomasv@gitorious>
Date:   Wed,  3 Dec 2014 22:35:05 +0100

add InvalidPassword exception

Diffstat:
Melectrum | 4++--
Mgui/gtk.py | 4++--
Mgui/qt/main_window.py | 10+++++-----
Mgui/qt/password_dialog.py | 4++--
Mlib/account.py | 7++++---
Mlib/bitcoin.py | 4++--
Mlib/util.py | 7++++++-
Mlib/wallet.py | 12++++++++----
8 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/electrum b/electrum @@ -45,7 +45,7 @@ if __builtin__.use_local_modules: from electrum import util from electrum import SimpleConfig, Network, Wallet, WalletStorage, NetworkProxy, Commands, known_commands, pick_random_server -from electrum.util import print_msg, print_stderr, print_json, set_verbosity +from electrum.util import print_msg, print_stderr, print_json, set_verbosity, InvalidPassword from electrum.daemon import get_daemon from electrum.plugins import init_plugins @@ -360,7 +360,7 @@ if __name__ == '__main__': # check password try: seed = wallet.get_seed(password) - except Exception: + except InvalidPassword: print_msg("Error: This password does not decode this wallet.") sys.exit(1) else: diff --git a/gui/gtk.py b/gui/gtk.py @@ -24,7 +24,7 @@ import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GObject, cairo from decimal import Decimal -from electrum.util import print_error +from electrum.util import print_error, InvalidPassword from electrum.bitcoin import is_valid from electrum import WalletStorage, Wallet @@ -596,7 +596,7 @@ class ElectrumWindow: try: wallet.get_seed(password) - except Exception: + except InvalidPassword: show_message("Incorrect password") return diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py @@ -1912,8 +1912,8 @@ class ElectrumWindow(QMainWindow): try: mnemonic = self.wallet.get_mnemonic(password) - except Exception: - QMessageBox.warning(self, _('Error'), _('Incorrect Password'), _('OK')) + except BaseException as e: + QMessageBox.warning(self, _('Error'), str(e), _('OK')) return from seed_dialog import SeedDialog d = SeedDialog(self, mnemonic, self.wallet.has_imported_keys()) @@ -2312,10 +2312,10 @@ class ElectrumWindow(QMainWindow): try: mnemonic = self.wallet.get_mnemonic(password) - except Exception: - QMessageBox.warning(self, _('Error'), _('Incorrect Password'), _('OK')) + except Exception as e: + QMessageBox.warning(self, _('Error'), str(e), _('OK')) return - + d = QDialog(self) d.setWindowTitle(_('Private keys')) d.setMinimumSize(850, 300) diff --git a/gui/qt/password_dialog.py b/gui/qt/password_dialog.py @@ -157,8 +157,8 @@ class PasswordDialog(QDialog): try: self.wallet.check_password(password) - except Exception: - QMessageBox.warning(self.parent, _('Error'), _('Incorrect Password'), _('OK')) + except BaseException as e: + QMessageBox.warning(self.parent, _('Error'), str(e), _('OK')) return False, None, None try: diff --git a/lib/account.py b/lib/account.py @@ -20,7 +20,7 @@ import bitcoin from bitcoin import * from i18n import _ from transaction import Transaction, is_extended_pubkey -from util import print_msg +from util import print_msg, InvalidPassword class Account(object): @@ -145,7 +145,8 @@ class ImportedAccount(Account): address = self.get_addresses(0)[i] pk = pw_decode(self.keypairs[address][1], password) # this checks the password - assert address == address_from_private_key(pk) + if address != address_from_private_key(pk): + raise InvalidPassword() return [pk] def has_change(self): @@ -242,7 +243,7 @@ class OldAccount(Account): master_public_key = master_private_key.get_verifying_key().to_string() if master_public_key != self.mpk: print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex')) - raise Exception('Invalid password') + raise InvalidPassword() return True def get_master_pubkeys(self): diff --git a/lib/bitcoin.py b/lib/bitcoin.py @@ -24,7 +24,7 @@ import sys import hmac import version -from util import print_error +from util import print_error, InvalidPassword try: import ecdsa @@ -98,7 +98,7 @@ def pw_decode(s, password): try: d = DecodeAES(secret, s).decode("utf8") except Exception: - raise Exception('Invalid password') + raise InvalidPassword() return d else: return s diff --git a/lib/util.py b/lib/util.py @@ -2,10 +2,14 @@ import os, sys, re, json import platform import shutil from datetime import datetime -is_verbose = False +from i18n import _ class NotEnoughFunds(Exception): pass +class InvalidPassword(Exception): + def __str__(self): + return _("Incorrect password") + class MyEncoder(json.JSONEncoder): def default(self, obj): from transaction import Transaction @@ -14,6 +18,7 @@ class MyEncoder(json.JSONEncoder): return super(MyEncoder, self).default(obj) +is_verbose = False def set_verbosity(b): global is_verbose is_verbose = b diff --git a/lib/wallet.py b/lib/wallet.py @@ -230,7 +230,8 @@ class Abstract_Wallet(object): sec = pw_decode(v, password) pubkey = public_key_from_private_key(sec) address = public_key_to_bc_address(pubkey.decode('hex')) - assert address == k + if address != k: + raise InvalidPassword() self.import_key(sec, password) self.imported_keys.pop(k) self.storage.put('imported_keys', self.imported_keys) @@ -713,8 +714,6 @@ class Abstract_Wallet(object): if total >= amount + fee: break else: raise NotEnoughFunds() - #print_error("Not enough funds", total, amount, fee) - #return None # change address if not change_addr: @@ -1284,12 +1283,17 @@ class BIP32_Wallet(Deterministic_Wallet): k = self.master_private_keys.get(account) if not k: return xprv = pw_decode(k, password) + try: + deserialize_xkey(xprv) + except: + raise InvalidPassword() return xprv def check_password(self, password): xpriv = self.get_master_private_key(self.root_name, password) xpub = self.master_public_keys[self.root_name] - assert deserialize_xkey(xpriv)[3] == deserialize_xkey(xpub)[3] + if deserialize_xkey(xpriv)[3] != deserialize_xkey(xpub)[3]: + raise InvalidPassword() def add_master_public_key(self, name, xpub): self.master_public_keys[name] = xpub