Skip to content

Commit c3c5959

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 c3c5959

31 files changed

+414
-442
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-30
Original file line numberDiff line numberDiff line change
@@ -27,8 +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,
31-
TranslatePk,
30+
BareCtx, Error, ForEach, ForEachKey, Key, Miniscript, Satisfier, ToPublicKey, TranslatePk,
3231
};
3332

3433
use super::{
@@ -39,12 +38,12 @@ use super::{
3938
/// Create a Bare Descriptor. That is descriptor that is
4039
/// not wrapped in sh or wsh. This covers the Pk descriptor
4140
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
42-
pub struct Bare<Pk: MiniscriptKey> {
41+
pub struct Bare<Pk: Key> {
4342
/// underlying miniscript
4443
ms: Miniscript<Pk, BareCtx>,
4544
}
4645

47-
impl<Pk: MiniscriptKey> Bare<Pk> {
46+
impl<Pk: Key> Bare<Pk> {
4847
/// Create a new raw descriptor
4948
pub fn new(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error> {
5049
// do the top-level checks
@@ -63,7 +62,7 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
6362
}
6463
}
6564

66-
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
65+
impl<Pk: Key + ToPublicKey> Bare<Pk> {
6766
/// Obtain the corresponding script pubkey for this descriptor
6867
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
6968
pub fn spk(&self) -> Script {
@@ -83,32 +82,32 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
8382
}
8483
}
8584

86-
impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
85+
impl<Pk: Key> fmt::Debug for Bare<Pk> {
8786
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8887
write!(f, "{:?}", self.ms)
8988
}
9089
}
9190

92-
impl<Pk: MiniscriptKey> fmt::Display for Bare<Pk> {
91+
impl<Pk: Key> fmt::Display for Bare<Pk> {
9392
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9493
let desc = format!("{}", self.ms);
9594
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
9695
write!(f, "{}#{}", &desc, &checksum)
9796
}
9897
}
9998

100-
impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
99+
impl<Pk: Key> Liftable<Pk> for Bare<Pk> {
101100
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
102101
self.ms.lift()
103102
}
104103
}
105104

106105
impl<Pk> FromTree for Bare<Pk>
107106
where
108-
Pk: MiniscriptKey + FromStr,
107+
Pk: Key + FromStr,
109108
Pk::Hash: FromStr,
110109
<Pk as FromStr>::Err: ToString,
111-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
110+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
112111
{
113112
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
114113
let sub = Miniscript::<Pk, BareCtx>::from_tree(&top)?;
@@ -119,10 +118,10 @@ where
119118

120119
impl<Pk> FromStr for Bare<Pk>
121120
where
122-
Pk: MiniscriptKey + FromStr,
121+
Pk: Key + FromStr,
123122
Pk::Hash: FromStr,
124123
<Pk as FromStr>::Err: ToString,
125-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
124+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
126125
{
127126
type Err = Error;
128127

@@ -133,7 +132,7 @@ where
133132
}
134133
}
135134

136-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
135+
impl<Pk: Key> DescriptorTrait<Pk> for Bare<Pk> {
137136
fn sanity_check(&self) -> Result<(), Error> {
138137
self.ms.sanity_check()?;
139138
Ok(())
@@ -202,7 +201,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
202201
}
203202
}
204203

205-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
204+
impl<Pk: Key> ForEachKey<Pk> for Bare<Pk> {
206205
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
207206
where
208207
Pk: 'a,
@@ -212,7 +211,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
212211
}
213212
}
214213

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

218217
fn translate_pk<Fpk, Fpkh, E>(
@@ -223,7 +222,7 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Bare<P> {
223222
where
224223
Fpk: FnMut(&P) -> Result<Q, E>,
225224
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
226-
Q: MiniscriptKey,
225+
Q: Key,
227226
{
228227
Ok(Bare::new(
229228
self.ms
@@ -235,12 +234,12 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Bare<P> {
235234

236235
/// A bare PkH descriptor at top level
237236
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
238-
pub struct Pkh<Pk: MiniscriptKey> {
237+
pub struct Pkh<Pk: Key> {
239238
/// underlying publickey
240239
pk: Pk,
241240
}
242241

243-
impl<Pk: MiniscriptKey> Pkh<Pk> {
242+
impl<Pk: Key> Pkh<Pk> {
244243
/// Create a new Pkh descriptor
245244
pub fn new(pk: Pk) -> Self {
246245
// do the top-level checks
@@ -258,7 +257,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
258257
}
259258
}
260259

261-
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
260+
impl<Pk: Key + ToPublicKey> Pkh<Pk> {
262261
/// Obtain the corresponding script pubkey for this descriptor
263262
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
264263
pub fn spk(&self) -> Script {
@@ -285,32 +284,32 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
285284
}
286285
}
287286

288-
impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {
287+
impl<Pk: Key> fmt::Debug for Pkh<Pk> {
289288
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
290289
write!(f, "pkh({:?})", self.pk)
291290
}
292291
}
293292

294-
impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
293+
impl<Pk: Key> fmt::Display for Pkh<Pk> {
295294
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
296295
let desc = format!("pkh({})", self.pk);
297296
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
298297
write!(f, "{}#{}", &desc, &checksum)
299298
}
300299
}
301300

302-
impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
301+
impl<Pk: Key> Liftable<Pk> for Pkh<Pk> {
303302
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
304303
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
305304
}
306305
}
307306

308307
impl<Pk> FromTree for Pkh<Pk>
309308
where
310-
Pk: MiniscriptKey + FromStr,
309+
Pk: Key + FromStr,
311310
Pk::Hash: FromStr,
312311
<Pk as FromStr>::Err: ToString,
313-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
312+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
314313
{
315314
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
316315
if top.name == "pkh" && top.args.len() == 1 {
@@ -329,10 +328,10 @@ where
329328

330329
impl<Pk> FromStr for Pkh<Pk>
331330
where
332-
Pk: MiniscriptKey + FromStr,
331+
Pk: Key + FromStr,
333332
Pk::Hash: FromStr,
334333
<Pk as FromStr>::Err: ToString,
335-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
334+
<<Pk as Key>::Hash as FromStr>::Err: ToString,
336335
{
337336
type Err = Error;
338337

@@ -343,7 +342,7 @@ where
343342
}
344343
}
345344

346-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
345+
impl<Pk: Key> DescriptorTrait<Pk> for Pkh<Pk> {
347346
fn sanity_check(&self) -> Result<(), Error> {
348347
Ok(())
349348
}
@@ -414,7 +413,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
414413
}
415414
}
416415

417-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
416+
impl<Pk: Key> ForEachKey<Pk> for Pkh<Pk> {
418417
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
419418
where
420419
Pk: 'a,
@@ -424,7 +423,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
424423
}
425424
}
426425

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

430429
fn translate_pk<Fpk, Fpkh, E>(
@@ -435,7 +434,7 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Pkh<P> {
435434
where
436435
Fpk: FnMut(&P) -> Result<Q, E>,
437436
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
438-
Q: MiniscriptKey,
437+
Q: Key,
439438
{
440439
Ok(Pkh::new(translatefpk(&self.pk)?))
441440
}

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)