Skip to content

Commit 10fd701

Browse files
committed
fix(keymap): backport use of 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.
1 parent 48fd25a commit 10fd701

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/descriptor/key_map.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ impl GetKey for DescriptorSecretKey {
7373
return Ok(Some(key));
7474
}
7575

76-
if descriptor_xkey.matches(key_source, secp).is_some() {
77-
let (_, derivation_path) = key_source;
76+
if let Some(matched_path) = descriptor_xkey.matches(key_source, secp) {
77+
let (_, full_path) = key_source;
78+
79+
let derivation_path = &full_path[matched_path.len()..];
80+
7881
return Ok(Some(
7982
descriptor_xkey
8083
.xkey
@@ -265,11 +268,16 @@ mod tests {
265268
_ => unreachable!(),
266269
};
267270

268-
let path = DerivationPath::from_str("84'/1'/0'/0").unwrap();
269-
let expected_pk = xpriv.xkey.derive_priv(&secp, &path).unwrap().to_priv();
271+
let expected_deriv_path: DerivationPath = (&[ChildNumber::Normal { index: 0 }][..]).into();
272+
let expected_pk = xpriv
273+
.xkey
274+
.derive_priv(&secp, &expected_deriv_path)
275+
.unwrap()
276+
.to_priv();
270277

278+
let derivation_path = DerivationPath::from_str("84'/1'/0'/0").unwrap();
271279
let (fp, _) = xpriv.origin.unwrap();
272-
let key_request = KeyRequest::Bip32((fp, path));
280+
let key_request = KeyRequest::Bip32((fp, derivation_path));
273281

274282
let pk = keymap_wrapper
275283
.get_key(key_request, &secp)

0 commit comments

Comments
 (0)