Skip to content

Commit b29c3cd

Browse files
committed
descriptor: add is_wildcard method to DescriptorPublicKey and Descriptor
1 parent b4f940a commit b29c3cd

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/descriptor/key.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ impl DescriptorPublicKey {
392392
}
393393
}
394394

395+
/// Whether or not the key has a wildcards
396+
pub fn is_deriveable(&self) -> bool {
397+
match *self {
398+
DescriptorPublicKey::SinglePub(..) => false,
399+
DescriptorPublicKey::XPub(ref xpub) => xpub.wildcard != Wildcard::None,
400+
}
401+
}
402+
395403
/// If this public key has a wildcard, replace it by the given index
396404
///
397405
/// Panics if given an index ≥ 2^31
@@ -748,6 +756,24 @@ mod test {
748756
);
749757
}
750758

759+
#[test]
760+
fn test_wildcard() {
761+
let public_key = DescriptorPublicKey::from_str("[abcdef00/0'/1']tpubDBrgjcxBxnXyL575sHdkpKohWu5qHKoQ7TJXKNrYznh5fVEGBv89hA8ENW7A8MFVpFUSvgLqc4Nj1WZcpePX6rrxviVtPowvMuGF5rdT2Vi/2").unwrap();
762+
assert_eq!(public_key.master_fingerprint().to_string(), "abcdef00");
763+
assert_eq!(public_key.full_derivation_path().to_string(), "m/0'/1'/2");
764+
assert_eq!(public_key.is_deriveable(), false);
765+
766+
let public_key = DescriptorPublicKey::from_str("[abcdef00/0'/1']tpubDBrgjcxBxnXyL575sHdkpKohWu5qHKoQ7TJXKNrYznh5fVEGBv89hA8ENW7A8MFVpFUSvgLqc4Nj1WZcpePX6rrxviVtPowvMuGF5rdT2Vi/*").unwrap();
767+
assert_eq!(public_key.master_fingerprint().to_string(), "abcdef00");
768+
assert_eq!(public_key.full_derivation_path().to_string(), "m/0'/1'");
769+
assert_eq!(public_key.is_deriveable(), true);
770+
771+
let public_key = DescriptorPublicKey::from_str("[abcdef00/0'/1']tpubDBrgjcxBxnXyL575sHdkpKohWu5qHKoQ7TJXKNrYznh5fVEGBv89hA8ENW7A8MFVpFUSvgLqc4Nj1WZcpePX6rrxviVtPowvMuGF5rdT2Vi/*h").unwrap();
772+
assert_eq!(public_key.master_fingerprint().to_string(), "abcdef00");
773+
assert_eq!(public_key.full_derivation_path().to_string(), "m/0'/1'");
774+
assert_eq!(public_key.is_deriveable(), true);
775+
}
776+
751777
#[test]
752778
fn test_deriv_on_xprv() {
753779
let secp = secp256k1::Secp256k1::signing_only();
@@ -757,6 +783,7 @@ mod test {
757783
assert_eq!(public_key.to_string(), "[2cbe2a6d/0'/1']tpubDBrgjcxBxnXyL575sHdkpKohWu5qHKoQ7TJXKNrYznh5fVEGBv89hA8ENW7A8MFVpFUSvgLqc4Nj1WZcpePX6rrxviVtPowvMuGF5rdT2Vi/2");
758784
assert_eq!(public_key.master_fingerprint().to_string(), "2cbe2a6d");
759785
assert_eq!(public_key.full_derivation_path().to_string(), "m/0'/1'/2");
786+
assert_eq!(public_key.is_deriveable(), false);
760787

761788
let secret_key = DescriptorSecretKey::from_str("tprv8ZgxMBicQKsPcwcD4gSnMti126ZiETsuX7qwrtMypr6FBwAP65puFn4v6c3jrN9VwtMRMph6nyT63NrfUL4C3nBzPcduzVSuHD7zbX2JKVc/0'/1'/2'").unwrap();
762789
let public_key = secret_key.as_public(&secp).unwrap();

src/descriptor/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
442442
}
443443

444444
impl Descriptor<DescriptorPublicKey> {
445+
/// Whether or not the descriptor has any wildcards
446+
pub fn is_deriveable(&self) -> bool {
447+
self.for_any_key(|key| key.as_key().is_deriveable())
448+
}
449+
445450
/// Derives all wildcard keys in the descriptor using the supplied index
446451
///
447452
/// Panics if given an index ≥ 2^31

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ pub enum ForEach<'a, Pk: MiniscriptKey + 'a> {
412412
Hash(&'a Pk::Hash),
413413
}
414414

415+
impl<'a, Pk: MiniscriptKey<Hash = Pk>> ForEach<'a, Pk> {
416+
/// Convenience method to avoid distinguishing between keys and hashes when these are the same type
417+
pub fn as_key(&self) -> &'a Pk {
418+
match *self {
419+
ForEach::Key(ref_key) => ref_key,
420+
ForEach::Hash(ref_key) => ref_key,
421+
}
422+
}
423+
}
424+
415425
/// Trait describing the ability to iterate over every key
416426
pub trait ForEachKey<Pk: MiniscriptKey> {
417427
/// Run a predicate on every key in the descriptor, returning whether

0 commit comments

Comments
 (0)