Require the cache argument on the internal helpers

Defaulting it to None means a call site that forgets it silently
derives without a cache. No caller omitted it; now none can.
_derive_with_cache keeps its optional cache, since that branch is the
plain embit derive() the equivalence test compares against.
This commit is contained in:
kdmukai
2026-08-14 17:56:44 -05:00
parent 0a735d58a0
commit 5791d55e2f
2 changed files with 8 additions and 8 deletions
+7 -7
View File
@@ -303,7 +303,7 @@ class PSBTParser():
@staticmethod
def _get_policy(scope, scriptpubkey, xpubs, child_key_derivation_cache=None):
def _get_policy(scope, scriptpubkey, xpubs, child_key_derivation_cache: dict | None):
"""Parse scope and get policy"""
# we don't know the policy yet, let's parse it
script_type = scriptpubkey.script_type()
@@ -371,7 +371,7 @@ class PSBTParser():
@staticmethod
def _derive_with_cache(parent_key: bip32.HDKey, derivation_path: List[int], cache: dict | None = None) -> bip32.HDKey:
def _derive_with_cache(parent_key: bip32.HDKey, derivation_path: List[int], child_key_derivation_cache: dict | None = None) -> bip32.HDKey:
"""
Derives the key that sits at the given derivation path below parent_key, reusing
any levels along the way that have already been derived during this parse.
@@ -397,7 +397,7 @@ class PSBTParser():
The cache stops accepting new levels at MAX_CACHED_DERIVATIONS.
"""
if cache is None:
if child_key_derivation_cache is None:
return parent_key.derive(derivation_path)
derived_key = parent_key
@@ -407,14 +407,14 @@ class PSBTParser():
for index in derivation_path:
derivation_path_so_far += (index,)
cache_key = (id(parent_key), derivation_path_so_far)
cached_entry = cache.get(cache_key)
cached_entry = child_key_derivation_cache.get(cache_key)
if cached_entry is None:
# First time deriving this level. Do the work to derive this level's child
# and store it in the cache.
already_derived = derived_key.child(index)
if len(cache) < PSBTParser.MAX_CACHED_DERIVATIONS:
if len(child_key_derivation_cache) < PSBTParser.MAX_CACHED_DERIVATIONS:
# Parent must also be stored to keep its id() from being reused
cache[cache_key] = (parent_key, already_derived)
child_key_derivation_cache[cache_key] = (parent_key, already_derived)
else:
cached_parent, already_derived = cached_entry
derived_key = already_derived
@@ -422,7 +422,7 @@ class PSBTParser():
@staticmethod
def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache=None):
def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache: dict | None):
"""Returns xpubs used to derive pubkeys using global xpub field from psbt"""
cosigners = []
for i, pubkey in enumerate(pubkeys):
+1 -1
View File
@@ -654,7 +654,7 @@ class TestPSBTParserOptimizations:
inp = psbt.inputs[0]
pubkeys = list(inp.bip32_derivations.keys())
uncached = PSBTParser._get_cosigners(pubkeys, inp.bip32_derivations, psbt.xpubs)
uncached = PSBTParser._get_cosigners(pubkeys, inp.bip32_derivations, psbt.xpubs, None)
child_key_derivation_cache = {}
cached = PSBTParser._get_cosigners(