Browse Source

keystore: make add_key_origin "API-user-friendly"

Power-users that know what they are doing can use this method
to populate key origin information for keystore (bip32 root fingerprint
and derivation path prefix).
Try to make method hard to misuse.

Qt console can now be used as e.g.:
```
wallet.get_keystores()[2].add_key_origin(derivation_prefix="m/48h/1h/0h/2h", root_fingerprint="deadbeef")
```

related #5715
related #5955
related #5969
hard-fail-on-bad-server-string
SomberNight 5 years ago
parent
commit
a987a2bbbe
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 21
      electrum/keystore.py

21
electrum/keystore.py

@ -41,7 +41,7 @@ from .ecc import string_to_number
from .crypto import (pw_decode, pw_encode, sha256, sha256d, PW_HASH_VERSION_LATEST, from .crypto import (pw_decode, pw_encode, sha256, sha256d, PW_HASH_VERSION_LATEST,
SUPPORTED_PW_HASH_VERSIONS, UnsupportedPasswordHashVersion, hash_160) SUPPORTED_PW_HASH_VERSIONS, UnsupportedPasswordHashVersion, hash_160)
from .util import (InvalidPassword, WalletFileException, from .util import (InvalidPassword, WalletFileException,
BitcoinException, bh2u, bfh, inv_dict) BitcoinException, bh2u, bfh, inv_dict, is_hex_str)
from .mnemonic import Mnemonic, load_wordlist, seed_type, is_seed 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
@ -463,10 +463,23 @@ class Xpub(MasterPublicKeyMixin):
self.add_key_origin(derivation_prefix=derivation_prefix, self.add_key_origin(derivation_prefix=derivation_prefix,
root_fingerprint=root_node.calc_fingerprint_of_this_node().hex().lower()) root_fingerprint=root_node.calc_fingerprint_of_this_node().hex().lower())
def add_key_origin(self, *, derivation_prefix: Optional[str], root_fingerprint: Optional[str]): def add_key_origin(self, *, derivation_prefix: str = None, root_fingerprint: str = None) -> None:
assert self.xpub assert self.xpub
self._root_fingerprint = root_fingerprint if not (root_fingerprint is None or (is_hex_str(root_fingerprint) and len(root_fingerprint) == 8)):
self._derivation_prefix = normalize_bip32_derivation(derivation_prefix) raise Exception("root fp must be 8 hex characters")
derivation_prefix = normalize_bip32_derivation(derivation_prefix)
calc_root_fp, calc_der_prefix = bip32.root_fp_and_der_prefix_from_xkey(self.xpub)
if (calc_root_fp is not None and root_fingerprint is not None
and calc_root_fp != root_fingerprint):
raise Exception("provided root fp inconsistent with xpub")
if (calc_der_prefix is not None and derivation_prefix is not None
and calc_der_prefix != derivation_prefix):
raise Exception("provided der prefix inconsistent with xpub")
if root_fingerprint is not None:
self._root_fingerprint = root_fingerprint
if derivation_prefix is not None:
self._derivation_prefix = derivation_prefix
self.is_requesting_to_be_rewritten_to_wallet_file = True
@lru_cache(maxsize=None) @lru_cache(maxsize=None)
def derive_pubkey(self, for_change: int, n: int) -> bytes: def derive_pubkey(self, for_change: int, n: int) -> bytes:

Loading…
Cancel
Save