Skip to content

Commit 7270edf

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 42ca0f3 commit 7270edf

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
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

+15-3
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ use crate::prelude::*;
151151
/// Trait representing a key which can be converted to a hash type.
152152
pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Hash {
153153
/// Returns true if the key is serialized uncompressed, defaults to `false`.
154-
fn is_uncompressed(&self) -> bool { false }
154+
fn is_uncompressed(&self) -> bool;
155155

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

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

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

184188
impl MiniscriptKey for bitcoin::PublicKey {
@@ -188,6 +192,8 @@ impl MiniscriptKey for bitcoin::PublicKey {
188192
type Hash160 = hash160::Hash;
189193

190194
fn is_uncompressed(&self) -> bool { !self.compressed }
195+
fn is_x_only_key(&self) -> bool { false }
196+
fn num_der_paths(&self) -> usize { 0 }
191197
}
192198

193199
impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
@@ -196,14 +202,20 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
196202
type Ripemd160 = ripemd160::Hash;
197203
type Hash160 = hash160::Hash;
198204

205+
fn is_uncompressed(&self) -> bool { false }
199206
fn is_x_only_key(&self) -> bool { true }
207+
fn num_der_paths(&self) -> usize { 0 }
200208
}
201209

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

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

0 commit comments

Comments
 (0)