Skip to content

Commit 43075d8

Browse files
committed
Merge #372: Refactor the TranslatePk types
a4bf096 Refactor the TranslatePk types (Tobin C. Harding) Pull request description: In an effort to make the code more readable refactor the 4 `TranslatePk[X]` types by doing: - Improve docs - Shorten parameter names to assist formatting with no loss of clarity - Use `where` instead of inline trait bounds ACKs for top commit: sanket1729: ACK a4bf096 Tree-SHA512: 2cbc5c660d4f9c353a4d2367a4fcf9aac65f65e5daedcf3337ed29209f69b326abeb068f07e19a65aee2f895e3a5deae6b8a27fd0fb4b4cb8fe388651699887b
2 parents 6fe4bce + a4bf096 commit 43075d8

File tree

1 file changed

+88
-83
lines changed

1 file changed

+88
-83
lines changed

src/lib.rs

Lines changed: 88 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -343,128 +343,133 @@ impl hash::Hash for DummyKeyHash {
343343
}
344344
}
345345

346-
/// Convert a descriptor using abstract keys to one using specific keys
347-
/// This will panic if translatefpk returns an uncompressed key when
348-
/// converting to a Segwit descriptor. To prevent this panic, ensure
349-
/// translatefpk returns an error in this case instead.
350-
pub trait TranslatePk<P: MiniscriptKey, Q: MiniscriptKey> {
351-
/// The associated output type. This must be Self<Q>
346+
/// Converts a descriptor using abstract keys to one using specific keys.
347+
///
348+
/// # Panics
349+
///
350+
/// If `fpk` returns an uncompressed key when converting to a segwit descriptor.
351+
/// To prevent this panic, ensure `fpk` returns an error in this case instead.
352+
pub trait TranslatePk<P, Q>
353+
where
354+
P: MiniscriptKey,
355+
Q: MiniscriptKey,
356+
{
357+
/// The associated output type. This must be `Self<Q>`.
352358
type Output;
353359

354-
/// Translate a struct from one Generic to another where the
355-
/// translation for Pk is provided by translatefpk, and translation for
356-
/// PkH is provided by translatefpkh
357-
fn translate_pk<Fpk, Fpkh, E>(
358-
&self,
359-
translatefpk: Fpk,
360-
translatefpkh: Fpkh,
361-
) -> Result<Self::Output, E>
360+
/// Translates a struct from one generic to another where the translation
361+
/// for Pk is provided by function `fpk`, and translation for PkH is
362+
/// provided by function `fpkh`.
363+
fn translate_pk<Fpk, Fpkh, E>(&self, fpk: Fpk, fpkh: Fpkh) -> Result<Self::Output, E>
362364
where
363365
Fpk: FnMut(&P) -> Result<Q, E>,
364366
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>;
365367

366-
/// Calls `translate_pk` with conversion functions that cannot fail
367-
fn translate_pk_infallible<Fpk, Fpkh>(
368-
&self,
369-
mut translatefpk: Fpk,
370-
mut translatefpkh: Fpkh,
371-
) -> Self::Output
368+
/// Calls `Self::translate_pk` with conversion functions that cannot fail.
369+
fn translate_pk_infallible<Fpk, Fpkh>(&self, mut fpk: Fpk, mut fpkh: Fpkh) -> Self::Output
372370
where
373371
Fpk: FnMut(&P) -> Q,
374372
Fpkh: FnMut(&P::Hash) -> Q::Hash,
375373
{
376-
self.translate_pk::<_, _, ()>(|pk| Ok(translatefpk(pk)), |pkh| Ok(translatefpkh(pkh)))
374+
self.translate_pk::<_, _, ()>(|pk| Ok(fpk(pk)), |pkh| Ok(fpkh(pkh)))
377375
.expect("infallible translation function")
378376
}
379377
}
380378

381379
/// Variant of `TranslatePk` where P and Q both have the same hash
382-
/// type, and the hashes can be converted by just cloning them
383-
pub trait TranslatePk1<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>>:
384-
TranslatePk<P, Q>
380+
/// type, and the hashes can be converted by just cloning them.
381+
pub trait TranslatePk1<P, Q>: TranslatePk<P, Q>
382+
where
383+
P: MiniscriptKey,
384+
Q: MiniscriptKey<Hash = P::Hash>,
385385
{
386-
/// Translate a struct from one generic to another where the
387-
/// translation for Pk is provided by translatefpk
388-
fn translate_pk1<Fpk, E>(
389-
&self,
390-
translatefpk: Fpk,
391-
) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
386+
/// Translates a struct from one generic to another where the translation
387+
/// for Pk is provided by function `fpk`.
388+
fn translate_pk1<Fpk, E>(&self, fpk: Fpk) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
392389
where
393390
Fpk: FnMut(&P) -> Result<Q, E>,
394391
{
395-
self.translate_pk(translatefpk, |h| Ok(h.clone()))
392+
self.translate_pk(fpk, |h| Ok(h.clone()))
396393
}
397394

398-
/// Translate a struct from one generic to another where the
399-
/// translation for Pk is provided by translatefpk
400-
fn translate_pk1_infallible<Fpk: FnMut(&P) -> Q>(
401-
&self,
402-
translatefpk: Fpk,
403-
) -> <Self as TranslatePk<P, Q>>::Output {
404-
self.translate_pk_infallible(translatefpk, P::Hash::clone)
395+
/// Translates a struct from one generic to another where the translation
396+
/// for Pk is provided by function `fpk`.
397+
fn translate_pk1_infallible<Fpk>(&self, fpk: Fpk) -> <Self as TranslatePk<P, Q>>::Output
398+
where
399+
Fpk: FnMut(&P) -> Q,
400+
{
401+
self.translate_pk_infallible(fpk, P::Hash::clone)
405402
}
406403
}
407-
impl<P: MiniscriptKey, Q: MiniscriptKey<Hash = P::Hash>, T: TranslatePk<P, Q>> TranslatePk1<P, Q>
408-
for T
404+
impl<P, Q, T> TranslatePk1<P, Q> for T
405+
where
406+
P: MiniscriptKey,
407+
Q: MiniscriptKey<Hash = P::Hash>,
408+
T: TranslatePk<P, Q>,
409409
{
410410
}
411411

412412
/// Variant of `TranslatePk` where P's hash is P, so the hashes
413-
/// can be converted by reusing the key-conversion function
414-
pub trait TranslatePk2<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey>: TranslatePk<P, Q> {
415-
/// Translate a struct from one generic to another where the
416-
/// translation for Pk is provided by translatefpk
417-
fn translate_pk2<Fpk: Fn(&P) -> Result<Q, E>, E>(
418-
&self,
419-
translatefpk: Fpk,
420-
) -> Result<<Self as TranslatePk<P, Q>>::Output, E> {
421-
self.translate_pk(&translatefpk, |h| {
422-
translatefpk(h).map(|q| q.to_pubkeyhash())
423-
})
424-
}
425-
426-
/// Translate a struct from one generic to another where the
427-
/// translation for Pk is provided by translatefpk
428-
fn translate_pk2_infallible<Fpk: Fn(&P) -> Q>(
429-
&self,
430-
translatefpk: Fpk,
431-
) -> <Self as TranslatePk<P, Q>>::Output {
432-
self.translate_pk_infallible(&translatefpk, |h| translatefpk(h).to_pubkeyhash())
433-
}
434-
}
435-
impl<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey, T: TranslatePk<P, Q>> TranslatePk2<P, Q> for T {}
413+
/// can be converted by reusing the key-conversion function.
414+
pub trait TranslatePk2<P, Q>: TranslatePk<P, Q>
415+
where
416+
P: MiniscriptKey<Hash = P>,
417+
Q: MiniscriptKey,
418+
{
419+
/// Translates a struct from one generic to another where the translation
420+
/// for Pk is provided by function `fpk`.
421+
fn translate_pk2<Fpk, E>(&self, fpk: Fpk) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
422+
where
423+
Fpk: Fn(&P) -> Result<Q, E>,
424+
{
425+
self.translate_pk(&fpk, |h| fpk(h).map(|q| q.to_pubkeyhash()))
426+
}
427+
428+
/// Translates a struct from one generic to another where the translation
429+
/// for Pk is provided by function `fpk`.
430+
fn translate_pk2_infallible<Fpk>(&self, fpk: Fpk) -> <Self as TranslatePk<P, Q>>::Output
431+
where
432+
Fpk: Fn(&P) -> Q,
433+
{
434+
self.translate_pk_infallible(&fpk, |h| fpk(h).to_pubkeyhash())
435+
}
436+
}
437+
impl<P, Q, T> TranslatePk2<P, Q> for T
438+
where
439+
P: MiniscriptKey<Hash = P>,
440+
Q: MiniscriptKey,
441+
T: TranslatePk<P, Q>,
442+
{
443+
}
436444

437445
/// Variant of `TranslatePk` where Q's hash is `hash160` so we can
438-
/// derive hashes by calling `hash_to_hash160`
446+
/// derive hashes by calling `hash_to_hash160`.
439447
pub trait TranslatePk3<P: MiniscriptKey + ToPublicKey, Q: MiniscriptKey<Hash = hash160::Hash>>:
440448
TranslatePk<P, Q>
441449
{
442-
/// Translate a struct from one generic to another where the
443-
/// translation for Pk is provided by translatefpk
444-
fn translate_pk3<Fpk, E>(
445-
&self,
446-
translatefpk: Fpk,
447-
) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
450+
/// Translates a struct from one generic to another where the translation
451+
/// for Pk is provided by function `fpk`.
452+
fn translate_pk3<Fpk, E>(&self, fpk: Fpk) -> Result<<Self as TranslatePk<P, Q>>::Output, E>
448453
where
449454
Fpk: FnMut(&P) -> Result<Q, E>,
450455
{
451-
self.translate_pk(translatefpk, |h| Ok(P::hash_to_hash160(h)))
456+
self.translate_pk(fpk, |h| Ok(P::hash_to_hash160(h)))
452457
}
453458

454-
/// Translate a struct from one generic to another where the
455-
/// translation for Pk is provided by translatefpk
456-
fn translate_pk3_infallible<Fpk: FnMut(&P) -> Q>(
457-
&self,
458-
translatefpk: Fpk,
459-
) -> <Self as TranslatePk<P, Q>>::Output {
460-
self.translate_pk_infallible(translatefpk, P::hash_to_hash160)
459+
/// Translates a struct from one generic to another where the translation
460+
/// for Pk is provided by function `fpk`.
461+
fn translate_pk3_infallible<Fpk>(&self, fpk: Fpk) -> <Self as TranslatePk<P, Q>>::Output
462+
where
463+
Fpk: FnMut(&P) -> Q,
464+
{
465+
self.translate_pk_infallible(fpk, P::hash_to_hash160)
461466
}
462467
}
463-
impl<
464-
P: MiniscriptKey + ToPublicKey,
465-
Q: MiniscriptKey<Hash = hash160::Hash>,
466-
T: TranslatePk<P, Q>,
467-
> TranslatePk3<P, Q> for T
468+
impl<P, Q, T> TranslatePk3<P, Q> for T
469+
where
470+
P: MiniscriptKey + ToPublicKey,
471+
Q: MiniscriptKey<Hash = hash160::Hash>,
472+
T: TranslatePk<P, Q>,
468473
{
469474
}
470475

0 commit comments

Comments
 (0)