Skip to content

Commit 62b1e40

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 and then running the formatter. To preserve backwards compatibility and make the library more ergonomical to use; add a 'trait alias' of `MiniscriptKey` -> `Key`.
1 parent c7c39f1 commit 62b1e40

30 files changed

+387
-413
lines changed

examples/sign_multisig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535
assert_eq!(descriptor.max_satisfaction_weight().unwrap(), 258);
3636

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

src/descriptor/bare.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,18 @@ use crate::policy::{semantic, Liftable};
3030
use crate::prelude::*;
3131
use crate::util::{varint_len, witness_to_scriptsig};
3232
use crate::{
33-
BareCtx, Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey, TranslatePk,
34-
Translator,
33+
BareCtx, Error, ForEachKey, Key, Miniscript, Satisfier, ToPublicKey, TranslatePk, Translator,
3534
};
3635

3736
/// Create a Bare Descriptor. That is descriptor that is
3837
/// not wrapped in sh or wsh. This covers the Pk descriptor
3938
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
40-
pub struct Bare<Pk: MiniscriptKey> {
39+
pub struct Bare<Pk: Key> {
4140
/// underlying miniscript
4241
ms: Miniscript<Pk, BareCtx>,
4342
}
4443

45-
impl<Pk: MiniscriptKey> Bare<Pk> {
44+
impl<Pk: Key> Bare<Pk> {
4645
/// Create a new raw descriptor
4746
pub fn new(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error> {
4847
// do the top-level checks
@@ -81,7 +80,7 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
8180
}
8281
}
8382

84-
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
83+
impl<Pk: Key + ToPublicKey> Bare<Pk> {
8584
/// Obtains the corresponding script pubkey for this descriptor.
8685
pub fn script_pubkey(&self) -> Script {
8786
self.ms.encode()
@@ -124,21 +123,21 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
124123
}
125124
}
126125

127-
impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
126+
impl<Pk: Key> fmt::Debug for Bare<Pk> {
128127
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129128
write!(f, "{:?}", self.ms)
130129
}
131130
}
132131

133-
impl<Pk: MiniscriptKey> fmt::Display for Bare<Pk> {
132+
impl<Pk: Key> fmt::Display for Bare<Pk> {
134133
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135134
let desc = format!("{}", self.ms);
136135
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
137136
write!(f, "{}#{}", &desc, &checksum)
138137
}
139138
}
140139

141-
impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
140+
impl<Pk: Key> Liftable<Pk> for Bare<Pk> {
142141
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
143142
self.ms.lift()
144143
}
@@ -163,7 +162,7 @@ impl_from_str!(
163162
}
164163
);
165164

166-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
165+
impl<Pk: Key> ForEachKey<Pk> for Bare<Pk> {
167166
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
168167
where
169168
Pk: 'a,
@@ -175,8 +174,8 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
175174

176175
impl<P, Q> TranslatePk<P, Q> for Bare<P>
177176
where
178-
P: MiniscriptKey,
179-
Q: MiniscriptKey,
177+
P: Key,
178+
Q: Key,
180179
{
181180
type Output = Bare<Q>;
182181

@@ -190,12 +189,12 @@ where
190189

191190
/// A bare PkH descriptor at top level
192191
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
193-
pub struct Pkh<Pk: MiniscriptKey> {
192+
pub struct Pkh<Pk: Key> {
194193
/// underlying publickey
195194
pk: Pk,
196195
}
197196

198-
impl<Pk: MiniscriptKey> Pkh<Pk> {
197+
impl<Pk: Key> Pkh<Pk> {
199198
/// Create a new Pkh descriptor
200199
pub fn new(pk: Pk) -> Self {
201200
// do the top-level checks
@@ -223,7 +222,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
223222
}
224223
}
225224

226-
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
225+
impl<Pk: Key + ToPublicKey> Pkh<Pk> {
227226
/// Obtains the corresponding script pubkey for this descriptor.
228227
pub fn script_pubkey(&self) -> Script {
229228
// Fine to hard code the `Network` here because we immediately call
@@ -278,21 +277,21 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
278277
}
279278
}
280279

281-
impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {
280+
impl<Pk: Key> fmt::Debug for Pkh<Pk> {
282281
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
283282
write!(f, "pkh({:?})", self.pk)
284283
}
285284
}
286285

287-
impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
286+
impl<Pk: Key> fmt::Display for Pkh<Pk> {
288287
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
289288
let desc = format!("pkh({})", self.pk);
290289
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
291290
write!(f, "{}#{}", &desc, &checksum)
292291
}
293292
}
294293

295-
impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
294+
impl<Pk: Key> Liftable<Pk> for Pkh<Pk> {
296295
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
297296
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
298297
}
@@ -325,7 +324,7 @@ impl_from_str!(
325324
}
326325
);
327326

328-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
327+
impl<Pk: Key> ForEachKey<Pk> for Pkh<Pk> {
329328
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
330329
where
331330
Pk: 'a,
@@ -337,8 +336,8 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
337336

338337
impl<P, Q> TranslatePk<P, Q> for Pkh<P>
339338
where
340-
P: MiniscriptKey,
341-
Q: MiniscriptKey,
339+
P: Key,
340+
Q: Key,
342341
{
343342
type Output = Pkh<Q>;
344343

src/descriptor/key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bitcoin::util::bip32;
1010
use bitcoin::{self, XOnlyPublicKey, XpubIdentifier};
1111

1212
use crate::prelude::*;
13-
use crate::{hash256, MiniscriptKey, ToPublicKey};
13+
use crate::{hash256, Key, ToPublicKey};
1414

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

732-
impl MiniscriptKey for DescriptorPublicKey {
732+
impl Key for DescriptorPublicKey {
733733
// This allows us to be able to derive public keys even for PkH s
734734
type RawPkHash = Self;
735735
type Sha256 = sha256::Hash;
@@ -817,7 +817,7 @@ impl fmt::Display for DefiniteDescriptorKey {
817817
}
818818
}
819819

820-
impl MiniscriptKey for DefiniteDescriptorKey {
820+
impl Key for DefiniteDescriptorKey {
821821
// This allows us to be able to derive public keys even for PkH s
822822
type RawPkHash = Self;
823823
type Sha256 = sha256::Hash;

src/descriptor/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use self::checksum::verify_checksum;
3737
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
3838
use crate::prelude::*;
3939
use crate::{
40-
expression, hash256, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator,
41-
Satisfier, ToPublicKey, TranslatePk, Translator,
40+
expression, hash256, miniscript, BareCtx, Error, ForEachKey, Key, PkTranslator, Satisfier,
41+
ToPublicKey, TranslatePk, Translator,
4242
};
4343

4444
mod bare;
@@ -72,7 +72,7 @@ pub type KeyMap = HashMap<DescriptorPublicKey, DescriptorSecretKey>;
7272

7373
/// Script descriptor
7474
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
75-
pub enum Descriptor<Pk: MiniscriptKey> {
75+
pub enum Descriptor<Pk: Key> {
7676
/// A raw scriptpubkey (including pay-to-pubkey) under Legacy context
7777
Bare(Bare<Pk>),
7878
/// Pay-to-PubKey-Hash
@@ -87,42 +87,42 @@ pub enum Descriptor<Pk: MiniscriptKey> {
8787
Tr(Tr<Pk>),
8888
}
8989

90-
impl<Pk: MiniscriptKey> From<Bare<Pk>> for Descriptor<Pk> {
90+
impl<Pk: Key> From<Bare<Pk>> for Descriptor<Pk> {
9191
#[inline]
9292
fn from(inner: Bare<Pk>) -> Self {
9393
Descriptor::Bare(inner)
9494
}
9595
}
9696

97-
impl<Pk: MiniscriptKey> From<Pkh<Pk>> for Descriptor<Pk> {
97+
impl<Pk: Key> From<Pkh<Pk>> for Descriptor<Pk> {
9898
#[inline]
9999
fn from(inner: Pkh<Pk>) -> Self {
100100
Descriptor::Pkh(inner)
101101
}
102102
}
103103

104-
impl<Pk: MiniscriptKey> From<Wpkh<Pk>> for Descriptor<Pk> {
104+
impl<Pk: Key> From<Wpkh<Pk>> for Descriptor<Pk> {
105105
#[inline]
106106
fn from(inner: Wpkh<Pk>) -> Self {
107107
Descriptor::Wpkh(inner)
108108
}
109109
}
110110

111-
impl<Pk: MiniscriptKey> From<Sh<Pk>> for Descriptor<Pk> {
111+
impl<Pk: Key> From<Sh<Pk>> for Descriptor<Pk> {
112112
#[inline]
113113
fn from(inner: Sh<Pk>) -> Self {
114114
Descriptor::Sh(inner)
115115
}
116116
}
117117

118-
impl<Pk: MiniscriptKey> From<Wsh<Pk>> for Descriptor<Pk> {
118+
impl<Pk: Key> From<Wsh<Pk>> for Descriptor<Pk> {
119119
#[inline]
120120
fn from(inner: Wsh<Pk>) -> Self {
121121
Descriptor::Wsh(inner)
122122
}
123123
}
124124

125-
impl<Pk: MiniscriptKey> From<Tr<Pk>> for Descriptor<Pk> {
125+
impl<Pk: Key> From<Tr<Pk>> for Descriptor<Pk> {
126126
#[inline]
127127
fn from(inner: Tr<Pk>) -> Self {
128128
Descriptor::Tr(inner)
@@ -172,7 +172,7 @@ impl DescriptorType {
172172
}
173173
}
174174

175-
impl<Pk: MiniscriptKey> Descriptor<Pk> {
175+
impl<Pk: Key> Descriptor<Pk> {
176176
// Keys
177177

178178
/// Create a new pk descriptor
@@ -339,7 +339,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
339339
}
340340
}
341341

342-
impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
342+
impl<Pk: Key + ToPublicKey> Descriptor<Pk> {
343343
/// Computes the Bitcoin address of the descriptor, if one exists
344344
///
345345
/// Some descriptors like pk() don't have an address.
@@ -472,8 +472,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
472472

473473
impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
474474
where
475-
P: MiniscriptKey,
476-
Q: MiniscriptKey,
475+
P: Key,
476+
Q: Key,
477477
{
478478
type Output = Descriptor<Q>;
479479

@@ -494,7 +494,7 @@ where
494494
}
495495
}
496496

497-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
497+
impl<Pk: Key> ForEachKey<Pk> for Descriptor<Pk> {
498498
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
499499
where
500500
Pk: 'a,
@@ -821,7 +821,7 @@ impl_from_str!(
821821
}
822822
);
823823

824-
impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
824+
impl<Pk: Key> fmt::Debug for Descriptor<Pk> {
825825
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
826826
match *self {
827827
Descriptor::Bare(ref sub) => write!(f, "{:?}", sub),
@@ -834,7 +834,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
834834
}
835835
}
836836

837-
impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {
837+
impl<Pk: Key> fmt::Display for Descriptor<Pk> {
838838
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
839839
match *self {
840840
Descriptor::Bare(ref sub) => write!(f, "{}", sub),

0 commit comments

Comments
 (0)