Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions src/descriptor/key_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,28 @@ impl GetKey for DescriptorSecretKey {
return Ok(Some(key));
}

if let Some(matched_path) = descriptor_xkey.matches(key_source, secp) {
if descriptor_xkey.matches(key_source, secp).is_some() {
let (_, full_path) = key_source;

let derivation_path = &full_path[matched_path.len()..];

return Ok(Some(
descriptor_xkey
.xkey
.derive_priv(secp, &derivation_path)
.map_err(GetKeyError::Bip32)?
.to_priv(),
));
match &descriptor_xkey.origin {
Some((_, origin_path)) => {
let derivation_path = &full_path[origin_path.len()..];
return Ok(Some(
descriptor_xkey
.xkey
.derive_priv(secp, &derivation_path)?
.to_priv(),
));
}
None => {
return Ok(Some(
descriptor_xkey
.xkey
.derive_priv(secp, &full_path)?
.to_priv(),
))
}
};
}

Ok(None)
Expand Down Expand Up @@ -314,7 +324,7 @@ mod tests {
}

#[test]
fn get_key_xpriv_with_key_origin() {
fn get_key_xpriv_with_key_origin_and_wildcard() {
let secp = Secp256k1::new();

let descriptor_str = "wpkh([d34db33f/84h/1h/0h]tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)";
Expand Down Expand Up @@ -345,6 +355,38 @@ mod tests {
assert_eq!(pk, expected_pk);
}

#[test]
fn get_key_xpriv_with_key_origin_and_no_wildcard() {
let secp = Secp256k1::new();

let descriptor_str = "wpkh([d34db33f/84h/1h/0h]tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/0)";
let (_descriptor_pk, keymap) = Descriptor::parse_descriptor(&secp, descriptor_str).unwrap();

let descriptor_sk = DescriptorSecretKey::from_str("[d34db33f/84h/1h/0h]tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/0").unwrap();
let xpriv = match descriptor_sk {
DescriptorSecretKey::XPrv(descriptor_xkey) => descriptor_xkey,
_ => unreachable!(),
};

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, derivation_path));

let pk = keymap
.get_key(key_request, &secp)
.expect("get_key should not fail")
.expect("get_key should return a `PrivateKey`");

assert_eq!(pk, expected_pk);
}

#[test]
fn get_key_keymap_no_match() {
let secp = Secp256k1::new();
Expand Down
1 change: 1 addition & 0 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
/// It is **not recommended** to use policy as a stable identifier for a miniscript. You should
/// use the policy compiler once, and then use the miniscript output as a stable identifier. See
/// the compiler document in [`doc/compiler.md`] for more details.
#[allow(rustdoc::broken_intra_doc_links)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the scenario that the intra docs link is being used (e.g [doc/compiler.md]), it has now become a warning.

Let me know if I should either keep this allow or just not use a intra-doc link anymore

#[cfg(feature = "compiler")]
pub fn compile_to_descriptor<Ctx: ScriptContext>(
&self,
Expand Down
Loading