Skip to content

Commit 4ad053b

Browse files
committed
Merge #620: Clean up the MiniscriptKey and ToPublicKey traits
c3a02d2 Remove whitespace (Tobin C. Harding) 2968a10 Move associated types to top of struct (Tobin C. Harding) 8dc371b Remove default trait method implementations (Tobin C. Harding) 438bbc5 Remove code comment on is_x_only_key (Tobin C. Harding) 2d8c06f Improve rustdocs on ToPublicKey (Tobin C. Harding) 02cd5ad Remove "what" comment (Tobin C. Harding) f93eca8 Move trait method (Tobin C. Harding) 765df89 Improve rustdoc on the MiniscriptKey trait (Tobin C. Harding) Pull request description: Clean up, all docs and refactors, except that we remove default implementations from public traits so this is a breaking change - inside the library there are no logic changes. ACKs for top commit: apoelstra: ACK c3a02d2; successfully ran local tests Tree-SHA512: c2f29bb43dfd1133bf07fc4c888f6017125bc78e1ac818c3d8a76d878cbccfc7cdb69113588a04490fef26f67c844dd26e7cfcd1aa86829409d214039492b62d
2 parents f1d1fc1 + c3a02d2 commit 4ad053b

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

fuzz/src/lib.rs

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

4548
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: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -147,52 +147,51 @@ pub use crate::primitives::absolute_locktime::{AbsLockTime, AbsLockTimeError};
147147
pub use crate::primitives::relative_locktime::{RelLockTime, RelLockTimeError};
148148
pub use crate::primitives::threshold::{Threshold, ThresholdError};
149149

150-
/// Public key trait which can be converted to Hash type
150+
/// 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 pubkey is uncompressed. Defaults to `false`.
153-
fn is_uncompressed(&self) -> bool { false }
154-
155-
/// Returns true if the pubkey is an x-only pubkey. Defaults to `false`.
156-
// This is required to know what in DescriptorPublicKey to know whether the inner
157-
// key in allowed in descriptor context
158-
fn is_x_only_key(&self) -> bool { false }
159-
160-
/// Returns the number of different derivation paths in this key. Only >1 for keys
161-
/// in BIP389 multipath descriptors.
162-
fn num_der_paths(&self) -> usize { 0 }
163-
164-
/// The associated [`bitcoin::hashes::sha256::Hash`] for this [`MiniscriptKey`], used in the
165-
/// sha256 fragment.
152+
/// The SHA256 hash type used in the `sha256` fragment.
166153
type Sha256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
167154

168-
/// The associated [`miniscript::hash256::Hash`] for this [`MiniscriptKey`], used in the
169-
/// hash256 fragment.
155+
/// The HASH256 hash type used in the `hash256` fragment.
170156
type Hash256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
171157

172-
/// The associated [`bitcoin::hashes::ripemd160::Hash`] for this [`MiniscriptKey`] type, used
173-
/// in the ripemd160 fragment.
158+
/// The RIPEMD256 hash type used in the `ripemd256` fragment.
174159
type Ripemd160: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
175160

176-
/// The associated [`bitcoin::hashes::hash160::Hash`] for this [`MiniscriptKey`] type, used in
177-
/// the hash160 fragment.
161+
/// The HASH160 hash type used in the `hash160` fragment.
178162
type Hash160: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash;
163+
164+
/// Returns true if the key is serialized uncompressed (defaults to `false`).
165+
fn is_uncompressed(&self) -> bool { false }
166+
167+
/// Returns true if the key is an x-only pubkey (defaults to `false`).
168+
fn is_x_only_key(&self) -> bool;
169+
170+
/// Returns the number of different derivation paths in this key (defaults to `0`).
171+
///
172+
/// Only >1 for keys in BIP389 multipath descriptors.
173+
fn num_der_paths(&self) -> usize;
179174
}
180175

181176
impl MiniscriptKey for bitcoin::secp256k1::PublicKey {
182177
type Sha256 = sha256::Hash;
183178
type Hash256 = hash256::Hash;
184179
type Ripemd160 = ripemd160::Hash;
185180
type Hash160 = hash160::Hash;
181+
182+
fn is_x_only_key(&self) -> bool { false }
183+
fn num_der_paths(&self) -> usize { 0 }
186184
}
187185

188186
impl MiniscriptKey for bitcoin::PublicKey {
189-
/// Returns the compressed-ness of the underlying secp256k1 key.
190-
fn is_uncompressed(&self) -> bool { !self.compressed }
191-
192187
type Sha256 = sha256::Hash;
193188
type Hash256 = hash256::Hash;
194189
type Ripemd160 = ripemd160::Hash;
195190
type Hash160 = hash160::Hash;
191+
192+
fn is_uncompressed(&self) -> bool { !self.compressed }
193+
fn is_x_only_key(&self) -> bool { false }
194+
fn num_der_paths(&self) -> usize { 0 }
196195
}
197196

198197
impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
@@ -202,71 +201,69 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
202201
type Hash160 = hash160::Hash;
203202

204203
fn is_x_only_key(&self) -> bool { true }
204+
fn num_der_paths(&self) -> usize { 0 }
205205
}
206206

207207
impl MiniscriptKey for String {
208-
type Sha256 = String; // specify hashes as string
208+
type Sha256 = String;
209209
type Hash256 = String;
210210
type Ripemd160 = String;
211211
type Hash160 = String;
212+
213+
fn is_x_only_key(&self) -> bool { false }
214+
fn num_der_paths(&self) -> usize { 0 }
212215
}
213216

214-
/// Trait describing public key types which can be converted to bitcoin pubkeys
217+
/// Trait describing key types that can be converted to bitcoin public keys.
215218
pub trait ToPublicKey: MiniscriptKey {
216-
/// Converts an object to a public key
219+
/// Converts key to a public key.
217220
fn to_public_key(&self) -> bitcoin::PublicKey;
218221

219-
/// Convert an object to x-only pubkey
222+
/// Converts key to an x-only public key.
220223
fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey {
221224
let pk = self.to_public_key();
222225
bitcoin::secp256k1::XOnlyPublicKey::from(pk.inner)
223226
}
224227

225-
/// Obtain the public key hash for this MiniscriptKey
226-
/// Expects an argument to specify the signature type.
227-
/// This would determine whether to serialize the key as 32 byte x-only pubkey
228-
/// or regular public key when computing the hash160
228+
/// Obtains the pubkey hash for this key (as a `MiniscriptKey`).
229+
///
230+
/// Expects an argument to specify the signature type. This determines whether to serialize
231+
/// the key as 32 byte x-only pubkey or regular public key when computing the hash160.
229232
fn to_pubkeyhash(&self, sig_type: SigType) -> hash160::Hash {
230233
match sig_type {
231234
SigType::Ecdsa => hash160::Hash::hash(&self.to_public_key().to_bytes()),
232235
SigType::Schnorr => hash160::Hash::hash(&self.to_x_only_pubkey().serialize()),
233236
}
234237
}
235238

236-
/// Converts the generic associated [`MiniscriptKey::Sha256`] to [`sha256::Hash`]
239+
/// Converts the associated [`MiniscriptKey::Sha256`] type to a [`sha256::Hash`].
237240
fn to_sha256(hash: &<Self as MiniscriptKey>::Sha256) -> sha256::Hash;
238241

239-
/// Converts the generic associated [`MiniscriptKey::Hash256`] to [`hash256::Hash`]
242+
/// Converts the associated [`MiniscriptKey::Hash256`] type to a [`hash256::Hash`].
240243
fn to_hash256(hash: &<Self as MiniscriptKey>::Hash256) -> hash256::Hash;
241244

242-
/// Converts the generic associated [`MiniscriptKey::Ripemd160`] to [`ripemd160::Hash`]
245+
/// Converts the associated [`MiniscriptKey::Ripemd160`] type to a [`ripemd160::Hash`].
243246
fn to_ripemd160(hash: &<Self as MiniscriptKey>::Ripemd160) -> ripemd160::Hash;
244247

245-
/// Converts the generic associated [`MiniscriptKey::Hash160`] to [`hash160::Hash`]
248+
/// Converts the associated [`MiniscriptKey::Hash160`] type to a [`hash160::Hash`].
246249
fn to_hash160(hash: &<Self as MiniscriptKey>::Hash160) -> hash160::Hash;
247250
}
248251

249252
impl ToPublicKey for bitcoin::PublicKey {
250253
fn to_public_key(&self) -> bitcoin::PublicKey { *self }
251254

252255
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash }
253-
254256
fn to_hash256(hash: &hash256::Hash) -> hash256::Hash { *hash }
255-
256257
fn to_ripemd160(hash: &ripemd160::Hash) -> ripemd160::Hash { *hash }
257-
258258
fn to_hash160(hash: &hash160::Hash) -> hash160::Hash { *hash }
259259
}
260260

261261
impl ToPublicKey for bitcoin::secp256k1::PublicKey {
262262
fn to_public_key(&self) -> bitcoin::PublicKey { bitcoin::PublicKey::new(*self) }
263263

264264
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash }
265-
266265
fn to_hash256(hash: &hash256::Hash) -> hash256::Hash { *hash }
267-
268266
fn to_ripemd160(hash: &ripemd160::Hash) -> ripemd160::Hash { *hash }
269-
270267
fn to_hash160(hash: &hash160::Hash) -> hash160::Hash { *hash }
271268
}
272269

@@ -283,11 +280,8 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
283280
fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey { *self }
284281

285282
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash }
286-
287283
fn to_hash256(hash: &hash256::Hash) -> hash256::Hash { *hash }
288-
289284
fn to_ripemd160(hash: &ripemd160::Hash) -> ripemd160::Hash { *hash }
290-
291285
fn to_hash160(hash: &hash160::Hash) -> hash160::Hash { *hash }
292286
}
293287

0 commit comments

Comments
 (0)