Skip to content

Commit 7c40009

Browse files
committed
Use Key instead of MiniscriptKey
One of the main traits in this lib is `MiniscriptKey`, we can shorten this to `Key` with no loss of meaning. This makes the whole codebase more terse. Terse code, if clear, is easier to read because there is less clutter. Also terseness assists the formatter and can reduce lines of code. This patch is the result of running `s/MiniscriptKey/Key/g` on all source files. To preserve backwards compatibility and make the library more ergonomical to use; add a 'trait alias' of `MiniscriptKey` -> `Key`.
1 parent de74dc0 commit 7c40009

31 files changed

+413
-409
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- Fixed miniscript type system bug. This is a security vulnerability and users are strongly encouraged to upgrade.
44
See this (link)[https://github.com/rust-bitcoin/rust-miniscript/pull/349/commits/db97c39afa4053c2c3917f04392f6e24964b3972] for details.
55
- Support for `tr` descriptors with miniscript leaves and multi_a fragment
6-
- Changes to MiniscriptKey and ToPublicKey traits for x-only keys support
6+
- Changes to Key and ToPublicKey traits for x-only keys support
77
- Add `PsbtExt` trait for psbt operations
88
- `Psbt::update_desc` adds information from a descriptor to a psbt. This figures
99
out the type of the descriptor and adds corresponding redeem script/witness script
@@ -33,7 +33,7 @@ See this (link)[https://github.com/rust-bitcoin/rust-miniscript/pull/349/commits
3333
- Remove `PkCtx` from the API
3434
- Move descriptors into their own types, with an enum containing all of them
3535
- Move descriptor functionality into a trait
36-
- Remove `FromStr` bound from `MiniscriptKey`and `MiniscriptKey::Hash`
36+
- Remove `FromStr` bound from `Key`and `Key::Hash`
3737
- Various `DescriptorPublicKey` improvements
3838
- Allow hardened paths in `DescriptorPublicKey`, remove direct `ToPublicKey` implementation
3939
- Change `Option` to `Result` in all APIs

examples/sign_multisig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn main() {
9090
assert_eq!(my_descriptor.max_satisfaction_weight().unwrap(), 258);
9191

9292
// Sometimes it is necessary to have additional information to get the bitcoin::PublicKey
93-
// from the MiniscriptKey which can supplied by `to_pk_ctx` parameter. For example,
93+
// from the Key which can supplied by `to_pk_ctx` parameter. For example,
9494
// when calculating the script pubkey of a descriptor with xpubs, the secp context and
9595
// child information maybe required.
9696

examples/verify_tx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn main() {
125125
// signatures are not treated as having participated in the script
126126
let secp = secp256k1::Secp256k1::new();
127127
// Sometimes it is necessary to have additional information to get the bitcoin::PublicKey
128-
// from the MiniscriptKey which can supplied by `to_pk_ctx` parameter. For example,
128+
// from the Key which can supplied by `to_pk_ctx` parameter. For example,
129129
// when calculating the script pubkey of a descriptor with xpubs, the secp context and
130130
// child information maybe required.
131131
let interpreter = miniscript::Interpreter::from_txdata(

integration_test/src/test_cpp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bitcoin::{self, Amount, OutPoint, Transaction, TxIn, TxOut, Txid};
1111
use bitcoincore_rpc::{json, Client, RpcApi};
1212
use miniscript::miniscript::iter;
1313
use miniscript::psbt::PsbtExt;
14-
use miniscript::MiniscriptKey;
14+
use miniscript::Key;
1515
use miniscript::Segwitv0;
1616
use miniscript::{Descriptor, DescriptorTrait, Miniscript};
1717
use std::collections::BTreeMap;

integration_test/src/test_desc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoincore_rpc::{json, Client, RpcApi};
1515
use miniscript::miniscript::iter;
1616
use miniscript::psbt::{PsbtInputExt, PsbtExt};
1717
use miniscript::{Descriptor, DescriptorTrait, Miniscript, ToPublicKey};
18-
use miniscript::{MiniscriptKey, ScriptContext};
18+
use miniscript::{Key, ScriptContext};
1919
use std::collections::BTreeMap;
2020

2121
use crate::test_util::{self, TestData};

src/descriptor/bare.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::miniscript::context::ScriptContext;
2727
use crate::policy::{semantic, Liftable};
2828
use crate::util::{varint_len, witness_to_scriptsig};
2929
use crate::{
30-
BareCtx, Error, ForEach, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey,
30+
BareCtx, Error, ForEach, ForEachKey, Miniscript, Key, Satisfier, ToPublicKey,
3131
TranslatePk,
3232
};
3333

@@ -39,12 +39,12 @@ use super::{
3939
/// Create a Bare Descriptor. That is descriptor that is
4040
/// not wrapped in sh or wsh. This covers the Pk descriptor
4141
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
42-
pub struct Bare<Pk: MiniscriptKey> {
42+
pub struct Bare<Pk: Key> {
4343
/// underlying miniscript
4444
ms: Miniscript<Pk, BareCtx>,
4545
}
4646

47-
impl<Pk: MiniscriptKey> Bare<Pk> {
47+
impl<Pk: Key> Bare<Pk> {
4848
/// Create a new raw descriptor
4949
pub fn new(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error> {
5050
// do the top-level checks
@@ -63,7 +63,7 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
6363
}
6464
}
6565

66-
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
66+
impl<Pk: Key + ToPublicKey> Bare<Pk> {
6767
/// Obtain the corresponding script pubkey for this descriptor
6868
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
6969
pub fn spk(&self) -> Script {
@@ -83,32 +83,32 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
8383
}
8484
}
8585

86-
impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
86+
impl<Pk: Key> fmt::Debug for Bare<Pk> {
8787
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8888
write!(f, "{:?}", self.ms)
8989
}
9090
}
9191

92-
impl<Pk: MiniscriptKey> fmt::Display for Bare<Pk> {
92+
impl<Pk: Key> fmt::Display for Bare<Pk> {
9393
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9494
let desc = format!("{}", self.ms);
9595
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
9696
write!(f, "{}#{}", &desc, &checksum)
9797
}
9898
}
9999

100-
impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
100+
impl<Pk: Key> Liftable<Pk> for Bare<Pk> {
101101
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
102102
self.ms.lift()
103103
}
104104
}
105105

106106
impl<Pk> FromTree for Bare<Pk>
107107
where
108-
Pk: MiniscriptKey + FromStr,
108+
Pk: Key + FromStr,
109109
Pk::Hash: FromStr,
110110
<Pk as FromStr>::Err: ToString,
111-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
111+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
112112
{
113113
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
114114
let sub = Miniscript::<Pk, BareCtx>::from_tree(&top)?;
@@ -119,10 +119,10 @@ where
119119

120120
impl<Pk> FromStr for Bare<Pk>
121121
where
122-
Pk: MiniscriptKey + FromStr,
122+
Pk: Key + FromStr,
123123
Pk::Hash: FromStr,
124124
<Pk as FromStr>::Err: ToString,
125-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
125+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
126126
{
127127
type Err = Error;
128128

@@ -133,7 +133,7 @@ where
133133
}
134134
}
135135

136-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
136+
impl<Pk: Key> DescriptorTrait<Pk> for Bare<Pk> {
137137
fn sanity_check(&self) -> Result<(), Error> {
138138
self.ms.sanity_check()?;
139139
Ok(())
@@ -202,7 +202,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
202202
}
203203
}
204204

205-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
205+
impl<Pk: Key> ForEachKey<Pk> for Bare<Pk> {
206206
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
207207
where
208208
Pk: 'a,
@@ -212,7 +212,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
212212
}
213213
}
214214

215-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Bare<P> {
215+
impl<P: Key, Q: Key> TranslatePk<P, Q> for Bare<P> {
216216
type Output = Bare<Q>;
217217

218218
fn translate_pk<Fpk, Fpkh, E>(
@@ -223,7 +223,7 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Bare<P> {
223223
where
224224
Fpk: FnMut(&P) -> Result<Q, E>,
225225
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
226-
Q: MiniscriptKey,
226+
Q: Key,
227227
{
228228
Ok(Bare::new(
229229
self.ms
@@ -235,12 +235,12 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Bare<P> {
235235

236236
/// A bare PkH descriptor at top level
237237
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
238-
pub struct Pkh<Pk: MiniscriptKey> {
238+
pub struct Pkh<Pk: Key> {
239239
/// underlying publickey
240240
pk: Pk,
241241
}
242242

243-
impl<Pk: MiniscriptKey> Pkh<Pk> {
243+
impl<Pk: Key> Pkh<Pk> {
244244
/// Create a new Pkh descriptor
245245
pub fn new(pk: Pk) -> Self {
246246
// do the top-level checks
@@ -258,7 +258,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
258258
}
259259
}
260260

261-
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
261+
impl<Pk: Key + ToPublicKey> Pkh<Pk> {
262262
/// Obtain the corresponding script pubkey for this descriptor
263263
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
264264
pub fn spk(&self) -> Script {
@@ -285,32 +285,32 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
285285
}
286286
}
287287

288-
impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {
288+
impl<Pk: Key> fmt::Debug for Pkh<Pk> {
289289
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
290290
write!(f, "pkh({:?})", self.pk)
291291
}
292292
}
293293

294-
impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
294+
impl<Pk: Key> fmt::Display for Pkh<Pk> {
295295
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
296296
let desc = format!("pkh({})", self.pk);
297297
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
298298
write!(f, "{}#{}", &desc, &checksum)
299299
}
300300
}
301301

302-
impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
302+
impl<Pk: Key> Liftable<Pk> for Pkh<Pk> {
303303
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
304304
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
305305
}
306306
}
307307

308308
impl<Pk> FromTree for Pkh<Pk>
309309
where
310-
Pk: MiniscriptKey + FromStr,
310+
Pk: Key + FromStr,
311311
Pk::Hash: FromStr,
312312
<Pk as FromStr>::Err: ToString,
313-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
313+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
314314
{
315315
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
316316
if top.name == "pkh" && top.args.len() == 1 {
@@ -329,10 +329,10 @@ where
329329

330330
impl<Pk> FromStr for Pkh<Pk>
331331
where
332-
Pk: MiniscriptKey + FromStr,
332+
Pk: Key + FromStr,
333333
Pk::Hash: FromStr,
334334
<Pk as FromStr>::Err: ToString,
335-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
335+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
336336
{
337337
type Err = Error;
338338

@@ -343,7 +343,7 @@ where
343343
}
344344
}
345345

346-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
346+
impl<Pk: Key> DescriptorTrait<Pk> for Pkh<Pk> {
347347
fn sanity_check(&self) -> Result<(), Error> {
348348
Ok(())
349349
}
@@ -414,7 +414,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
414414
}
415415
}
416416

417-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
417+
impl<Pk: Key> ForEachKey<Pk> for Pkh<Pk> {
418418
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
419419
where
420420
Pk: 'a,
@@ -424,7 +424,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
424424
}
425425
}
426426

427-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Pkh<P> {
427+
impl<P: Key, Q: Key> TranslatePk<P, Q> for Pkh<P> {
428428
type Output = Pkh<Q>;
429429

430430
fn translate_pk<Fpk, Fpkh, E>(
@@ -435,7 +435,7 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Pkh<P> {
435435
where
436436
Fpk: FnMut(&P) -> Result<Q, E>,
437437
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
438-
Q: MiniscriptKey,
438+
Q: Key,
439439
{
440440
Ok(Pkh::new(translatefpk(&self.pk)?))
441441
}

src/descriptor/key.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bitcoin::{
1010
XOnlyPublicKey, XpubIdentifier,
1111
};
1212

13-
use crate::{MiniscriptKey, ToPublicKey};
13+
use crate::{Key, ToPublicKey};
1414

1515
/// The descriptor pubkey, either a single pubkey or an xpub.
1616
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
@@ -684,7 +684,7 @@ impl<K: InnerXKey> DescriptorXKey<K> {
684684
}
685685
}
686686

687-
impl MiniscriptKey for DescriptorPublicKey {
687+
impl Key for DescriptorPublicKey {
688688
// This allows us to be able to derive public keys even for PkH s
689689
type Hash = Self;
690690

0 commit comments

Comments
 (0)