Skip to content

Commit b18ba59

Browse files
author
Scott Robinson
committed
Make DescriptorPublicKey a DescriptorKey
1 parent 9c55ad3 commit b18ba59

File tree

2 files changed

+56
-68
lines changed

2 files changed

+56
-68
lines changed

src/descriptor/key.rs

+55-68
Original file line numberDiff line numberDiff line change
@@ -804,80 +804,18 @@ impl DescriptorKey for DescriptorMultiExtendedPublicKey {
804804
}
805805

806806
impl DescriptorPublicKey {
807-
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
808-
pub fn master_fingerprint(&self) -> bip32::Fingerprint {
809-
match *self {
810-
DescriptorPublicKey::XPub(ref xpub) => xpub.master_fingerprint(),
811-
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.master_fingerprint(),
812-
DescriptorPublicKey::Single(ref single) => single.master_fingerprint(),
813-
}
814-
}
815-
816-
/// Full path, from the master key
817-
///
818-
/// For wildcard keys this will return the path up to the wildcard, so you
819-
/// can get full paths by appending one additional derivation step, according
820-
/// to the wildcard type (hardened or normal).
821-
///
822-
/// For multipath extended keys, this returns `None`.
823-
pub fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
824-
match *self {
825-
DescriptorPublicKey::XPub(ref xpub) => xpub.full_derivation_path(),
826-
DescriptorPublicKey::Single(ref single) => single.full_derivation_path(),
827-
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.full_derivation_path(),
828-
}
829-
}
830-
831807
/// Whether or not the key has a wildcard
832808
#[deprecated(note = "use has_wildcard instead")]
833809
pub fn is_deriveable(&self) -> bool {
834810
self.has_wildcard()
835811
}
836812

837-
/// Whether or not the key has a wildcard
838-
pub fn has_wildcard(&self) -> bool {
839-
match *self {
840-
DescriptorPublicKey::Single(ref single) => single.has_wildcard(),
841-
DescriptorPublicKey::XPub(ref xpub) => xpub.has_wildcard(),
842-
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.has_wildcard(),
843-
}
844-
}
845-
846813
#[deprecated(note = "use at_derivation_index instead")]
847814
/// Deprecated name for [`Self::at_derivation_index`].
848815
pub fn derive(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
849816
self.at_derivation_index(index)
850817
}
851818

852-
/// Replaces any wildcard (i.e. `/*`) in the key with a particular derivation index, turning it into a
853-
/// *definite* key (i.e. one where all the derivation paths are set).
854-
///
855-
/// # Returns
856-
///
857-
/// - If this key is not an xpub, returns `self`.
858-
/// - If this key is an xpub but does not have a wildcard, returns `self`.
859-
/// - Otherwise, returns the xpub at derivation `index` (removing the wildcard).
860-
///
861-
/// # Errors
862-
///
863-
/// - If `index` is hardened.
864-
pub fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
865-
match self {
866-
DescriptorPublicKey::Single(single) => single.at_derivation_index(index),
867-
DescriptorPublicKey::XPub(xpub) => xpub.at_derivation_index(index),
868-
DescriptorPublicKey::MultiXPub(xpub) => xpub.at_derivation_index(index),
869-
}
870-
}
871-
872-
/// Whether or not this key has multiple derivation paths.
873-
pub fn is_multipath(&self) -> bool {
874-
match self {
875-
DescriptorPublicKey::Single(single) => single.is_multipath(),
876-
DescriptorPublicKey::XPub(xpub) => xpub.is_multipath(),
877-
DescriptorPublicKey::MultiXPub(xpub) => xpub.is_multipath(),
878-
}
879-
}
880-
881819
/// Get as many keys as derivation paths in this key.
882820
///
883821
/// For raw public key and single-path extended keys it will return the key itself.
@@ -910,6 +848,59 @@ impl DescriptorPublicKey {
910848
}
911849
}
912850

851+
impl DescriptorKey for DescriptorPublicKey {
852+
fn master_fingerprint(&self) -> bip32::Fingerprint {
853+
match *self {
854+
DescriptorPublicKey::XPub(ref xpub) => xpub.master_fingerprint(),
855+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.master_fingerprint(),
856+
DescriptorPublicKey::Single(ref single) => single.master_fingerprint(),
857+
}
858+
}
859+
860+
fn full_derivation_path(&self) -> Option<bip32::DerivationPath> {
861+
match *self {
862+
DescriptorPublicKey::XPub(ref xpub) => xpub.full_derivation_path(),
863+
DescriptorPublicKey::Single(ref single) => single.full_derivation_path(),
864+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.full_derivation_path(),
865+
}
866+
}
867+
868+
fn has_wildcard(&self) -> bool {
869+
match *self {
870+
DescriptorPublicKey::Single(ref single) => single.has_wildcard(),
871+
DescriptorPublicKey::XPub(ref xpub) => xpub.has_wildcard(),
872+
DescriptorPublicKey::MultiXPub(ref xpub) => xpub.has_wildcard(),
873+
}
874+
}
875+
876+
fn at_derivation_index(self, index: u32) -> Result<DefiniteDescriptorKey, ConversionError> {
877+
match self {
878+
DescriptorPublicKey::Single(single) => single.at_derivation_index(index),
879+
DescriptorPublicKey::XPub(xpub) => xpub.at_derivation_index(index),
880+
DescriptorPublicKey::MultiXPub(xpub) => xpub.at_derivation_index(index),
881+
}
882+
}
883+
884+
fn is_multipath(&self) -> bool {
885+
match self {
886+
DescriptorPublicKey::Single(single) => single.is_multipath(),
887+
DescriptorPublicKey::XPub(xpub) => xpub.is_multipath(),
888+
DescriptorPublicKey::MultiXPub(xpub) => xpub.is_multipath(),
889+
}
890+
}
891+
892+
fn derive_public_key<C: Verification>(
893+
&self,
894+
secp: &Secp256k1<C>,
895+
) -> Result<bitcoin::PublicKey, ConversionError> {
896+
match self {
897+
DescriptorPublicKey::Single(single) => single.derive_public_key(secp),
898+
DescriptorPublicKey::XPub(xpub) => xpub.derive_public_key(secp),
899+
DescriptorPublicKey::MultiXPub(xpub) => xpub.derive_public_key(secp),
900+
}
901+
}
902+
}
903+
913904
impl FromStr for DescriptorSecretKey {
914905
type Err = DescriptorKeyParseError;
915906

@@ -1245,11 +1236,7 @@ impl DefiniteDescriptorKey {
12451236
&self,
12461237
secp: &Secp256k1<C>,
12471238
) -> Result<bitcoin::PublicKey, ConversionError> {
1248-
match self.0 {
1249-
DescriptorPublicKey::Single(ref pk) => pk.derive_public_key(secp),
1250-
DescriptorPublicKey::XPub(ref xpk) => xpk.derive_public_key(secp),
1251-
DescriptorPublicKey::MultiXPub(ref xpk) => xpk.derive_public_key(secp),
1252-
}
1239+
self.0.derive_public_key(secp)
12531240
}
12541241

12551242
/// Construct an instance from a descriptor key and a derivation index
@@ -1386,7 +1373,7 @@ mod test {
13861373

13871374
use super::{
13881375
DescriptorKeyParseError, DescriptorMultiXKey, DescriptorPublicKey, DescriptorSecretKey,
1389-
MiniscriptKey, Wildcard,
1376+
DescriptorKey, MiniscriptKey, Wildcard,
13901377
};
13911378
use crate::prelude::*;
13921379

src/descriptor/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use bitcoin::{secp256k1, Address, Network, Script, ScriptBuf, TxIn, Witness};
2222
use sync::Arc;
2323

2424
use self::checksum::verify_checksum;
25+
use self::key::DescriptorKey;
2526
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
2627
use crate::prelude::*;
2728
use crate::{

0 commit comments

Comments
 (0)