Skip to content

Commit b7d4536

Browse files
committed
descriptor: rename PkTranslate to TranslatePk; re-export at the root
1 parent 7767f12 commit b7d4536

File tree

8 files changed

+39
-48
lines changed

8 files changed

+39
-48
lines changed

examples/xpub_descriptors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
extern crate miniscript;
1818

1919
use miniscript::bitcoin::{self, secp256k1};
20-
use miniscript::descriptor::PkTranslate2;
21-
use miniscript::{Descriptor, DescriptorPublicKey, DescriptorTrait};
20+
use miniscript::{Descriptor, DescriptorPublicKey, DescriptorTrait, TranslatePk2};
2221

2322
use std::str::FromStr;
2423
fn main() {

src/descriptor/bare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use {BareCtx, Error, Miniscript, MiniscriptKey, Satisfier, ToPublicKey};
3030

3131
use super::{
3232
checksum::{desc_checksum, verify_checksum},
33-
DescriptorTrait, PkTranslate,
33+
DescriptorTrait, TranslatePk,
3434
};
3535

3636
/// Create a Bare Descriptor. That is descriptor that is
@@ -163,7 +163,7 @@ where
163163
}
164164
}
165165

166-
impl<P: MiniscriptKey, Q: MiniscriptKey> PkTranslate<P, Q> for Bare<P> {
166+
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Bare<P> {
167167
type Output = Bare<Q>;
168168

169169
fn translate_pk<Fpk, Fpkh, E>(
@@ -327,7 +327,7 @@ where
327327
}
328328
}
329329

330-
impl<P: MiniscriptKey, Q: MiniscriptKey> PkTranslate<P, Q> for Pkh<P> {
330+
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Pkh<P> {
331331
type Output = Pkh<Q>;
332332

333333
fn translate_pk<Fpk, Fpkh, E>(

src/descriptor/mod.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
157157
/// This will panic if translatefpk returns an uncompressed key when
158158
/// converting to a Segwit descriptor. To prevent this panic, ensure
159159
/// translatefpk returns an error in this case instead.
160-
pub trait PkTranslate<P: MiniscriptKey, Q: MiniscriptKey> {
160+
pub trait TranslatePk<P: MiniscriptKey, Q: MiniscriptKey> {
161161
/// The associated output type. This must be Self<Q>
162162
type Output;
163163

@@ -188,17 +188,17 @@ pub trait PkTranslate<P: MiniscriptKey, Q: MiniscriptKey> {
188188
}
189189
}
190190

191-
/// Variant of `PkTranslate` where P and Q both have the same hash
191+
/// Variant of `TranslatePk` where P and Q both have the same hash
192192
/// type, and the hashes can be converted by just cloning them
193-
pub trait PkTranslate1<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>>:
194-
PkTranslate<P, Q>
193+
pub trait TranslatePk1<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>>:
194+
TranslatePk<P, Q>
195195
{
196196
/// Translate a struct from one generic to another where the
197197
/// translation for Pk is provided by translatefpk
198198
fn translate_pk1<Fpk, E>(
199199
&self,
200200
translatefpk: Fpk,
201-
) -> Result<<Self as PkTranslate<P, Q>>::Output, E>
201+
) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
202202
where
203203
Fpk: FnMut(&P) -> Result<Q, E>,
204204
{
@@ -210,24 +210,24 @@ pub trait PkTranslate1<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>>:
210210
fn translate_pk1_infallible<Fpk: FnMut(&P) -> Q>(
211211
&self,
212212
translatefpk: Fpk,
213-
) -> <Self as PkTranslate<P, Q>>::Output {
213+
) -> <Self as TranslatePk<P, Q>>::Output {
214214
self.translate_pk_infallible(translatefpk, P::Hash::clone)
215215
}
216216
}
217-
impl<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>, T: PkTranslate<P, Q>> PkTranslate1<P, Q>
217+
impl<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>, T: TranslatePk<P, Q>> TranslatePk1<P, Q>
218218
for T
219219
{
220220
}
221221

222-
/// Variant of `PkTranslate` where P's hash is P, so the hashes
222+
/// Variant of `TranslatePk` where P's hash is P, so the hashes
223223
/// can be converted by reusing the key-conversion function
224-
pub trait PkTranslate2<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey>: PkTranslate<P, Q> {
224+
pub trait TranslatePk2<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey>: TranslatePk<P, Q> {
225225
/// Translate a struct from one generic to another where the
226226
/// translation for Pk is provided by translatefpk
227227
fn translate_pk2<Fpk: Fn(&P) -> Result<Q, E>, E>(
228228
&self,
229229
translatefpk: Fpk,
230-
) -> Result<<Self as PkTranslate<P, Q>>::Output, E> {
230+
) -> Result<<Self as TranslatePk<P, Q>>::Output, E> {
231231
self.translate_pk(&translatefpk, |h| {
232232
translatefpk(h).map(|q| q.to_pubkeyhash())
233233
})
@@ -238,23 +238,23 @@ pub trait PkTranslate2<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey>: PkTranslat
238238
fn translate_pk2_infallible<Fpk: Fn(&P) -> Q>(
239239
&self,
240240
translatefpk: Fpk,
241-
) -> <Self as PkTranslate<P, Q>>::Output {
241+
) -> <Self as TranslatePk<P, Q>>::Output {
242242
self.translate_pk_infallible(&translatefpk, |h| translatefpk(h).to_pubkeyhash())
243243
}
244244
}
245-
impl<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey, T: PkTranslate<P, Q>> PkTranslate2<P, Q> for T {}
245+
impl<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey, T: TranslatePk<P, Q>> TranslatePk2<P, Q> for T {}
246246

247-
/// Variant of `PkTranslate` where Q's hash is `hash160` so we can
247+
/// Variant of `TranslatePk` where Q's hash is `hash160` so we can
248248
/// derive hashes by calling `hash_to_hash160`
249-
pub trait PkTranslate3<P: MiniscriptKey + ToPublicKey, Q: MiniscriptKey<Hash = hash160::Hash>>:
250-
PkTranslate<P, Q>
249+
pub trait TranslatePk3<P: MiniscriptKey + ToPublicKey, Q: MiniscriptKey<Hash = hash160::Hash>>:
250+
TranslatePk<P, Q>
251251
{
252252
/// Translate a struct from one generic to another where the
253253
/// translation for Pk is provided by translatefpk
254254
fn translate_pk3<Fpk, E>(
255255
&self,
256256
translatefpk: Fpk,
257-
) -> Result<<Self as PkTranslate<P, Q>>::Output, E>
257+
) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
258258
where
259259
Fpk: FnMut(&P) -> Result<Q, E>,
260260
{
@@ -266,19 +266,19 @@ pub trait PkTranslate3<P: MiniscriptKey + ToPublicKey, Q: MiniscriptKey<Hash = h
266266
fn translate_pk3_infallible<Fpk: FnMut(&P) -> Q>(
267267
&self,
268268
translatefpk: Fpk,
269-
) -> <Self as PkTranslate<P, Q>>::Output {
269+
) -> <Self as TranslatePk<P, Q>>::Output {
270270
self.translate_pk_infallible(translatefpk, P::hash_to_hash160)
271271
}
272272
}
273273
impl<
274274
P: MiniscriptKey + ToPublicKey,
275275
Q: MiniscriptKey<Hash = hash160::Hash>,
276-
T: PkTranslate<P, Q>,
277-
> PkTranslate3<P, Q> for T
276+
T: TranslatePk<P, Q>,
277+
> TranslatePk3<P, Q> for T
278278
{
279279
}
280280

281-
// There are some additional convenience functions we could add to `PkTranslate`
281+
// There are some additional convenience functions we could add to `TranslatePk`
282282
// for the common special cases where `P::Hash == Q::Hash`, or when `P == P::Hash`,
283283
// but these are blocked on https://github.com/rust-lang/rust/issues/20041 which
284284
// is unlikely to arrive in Rust very soon, as of Jan 2021. Should revisit this.
@@ -383,7 +383,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
383383
}
384384
}
385385

386-
impl<P: MiniscriptKey, Q: MiniscriptKey> PkTranslate<P, Q> for Descriptor<P> {
386+
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
387387
type Output = Descriptor<Q>;
388388
/// Convert a descriptor using abstract keys to one using specific keys
389389
/// This will panic if translatefpk returns an uncompressed key when
@@ -683,7 +683,7 @@ serde_string_impl_pk!(Descriptor, "a script descriptor");
683683
#[cfg(test)]
684684
mod tests {
685685
use super::checksum::desc_checksum;
686-
use super::{DescriptorTrait, PkTranslate2};
686+
use super::{DescriptorTrait, TranslatePk2};
687687
use bitcoin::blockdata::opcodes::all::{OP_CLTV, OP_CSV};
688688
use bitcoin::blockdata::script::Instruction;
689689
use bitcoin::blockdata::{opcodes, script};

src/descriptor/segwitv0.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use {Error, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey};
2828

2929
use super::{
3030
checksum::{desc_checksum, verify_checksum},
31-
DescriptorTrait, PkTranslate, SortedMultiVec,
31+
DescriptorTrait, SortedMultiVec, TranslatePk,
3232
};
3333
/// A Segwitv0 wsh descriptor
3434
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
@@ -232,7 +232,7 @@ where
232232
}
233233
}
234234

235-
impl<P: MiniscriptKey, Q: MiniscriptKey> PkTranslate<P, Q> for Wsh<P> {
235+
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Wsh<P> {
236236
type Output = Wsh<Q>;
237237

238238
fn translate_pk<Fpk, Fpkh, E>(
@@ -411,7 +411,7 @@ where
411411
}
412412
}
413413

414-
impl<P: MiniscriptKey, Q: MiniscriptKey> PkTranslate<P, Q> for Wpkh<P> {
414+
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Wpkh<P> {
415415
type Output = Wpkh<Q>;
416416

417417
fn translate_pk<Fpk, Fpkh, E>(

src/descriptor/sh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use {Error, Legacy, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey}
3131

3232
use super::{
3333
checksum::{desc_checksum, verify_checksum},
34-
DescriptorTrait, PkTranslate, SortedMultiVec, Wpkh, Wsh,
34+
DescriptorTrait, SortedMultiVec, TranslatePk, Wpkh, Wsh,
3535
};
3636

3737
/// A Legacy p2sh Descriptor
@@ -322,7 +322,7 @@ where
322322
}
323323
}
324324

325-
impl<P: MiniscriptKey, Q: MiniscriptKey> PkTranslate<P, Q> for Sh<P> {
325+
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Sh<P> {
326326
type Output = Sh<Q>;
327327

328328
fn translate_pk<Fpk, Fpkh, E>(

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ use bitcoin::blockdata::{opcodes, script};
129129
use bitcoin::hashes::{hash160, sha256, Hash};
130130

131131
pub use descriptor::{Descriptor, DescriptorPublicKey, DescriptorTrait};
132+
pub use descriptor::{TranslatePk, TranslatePk1, TranslatePk2, TranslatePk3};
132133
pub use interpreter::Interpreter;
133134
pub use miniscript::context::{BareCtx, Legacy, ScriptContext, Segwitv0};
134135
pub use miniscript::decode::Terminal;

src/miniscript/astelem.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,20 @@
1919
//! encoding in Bitcoin script, as well as a datatype. Full details
2020
//! are given on the Miniscript website.
2121
22+
use std::str::FromStr;
23+
use std::sync::Arc;
2224
use std::{fmt, str};
2325

2426
use bitcoin::blockdata::{opcodes, script};
2527
use bitcoin::hashes::hex::FromHex;
2628
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
2729

28-
use descriptor::PkTranslate;
2930
use errstr;
3031
use expression;
3132
use miniscript::types::{self, Property};
3233
use miniscript::ScriptContext;
3334
use script_num_size;
34-
use std::sync::Arc;
35-
use str::FromStr;
36-
use Error;
37-
use Miniscript;
38-
use MiniscriptKey;
39-
use Terminal;
40-
use ToPublicKey;
35+
use {Error, Miniscript, MiniscriptKey, Terminal, ToPublicKey, TranslatePk};
4136

4237
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
4338
/// Internal helper function for displaying wrapper types; returns
@@ -60,7 +55,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
6055
}
6156
}
6257

63-
impl<Pk: MiniscriptKey, Q: MiniscriptKey, Ctx: ScriptContext> PkTranslate<Pk, Q>
58+
impl<Pk: MiniscriptKey, Q: MiniscriptKey, Ctx: ScriptContext> TranslatePk<Pk, Q>
6459
for Terminal<Pk, Ctx>
6560
{
6661
type Output = Terminal<Q, Ctx>;

src/miniscript/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub mod types;
4444

4545
use self::lex::{lex, TokenIter};
4646
use self::types::Property;
47-
use descriptor::PkTranslate;
4847
pub use miniscript::context::ScriptContext;
4948
use miniscript::decode::Terminal;
5049
use miniscript::types::extra_props::ExtData;
@@ -53,7 +52,7 @@ use miniscript::types::Type;
5352
use std::cmp;
5453
use std::sync::Arc;
5554
use MiniscriptKey;
56-
use {expression, Error, ToPublicKey};
55+
use {expression, Error, ToPublicKey, TranslatePk};
5756

5857
/// Top-level script AST type
5958
#[derive(Clone, Hash)]
@@ -231,7 +230,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
231230
}
232231
}
233232

234-
impl<Pk: MiniscriptKey, Q: MiniscriptKey, Ctx: ScriptContext> PkTranslate<Pk, Q>
233+
impl<Pk: MiniscriptKey, Q: MiniscriptKey, Ctx: ScriptContext> TranslatePk<Pk, Q>
235234
for Miniscript<Pk, Ctx>
236235
{
237236
type Output = Miniscript<Q, Ctx>;
@@ -400,21 +399,18 @@ serde_string_impl_pk!(Miniscript, "a miniscript", Ctx; ScriptContext);
400399
mod tests {
401400
use super::Segwitv0;
402401
use super::{Miniscript, ScriptContext};
403-
use descriptor::{PkTranslate, PkTranslate1};
404402
use hex_script;
405403
use miniscript::types::{self, ExtData, Property, Type};
406404
use miniscript::Terminal;
407405
use policy::Liftable;
408406
use std::marker::PhantomData;
409-
use DummyKey;
410-
use DummyKeyHash;
407+
use {DummyKey, DummyKeyHash, MiniscriptKey, TranslatePk, TranslatePk1};
411408

412409
use bitcoin::hashes::{hash160, sha256, Hash};
413410
use bitcoin::{self, secp256k1};
414411
use std::str;
415412
use std::str::FromStr;
416413
use std::sync::Arc;
417-
use MiniscriptKey;
418414

419415
type Segwitv0Script = Miniscript<bitcoin::PublicKey, Segwitv0>;
420416

0 commit comments

Comments
 (0)