From b54ba556bca11f17c6236ead7a6c3cb754555fa1 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Mon, 8 Feb 2016 22:32:49 +0900 Subject: [PATCH] Wallet: encapsulate wallet_types Previously plugins would append lines to it and wizard.py would hack into it too. --- lib/plugins.py | 7 +++++-- lib/wallet.py | 38 +++++++++++++++++++++++--------------- lib/wizard.py | 4 ++-- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/plugins.py b/lib/plugins.py index 6291e7fd..cb9d4ea8 100644 --- a/lib/plugins.py +++ b/lib/plugins.py @@ -27,7 +27,7 @@ import time from util import * from i18n import _ from util import profiler, PrintError, DaemonThread, UserCancelled -import wallet + class Plugins(DaemonThread): @@ -140,13 +140,16 @@ class Plugins(DaemonThread): return wallet_types, descs def register_plugin_wallet(self, name, gui_good, details): + from wallet import Wallet + def dynamic_constructor(storage): return self.wallet_plugin_loader(name).wallet_class(storage) if details[0] == 'hardware': self.hw_wallets[name] = (gui_good, details) self.print_error("registering wallet %s: %s" %(name, details)) - wallet.wallet_types.append(details + (dynamic_constructor,)) + Wallet.register_plugin_wallet(details[0], details[1], + dynamic_constructor) def wallet_plugin_loader(self, name): if not name in self.plugins: diff --git a/lib/wallet.py b/lib/wallet.py index ee576375..93f2d1fd 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -27,6 +27,7 @@ import copy import re from functools import partial from unicodedata import normalize +from collections import namedtuple from i18n import _ from util import NotEnoughFunds, PrintError, profiler @@ -1908,18 +1909,7 @@ class OldWallet(Deterministic_Wallet): return ' '.join(old_mnemonic.mn_encode(s)) - - -wallet_types = [ - # category type description constructor - ('standard', 'old', ("Old wallet"), OldWallet), - ('standard', 'xpub', ("BIP32 Import"), BIP32_Simple_Wallet), - ('standard', 'standard', ("Standard wallet"), NewWallet), - ('standard', 'imported', ("Imported wallet"), Imported_Wallet), - ('multisig', '2of2', ("Multisig wallet (2 of 2)"), Multisig_Wallet), - ('multisig', '2of3', ("Multisig wallet (2 of 3)"), Multisig_Wallet), - ('bip44', 'bip44', ("Restored hardware wallet"), BIP44_Wallet), -] +WalletType = namedtuple("WalletType", "category type constructor") # former WalletFactory class Wallet(object): @@ -1927,6 +1917,16 @@ class Wallet(object): This class is actually a factory that will return a wallet of the correct type when passed a WalletStorage instance.""" + wallets = [ # category type constructor + WalletType('standard', 'old', OldWallet), + WalletType('standard', 'xpub', BIP32_Simple_Wallet), + WalletType('standard', 'standard', NewWallet), + WalletType('standard', 'imported', Imported_Wallet), + WalletType('multisig', '2of2', Multisig_Wallet), + WalletType('multisig', '2of3', Multisig_Wallet), + WalletType('bip44', 'bip44', BIP44_Wallet), + ] + def __new__(self, storage): seed_version = storage.get('seed_version') if not seed_version: @@ -1963,15 +1963,23 @@ class Wallet(object): return wallet + @staticmethod + def categories(): + return [wallet.category for wallet in Wallet.wallets] + + @staticmethod + def register_plugin_wallet(category, type, constructor): + Wallet.wallets.append(WalletType(category, type, constructor)) + @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] + for wallet in Wallet.wallets: + if wallet.type == wallet_type: + return wallet.constructor raise RuntimeError("Unknown wallet type: " + wallet_type) diff --git a/lib/wizard.py b/lib/wizard.py index 80a8c526..9d6bdef1 100644 --- a/lib/wizard.py +++ b/lib/wizard.py @@ -19,7 +19,7 @@ from electrum import WalletStorage from electrum.plugins import run_hook from util import PrintError -from wallet import Wallet, wallet_types +from wallet import Wallet from i18n import _ MSG_GENERATING_WAIT = _("Electrum is generating your addresses, please wait...") @@ -198,7 +198,7 @@ class WizardBase(PrintError): self.remove_from_recently_open(storage.path) # Filter out any unregistered wallet kinds - registered_kinds = zip(*wallet_types)[0] + registered_kinds = Wallet.categories() kinds, descriptions = zip(*[pair for pair in WizardBase.wallet_kinds if pair[0] in registered_kinds]) action, kind_index = self.query_create_or_restore(descriptions)