Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
DescriptorKey trait
  • Loading branch information
Scott Robinson committed Jul 24, 2023
commit ffe62effe70a5b06e908b75c97fa9677aed24bc1
47 changes: 47 additions & 0 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,53 @@ impl error::Error for ConversionError {
}
}

pub trait DescriptorKey : Clone + fmt::Debug + fmt::Display + Eq + FromStr + std::hash::Hash + Ord {
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
fn master_fingerprint(&self) -> bip32::Fingerprint;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In e4d9af4:

As a separate issue, I think we should return an option here and then upstream we should have a bip32::Fingerprint::from_opt function that would do the 0x00000000 thing. Though I'm unsure. Maybe this function is only ever used in contexts where the result needs to be serialized (PSBT, encoding xpubs) in which case users always want the existng behavior.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's sure where I'm using it. 😅


/// Full path, from the master key
///
/// For wildcard keys this will return the path up to the wildcard, so you
/// can get full paths by appending one additional derivation step, according
/// to the wildcard type (hardened or normal).
///
/// For multipath extended keys, this returns `None`.
fn full_derivation_path(&self) -> Option<bip32::DerivationPath>;

/// Whether or not the key has a wildcard
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).
///
/// # Returns
///
/// - If this key is not an xpub, returns `self`.
/// - If this key is an xpub but does not have a wildcard, returns `self`.
/// - Otherwise, returns the xpub at derivation `index` (removing the wildcard).
///
/// # Errors
///
/// - If `index` is hardened.
fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError>;

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

/// Computes the public key corresponding to this descriptor key.
/// When deriving from an XOnlyPublicKey, it adds the default 0x02 y-coordinate
/// and returns the obtained full [`bitcoin::PublicKey`]. All BIP32 derivations
/// always return a compressed key
///
/// Will return an error if the descriptor key has any hardened derivation steps in its path. To
/// avoid this error you should replace any such public keys first with [`translate_pk`].
///
/// [`translate_pk`]: crate::TranslatePk::translate_pk
fn derive_public_key<C: Verification>(
&self,
secp: &Secp256k1<C>,
) -> Result<bitcoin::PublicKey, ConversionError>;
}
impl DescriptorPublicKey {
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
pub fn master_fingerprint(&self) -> bip32::Fingerprint {
Expand Down