Skip to content

Commit

Permalink
Extract DescriptorInnerKey trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Robinson committed Jul 23, 2023
1 parent a6a3b82 commit 8dd1ae1
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,15 +531,9 @@ impl error::Error for ConversionError {
}
}

impl DescriptorExtendedPublicKey {
pub trait DescriptorInnerKey {
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
pub fn master_fingerprint(&self) -> bip32::Fingerprint {
if let Some((fingerprint, _)) = self.origin {
fingerprint
} else {
self.xkey.fingerprint()
}
}
fn master_fingerprint(&self) -> bip32::Fingerprint;

/// Full path, from the master key
///
Expand All @@ -548,19 +542,10 @@ impl DescriptorExtendedPublicKey {
/// to the wildcard type (hardened or normal).
///
/// For multipath extended keys, this returns `None`.
pub fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
let origin_path = if let Some((_, ref path)) = self.origin {
path.clone()
} else {
bip32::DerivationPath::from(vec![])
};
Some(origin_path.extend(&self.derivation_path))
}
fn full_derivation_path(&self) -> Option<bip32::DerivationPath>;

/// Whether or not the key has a wildcard
pub fn has_wildcard(&self) -> bool {
self.wildcard != Wildcard::None
}
fn has_wildcard(&self) -> bool;

/// Replaces any wildcard (i.e. `/*`) in the key with a particular derivation index, turning it into a
/// *definite* key (i.e. one where all the derivation paths are set).
Expand All @@ -574,7 +559,35 @@ impl DescriptorExtendedPublicKey {
/// # Errors
///
/// - If `index` is hardened.
pub fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError>;

/// Whether or not this key has multiple derivation paths.
fn is_multipath(&self) -> bool;
}

impl DescriptorInnerKey for DescriptorExtendedPublicKey {
fn master_fingerprint(&self) -> bip32::Fingerprint {
if let Some((fingerprint, _)) = self.origin {
fingerprint
} else {
self.xkey.fingerprint()
}
}

fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
let origin_path = if let Some((_, ref path)) = self.origin {
path.clone()
} else {
bip32::DerivationPath::from(vec![])
};
Some(origin_path.extend(&self.derivation_path))
}

fn has_wildcard(&self) -> bool {
self.wildcard != Wildcard::None
}

fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
let derivation_path = match self.wildcard {
Wildcard::None => self.derivation_path,
Wildcard::Unhardened => self.derivation_path.into_child(
Expand All @@ -599,8 +612,7 @@ impl DescriptorExtendedPublicKey {
.expect("The key should not contain any wildcards at this point"))
}

/// Whether or not this key has multiple derivation paths.
pub fn is_multipath(&self) -> bool {
fn is_multipath(&self) -> bool {
false
}
}
Expand Down

0 comments on commit 8dd1ae1

Please sign in to comment.