Skip to content

Commit c3c7f27

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 2087b94 commit c3c7f27

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

fuzz/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ impl MiniscriptKey for FuzzPk {
4040
type Ripemd160 = u8;
4141
type Hash160 = u8;
4242
type Hash256 = u8;
43+
44+
fn is_uncompressed(&self) -> bool { false }
45+
fn is_x_only_key(&self) -> bool { false }
46+
fn num_der_paths(&self) -> usize { 0 }
4347
}
4448

4549
impl ToPublicKey for FuzzPk {

src/interpreter/mod.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ pub use crate::primitives::threshold::{Threshold, ThresholdError};
149149

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

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

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

163163
/// The type used in the sha256 fragment.
164164
type Sha256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
@@ -178,6 +178,10 @@ impl MiniscriptKey for bitcoin::secp256k1::PublicKey {
178178
type Hash256 = hash256::Hash;
179179
type Ripemd160 = ripemd160::Hash;
180180
type Hash160 = hash160::Hash;
181+
182+
fn is_uncompressed(&self) -> bool { false }
183+
fn is_x_only_key(&self) -> bool { false }
184+
fn num_der_paths(&self) -> usize { 0 }
181185
}
182186

183187
impl MiniscriptKey for bitcoin::PublicKey {
@@ -187,6 +191,8 @@ impl MiniscriptKey for bitcoin::PublicKey {
187191
type Hash160 = hash160::Hash;
188192

189193
fn is_uncompressed(&self) -> bool { !self.compressed }
194+
fn is_x_only_key(&self) -> bool { false }
195+
fn num_der_paths(&self) -> usize { 0 }
190196
}
191197

192198
impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
@@ -195,14 +201,20 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
195201
type Ripemd160 = ripemd160::Hash;
196202
type Hash160 = hash160::Hash;
197203

204+
fn is_uncompressed(&self) -> bool { false }
198205
fn is_x_only_key(&self) -> bool { true }
206+
fn num_der_paths(&self) -> usize { 0 }
199207
}
200208

201209
impl MiniscriptKey for String {
202210
type Sha256 = String;
203211
type Hash256 = String;
204212
type Ripemd160 = String;
205213
type Hash160 = String;
214+
215+
fn is_uncompressed(&self) -> bool { false }
216+
fn is_x_only_key(&self) -> bool { false }
217+
fn num_der_paths(&self) -> usize { 0 }
206218
}
207219

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

0 commit comments

Comments
 (0)