Skip to content

Commit 0537c4d

Browse files
committed
Refactor implementations of TranslatePk
We recently refactored the `TranslatePk` traits but did not do _all_ the implementations. We should be uniform in such things. Refactor the implementations of `TranslatePk`. One non-uniformity remains, use of generic `P` is almost universal except in the `miniscript` module `Pk` is used. This patch leaves that as it is.
1 parent fdc8174 commit 0537c4d

File tree

7 files changed

+148
-175
lines changed

7 files changed

+148
-175
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/tr.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -652,23 +652,22 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
652652
}
653653
}
654654

655-
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Tr<P> {
655+
impl<P, Q> TranslatePk<P, Q> for Tr<P>
656+
where
657+
P: MiniscriptKey,
658+
Q: MiniscriptKey,
659+
{
656660
type Output = Tr<Q>;
657661

658-
fn translate_pk<Fpk, Fpkh, E>(
659-
&self,
660-
mut translatefpk: Fpk,
661-
mut translatefpkh: Fpkh,
662-
) -> Result<Self::Output, E>
662+
fn translate_pk<Fpk, Fpkh, E>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Result<Self::Output, E>
663663
where
664664
Fpk: FnMut(&P) -> Result<Q, E>,
665665
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
666-
Q: MiniscriptKey,
667666
{
668667
let translate_desc = Tr {
669-
internal_key: translatefpk(&self.internal_key)?,
668+
internal_key: fpk(&self.internal_key)?,
670669
tree: match &self.tree {
671-
Some(tree) => Some(tree.translate_helper(&mut translatefpk, &mut translatefpkh)?),
670+
Some(tree) => Some(tree.translate_helper(&mut fpk, &mut fpkh)?),
672671
None => None,
673672
},
674673
spend_info: Mutex::new(None),

0 commit comments

Comments
 (0)