keystore: cache derive_pubkey

This commit is contained in:
SomberNight 2019-12-09 03:35:20 +01:00
parent 5f6f7da2a1
commit f73b6b5d23
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
3 changed files with 14 additions and 8 deletions

View file

@ -369,7 +369,7 @@ class BaseWizard(Logger):
elif purpose == HWD_SETUP_DECRYPT_WALLET:
derivation = get_derivation_used_for_hw_device_encryption()
xpub = self.plugin.get_xpub(device_info.device.id_, derivation, 'standard', self)
password = keystore.Xpub.get_pubkey_from_xpub(xpub, ())
password = keystore.Xpub.get_pubkey_from_xpub(xpub, ()).hex()
try:
storage.decrypt(password)
except InvalidPassword:

View file

@ -28,6 +28,7 @@ from unicodedata import normalize
import hashlib
import re
from typing import Tuple, TYPE_CHECKING, Union, Sequence, Optional, Dict, List, NamedTuple
from functools import lru_cache
from . import bitcoin, ecc, constants, bip32
from .bitcoin import deserialize_privkey, serialize_privkey
@ -402,8 +403,8 @@ class Xpub:
self._root_fingerprint = root_fingerprint
self._derivation_prefix = normalize_bip32_derivation(derivation_prefix)
def derive_pubkey(self, for_change, n) -> str:
for_change = int(for_change)
@lru_cache(maxsize=None)
def _derive_pubkey_bytes(self, for_change: int, n: int) -> bytes:
assert for_change in (0, 1)
xpub = self.xpub_change if for_change else self.xpub_receive
if xpub is None:
@ -415,10 +416,15 @@ class Xpub:
self.xpub_receive = xpub
return self.get_pubkey_from_xpub(xpub, (n,))
def derive_pubkey(self, for_change: int, n: int) -> str:
for_change = int(for_change)
assert for_change in (0, 1)
return self._derive_pubkey_bytes(for_change, n).hex()
@classmethod
def get_pubkey_from_xpub(self, xpub, sequence):
def get_pubkey_from_xpub(self, xpub: str, sequence) -> bytes:
node = BIP32Node.from_xkey(xpub).subkey_at_public_derivation(sequence)
return node.eckey.get_public_key_hex(compressed=True)
return node.eckey.get_public_key_bytes(compressed=True)
class BIP32_KeyStore(Deterministic_KeyStore, Xpub):
@ -692,7 +698,7 @@ class Hardware_KeyStore(KeyStore, Xpub):
client = self.plugin.get_client(self)
derivation = get_derivation_used_for_hw_device_encryption()
xpub = client.get_xpub(derivation, "standard")
password = self.get_pubkey_from_xpub(xpub, ())
password = self.get_pubkey_from_xpub(xpub, ()).hex()
return password
def has_usable_connection_with_device(self) -> bool:

View file

@ -178,7 +178,7 @@ def get_connected_hw_devices(plugins):
return devices
def get_password_for_hw_device_encrypted_storage(plugins):
def get_password_for_hw_device_encrypted_storage(plugins) -> str:
devices = get_connected_hw_devices(plugins)
if len(devices) == 0:
print_msg("Error: No connected hw device found. Cannot decrypt this wallet.")
@ -194,7 +194,7 @@ def get_password_for_hw_device_encrypted_storage(plugins):
xpub = plugin.get_xpub(device_info.device.id_, derivation, 'standard', plugin.handler)
except UserCancelled:
sys.exit(0)
password = keystore.Xpub.get_pubkey_from_xpub(xpub, ())
password = keystore.Xpub.get_pubkey_from_xpub(xpub, ()).hex()
return password