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