Browse Source

Wallet: encapsulate wallet_types

Previously plugins would append lines to it and wizard.py would
hack into it too.
283
Neil Booth 9 years ago
parent
commit
b54ba556bc
  1. 7
      lib/plugins.py
  2. 38
      lib/wallet.py
  3. 4
      lib/wizard.py

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

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

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

Loading…
Cancel
Save