electrum

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

commit 1d079602901047f551736002b4189f21e9bbf3d3
parent cf4ee487cd66a139a74c1690ce581c592e7a752d
Author: Neil Booth <kyuupichan@gmail.com>
Date:   Fri,  1 Jan 2016 23:39:19 +0900

Remove constructor method of plugins

Clean up wallet class loading.

Diffstat:
Mlib/plugins.py | 7+++++--
Mlib/wallet.py | 34+++++++++++++++-------------------
Mplugins/ledger/ledger.py | 3---
Mplugins/trezor/plugin.py | 3---
Mplugins/trustedcoin/qt.py | 8+++++---
Mplugins/trustedcoin/trustedcoin.py | 8+++-----
6 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/lib/plugins.py b/lib/plugins.py @@ -118,10 +118,13 @@ class Plugins(DaemonThread): return result def register_plugin_wallet(self, name, gui_good, details): + def dynamic_constructor(storage): + return self.wallet_plugin_loader(name).wallet_class(storage) + if details[0] == 'hardware': self.hw_wallets[name] = (gui_good, details) - register = details + (lambda: self.wallet_plugin_loader(name),) - wallet.wallet_types.append(register) + self.print_error("registering wallet %s: %s" %(name, details)) + wallet.wallet_types.append(details + (dynamic_constructor,)) def wallet_plugin_loader(self, name): if not name in self.plugins: diff --git a/lib/wallet.py b/lib/wallet.py @@ -1908,25 +1908,7 @@ class Wallet(object): raise BaseException(msg) wallet_type = storage.get('wallet_type') - if wallet_type: - for cat, t, name, loader in wallet_types: - if t == wallet_type: - if cat in ['hardware', 'twofactor']: - WalletClass = lambda storage: apply(loader().constructor, (storage,)) - else: - WalletClass = loader - break - else: - if re.match('(\d+)of(\d+)', wallet_type): - WalletClass = Multisig_Wallet - else: - raise RuntimeError("Unknown wallet type: " + wallet_type) - else: - if seed_version == OLD_SEED_VERSION: - WalletClass = OldWallet - else: - WalletClass = NewWallet - + WalletClass = Wallet.wallet_class(wallet_type, seed_version) wallet = WalletClass(storage) # Convert hardware wallets restored with older versions of @@ -1941,6 +1923,20 @@ class Wallet(object): return wallet @staticmethod + def wallet_class(wallet_type, seed_version): + if wallet_type: + if Wallet.multisig_type(wallet_type): + return Multisig_Wallet + + for info in wallet_types: + if wallet_type == info[1]: + return info[3] + + raise RuntimeError("Unknown wallet type: " + wallet_type) + + return OldWallet if seed_version == OLD_SEED_VERSION else NewWallet + + @staticmethod def is_seed(seed): return is_old_seed(seed) or is_new_seed(seed) diff --git a/plugins/ledger/ledger.py b/plugins/ledger/ledger.py @@ -419,9 +419,6 @@ class LedgerPlugin(BasePlugin): self.device = self.wallet_class.device self.handler = None - def constructor(self, s): - return BTChipWallet(s) - def is_enabled(self): return BTCHIP diff --git a/plugins/trezor/plugin.py b/plugins/trezor/plugin.py @@ -129,9 +129,6 @@ class TrezorCompatiblePlugin(BasePlugin): self.client = None self.wallet_class.plugin = self - def constructor(self, s): - return self.wallet_class(s) - def give_error(self, message): self.print_error(message) raise Exception(message) diff --git a/plugins/trustedcoin/qt.py b/plugins/trustedcoin/qt.py @@ -31,7 +31,7 @@ from electrum.i18n import _ from electrum.plugins import hook from electrum import wizard -from trustedcoin import TrustedCoinPlugin, Wallet_2fa, DISCLAIMER, server +from trustedcoin import TrustedCoinPlugin, DISCLAIMER, server def need_server(wallet, tx): from electrum.account import BIP32_Account @@ -79,7 +79,8 @@ class Plugin(TrustedCoinPlugin): def sign_tx(self, window, tx): self.print_error("twofactor:sign_tx") wallet = window.wallet - if type(wallet) is Wallet_2fa and not wallet.can_sign_without_server(): + assert isinstace(wallet, self.wallet_class) + if not wallet.can_sign_without_server(): auth_code = None if need_server(wallet, tx): auth_code = self.auth_dialog(window) @@ -100,7 +101,8 @@ class Plugin(TrustedCoinPlugin): @hook def abort_send(self, window): wallet = window.wallet - if type(wallet) is Wallet_2fa and not wallet.can_sign_without_server(): + assert isinstace(wallet, self.wallet_class) + if not wallet.can_sign_without_server(): if wallet.billing_info is None: # request billing info before forming the transaction waiting_dialog(self, window).wait() diff --git a/plugins/trustedcoin/trustedcoin.py b/plugins/trustedcoin/trustedcoin.py @@ -287,13 +287,11 @@ def make_billing_address(wallet, num): class TrustedCoinPlugin(BasePlugin): + wallet_class = Wallet_2fa def __init__(self, parent, config, name): BasePlugin.__init__(self, parent, config, name) - Wallet_2fa.plugin = self - - def constructor(self, s): - return Wallet_2fa(s) + self.wallet_class.plugin = self @staticmethod def is_valid_seed(seed): @@ -348,7 +346,7 @@ class TrustedCoinPlugin(BasePlugin): window.wallet.is_billing = False def on_restore_wallet(self, wallet, wizard): - assert isinstance(wallet, Wallet_2fa) + assert isinstance(wallet, self.wallet_class) seed = wizard.request_seed(RESTORE_MSG, is_valid=self.is_valid_seed) password = wizard.request_password()