Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn user if imported key has derivation path different from xpub's depth #2454

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/cryptoadvance/specter/key.py
Original file line number Diff line number Diff line change
@@ -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
from embit.descriptor import Key as DescriptorKey

logger = logging.getLogger(__name__)

purposes = OrderedDict(
{
Expand Down Expand Up @@ -93,8 +95,8 @@ def parse_xpub(cls, xpub, purpose=""):
if path[-1] == "h":
path = path[:-1]
try:
i = int(path)
except:
int(path)
except ValueError:
raise Exception("Incorrect index")
derivation_path[0] = "m"
derivation = "/".join(derivation_path)
Expand Down Expand Up @@ -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"
Expand Down
Loading