Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9c08aee

Browse files
author
Scott Robinson
committedJul 23, 2023
Extract specialised DescriptorPublicKey functionality into DescriptorMultiExtendedPublicKey
1 parent e770ebc commit 9c08aee

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed
 

‎src/descriptor/key.rs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{hash256, MiniscriptKey, ToPublicKey};
2020

2121
type DescriptorSinglePublicKey = SinglePub;
2222
type DescriptorExtendedPublicKey = DescriptorXKey<bip32::ExtendedPubKey>;
23+
type DescriptorMultiExtendedPublicKey = DescriptorMultiXKey<bip32::ExtendedPubKey>;
2324

2425
/// The descriptor pubkey, either a single pubkey or an xpub.
2526
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
@@ -29,7 +30,7 @@ pub enum DescriptorPublicKey {
2930
/// Extended public key (xpub).
3031
XPub(DescriptorExtendedPublicKey),
3132
/// Multiple extended public keys.
32-
MultiXPub(DescriptorMultiXKey<bip32::ExtendedPubKey>),
33+
MultiXPub(DescriptorMultiExtendedPublicKey),
3334
}
3435

3536
/// The descriptor secret key, either a single private key or an xprv.
@@ -310,22 +311,26 @@ impl fmt::Display for DescriptorExtendedPublicKey {
310311
}
311312
}
312313

314+
impl fmt::Display for DescriptorMultiExtendedPublicKey {
315+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
316+
maybe_fmt_master_id(f, &self.origin)?;
317+
self.xkey.fmt(f)?;
318+
fmt_derivation_paths(f, self.derivation_paths.paths())?;
319+
match self.wildcard {
320+
Wildcard::None => {}
321+
Wildcard::Unhardened => write!(f, "/*")?,
322+
Wildcard::Hardened => write!(f, "/*h")?,
323+
}
324+
Ok(())
325+
}
326+
}
327+
313328
impl fmt::Display for DescriptorPublicKey {
314329
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
315330
match *self {
316331
DescriptorPublicKey::Single(ref pk) => pk.fmt(f),
317332
DescriptorPublicKey::XPub(ref xpub) => xpub.fmt(f),
318-
DescriptorPublicKey::MultiXPub(ref xpub) => {
319-
maybe_fmt_master_id(f, &xpub.origin)?;
320-
xpub.xkey.fmt(f)?;
321-
fmt_derivation_paths(f, xpub.derivation_paths.paths())?;
322-
match xpub.wildcard {
323-
Wildcard::None => {}
324-
Wildcard::Unhardened => write!(f, "/*")?,
325-
Wildcard::Hardened => write!(f, "/*h")?,
326-
}
327-
Ok(())
328-
}
333+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.fmt(f),
329334
}
330335
}
331336
}
@@ -687,18 +692,38 @@ impl DescriptorInnerKey for DescriptorExtendedPublicKey {
687692
}
688693
}
689694

695+
impl DescriptorInnerKey for DescriptorMultiExtendedPublicKey {
696+
fn master_fingerprint(&self) -> bip32::Fingerprint {
697+
if let Some((fingerprint, _)) = self.origin {
698+
fingerprint
699+
} else {
700+
self.xkey.fingerprint()
701+
}
702+
}
703+
704+
fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
705+
None
706+
}
707+
708+
fn has_wildcard(&self) -> bool {
709+
self.wildcard != Wildcard::None
710+
}
711+
712+
fn at_derivation_index(self, _index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
713+
Err(ConversionError::MultiKey)
714+
}
715+
716+
fn is_multipath(&self) -> bool {
717+
true
718+
}
719+
}
720+
690721
impl DescriptorPublicKey {
691722
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
692723
pub fn master_fingerprint(&self) -> bip32::Fingerprint {
693724
match *self {
694725
DescriptorPublicKey::XPub(ref xpub) => xpub.master_fingerprint(),
695-
DescriptorPublicKey::MultiXPub(ref xpub) => {
696-
if let Some((fingerprint, _)) = xpub.origin {
697-
fingerprint
698-
} else {
699-
xpub.xkey.fingerprint()
700-
}
701-
}
726+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.master_fingerprint(),
702727
DescriptorPublicKey::Single(ref single) => single.master_fingerprint(),
703728
}
704729
}
@@ -714,7 +739,7 @@ impl DescriptorPublicKey {
714739
match *self {
715740
DescriptorPublicKey::XPub(ref xpub) => xpub.full_derivation_path(),
716741
DescriptorPublicKey::Single(ref single) => single.full_derivation_path(),
717-
DescriptorPublicKey::MultiXPub(_) => None,
742+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.full_derivation_path(),
718743
}
719744
}
720745

@@ -729,7 +754,7 @@ impl DescriptorPublicKey {
729754
match *self {
730755
DescriptorPublicKey::Single(ref single) => single.has_wildcard(),
731756
DescriptorPublicKey::XPub(ref xpub) => xpub.has_wildcard(),
732-
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.wildcard != Wildcard::None,
757+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.has_wildcard(),
733758
}
734759
}
735760

@@ -755,7 +780,7 @@ impl DescriptorPublicKey {
755780
match self {
756781
DescriptorPublicKey::Single(single) => single.at_derivation_index(index),
757782
DescriptorPublicKey::XPub(xpub) => xpub.at_derivation_index(index),
758-
DescriptorPublicKey::MultiXPub(_) => Err(ConversionError::MultiKey),
783+
DescriptorPublicKey::MultiXPub(xpub) => xpub.at_derivation_index(index),
759784
}
760785
}
761786

@@ -764,7 +789,7 @@ impl DescriptorPublicKey {
764789
match self {
765790
DescriptorPublicKey::Single(single) => single.is_multipath(),
766791
DescriptorPublicKey::XPub(xpub) => xpub.is_multipath(),
767-
DescriptorPublicKey::MultiXPub(_) => true,
792+
DescriptorPublicKey::MultiXPub(xpub) => xpub.is_multipath(),
768793
}
769794
}
770795

0 commit comments

Comments
 (0)