Skip to content

Commit e5a5db6

Browse files
committed
Use more terse names for descriptor keys
We have a plethora of key structs in the `key` module. Attempt to use more terse names without loosing any clarity. In particular: 1. Variants - Replace SinglePub with Single - Replace SinglePriv with Single The variants `SinglePub`, and `SinglePriv` stutter, we know what keys they are so can use `Single` with no loss of clarity. 2. Remove `Descriptor` from the nested descriptor types, users can use `descriptor::singlePub` if needed.
1 parent 51e07db commit e5a5db6

File tree

3 files changed

+44
-52
lines changed

3 files changed

+44
-52
lines changed

integration_test/src/test_util.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ extern crate rand;
2121

2222
use self::rand::RngCore;
2323
use bitcoin::hashes::{hex::ToHex, Hash};
24-
use miniscript::{
25-
descriptor::{DescriptorSinglePub, SinglePubKey},
26-
Descriptor, DescriptorPublicKey, Miniscript, ScriptContext, TranslatePk,
27-
};
24+
use miniscript::descriptor::{SinglePub, SinglePubkey};
25+
use miniscript::{Descriptor, DescriptorPublicKey, Miniscript, ScriptContext, TranslatePk};
2826
use std::str::FromStr;
2927

3028
use bitcoin;
@@ -163,24 +161,24 @@ pub fn parse_insane_ms<Ctx: ScriptContext>(
163161
if avail {
164162
i = i + 1;
165163
if pk_str.starts_with("K") {
166-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
164+
DescriptorPublicKey::Single(SinglePub {
167165
origin: None,
168166
key: SinglePubKey::FullKey(pubdata.pks[i]),
169167
})
170168
} else if pk_str.starts_with("X") {
171-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
169+
DescriptorPublicKey::Single(SinglePub {
172170
origin: None,
173171
key: SinglePubKey::XOnly(pubdata.x_only_pks[i]),
174172
})
175173
} else {
176174
// Parse any other keys as known to allow compatibility with existing tests
177-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
175+
DescriptorPublicKey::Single(SinglePub {
178176
origin: None,
179177
key: SinglePubKey::FullKey(pubdata.pks[i]),
180178
})
181179
}
182180
} else {
183-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
181+
DescriptorPublicKey::Single(SinglePub {
184182
origin: None,
185183
key: SinglePubKey::FullKey(random_pk(59)),
186184
})
@@ -191,24 +189,24 @@ pub fn parse_insane_ms<Ctx: ScriptContext>(
191189
if avail {
192190
j = j - 1;
193191
if pk_str.starts_with("K") {
194-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
192+
DescriptorPublicKey::Single(SinglePub {
195193
origin: None,
196194
key: SinglePubKey::FullKey(pubdata.pks[j]),
197195
})
198196
} else if pk_str.starts_with("X") {
199-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
197+
DescriptorPublicKey::Single(SinglePub {
200198
origin: None,
201199
key: SinglePubKey::XOnly(pubdata.x_only_pks[j]),
202200
})
203201
} else {
204202
// Parse any other keys as known to allow compatibility with existing tests
205-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
203+
DescriptorPublicKey::Single(SinglePub {
206204
origin: None,
207205
key: SinglePubKey::FullKey(pubdata.pks[j]),
208206
})
209207
}
210208
} else {
211-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
209+
DescriptorPublicKey::Single(SinglePub {
212210
origin: None,
213211
key: SinglePubKey::FullKey(random_pk(59)),
214212
})
@@ -230,20 +228,20 @@ pub fn parse_test_desc(desc: &str, pubdata: &PubData) -> Descriptor<DescriptorPu
230228
if avail {
231229
i = i + 1;
232230
if pk_str.starts_with("K") {
233-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
231+
Ok(DescriptorPublicKey::Single(SinglePub {
234232
origin: None,
235233
key: SinglePubKey::FullKey(pubdata.pks[i]),
236234
}))
237235
} else if pk_str.starts_with("X") {
238-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
236+
Ok(DescriptorPublicKey::Single(SinglePub {
239237
origin: None,
240238
key: SinglePubKey::XOnly(pubdata.x_only_pks[i]),
241239
}))
242240
} else {
243241
panic!("Key must start with either K or X")
244242
}
245243
} else {
246-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
244+
Ok(DescriptorPublicKey::Single(SinglePub {
247245
origin: None,
248246
key: SinglePubKey::FullKey(random_pk(59)),
249247
}))
@@ -254,20 +252,20 @@ pub fn parse_test_desc(desc: &str, pubdata: &PubData) -> Descriptor<DescriptorPu
254252
if avail {
255253
j = j - 1;
256254
if pkh_str.starts_with("K") {
257-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
255+
Ok(DescriptorPublicKey::Single(SinglePub {
258256
origin: None,
259257
key: SinglePubKey::FullKey(pubdata.pks[j]),
260258
}))
261259
} else if pkh_str.starts_with("X") {
262-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
260+
Ok(DescriptorPublicKey::Single(SinglePub {
263261
origin: None,
264262
key: SinglePubKey::XOnly(pubdata.x_only_pks[j]),
265263
}))
266264
} else {
267265
panic!("Key must start with either K or X")
268266
}
269267
} else {
270-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
268+
Ok(DescriptorPublicKey::Single(SinglePub {
271269
origin: None,
272270
key: SinglePubKey::FullKey(random_pk(61)),
273271
}))

src/descriptor/key.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use {MiniscriptKey, ToPublicKey};
1616
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
1717
pub enum DescriptorPublicKey {
1818
/// Single public key.
19-
SinglePub(DescriptorSinglePub),
19+
Single(SinglePub),
2020
/// Extended public key (xpub).
2121
XPub(DescriptorXKey<bip32::ExtendedPubKey>),
2222
}
@@ -25,14 +25,14 @@ pub enum DescriptorPublicKey {
2525
#[derive(Debug)]
2626
pub enum DescriptorSecretKey {
2727
/// Single private key.
28-
SinglePriv(DescriptorSinglePriv),
28+
Single(SinglePriv),
2929
/// Extended private key (xpriv).
3030
XPrv(DescriptorXKey<bip32::ExtendedPrivKey>),
3131
}
3232

3333
/// A descriptor [`SinglePubKey`] with optional origin information.
3434
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
35-
pub struct DescriptorSinglePub {
35+
pub struct SinglePub {
3636
/// Origin information (fingerprint and derivation path).
3737
pub origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>,
3838
/// The public key.
@@ -41,7 +41,7 @@ pub struct DescriptorSinglePub {
4141

4242
/// A descriptor [`bitcoin::PrivateKey`] with optional origin information.
4343
#[derive(Debug)]
44-
pub struct DescriptorSinglePriv {
44+
pub struct SinglePriv {
4545
/// Origin information (fingerprint and derivation path).
4646
pub origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>,
4747
/// The private key.
@@ -73,7 +73,7 @@ pub enum SinglePubKey {
7373
impl fmt::Display for DescriptorSecretKey {
7474
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7575
match self {
76-
&DescriptorSecretKey::SinglePriv(ref sk) => {
76+
&DescriptorSecretKey::Single(ref sk) => {
7777
maybe_fmt_master_id(f, &sk.origin)?;
7878
sk.key.fmt(f)?;
7979
Ok(())
@@ -136,15 +136,15 @@ pub enum Wildcard {
136136
Hardened,
137137
}
138138

139-
impl DescriptorSinglePriv {
139+
impl SinglePriv {
140140
/// Returns the public key of this key
141141
fn as_public<C: Signing>(
142142
&self,
143143
secp: &Secp256k1<C>,
144-
) -> Result<DescriptorSinglePub, DescriptorKeyParseError> {
144+
) -> Result<SinglePub, DescriptorKeyParseError> {
145145
let pub_key = self.key.public_key(secp);
146146

147-
Ok(DescriptorSinglePub {
147+
Ok(SinglePub {
148148
origin: self.origin.clone(),
149149
key: SinglePubKey::FullKey(pub_key),
150150
})
@@ -218,7 +218,7 @@ impl error::Error for DescriptorKeyParseError {}
218218
impl fmt::Display for DescriptorPublicKey {
219219
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
220220
match *self {
221-
DescriptorPublicKey::SinglePub(ref pk) => {
221+
DescriptorPublicKey::Single(ref pk) => {
222222
maybe_fmt_master_id(f, &pk.origin)?;
223223
match pk.key {
224224
SinglePubKey::FullKey(full_key) => full_key.fmt(f),
@@ -243,7 +243,7 @@ impl fmt::Display for DescriptorPublicKey {
243243

244244
impl DescriptorSecretKey {
245245
/// Return the public version of this key, by applying either
246-
/// [`DescriptorSinglePriv::as_public`] or [`DescriptorXKey<bip32::ExtendedPrivKey>::as_public`]
246+
/// [`SinglePriv::as_public`] or [`DescriptorXKey<bip32::ExtendedPrivKey>::as_public`]
247247
/// depending on the type of key.
248248
///
249249
/// If the key is an "XPrv", the hardened derivation steps will be applied before converting it
@@ -254,8 +254,8 @@ impl DescriptorSecretKey {
254254
secp: &Secp256k1<C>,
255255
) -> Result<DescriptorPublicKey, DescriptorKeyParseError> {
256256
Ok(match self {
257-
&DescriptorSecretKey::SinglePriv(ref sk) => {
258-
DescriptorPublicKey::SinglePub(sk.as_public(secp)?)
257+
&DescriptorSecretKey::Single(ref sk) => {
258+
DescriptorPublicKey::Single(sk.as_public(secp)?)
259259
}
260260
&DescriptorSecretKey::XPrv(ref xprv) => {
261261
DescriptorPublicKey::XPub(xprv.as_public(secp)?)
@@ -340,10 +340,7 @@ impl FromStr for DescriptorPublicKey {
340340
))
341341
}
342342
};
343-
Ok(DescriptorPublicKey::SinglePub(DescriptorSinglePub {
344-
key,
345-
origin,
346-
}))
343+
Ok(DescriptorPublicKey::Single(SinglePub { key, origin }))
347344
}
348345
}
349346
}
@@ -384,7 +381,7 @@ impl DescriptorPublicKey {
384381
xpub.xkey.fingerprint()
385382
}
386383
}
387-
DescriptorPublicKey::SinglePub(ref single) => {
384+
DescriptorPublicKey::Single(ref single) => {
388385
if let Some((fingerprint, _)) = single.origin {
389386
fingerprint
390387
} else {
@@ -416,7 +413,7 @@ impl DescriptorPublicKey {
416413
};
417414
origin_path.extend(&xpub.derivation_path)
418415
}
419-
DescriptorPublicKey::SinglePub(ref single) => {
416+
DescriptorPublicKey::Single(ref single) => {
420417
if let Some((_, ref path)) = single.origin {
421418
path.clone()
422419
} else {
@@ -429,7 +426,7 @@ impl DescriptorPublicKey {
429426
/// Whether or not the key has a wildcards
430427
pub fn is_deriveable(&self) -> bool {
431428
match *self {
432-
DescriptorPublicKey::SinglePub(..) => false,
429+
DescriptorPublicKey::Single(..) => false,
433430
DescriptorPublicKey::XPub(ref xpub) => xpub.wildcard != Wildcard::None,
434431
}
435432
}
@@ -475,7 +472,7 @@ impl DescriptorPublicKey {
475472
secp: &Secp256k1<C>,
476473
) -> Result<bitcoin::PublicKey, ConversionError> {
477474
match *self {
478-
DescriptorPublicKey::SinglePub(ref pk) => match pk.key {
475+
DescriptorPublicKey::Single(ref pk) => match pk.key {
479476
SinglePubKey::FullKey(pk) => Ok(pk),
480477
SinglePubKey::XOnly(xpk) => Ok(xpk.to_public_key()),
481478
},
@@ -503,7 +500,7 @@ impl FromStr for DescriptorSecretKey {
503500
if key_part.len() <= 52 {
504501
let sk = bitcoin::PrivateKey::from_str(key_part)
505502
.map_err(|_| DescriptorKeyParseError("Error while parsing a WIF private key"))?;
506-
Ok(DescriptorSecretKey::SinglePriv(DescriptorSinglePriv {
503+
Ok(DescriptorSecretKey::Single(SinglePriv {
507504
key: sk,
508505
origin: None,
509506
}))
@@ -693,7 +690,7 @@ impl MiniscriptKey for DescriptorPublicKey {
693690

694691
fn is_uncompressed(&self) -> bool {
695692
match self {
696-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
693+
DescriptorPublicKey::Single(SinglePub {
697694
key: SinglePubKey::FullKey(ref key),
698695
..
699696
}) => key.is_uncompressed(),
@@ -703,7 +700,7 @@ impl MiniscriptKey for DescriptorPublicKey {
703700

704701
fn is_x_only_key(&self) -> bool {
705702
match self {
706-
DescriptorPublicKey::SinglePub(DescriptorSinglePub {
703+
DescriptorPublicKey::Single(SinglePub {
707704
key: SinglePubKey::XOnly(ref _key),
708705
..
709706
}) => true,

src/descriptor/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod key;
6262

6363
pub use self::key::{
6464
ConversionError, DescriptorKeyParseError, DescriptorPublicKey, DescriptorSecretKey,
65-
DescriptorSinglePriv, DescriptorSinglePub, DescriptorXKey, InnerXKey, SinglePubKey, Wildcard,
65+
DescriptorXKey, InnerXKey, SinglePriv, SinglePub, SinglePubKey, Wildcard,
6666
};
6767

6868
/// Alias type for a map of public key to secret key
@@ -835,9 +835,7 @@ mod tests {
835835
use bitcoin::util::bip32;
836836
use bitcoin::{self, secp256k1, EcdsaSighashType, PublicKey};
837837
use descriptor::key::Wildcard;
838-
use descriptor::{
839-
DescriptorPublicKey, DescriptorSecretKey, DescriptorSinglePub, DescriptorXKey,
840-
};
838+
use descriptor::{DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey, SinglePub};
841839
use hex_script;
842840
use std::cmp;
843841
use std::collections::HashMap;
@@ -854,10 +852,9 @@ mod tests {
854852
impl cmp::PartialEq for DescriptorSecretKey {
855853
fn eq(&self, other: &Self) -> bool {
856854
match (self, other) {
857-
(
858-
&DescriptorSecretKey::SinglePriv(ref a),
859-
&DescriptorSecretKey::SinglePriv(ref b),
860-
) => a.origin == b.origin && a.key == b.key,
855+
(&DescriptorSecretKey::Single(ref a), &DescriptorSecretKey::Single(ref b)) => {
856+
a.origin == b.origin && a.key == b.key
857+
}
861858
(&DescriptorSecretKey::XPrv(ref a), &DescriptorSecretKey::XPrv(ref b)) => {
862859
a.origin == b.origin
863860
&& a.xkey == b.xkey
@@ -1531,7 +1528,7 @@ mod tests {
15311528

15321529
// Raw (compressed) pubkey
15331530
let key = "03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8";
1534-
let expected = DescriptorPublicKey::SinglePub(DescriptorSinglePub {
1531+
let expected = DescriptorPublicKey::Single(SinglePub {
15351532
key: SinglePubKey::FullKey(
15361533
bitcoin::PublicKey::from_str(
15371534
"03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8",
@@ -1545,7 +1542,7 @@ mod tests {
15451542

15461543
// Raw (uncompressed) pubkey
15471544
let key = "04f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446a";
1548-
let expected = DescriptorPublicKey::SinglePub(DescriptorSinglePub {
1545+
let expected = DescriptorPublicKey::Single(SinglePub {
15491546
key: SinglePubKey::FullKey(bitcoin::PublicKey::from_str(
15501547
"04f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446a",
15511548
)
@@ -1558,7 +1555,7 @@ mod tests {
15581555
// Raw pubkey with origin
15591556
let desc =
15601557
"[78412e3a/0'/42/0']0231c7d3fc85c148717848033ce276ae2b464a4e2c367ed33886cc428b8af48ff8";
1561-
let expected = DescriptorPublicKey::SinglePub(DescriptorSinglePub {
1558+
let expected = DescriptorPublicKey::Single(SinglePub {
15621559
key: SinglePubKey::FullKey(
15631560
bitcoin::PublicKey::from_str(
15641561
"0231c7d3fc85c148717848033ce276ae2b464a4e2c367ed33886cc428b8af48ff8",

0 commit comments

Comments
 (0)