Remove constructor method of plugins

Clean up wallet class loading.
This commit is contained in:
Neil Booth 2016-01-01 23:39:19 +09:00
parent cf4ee487cd
commit 1d07960290
6 changed files with 28 additions and 35 deletions

View File

@ -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:

View File

@ -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
@ -1940,6 +1922,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)

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -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()