From 417b178a96bcd092a064ec9f071a283ad548bf0d Mon Sep 17 00:00:00 2001 From: "Simon J. Castano" Date: Sun, 2 Jun 2024 16:50:23 +0200 Subject: [PATCH] Warn user if imported key has derivation path different from xpub's depth --- src/cryptoadvance/specter/key.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cryptoadvance/specter/key.py b/src/cryptoadvance/specter/key.py index 67fbaf83b..c7ad40ecc 100644 --- a/src/cryptoadvance/specter/key.py +++ b/src/cryptoadvance/specter/key.py @@ -1,8 +1,10 @@ +import logging from collections import OrderedDict from binascii import hexlify from embit import base58 from .util.xpub import get_xpub_fingerprint +logger = logging.getLogger(__name__) purposes = OrderedDict( { @@ -135,9 +137,16 @@ def parse_xpub(cls, xpub, purpose=""): elif derivation_path[4] == "2h": key_type = "wsh" + # warn user if imported key has derivation path different from xpub's depth + depth = xpub_bytes[4] + if derivation != "" and depth != len(derivation_path): + logger.warning( + f"xpup has a depth of {depth} while derivation path " + f"indicates the key is {len(derivation_path)} levels deep" + ) + # infer fingerprint and derivation if depth == 0 or depth == 1 xpub_bytes = base58.decode_check(xpub) - depth = xpub_bytes[4] if depth == 0: fingerprint = hexlify(get_xpub_fingerprint(xpub)).decode() derivation = "m"