Skip to content

Commit 2887782

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 2887782

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/descriptor/key.rs

Lines changed: 41 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,9 @@ mod test {
844860

845861
use bitcoin::secp256k1;
846862

847-
use super::{DescriptorKeyParseError, DescriptorPublicKey, DescriptorSecretKey};
863+
use super::{
864+
DefiniteDescriptorKey, DescriptorKeyParseError, DescriptorPublicKey, DescriptorSecretKey,
865+
};
848866
use crate::prelude::*;
849867

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

0 commit comments

Comments
 (0)