Skip to content

Commit 2f72b27

Browse files
committed
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.
1 parent 4365b0e commit 2f72b27

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
@@ -136,8 +136,11 @@ impl GetKey for DescriptorSecretKey {
136136
return Ok(Some(key));
137137
}
138138

139-
if descriptor_xkey.matches(key_source, secp).is_some() {
140-
let (_, derivation_path) = key_source;
139+
if let Some(matched_path) = descriptor_xkey.matches(key_source, secp) {
140+
let (_, full_path) = key_source;
141+
142+
let derivation_path = &full_path[matched_path.len()..];
143+
141144
return Ok(Some(
142145
descriptor_xkey
143146
.xkey
@@ -323,11 +326,16 @@ mod tests {
323326
_ => unreachable!(),
324327
};
325328

326-
let path = DerivationPath::from_str("84'/1'/0'/0").unwrap();
327-
let expected_pk = xpriv.xkey.derive_priv(&secp, &path).unwrap().to_priv();
329+
let expected_deriv_path: DerivationPath = (&[ChildNumber::Normal { index: 0 }][..]).into();
330+
let expected_pk = xpriv
331+
.xkey
332+
.derive_priv(&secp, &expected_deriv_path)
333+
.unwrap()
334+
.to_priv();
328335

336+
let derivation_path = DerivationPath::from_str("84'/1'/0'/0").unwrap();
329337
let (fp, _) = xpriv.origin.unwrap();
330-
let key_request = KeyRequest::Bip32((fp, path));
338+
let key_request = KeyRequest::Bip32((fp, derivation_path));
331339

332340
let pk = keymap
333341
.get_key(key_request, &secp)

0 commit comments

Comments
 (0)