Skip to content

Commit d458312

Browse files
committed
Merge #393: Refactor implementations of TranslatePk
13c0771 Shorten translatefpk identifier (Tobin C. Harding) 0537c4d Refactor implementations of TranslatePk (Tobin C. Harding) fdc8174 Add missing whitespace (Tobin C. Harding) Pull request description: We recently refactored the `TranslatePk` traits but did not do _all_ the implementations. - Patch 1 trivial cleanup. - Patch 2 does the remaining implementations. - Patch 3 shortens other instances of the `translatefpk[h]` identifier so as to be uniform. ACKs for top commit: apoelstra: ACK 13c0771 sanket1729: ACK 13c0771 Tree-SHA512: dcce3fcc288497fed8b90736c9620a9ee31b1c357e7992789966f5837f43ee66c14727dd8a116084b1a8b511fe3675594a6433165ae4550847dc4760ca0a4768
2 parents 16a05e9 + 13c0771 commit d458312

File tree

11 files changed

+174
-204
lines changed

11 files changed

+174
-204
lines changed

src/descriptor/bare.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,21 @@ 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, Q> TranslatePk<P, Q> for Bare<P>
216+
where
217+
P: MiniscriptKey,
218+
Q: MiniscriptKey,
219+
{
216220
type Output = Bare<Q>;
217221

218-
fn translate_pk<Fpk, Fpkh, E>(
219-
&self,
220-
mut translatefpk: Fpk,
221-
mut translatefpkh: Fpkh,
222-
) -> Result<Self::Output, E>
222+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Result<Self::Output, E>
223223
where
224224
Fpk: FnMut(&P) -> Result<Q, E>,
225225
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
226226
Q: MiniscriptKey,
227227
{
228-
Ok(Bare::new(
229-
self.ms
230-
.translate_pk(&mut translatefpk, &mut translatefpkh)?,
231-
)
232-
.expect("Translation cannot fail inside Bare"))
228+
Ok(Bare::new(self.ms.translate_pk(&mut fpk, &mut fpkh)?)
229+
.expect("Translation cannot fail inside Bare"))
233230
}
234231
}
235232

@@ -424,19 +421,18 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
424421
}
425422
}
426423

427-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Pkh<P> {
424+
impl<P, Q> TranslatePk<P, Q> for Pkh<P>
425+
where
426+
P: MiniscriptKey,
427+
Q: MiniscriptKey,
428+
{
428429
type Output = Pkh<Q>;
429430

430-
fn translate_pk<Fpk, Fpkh, E>(
431-
&self,
432-
mut translatefpk: Fpk,
433-
_translatefpkh: Fpkh,
434-
) -> Result<Self::Output, E>
431+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, _fpkh: Fpkh) -> Result<Self::Output, E>
435432
where
436433
Fpk: FnMut(&P) -> Result<Q, E>,
437434
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
438-
Q: MiniscriptKey,
439435
{
440-
Ok(Pkh::new(translatefpk(&self.pk)?))
436+
Ok(Pkh::new(fpk(&self.pk)?))
441437
}
442438
}

src/descriptor/mod.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -440,41 +440,31 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
440440
}
441441
}
442442

443-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
443+
impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
444+
where
445+
P: MiniscriptKey,
446+
Q: MiniscriptKey,
447+
{
444448
type Output = Descriptor<Q>;
445-
/// Convert a descriptor using abstract keys to one using specific keys
446-
/// This will panic if translatefpk returns an uncompressed key when
447-
/// converting to a Segwit descriptor. To prevent this panic, ensure
448-
/// translatefpk returns an error in this case instead.
449-
fn translate_pk<Fpk, Fpkh, E>(
450-
&self,
451-
mut translatefpk: Fpk,
452-
mut translatefpkh: Fpkh,
453-
) -> Result<Descriptor<Q>, E>
449+
/// Converts a descriptor using abstract keys to one using specific keys.
450+
///
451+
/// # Panics
452+
///
453+
/// If `fpk` returns an uncompressed key when converting to a Segwit descriptor.
454+
/// To prevent this panic, ensure `fpk` returns an error in this case instead.
455+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Result<Descriptor<Q>, E>
454456
where
455457
Fpk: FnMut(&P) -> Result<Q, E>,
456458
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
457459
Q: MiniscriptKey,
458460
{
459461
let desc = match *self {
460-
Descriptor::Bare(ref bare) => {
461-
Descriptor::Bare(bare.translate_pk(&mut translatefpk, &mut translatefpkh)?)
462-
}
463-
Descriptor::Pkh(ref pk) => {
464-
Descriptor::Pkh(pk.translate_pk(&mut translatefpk, &mut translatefpkh)?)
465-
}
466-
Descriptor::Wpkh(ref pk) => {
467-
Descriptor::Wpkh(pk.translate_pk(&mut translatefpk, &mut translatefpkh)?)
468-
}
469-
Descriptor::Sh(ref sh) => {
470-
Descriptor::Sh(sh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
471-
}
472-
Descriptor::Wsh(ref wsh) => {
473-
Descriptor::Wsh(wsh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
474-
}
475-
Descriptor::Tr(ref tr) => {
476-
Descriptor::Tr(tr.translate_pk(&mut translatefpk, &mut translatefpkh)?)
477-
}
462+
Descriptor::Bare(ref bare) => Descriptor::Bare(bare.translate_pk(&mut fpk, &mut fpkh)?),
463+
Descriptor::Pkh(ref pk) => Descriptor::Pkh(pk.translate_pk(&mut fpk, &mut fpkh)?),
464+
Descriptor::Wpkh(ref pk) => Descriptor::Wpkh(pk.translate_pk(&mut fpk, &mut fpkh)?),
465+
Descriptor::Sh(ref sh) => Descriptor::Sh(sh.translate_pk(&mut fpk, &mut fpkh)?),
466+
Descriptor::Wsh(ref wsh) => Descriptor::Wsh(wsh.translate_pk(&mut fpk, &mut fpkh)?),
467+
Descriptor::Tr(ref tr) => Descriptor::Tr(tr.translate_pk(&mut fpk, &mut fpkh)?),
478468
};
479469
Ok(desc)
480470
}

src/descriptor/segwitv0.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -297,26 +297,21 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
297297
}
298298
}
299299

300-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Wsh<P> {
300+
impl<P, Q> TranslatePk<P, Q> for Wsh<P>
301+
where
302+
P: MiniscriptKey,
303+
Q: MiniscriptKey,
304+
{
301305
type Output = Wsh<Q>;
302306

303-
fn translate_pk<Fpk, Fpkh, E>(
304-
&self,
305-
mut translatefpk: Fpk,
306-
mut translatefpkh: Fpkh,
307-
) -> Result<Self::Output, E>
307+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Result<Self::Output, E>
308308
where
309309
Fpk: FnMut(&P) -> Result<Q, E>,
310310
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
311-
Q: MiniscriptKey,
312311
{
313312
let inner = match self.inner {
314-
WshInner::SortedMulti(ref smv) => {
315-
WshInner::SortedMulti(smv.translate_pk(&mut translatefpk)?)
316-
}
317-
WshInner::Ms(ref ms) => {
318-
WshInner::Ms(ms.translate_pk(&mut translatefpk, &mut translatefpkh)?)
319-
}
313+
WshInner::SortedMulti(ref smv) => WshInner::SortedMulti(smv.translate_pk(&mut fpk)?),
314+
WshInner::Ms(ref ms) => WshInner::Ms(ms.translate_pk(&mut fpk, &mut fpkh)?),
320315
};
321316
Ok(Wsh { inner: inner })
322317
}
@@ -534,19 +529,18 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
534529
}
535530
}
536531

537-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Wpkh<P> {
532+
impl<P, Q> TranslatePk<P, Q> for Wpkh<P>
533+
where
534+
P: MiniscriptKey,
535+
Q: MiniscriptKey,
536+
{
538537
type Output = Wpkh<Q>;
539538

540-
fn translate_pk<Fpk, Fpkh, E>(
541-
&self,
542-
mut translatefpk: Fpk,
543-
_translatefpkh: Fpkh,
544-
) -> Result<Self::Output, E>
539+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, _fpkh: Fpkh) -> Result<Self::Output, E>
545540
where
546541
Fpk: FnMut(&P) -> Result<Q, E>,
547542
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
548-
Q: MiniscriptKey,
549543
{
550-
Ok(Wpkh::new(translatefpk(&self.pk)?).expect("Uncompressed keys in Wpkh"))
544+
Ok(Wpkh::new(fpk(&self.pk)?).expect("Uncompressed keys in Wpkh"))
551545
}
552546
}

src/descriptor/sh.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -421,32 +421,23 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {
421421
}
422422
}
423423

424-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Sh<P> {
424+
impl<P, Q> TranslatePk<P, Q> for Sh<P>
425+
where
426+
P: MiniscriptKey,
427+
Q: MiniscriptKey,
428+
{
425429
type Output = Sh<Q>;
426430

427-
fn translate_pk<Fpk, Fpkh, E>(
428-
&self,
429-
mut translatefpk: Fpk,
430-
mut translatefpkh: Fpkh,
431-
) -> Result<Self::Output, E>
431+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Result<Self::Output, E>
432432
where
433433
Fpk: FnMut(&P) -> Result<Q, E>,
434434
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
435-
Q: MiniscriptKey,
436435
{
437436
let inner = match self.inner {
438-
ShInner::Wsh(ref wsh) => {
439-
ShInner::Wsh(wsh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
440-
}
441-
ShInner::Wpkh(ref wpkh) => {
442-
ShInner::Wpkh(wpkh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
443-
}
444-
ShInner::SortedMulti(ref smv) => {
445-
ShInner::SortedMulti(smv.translate_pk(&mut translatefpk)?)
446-
}
447-
ShInner::Ms(ref ms) => {
448-
ShInner::Ms(ms.translate_pk(&mut translatefpk, &mut translatefpkh)?)
449-
}
437+
ShInner::Wsh(ref wsh) => ShInner::Wsh(wsh.translate_pk(&mut fpk, &mut fpkh)?),
438+
ShInner::Wpkh(ref wpkh) => ShInner::Wpkh(wpkh.translate_pk(&mut fpk, &mut fpkh)?),
439+
ShInner::SortedMulti(ref smv) => ShInner::SortedMulti(smv.translate_pk(&mut fpk)?),
440+
ShInner::Ms(ref ms) => ShInner::Ms(ms.translate_pk(&mut fpk, &mut fpkh)?),
450441
};
451442
Ok(Sh { inner: inner })
452443
}

src/descriptor/sortedmulti.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
9090
pks.map(|pks| SortedMultiVec::new(k as usize, pks))?
9191
}
9292

93-
/// This will panic if translatefpk returns an uncompressed key when
93+
/// This will panic if fpk returns an uncompressed key when
9494
/// converting to a Segwit descriptor. To prevent this panic, ensure
95-
/// translatefpk returns an error in this case instead.
95+
/// fpk returns an error in this case instead.
9696
pub fn translate_pk<FPk, Q, FuncError>(
9797
&self,
98-
translatefpk: &mut FPk,
98+
fpk: &mut FPk,
9999
) -> Result<SortedMultiVec<Q, Ctx>, FuncError>
100100
where
101101
FPk: FnMut(&Pk) -> Result<Q, FuncError>,
102102
Q: MiniscriptKey,
103103
{
104-
let pks: Result<Vec<Q>, _> = self.pks.iter().map(&mut *translatefpk).collect();
104+
let pks: Result<Vec<Q>, _> = self.pks.iter().map(&mut *fpk).collect();
105105
Ok(SortedMultiVec {
106106
k: self.k,
107107
pks: pks?,

src/descriptor/tr.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ impl<Pk: MiniscriptKey> TapTree<Pk> {
129129
// Helper function to translate keys
130130
fn translate_helper<FPk, FPkh, Q, Error>(
131131
&self,
132-
translatefpk: &mut FPk,
133-
translatefpkh: &mut FPkh,
132+
fpk: &mut FPk,
133+
fpkh: &mut FPkh,
134134
) -> Result<TapTree<Q>, Error>
135135
where
136136
FPk: FnMut(&Pk) -> Result<Q, Error>,
@@ -139,12 +139,10 @@ impl<Pk: MiniscriptKey> TapTree<Pk> {
139139
{
140140
let frag = match self {
141141
TapTree::Tree(l, r) => TapTree::Tree(
142-
Arc::new(l.translate_helper(translatefpk, translatefpkh)?),
143-
Arc::new(r.translate_helper(translatefpk, translatefpkh)?),
142+
Arc::new(l.translate_helper(fpk, fpkh)?),
143+
Arc::new(r.translate_helper(fpk, fpkh)?),
144144
),
145-
TapTree::Leaf(ms) => {
146-
TapTree::Leaf(Arc::new(ms.translate_pk(translatefpk, translatefpkh)?))
147-
}
145+
TapTree::Leaf(ms) => TapTree::Leaf(Arc::new(ms.translate_pk(fpk, fpkh)?)),
148146
};
149147
Ok(frag)
150148
}
@@ -652,23 +650,22 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
652650
}
653651
}
654652

655-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Tr<P> {
653+
impl<P, Q> TranslatePk<P, Q> for Tr<P>
654+
where
655+
P: MiniscriptKey,
656+
Q: MiniscriptKey,
657+
{
656658
type Output = Tr<Q>;
657659

658-
fn translate_pk<Fpk, Fpkh, E>(
659-
&self,
660-
mut translatefpk: Fpk,
661-
mut translatefpkh: Fpkh,
662-
) -> Result<Self::Output, E>
660+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Result<Self::Output, E>
663661
where
664662
Fpk: FnMut(&P) -> Result<Q, E>,
665663
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
666-
Q: MiniscriptKey,
667664
{
668665
let translate_desc = Tr {
669-
internal_key: translatefpk(&self.internal_key)?,
666+
internal_key: fpk(&self.internal_key)?,
670667
tree: match &self.tree {
671-
Some(tree) => Some(tree.translate_helper(&mut translatefpk, &mut translatefpkh)?),
668+
Some(tree) => Some(tree.translate_helper(&mut fpk, &mut fpkh)?),
672669
None => None,
673670
},
674671
spend_info: Mutex::new(None),

src/miniscript/analyzable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::miniscript::iter::PkPkh;
2222
use crate::{Miniscript, MiniscriptKey, ScriptContext};
2323
use std::collections::HashSet;
2424
use std::fmt;
25+
2526
/// Possible reasons Miniscript guarantees can fail
2627
/// We currently mark Miniscript as Non-Analyzable if
2728
/// 1. It is unsafe(does not require a digital signature to spend it)

0 commit comments

Comments
 (0)