Skip to content

Commit 1179ea2

Browse files
committed
Merge #863: fix(keymap): use correct derivation_path for keys with origin
2f72b27 fix(keymap): use correct `derivation_path` for keys with origin (Leonardo Lima) Pull request description: - 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. ACKs for top commit: apoelstra: ACK 2f72b27; successfully ran local tests Tree-SHA512: 0357a1fec8076b64c3e47a5373c20630621b3fd05687a98af08fb34b186c58c0c086a7a3eee674397860df9b4cb891c2e1d68b66623e836824f0f5498029a2c4
2 parents 4365b0e + 2f72b27 commit 1179ea2

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)