Skip to content

Commit bdddc39

Browse files
committed
Remove default trait method implementations
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.
1 parent ef1d28d commit bdddc39

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/interpreter/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ impl MiniscriptKey for BitcoinKey {
130130
BitcoinKey::XOnlyPublicKey(_) => false,
131131
}
132132
}
133+
fn is_x_only_key(&self) -> bool { false }
134+
fn num_der_paths(&self) -> usize { 0 }
133135
}
134136

135137
impl<'txin> Interpreter<'txin> {

src/lib.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ pub use crate::primitives::relative_locktime::{RelLockTime, RelLockTimeError};
155155

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

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

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

169169
/// The type used in the sha256 fragment.
170170
type Sha256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
@@ -184,6 +184,10 @@ impl MiniscriptKey for bitcoin::secp256k1::PublicKey {
184184
type Hash256 = hash256::Hash;
185185
type Ripemd160 = ripemd160::Hash;
186186
type Hash160 = hash160::Hash;
187+
188+
fn is_uncompressed(&self) -> bool { false }
189+
fn is_x_only_key(&self) -> bool { false }
190+
fn num_der_paths(&self) -> usize { 0 }
187191
}
188192

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

195199
fn is_uncompressed(&self) -> bool { !self.compressed }
200+
fn is_x_only_key(&self) -> bool { false }
201+
fn num_der_paths(&self) -> usize { 0 }
196202
}
197203

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

210+
fn is_uncompressed(&self) -> bool { false }
204211
fn is_x_only_key(&self) -> bool { true }
212+
fn num_der_paths(&self) -> usize { 0 }
205213
}
206214

207215
impl MiniscriptKey for String {
208216
type Sha256 = String;
209217
type Hash256 = String;
210218
type Ripemd160 = String;
211219
type Hash160 = String;
220+
221+
fn is_uncompressed(&self) -> bool { false }
222+
fn is_x_only_key(&self) -> bool { false }
223+
fn num_der_paths(&self) -> usize { 0 }
212224
}
213225

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

0 commit comments

Comments
 (0)