Skip to content

Commit

Permalink
Remove default trait method implementations
Browse files Browse the repository at this point in the history
Implementors of `MiniscriptKey` must reason about the three trait
methods, this implies the trait methods are required, not provided.

We are using the default impls to remove code duplication, this is an
abuse of the default impls. It makes the docs read incorrectly, by implying
that implementors _do not_ need to think reason about these trait methods.

Remove default trait method implementations and manually implement the
trait methods for all current implementors.
  • Loading branch information
tcharding committed Mar 28, 2024
1 parent ef1d28d commit bdddc39
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ impl MiniscriptKey for BitcoinKey {
BitcoinKey::XOnlyPublicKey(_) => false,
}
}
fn is_x_only_key(&self) -> bool { false }
fn num_der_paths(&self) -> usize { 0 }
}

impl<'txin> Interpreter<'txin> {
Expand Down
24 changes: 18 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ pub use crate::primitives::relative_locktime::{RelLockTime, RelLockTimeError};

/// Trait representing a key which can be converted to a hash type.
pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Hash {
/// Returns true if the key is serialized uncompressed, defaults to `false`.
fn is_uncompressed(&self) -> bool { false }
/// Returns true if the key is serialized uncompressed.
fn is_uncompressed(&self) -> bool;

/// Returns true if the key is an x-only pubkey, defaults to `false`.
fn is_x_only_key(&self) -> bool { false }
/// Returns true if the key is an x-only pubkey.
fn is_x_only_key(&self) -> bool;

/// Returns the number of different derivation paths in this key, defaults to `0`.
/// Returns the number of different derivation paths in this key.
///
/// Only >1 for keys in BIP389 multipath descriptors.
fn num_der_paths(&self) -> usize { 0 }
fn num_der_paths(&self) -> usize;

/// The type used in the sha256 fragment.
type Sha256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
Expand All @@ -184,6 +184,10 @@ impl MiniscriptKey for bitcoin::secp256k1::PublicKey {
type Hash256 = hash256::Hash;
type Ripemd160 = ripemd160::Hash;
type Hash160 = hash160::Hash;

fn is_uncompressed(&self) -> bool { false }
fn is_x_only_key(&self) -> bool { false }
fn num_der_paths(&self) -> usize { 0 }
}

impl MiniscriptKey for bitcoin::PublicKey {
Expand All @@ -193,6 +197,8 @@ impl MiniscriptKey for bitcoin::PublicKey {
type Hash160 = hash160::Hash;

fn is_uncompressed(&self) -> bool { !self.compressed }
fn is_x_only_key(&self) -> bool { false }
fn num_der_paths(&self) -> usize { 0 }
}

impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
Expand All @@ -201,14 +207,20 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
type Ripemd160 = ripemd160::Hash;
type Hash160 = hash160::Hash;

fn is_uncompressed(&self) -> bool { false }
fn is_x_only_key(&self) -> bool { true }
fn num_der_paths(&self) -> usize { 0 }
}

impl MiniscriptKey for String {
type Sha256 = String;
type Hash256 = String;
type Ripemd160 = String;
type Hash160 = String;

fn is_uncompressed(&self) -> bool { false }
fn is_x_only_key(&self) -> bool { false }
fn num_der_paths(&self) -> usize { 0 }
}

/// Trait describing key types that can be converted to bitcoin public keys.
Expand Down

0 comments on commit bdddc39

Please sign in to comment.