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
This commit is contained in:
SomberNight 2020-02-27 04:18:27 +01:00
parent bea038ea6b
commit a987a2bbbe
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9

View file

@ -41,7 +41,7 @@ from .ecc import string_to_number
from .crypto import (pw_decode, pw_encode, sha256, sha256d, PW_HASH_VERSION_LATEST,
SUPPORTED_PW_HASH_VERSIONS, UnsupportedPasswordHashVersion, hash_160)
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 .plugin import run_hook
from .logging import Logger
@ -463,10 +463,23 @@ class Xpub(MasterPublicKeyMixin):
self.add_key_origin(derivation_prefix=derivation_prefix,
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
if not (root_fingerprint is None or (is_hex_str(root_fingerprint) and len(root_fingerprint) == 8)):
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
self._derivation_prefix = normalize_bip32_derivation(derivation_prefix)
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)
def derive_pubkey(self, for_change: int, n: int) -> bytes: