Skip to content

Commit ccd7ef1

Browse files
committed
desc keys: make parse_xkey_deriv a standalone function
We'll need it for extended keys with multipath.
1 parent fb3cb90 commit ccd7ef1

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/descriptor/key.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl FromStr for DescriptorPublicKey {
308308

309309
if key_part.contains("pub") {
310310
let (xpub, derivation_path, wildcard) =
311-
DescriptorXKey::<bip32::ExtendedPubKey>::parse_xkey_deriv(key_part)?;
311+
parse_xkey_deriv::<bip32::ExtendedPubKey>(key_part)?;
312312

313313
Ok(DescriptorPublicKey::XPub(DescriptorXKey {
314314
origin,
@@ -502,7 +502,7 @@ impl FromStr for DescriptorSecretKey {
502502
}))
503503
} else {
504504
let (xprv, derivation_path, wildcard) =
505-
DescriptorXKey::<bip32::ExtendedPrivKey>::parse_xkey_deriv(key_part)?;
505+
parse_xkey_deriv::<bip32::ExtendedPrivKey>(key_part)?;
506506
Ok(DescriptorSecretKey::XPrv(DescriptorXKey {
507507
origin,
508508
xkey: xprv,
@@ -567,42 +567,42 @@ fn parse_key_origin(s: &str) -> Result<(&str, Option<bip32::KeySource>), Descrip
567567
}
568568
}
569569

570-
impl<K: InnerXKey> DescriptorXKey<K> {
571-
/// Parse an extended key concatenated to a derivation path.
572-
fn parse_xkey_deriv(
573-
key_deriv: &str,
574-
) -> Result<(K, bip32::DerivationPath, Wildcard), DescriptorKeyParseError> {
575-
let mut key_deriv = key_deriv.split('/');
576-
let xkey_str = key_deriv.next().ok_or(DescriptorKeyParseError(
577-
"No key found after origin description",
578-
))?;
579-
let xkey = K::from_str(xkey_str)
580-
.map_err(|_| DescriptorKeyParseError("Error while parsing xkey."))?;
581-
582-
let mut wildcard = Wildcard::None;
583-
let derivation_path = key_deriv
584-
.filter_map(|p| {
585-
if wildcard == Wildcard::None && p == "*" {
586-
wildcard = Wildcard::Unhardened;
587-
None
588-
} else if wildcard == Wildcard::None && (p == "*'" || p == "*h") {
589-
wildcard = Wildcard::Hardened;
590-
None
591-
} else if wildcard != Wildcard::None {
592-
Some(Err(DescriptorKeyParseError(
593-
"'*' may only appear as last element in a derivation path.",
594-
)))
595-
} else {
596-
Some(bip32::ChildNumber::from_str(p).map_err(|_| {
597-
DescriptorKeyParseError("Error while parsing key derivation path")
598-
}))
599-
}
600-
})
601-
.collect::<Result<bip32::DerivationPath, _>>()?;
570+
/// Parse an extended key concatenated to a derivation path.
571+
fn parse_xkey_deriv<K: InnerXKey>(
572+
key_deriv: &str,
573+
) -> Result<(K, bip32::DerivationPath, Wildcard), DescriptorKeyParseError> {
574+
let mut key_deriv = key_deriv.split('/');
575+
let xkey_str = key_deriv.next().ok_or(DescriptorKeyParseError(
576+
"No key found after origin description",
577+
))?;
578+
let xkey =
579+
K::from_str(xkey_str).map_err(|_| DescriptorKeyParseError("Error while parsing xkey."))?;
580+
581+
let mut wildcard = Wildcard::None;
582+
let derivation_path = key_deriv
583+
.filter_map(|p| {
584+
if wildcard == Wildcard::None && p == "*" {
585+
wildcard = Wildcard::Unhardened;
586+
None
587+
} else if wildcard == Wildcard::None && (p == "*'" || p == "*h") {
588+
wildcard = Wildcard::Hardened;
589+
None
590+
} else if wildcard != Wildcard::None {
591+
Some(Err(DescriptorKeyParseError(
592+
"'*' may only appear as last element in a derivation path.",
593+
)))
594+
} else {
595+
Some(bip32::ChildNumber::from_str(p).map_err(|_| {
596+
DescriptorKeyParseError("Error while parsing key derivation path")
597+
}))
598+
}
599+
})
600+
.collect::<Result<bip32::DerivationPath, _>>()?;
602601

603-
Ok((xkey, derivation_path, wildcard))
604-
}
602+
Ok((xkey, derivation_path, wildcard))
603+
}
605604

605+
impl<K: InnerXKey> DescriptorXKey<K> {
606606
/// Compares this key with a `keysource` and returns the matching derivation path, if any.
607607
///
608608
/// For keys that have an origin, the `keysource`'s fingerprint will be compared

0 commit comments

Comments
 (0)