Skip to content

backport #839 (DefiniteDescriptorKey fixes) to 10.x #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
2 changes: 1 addition & 1 deletion Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ dependencies = [

[[package]]
name = "miniscript"
version = "10.2.2"
version = "10.2.3"
dependencies = [
"base64",
"bitcoin",
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniscript"
version = "10.2.2"
version = "10.2.3"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>, Sanket Kanjalkar <sanket1729@gmail.com>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-miniscript/"
Expand Down
26 changes: 24 additions & 2 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,23 @@ impl DescriptorPublicKey {
}
}

/// Whether or not the key has a wildcard
pub fn has_hardened_step(&self) -> bool {
let paths = match self {
DescriptorPublicKey::Single(..) => &[],
DescriptorPublicKey::XPub(xpub) => core::slice::from_ref(&xpub.derivation_path),
DescriptorPublicKey::MultiXPub(xpub) => &xpub.derivation_paths.paths()[..],
};
for p in paths {
for step in p.into_iter() {
if step.is_hardened() {
return true;
}
}
}
false
}

#[deprecated(note = "use at_derivation_index instead")]
/// Deprecated name for [`Self::at_derivation_index`].
pub fn derive(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
Expand Down Expand Up @@ -1055,7 +1072,7 @@ impl DefiniteDescriptorKey {
///
/// Returns `None` if the key contains a wildcard
fn new(key: DescriptorPublicKey) -> Option<Self> {
if key.has_wildcard() || key.is_multipath() {
if key.has_wildcard() || key.is_multipath() || key.has_hardened_step() {
None
} else {
Some(Self(key))
Expand Down Expand Up @@ -1089,7 +1106,7 @@ impl FromStr for DefiniteDescriptorKey {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let inner = DescriptorPublicKey::from_str(s)?;
DefiniteDescriptorKey::new(inner).ok_or(DescriptorKeyParseError(
"cannot parse multi-path keys or keys with a wilcard as a DerivedDescriptorKey",
"cannot parse multi-path keys, keys with a wildcard or keys with hardened derivation steps as a DerivedDescriptorKey",
))
}
}
Expand Down Expand Up @@ -1587,5 +1604,10 @@ mod test {
.parse::<DescriptorPublicKey>()
.unwrap();
assert!(DefiniteDescriptorKey::new(desc).is_none());
// xpub with hardened path
let desc = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8/1'/2"
.parse::<DescriptorPublicKey>()
.unwrap();
assert!(DefiniteDescriptorKey::new(desc).is_none());
}
}
Loading