talways_hook. fix trezor handler. fixes #1146 - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 21b2fcbe85b1acf12c46ecdf83461538a925931f DIR parent 8f940393553b719036cef990f65bb2c6a6ed34f2 HTML Author: ThomasV <thomasv@gitorious> Date: Sat, 18 Apr 2015 14:59:46 +0200 always_hook. fix trezor handler. fixes #1146 Diffstat: M gui/qt/__init__.py | 6 ++++-- M gui/qt/installwizard.py | 6 +++--- M lib/plugins.py | 16 +++++++++------- M plugins/trezor.py | 36 +++++++++++++++++++------------ M plugins/trustedcoin.py | 4 ---- 5 files changed, 38 insertions(+), 30 deletions(-) --- DIR diff --git a/gui/qt/__init__.py b/gui/qt/__init__.py t@@ -36,7 +36,7 @@ import PyQt4.QtCore as QtCore from electrum.i18n import _, set_language from electrum.util import print_error, print_msg -from electrum.plugins import run_hook +from electrum.plugins import run_hook, always_hook from electrum import WalletStorage, Wallet from electrum.bitcoin import MIN_RELAY_TX_FEE t@@ -73,6 +73,8 @@ class ElectrumGui: if app is None: self.app = QApplication(sys.argv) self.app.installEventFilter(self.efilter) + # let plugins know that we are using the qt gui + always_hook('init_qt_app', self.app) def build_tray_menu(self): t@@ -222,7 +224,7 @@ class ElectrumGui: else: self.go_full() - # plugins that need to change the GUI do it here + # plugins interact with main window run_hook('init_qt', self) w.load_wallet(wallet) DIR diff --git a/gui/qt/installwizard.py b/gui/qt/installwizard.py t@@ -15,7 +15,7 @@ from amountedit import AmountEdit import sys import threading -from electrum.plugins import run_hook +from electrum.plugins import always_hook from electrum.mnemonic import prepare_seed MSG_ENTER_ANYTHING = _("Please enter a wallet seed, a master public key, a list of Bitcoin addresses, or a list of private keys") t@@ -385,7 +385,7 @@ class InstallWizard(QDialog): self.waiting_dialog(wallet.synchronize) else: - f = run_hook('get_wizard_action', self, wallet, action) + f = always_hook('get_wizard_action', self, wallet, action) if not f: raise BaseException('unknown wizard action', action) r = f(wallet, self) t@@ -452,7 +452,7 @@ class InstallWizard(QDialog): else: self.storage.put('wallet_type', t) - wallet = run_hook('installwizard_restore', self, self.storage) + wallet = always_hook('installwizard_restore', self, self.storage) if not wallet: return DIR diff --git a/lib/plugins.py b/lib/plugins.py t@@ -33,23 +33,25 @@ hook_names = set() hooks = {} def hook(func): - n = func.func_name - if n not in hook_names: - hook_names.add(n) + hook_names.add(func.func_name) return func - def run_hook(name, *args): - SPECIAL_HOOKS = ['get_wizard_action','installwizard_restore'] + return _run_hook(name, False, *args) + +def always_hook(name, *args): + return _run_hook(name, True, *args) + +def _run_hook(name, always, *args): results = [] - f_list = hooks.get(name,[]) + f_list = hooks.get(name, []) for p, f in f_list: if name == 'load_wallet': p.wallet = args[0] if name == 'init_qt': gui = args[0] p.window = gui.main_window - if name in SPECIAL_HOOKS or p.is_enabled(): + if always or p.is_enabled(): try: r = f(*args) except Exception: DIR diff --git a/plugins/trezor.py b/plugins/trezor.py t@@ -12,7 +12,7 @@ import electrum from electrum.account import BIP32_Account from electrum.bitcoin import EncodeBase58Check, public_key_to_bc_address, bc_address_to_hash_160 from electrum.i18n import _ -from electrum.plugins import BasePlugin, hook +from electrum.plugins import BasePlugin, hook, always_hook, run_hook from electrum.transaction import deserialize from electrum.wallet import BIP32_HD_Wallet from electrum.util import print_error t@@ -91,16 +91,24 @@ class Plugin(BasePlugin): return True @hook + def add_plugin(self, wallet): + wallet.plugin = self + + @hook def close_wallet(self): print_error("trezor: clear session") if self.wallet and self.wallet.client: self.wallet.client.clear_session() self.wallet.client.transport.close() + self.wallet = None + + @hook + def init_qt_app(self, app): + self.handler = TrezorQtHandler(app) @hook def load_wallet(self, wallet): - self.twd = TrezorQtHandler(self.window) - self.wallet.twd = self.twd + self.wallet = wallet if self.trezor_is_connected(): if not self.wallet.check_proper_device(): QMessageBox.information(self.window, _('Error'), _("This wallet does not match your Trezor device"), _('OK')) t@@ -118,7 +126,7 @@ class Plugin(BasePlugin): return wallet = TrezorWallet(storage) self.wallet = wallet - passphrase = self.twd.get_passphrase(_("Please enter your Trezor passphrase.") + '\n' + _("Press OK if you do not use one.")) + passphrase = self.handler.get_passphrase(_("Please enter your Trezor passphrase.") + '\n' + _("Press OK if you do not use one.")) if passphrase is None: return password = wizard.password_dialog() t@@ -152,9 +160,9 @@ class Plugin(BasePlugin): if not response[1]: return new_label = str(response[0]) - self.twd.show_message("Please confirm label change on Trezor") + self.handler.show_message("Please confirm label change on Trezor") status = self.wallet.get_client().apply_settings(label=new_label) - self.twd.stop() + self.handler.stop() update_label() current_label_label = QLabel() t@@ -182,6 +190,7 @@ class TrezorWallet(BIP32_HD_Wallet): self.mpk = None self.device_checked = False self.force_watching_only = False + always_hook('add_plugin', self) def get_action(self): if not self.accounts: t@@ -192,7 +201,6 @@ class TrezorWallet(BIP32_HD_Wallet): def can_sign_xpubkey(self, x_pubkey): xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey) - print "z", xpub return xpub in self.master_public_keys.values() def can_export(self): t@@ -218,7 +226,7 @@ class TrezorWallet(BIP32_HD_Wallet): except: give_error('Could not connect to your Trezor. Please verify the cable is connected and that no other app is using it.') self.client = QtGuiTrezorClient(self.transport) - self.client.twd = self.twd + self.client.handler = self.plugin.handler self.client.set_tx_api(self) #self.client.clear_session()# TODO Doesn't work with firmware 1.1, returns proto.Failure self.client.bad = False t@@ -305,7 +313,7 @@ class TrezorWallet(BIP32_HD_Wallet): except Exception, e: give_error(e) finally: - self.twd.stop() + self.plugin.handler.stop() def sign_message(self, address, message, password): if not self.check_proper_device(): t@@ -320,7 +328,7 @@ class TrezorWallet(BIP32_HD_Wallet): except Exception, e: give_error(e) finally: - self.twd.stop() + self.plugin.handler.stop() b64_msg_sig = b64encode(msg_sig.signature) return str(b64_msg_sig) t@@ -337,7 +345,7 @@ class TrezorWallet(BIP32_HD_Wallet): except Exception, e: give_error(e) finally: - self.twd.stop() + self.plugin.handler.stop() #values = [i['value'] for i in tx.inputs] raw = signed_tx.encode('hex') tx.update(raw) t@@ -455,7 +463,7 @@ class TrezorGuiMixin(object): message = "Confirm address on Trezor device to continue" else: message = "Check Trezor device to continue" - self.twd.show_message(message) + self.handler.show_message(message) return proto.ButtonAck() def callback_PinMatrixRequest(self, msg): t@@ -467,14 +475,14 @@ class TrezorGuiMixin(object): desc = 'new PIN again' else: desc = 'PIN' - pin = self.twd.get_pin("Please enter Trezor %s" % desc) + pin = self.handler.get_pin("Please enter Trezor %s" % desc) if not pin: return proto.Cancel() return proto.PinMatrixAck(pin=pin) def callback_PassphraseRequest(self, req): msg = _("Please enter your Trezor passphrase.") - passphrase = self.twd.get_passphrase(msg) + passphrase = self.handler.get_passphrase(msg) if passphrase is None: return proto.Cancel() return proto.PassphraseAck(passphrase=passphrase) DIR diff --git a/plugins/trustedcoin.py b/plugins/trustedcoin.py t@@ -330,10 +330,6 @@ class Plugin(BasePlugin): wallet.add_master_public_key('x3/', xpub3) @hook - def init_qt(self, gui): - self.window = gui.main_window - - @hook def do_clear(self): self.is_billing = False