Skip to content

Commit ef09f63

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 0679c7a commit ef09f63

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/descriptor/key.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,22 @@ impl DescriptorPublicKey {
442442
}
443443
}
444444

445+
/// Whether or not the key has a wildcard
446+
fn has_hardened_step(&self) -> bool {
447+
let paths = match self {
448+
DescriptorPublicKey::Single(..) => &[],
449+
DescriptorPublicKey::XPub(xpub) => core::slice::from_ref(&xpub.derivation_path),
450+
};
451+
for p in paths {
452+
for step in p.into_iter() {
453+
if step.is_hardened() {
454+
return true;
455+
}
456+
}
457+
}
458+
false
459+
}
460+
445461
#[doc(hidden)]
446462
#[deprecated(note = "use at_derivation_index instead")]
447463
pub fn derive(self, index: u32) -> DefiniteDescriptorKey {
@@ -759,7 +775,7 @@ impl DefiniteDescriptorKey {
759775
///
760776
/// Returns `None` if the key contains a wildcard
761777
fn new(key: DescriptorPublicKey) -> Option<Self> {
762-
if key.has_wildcard() {
778+
if key.has_wildcard() || key.has_hardened_step() {
763779
None
764780
} else {
765781
Some(Self(key))
@@ -783,8 +799,8 @@ impl FromStr for DefiniteDescriptorKey {
783799
fn from_str(s: &str) -> Result<Self, Self::Err> {
784800
let inner = DescriptorPublicKey::from_str(s)?;
785801
DefiniteDescriptorKey::new(inner).ok_or(DescriptorKeyParseError(
786-
"cannot parse key with a wilcard as a DerivedDescriptorKey",
787-
))
802+
"cannot parse multi-path keys, keys with a wildcard or keys with hardened derivation steps as a DerivedDescriptorKey",
803+
))
788804
}
789805
}
790806

@@ -844,7 +860,7 @@ mod test {
844860

845861
use bitcoin::secp256k1;
846862

847-
use super::{DescriptorKeyParseError, DescriptorPublicKey, DescriptorSecretKey};
863+
use super::{DefiniteDescriptorKey, DescriptorKeyParseError, DescriptorPublicKey, DescriptorSecretKey};
848864
use crate::prelude::*;
849865

850866
#[test]
@@ -1008,4 +1024,23 @@ mod test {
10081024
b"\xb0\x59\x11\x6a"
10091025
);
10101026
}
1027+
1028+
#[test]
1029+
fn definite_keys() {
1030+
// basic xpub
1031+
let desc = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
1032+
.parse::<DescriptorPublicKey>()
1033+
.unwrap();
1034+
assert!(DefiniteDescriptorKey::new(desc).is_some());
1035+
// xpub with wildcard
1036+
let desc = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8/*"
1037+
.parse::<DescriptorPublicKey>()
1038+
.unwrap();
1039+
assert!(DefiniteDescriptorKey::new(desc).is_none());
1040+
// xpub with hardened path
1041+
let desc = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7uUDFdp6W1EGMcet8/1'/2"
1042+
.parse::<DescriptorPublicKey>()
1043+
.unwrap();
1044+
assert!(DefiniteDescriptorKey::new(desc).is_none());
1045+
}
10111046
}

0 commit comments

Comments
 (0)