electrum

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

commit 073a09f9261fb6d9f3c6240983225450f9ef7d03
parent a4b24df4bb123db86d0d75113b889c2453bdf25f
Author: SomberNight <somber.night@protonmail.com>
Date:   Tue,  3 Sep 2019 16:24:05 +0200

wallet/keystore: small inheritance clean-up

Diffstat:
Melectrum/keystore.py | 24+++++++++++++++++++++---
Melectrum/wallet.py | 25++++++++++++-------------
2 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/electrum/keystore.py b/electrum/keystore.py @@ -26,7 +26,7 @@ from unicodedata import normalize import hashlib -from typing import Tuple +from typing import Tuple, TYPE_CHECKING, Union, Sequence from . import bitcoin, ecc, constants, bip32 from .bitcoin import (deserialize_privkey, serialize_privkey, @@ -42,6 +42,9 @@ from .mnemonic import Mnemonic, load_wordlist, seed_type, is_seed from .plugin import run_hook from .logging import Logger +if TYPE_CHECKING: + from .transaction import Transaction + class KeyStore(Logger): @@ -93,6 +96,21 @@ class KeyStore(Logger): def ready_to_sign(self): return not self.is_watching_only() + def dump(self) -> dict: + raise NotImplementedError() # implemented by subclasses + + def is_deterministic(self) -> bool: + raise NotImplementedError() # implemented by subclasses + + def sign_message(self, sequence, message, password) -> bytes: + raise NotImplementedError() # implemented by subclasses + + def decrypt_message(self, sequence, message, password) -> bytes: + raise NotImplementedError() # implemented by subclasses + + def sign_transaction(self, tx: 'Transaction', password) -> None: + raise NotImplementedError() # implemented by subclasses + class Software_KeyStore(KeyStore): @@ -723,7 +741,7 @@ hw_keystores = {} def register_keystore(hw_type, constructor): hw_keystores[hw_type] = constructor -def hardware_keystore(d): +def hardware_keystore(d) -> Hardware_KeyStore: hw_type = d['hw_type'] if hw_type in hw_keystores: constructor = hw_keystores[hw_type] @@ -731,7 +749,7 @@ def hardware_keystore(d): raise WalletFileException(f'unknown hardware type: {hw_type}. ' f'hw_keystores: {list(hw_keystores)}') -def load_keystore(storage, name): +def load_keystore(storage, name) -> KeyStore: d = storage.get(name, {}) t = d.get('type') if not t: diff --git a/electrum/wallet.py b/electrum/wallet.py @@ -46,14 +46,13 @@ from .util import (NotEnoughFunds, UserCancelled, profiler, WalletFileException, BitcoinException, InvalidPassword, format_time, timestamp_to_datetime, Satoshis, Fiat, bfh, bh2u, TxMinedInfo, quantize_feerate, create_bip21_uri, OrderedDictWithIndex) -from .util import age from .util import PR_TYPE_ADDRESS, PR_TYPE_BIP70, PR_TYPE_LN from .simple_config import get_config from .bitcoin import (COIN, TYPE_ADDRESS, is_address, address_to_script, is_minikey, relayfee, dust_threshold) from .crypto import sha256d from . import keystore -from .keystore import load_keystore, Hardware_KeyStore +from .keystore import load_keystore, Hardware_KeyStore, KeyStore from .util import multisig_type from .storage import STO_EV_PLAINTEXT, STO_EV_USER_PW, STO_EV_XPUB_PW, WalletStorage from . import transaction, bitcoin, coinchooser, paymentrequest, ecc, bip32 @@ -216,6 +215,7 @@ class Abstract_Wallet(AddressSynchronizer): self.storage = storage # load addresses needs to be called before constructor for sanity checks self.storage.db.load_addresses(self.wallet_type) + self.keystore = None # type: Optional[KeyStore] # will be set by load_keystore AddressSynchronizer.__init__(self, storage.db) # saved fields @@ -234,7 +234,6 @@ class Abstract_Wallet(AddressSynchronizer): if self.storage.get('wallet_type') is None: self.storage.put('wallet_type', self.wallet_type) - # contacts self.contacts = Contacts(self.storage) self._coin_price_cache = {} self.lnworker = LNWallet(self) if get_config().get('lightning') else None @@ -261,6 +260,9 @@ class Abstract_Wallet(AddressSynchronizer): self.test_addresses_sanity() super().load_and_cleanup() + def load_keystore(self) -> None: + raise NotImplementedError() # implemented by subclasses + def diagnostic_name(self): return self.basename() @@ -1587,21 +1589,21 @@ class Abstract_Wallet(AddressSynchronizer): return p * txin_value/Decimal(COIN) def is_billing_address(self, addr): - # overloaded for TrustedCoin wallets + # overridden for TrustedCoin wallets return False def is_watching_only(self) -> bool: raise NotImplementedError() + def get_keystore(self) -> Optional[KeyStore]: + return self.keystore -class Simple_Wallet(Abstract_Wallet): - # wallet with a single keystore + def get_keystores(self) -> Sequence[KeyStore]: + return [self.keystore] if self.keystore else [] - def get_keystore(self): - return self.keystore - def get_keystores(self): - return [self.keystore] +class Simple_Wallet(Abstract_Wallet): + # wallet with a single keystore def is_watching_only(self): return self.keystore.is_watching_only() @@ -1627,9 +1629,6 @@ class Imported_Wallet(Simple_Wallet): def is_watching_only(self): return self.keystore is None - def get_keystores(self): - return [self.keystore] if self.keystore else [] - def can_import_privkey(self): return bool(self.keystore)