Skip to content

Commit de73db9

Browse files
committed
key: fix DefiniteDescriptorKey construction from key with hardened step
Our current API allows constructing a DefiniteDescriptorKey with hardened steps, which means that you can't actually derive a public key. However, the point of this type is to provide a DescriptorPublicKey which implements the ToPublicKey trait. This is a backport, so it introduces the `has_hardened_steps` helper function but does *not* make it pub, in the interest of avoiding gratuitious API changes.
1 parent af2664d commit de73db9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/descriptor/key.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,23 @@ impl DescriptorPublicKey {
609609
}
610610
}
611611

612+
/// Whether or not the key has a wildcard
613+
pub fn has_hardened_step(&self) -> bool {
614+
let paths = match self {
615+
DescriptorPublicKey::Single(..) => &[],
616+
DescriptorPublicKey::XPub(xpub) => core::slice::from_ref(&xpub.derivation_path),
617+
DescriptorPublicKey::MultiXPub(xpub) => &xpub.derivation_paths.paths()[..],
618+
};
619+
for p in paths {
620+
for step in p.into_iter() {
621+
if step.is_hardened() {
622+
return true;
623+
}
624+
}
625+
}
626+
false
627+
}
628+
612629
#[deprecated(note = "use at_derivation_index instead")]
613630
/// Deprecated name for [`Self::at_derivation_index`].
614631
pub fn derive(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
@@ -1037,7 +1054,7 @@ impl DefiniteDescriptorKey {
10371054
///
10381055
/// Returns `None` if the key contains a wildcard
10391056
fn new(key: DescriptorPublicKey) -> Option<Self> {
1040-
if key.has_wildcard() || key.is_multipath() {
1057+
if key.has_wildcard() || key.is_multipath() || key.has_hardened_step() {
10411058
None
10421059
} else {
10431060
Some(Self(key))
@@ -1071,7 +1088,7 @@ impl FromStr for DefiniteDescriptorKey {
10711088
fn from_str(s: &str) -> Result<Self, Self::Err> {
10721089
let inner = DescriptorPublicKey::from_str(s)?;
10731090
DefiniteDescriptorKey::new(inner).ok_or(DescriptorKeyParseError(
1074-
"cannot parse multi-path keys or keys with a wilcard as a DerivedDescriptorKey",
1091+
"cannot parse multi-path keys, keys with a wildcard or keys with hardened derivation steps as a DerivedDescriptorKey",
10751092
))
10761093
}
10771094
}
@@ -1516,5 +1533,10 @@ mod test {
15161533
.parse::<DescriptorPublicKey>()
15171534
.unwrap();
15181535
assert!(DefiniteDescriptorKey::new(desc).is_none());
1536+
// xpub with hardened path
1537+
let desc = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8/1'/2"
1538+
.parse::<DescriptorPublicKey>()
1539+
.unwrap();
1540+
assert!(DefiniteDescriptorKey::new(desc).is_none());
15191541
}
15201542
}

0 commit comments

Comments
 (0)