Skip to content

Commit ffdb368

Browse files
committed
Merge #843: backport #839 (DefiniteDescriptorKey fixes) to 9.x
d34188c bump version to 9.2.1 (Andrew Poelstra) 2887782 key: fix DefiniteDescriptorKey construction from key with hardened step (Andrew Poelstra) Pull request description: Builds on #837 ACKs for top commit: sanket1729: utACK d34188c Tree-SHA512: 2f6a685b614ac4d8591781a79fba18af2f0ca9980d1bf0d6612b47bb46d4936e0a776424498a26ba98925c30abb4f8332926489e2b4b45ec3f9aa269ca1d4e78
2 parents 87bb165 + d34188c commit ffdb368

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

Cargo-recent.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
118118

119119
[[package]]
120120
name = "miniscript"
121-
version = "9.2.0"
121+
version = "9.2.1"
122122
dependencies = [
123123
"bitcoin",
124124
"hashbrown 0.11.0",

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "miniscript"
3-
version = "9.2.0"
3+
version = "9.2.1"
44
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>, Sanket Kanjalkar <sanket1729@gmail.com>"]
55
license = "CC0-1.0"
66
homepage = "https://github.com/rust-bitcoin/rust-miniscript/"

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)