From 2f72b276901aa3150658175488eac03af922413b Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Fri, 10 Oct 2025 12:16:53 +1100 Subject: [PATCH] fix(keymap): use correct `derivation_path` for keys with origin - Fixes the implementation of `GetKey` for `KeyRequest::Bip32` when there's `Xpriv` has an origin, and it matches with the given `KeyRequest::Bip32` derivation_path. It should strip the matching part, to correctly derive the key at the correct child number. - Updates the existing test to the correct and expected behavior. --- src/descriptor/key_map.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/descriptor/key_map.rs b/src/descriptor/key_map.rs index cb5cb99de..3e193ec87 100644 --- a/src/descriptor/key_map.rs +++ b/src/descriptor/key_map.rs @@ -136,8 +136,11 @@ impl GetKey for DescriptorSecretKey { return Ok(Some(key)); } - if descriptor_xkey.matches(key_source, secp).is_some() { - let (_, derivation_path) = key_source; + if let Some(matched_path) = descriptor_xkey.matches(key_source, secp) { + let (_, full_path) = key_source; + + let derivation_path = &full_path[matched_path.len()..]; + return Ok(Some( descriptor_xkey .xkey @@ -323,11 +326,16 @@ mod tests { _ => unreachable!(), }; - let path = DerivationPath::from_str("84'/1'/0'/0").unwrap(); - let expected_pk = xpriv.xkey.derive_priv(&secp, &path).unwrap().to_priv(); + let expected_deriv_path: DerivationPath = (&[ChildNumber::Normal { index: 0 }][..]).into(); + let expected_pk = xpriv + .xkey + .derive_priv(&secp, &expected_deriv_path) + .unwrap() + .to_priv(); + let derivation_path = DerivationPath::from_str("84'/1'/0'/0").unwrap(); let (fp, _) = xpriv.origin.unwrap(); - let key_request = KeyRequest::Bip32((fp, path)); + let key_request = KeyRequest::Bip32((fp, derivation_path)); let pk = keymap .get_key(key_request, &secp)