Skip to content

Commit eaba815

Browse files
author
Scott Robinson
committed
Extract specialised DescriptorPublicKey functionality into DescriptorSinglePublicKey
1 parent 8dd1ae1 commit eaba815

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

Diff for: src/descriptor/key.rs

+60-38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::prelude::*;
1818
use crate::serde::{Deserialize, Deserializer, Serialize, Serializer};
1919
use crate::{hash256, MiniscriptKey, ToPublicKey};
2020

21+
type DescriptorSinglePublicKey = SinglePub;
2122
type DescriptorExtendedPublicKey = DescriptorXKey<bip32::ExtendedPubKey>;
2223

2324
/// The descriptor pubkey, either a single pubkey or an xpub.
@@ -285,6 +286,17 @@ impl error::Error for DescriptorKeyParseError {
285286
}
286287
}
287288

289+
impl fmt::Display for DescriptorSinglePublicKey {
290+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291+
maybe_fmt_master_id(f, &self.origin)?;
292+
match self.key {
293+
SinglePubKey::FullKey(full_key) => full_key.fmt(f),
294+
SinglePubKey::XOnly(x_only_key) => x_only_key.fmt(f),
295+
}?;
296+
Ok(())
297+
}
298+
}
299+
288300
impl fmt::Display for DescriptorExtendedPublicKey {
289301
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
290302
maybe_fmt_master_id(f, &self.origin)?;
@@ -302,14 +314,7 @@ impl fmt::Display for DescriptorExtendedPublicKey {
302314
impl fmt::Display for DescriptorPublicKey {
303315
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
304316
match *self {
305-
DescriptorPublicKey::Single(ref pk) => {
306-
maybe_fmt_master_id(f, &pk.origin)?;
307-
match pk.key {
308-
SinglePubKey::FullKey(full_key) => full_key.fmt(f),
309-
SinglePubKey::XOnly(x_only_key) => x_only_key.fmt(f),
310-
}?;
311-
Ok(())
312-
}
317+
DescriptorPublicKey::Single(ref pk) => pk.fmt(f),
313318
DescriptorPublicKey::XPub(ref xpub) => xpub.fmt(f),
314319
DescriptorPublicKey::MultiXPub(ref xpub) => {
315320
maybe_fmt_master_id(f, &xpub.origin)?;
@@ -565,6 +570,48 @@ pub trait DescriptorInnerKey {
565570
fn is_multipath(&self) -> bool;
566571
}
567572

573+
impl DescriptorInnerKey for DescriptorSinglePublicKey {
574+
fn master_fingerprint(&self) -> bip32::Fingerprint {
575+
if let Some((fingerprint, _)) = self.origin {
576+
fingerprint
577+
} else {
578+
let mut engine = XpubIdentifier::engine();
579+
match self.key {
580+
SinglePubKey::FullKey(pk) => {
581+
pk.write_into(&mut engine).expect("engines don't error")
582+
}
583+
SinglePubKey::XOnly(x_only_pk) => engine.input(&x_only_pk.serialize()),
584+
};
585+
bip32::Fingerprint::from(
586+
&XpubIdentifier::from_engine(engine)[..4]
587+
.try_into()
588+
.expect("4 byte slice"),
589+
)
590+
}
591+
}
592+
593+
fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
594+
Some(if let Some((_, ref path)) = self.origin {
595+
path.clone()
596+
} else {
597+
bip32::DerivationPath::from(vec![])
598+
})
599+
}
600+
601+
fn has_wildcard(&self) -> bool {
602+
false
603+
}
604+
605+
fn at_derivation_index(self, _index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
606+
Ok(DefiniteDescriptorKey::new(DescriptorPublicKey::Single(self))
607+
.expect("The key should not contain any wildcards at this point"))
608+
}
609+
610+
fn is_multipath(&self) -> bool {
611+
false
612+
}
613+
}
614+
568615
impl DescriptorInnerKey for DescriptorExtendedPublicKey {
569616
fn master_fingerprint(&self) -> bip32::Fingerprint {
570617
if let Some((fingerprint, _)) = self.origin {
@@ -629,24 +676,7 @@ impl DescriptorPublicKey {
629676
xpub.xkey.fingerprint()
630677
}
631678
}
632-
DescriptorPublicKey::Single(ref single) => {
633-
if let Some((fingerprint, _)) = single.origin {
634-
fingerprint
635-
} else {
636-
let mut engine = XpubIdentifier::engine();
637-
match single.key {
638-
SinglePubKey::FullKey(pk) => {
639-
pk.write_into(&mut engine).expect("engines don't error")
640-
}
641-
SinglePubKey::XOnly(x_only_pk) => engine.input(&x_only_pk.serialize()),
642-
};
643-
bip32::Fingerprint::from(
644-
&XpubIdentifier::from_engine(engine)[..4]
645-
.try_into()
646-
.expect("4 byte slice"),
647-
)
648-
}
649-
}
679+
DescriptorPublicKey::Single(ref single) => single.master_fingerprint(),
650680
}
651681
}
652682

@@ -660,13 +690,7 @@ impl DescriptorPublicKey {
660690
pub fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
661691
match *self {
662692
DescriptorPublicKey::XPub(ref xpub) => xpub.full_derivation_path(),
663-
DescriptorPublicKey::Single(ref single) => {
664-
Some(if let Some((_, ref path)) = single.origin {
665-
path.clone()
666-
} else {
667-
bip32::DerivationPath::from(vec![])
668-
})
669-
}
693+
DescriptorPublicKey::Single(ref single) => single.full_derivation_path(),
670694
DescriptorPublicKey::MultiXPub(_) => None,
671695
}
672696
}
@@ -680,7 +704,7 @@ impl DescriptorPublicKey {
680704
/// Whether or not the key has a wildcard
681705
pub fn has_wildcard(&self) -> bool {
682706
match *self {
683-
DescriptorPublicKey::Single(..) => false,
707+
DescriptorPublicKey::Single(ref single) => single.has_wildcard(),
684708
DescriptorPublicKey::XPub(ref xpub) => xpub.has_wildcard(),
685709
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.wildcard != Wildcard::None,
686710
}
@@ -706,8 +730,7 @@ impl DescriptorPublicKey {
706730
/// - If `index` is hardened.
707731
pub fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
708732
match self {
709-
DescriptorPublicKey::Single(_) => Ok(DefiniteDescriptorKey::new(self)
710-
.expect("The key should not contain any wildcards at this point")),
733+
DescriptorPublicKey::Single(single) => single.at_derivation_index(index),
711734
DescriptorPublicKey::XPub(xpub) => xpub.at_derivation_index(index),
712735
DescriptorPublicKey::MultiXPub(_) => Err(ConversionError::MultiKey),
713736
}
@@ -716,8 +739,7 @@ impl DescriptorPublicKey {
716739
/// Whether or not this key has multiple derivation paths.
717740
pub fn is_multipath(&self) -> bool {
718741
match *self {
719-
DescriptorPublicKey::Single(..) => false,
720-
DescriptorPublicKey::XPub(..) => self.is_multipath(),
742+
DescriptorPublicKey::Single(..) | DescriptorPublicKey::XPub(..) => self.is_multipath(),
721743
DescriptorPublicKey::MultiXPub(_) => true,
722744
}
723745
}

0 commit comments

Comments
 (0)