Skip to content

Commit 9766e6e

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 a7997c6 commit 9766e6e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/descriptor/key.rs

Lines changed: 9 additions & 6 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,9 +1307,8 @@ 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)
1308-
.ok_or(DescriptorKeyParseError::NonDefiniteKey(NonDefiniteKeyError::Wildcard))
1310+
let d = DescriptorPublicKey::from_str(s)?;
1311+
DefiniteDescriptorKey::new(d).map_err(DescriptorKeyParseError::NonDefiniteKey)
13091312
}
13101313
}
13111314

0 commit comments

Comments
 (0)