diff --git a/electrum/bip32.py b/electrum/bip32.py index 87d50e849..4f9a82041 100644 --- a/electrum/bip32.py +++ b/electrum/bip32.py @@ -273,6 +273,7 @@ class BIP32Node(NamedTuple): """Returns the fingerprint of this node. Note that self.fingerprint is of the *parent*. """ + # TODO cache this return hash_160(self.eckey.get_public_key_bytes(compressed=True))[0:4] diff --git a/electrum/keystore.py b/electrum/keystore.py index 95c6cb239..264f2db93 100644 --- a/electrum/keystore.py +++ b/electrum/keystore.py @@ -143,8 +143,8 @@ class KeyStore(Logger): if not test_der_suffix_against_pubkey(der_suffix, pubkey): der_suffix = None # try fp against our intermediate fingerprint - if (der_suffix is None and hasattr(self, 'xpub') and - fp_found == BIP32Node.from_xkey(self.xpub).calc_fingerprint_of_this_node()): + if (der_suffix is None and hasattr(self, 'get_bip32_node_for_xpub') and + fp_found == self.get_bip32_node_for_xpub().calc_fingerprint_of_this_node()): der_suffix = path_found if not test_der_suffix_against_pubkey(der_suffix, pubkey): der_suffix = None @@ -331,6 +331,7 @@ class Xpub: self.xpub = None self.xpub_receive = None self.xpub_change = None + self._xpub_bip32_node = None # type: Optional[BIP32Node] # "key origin" info (subclass should persist these): self._derivation_prefix = derivation_prefix # type: Optional[str] @@ -339,6 +340,13 @@ class Xpub: def get_master_public_key(self): return self.xpub + def get_bip32_node_for_xpub(self) -> Optional[BIP32Node]: + if self._xpub_bip32_node is None: + if self.xpub is None: + return None + self._xpub_bip32_node = BIP32Node.from_xkey(self.xpub) + return self._xpub_bip32_node + def get_derivation_prefix(self) -> Optional[str]: """Returns to bip32 path from some root node to self.xpub Note that the return value might be None; if it is unknown. @@ -367,7 +375,7 @@ class Xpub: der_prefix_ints = convert_bip32_path_to_list_of_uint32(der_prefix_str) else: # use intermediate fp, and claim der suffix is the full path - fingerprint_bytes = BIP32Node.from_xkey(self.xpub).calc_fingerprint_of_this_node() + fingerprint_bytes = self.get_bip32_node_for_xpub().calc_fingerprint_of_this_node() der_prefix_ints = convert_bip32_path_to_list_of_uint32('m') der_full = der_prefix_ints + list(der_suffix) return fingerprint_bytes, der_full @@ -376,7 +384,7 @@ class Xpub: assert self.xpub fp_bytes, der_full = self.get_fp_and_derivation_to_be_used_in_partial_tx(der_suffix=[], only_der_suffix=only_der_suffix) - bip32node = BIP32Node.from_xkey(self.xpub) + bip32node = self.get_bip32_node_for_xpub() depth = len(der_full) child_number_int = der_full[-1] if len(der_full) >= 1 else 0 child_number_bytes = child_number_int.to_bytes(length=4, byteorder="big") @@ -391,7 +399,7 @@ class Xpub: # try to derive ourselves from what we were given child_node1 = root_node.subkey_at_private_derivation(derivation_prefix) child_pubkey_bytes1 = child_node1.eckey.get_public_key_bytes(compressed=True) - child_node2 = BIP32Node.from_xkey(self.xpub) + child_node2 = self.get_bip32_node_for_xpub() child_pubkey_bytes2 = child_node2.eckey.get_public_key_bytes(compressed=True) if child_pubkey_bytes1 != child_pubkey_bytes2: raise Exception("(xpub, derivation_prefix, root_node) inconsistency") @@ -408,7 +416,7 @@ class Xpub: assert for_change in (0, 1) xpub = self.xpub_change if for_change else self.xpub_receive if xpub is None: - rootnode = BIP32Node.from_xkey(self.xpub) + rootnode = self.get_bip32_node_for_xpub() xpub = rootnode.subkey_at_public_derivation((for_change,)).to_xpub() if for_change: self.xpub_change = xpub @@ -453,7 +461,7 @@ class BIP32_KeyStore(Deterministic_KeyStore, Xpub): def check_password(self, password): xprv = pw_decode(self.xprv, password, version=self.pw_hash_version) - if BIP32Node.from_xkey(xprv).chaincode != BIP32Node.from_xkey(self.xpub).chaincode: + if BIP32Node.from_xkey(xprv).chaincode != self.get_bip32_node_for_xpub().chaincode: raise InvalidPassword() def update_password(self, old_password, new_password):