Skip to content

Commit c9dcbd5

Browse files
committed
DefiniteDescriptorKey: disallow multipath keys
When deriving keys we refuse to allow multipath keys. We should forbid them when directly constructing them at all.
1 parent c5d8898 commit c9dcbd5

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/descriptor/key.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,14 @@ impl DescriptorMultiXKey<bip32::Xpriv> {
333333
#[allow(missing_docs)]
334334
pub enum NonDefiniteKeyError {
335335
Wildcard,
336+
Multipath,
336337
}
337338

338339
impl fmt::Display for NonDefiniteKeyError {
339340
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
340341
match *self {
341342
Self::Wildcard => f.write_str("key with a wildcard cannot be a DerivedDescriptorKey"),
343+
Self::Multipath => f.write_str("multipath key cannot be a DerivedDescriptorKey"),
342344
}
343345
}
344346
}
@@ -1270,11 +1272,13 @@ impl DefiniteDescriptorKey {
12701272
/// Construct an instance from a descriptor key and a derivation index
12711273
///
12721274
/// Returns `None` if the key contains a wildcard
1273-
pub fn new(key: DescriptorPublicKey) -> Option<Self> {
1275+
pub fn new(key: DescriptorPublicKey) -> Result<Self, NonDefiniteKeyError> {
12741276
if key.has_wildcard() {
1275-
None
1277+
Err(NonDefiniteKeyError::Wildcard)
1278+
} else if key.is_multipath() {
1279+
Err(NonDefiniteKeyError::Multipath)
12761280
} else {
1277-
Some(Self(key))
1281+
Ok(Self(key))
12781282
}
12791283
}
12801284

@@ -1303,10 +1307,9 @@ impl FromStr for DefiniteDescriptorKey {
13031307
type Err = DescriptorKeyParseError;
13041308

13051309
fn from_str(s: &str) -> Result<Self, Self::Err> {
1306-
let inner = DescriptorPublicKey::from_str(s)?;
1307-
DefiniteDescriptorKey::new(inner).ok_or(DescriptorKeyParseError::NonDefiniteKey(
1308-
NonDefiniteKeyError::Wildcard,
1309-
))
1310+
let d = DescriptorPublicKey::from_str(s)?;
1311+
DefiniteDescriptorKey::new(d)
1312+
.map_err(DescriptorKeyParseError::NonDefiniteKey)
13101313
}
13111314
}
13121315

0 commit comments

Comments
 (0)