Skip to content

Commit 0d32407

Browse files
committed
descriptor: add convenience method for case when target hash is hash160
1 parent f8c8176 commit 0d32407

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/descriptor/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::{
2929
str::{self, FromStr},
3030
};
3131

32+
use bitcoin::hashes::hash160;
3233
use bitcoin::secp256k1;
3334
use bitcoin::util::bip32;
3435
use bitcoin::{self, Script};
@@ -295,6 +296,46 @@ pub trait PkTranslate2<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey>: PkTranslat
295296
}
296297
impl<P: MiniscriptKey<Hash = P>, Q: MiniscriptKey, T: PkTranslate<P, Q>> PkTranslate2<P, Q> for T {}
297298

299+
/// Variant of `PkTranslate` where Q's hash is `hash160` so we can
300+
/// derive hashes by calling `hash_to_hash160`
301+
pub trait PkTranslate3<
302+
P: MiniscriptKey + ToPublicKey<PkCtx>,
303+
PkCtx: Copy,
304+
Q: MiniscriptKey<Hash = hash160::Hash>,
305+
>: PkTranslate<P, Q>
306+
{
307+
/// Translate a struct from one generic to another where the
308+
/// translation for Pk is provided by translatefpk
309+
fn translate_pk3<Fpk, E>(
310+
&self,
311+
translatefpk: Fpk,
312+
pk_ctx: PkCtx,
313+
) -> Result<<Self as PkTranslate<P, Q>>::Output, E>
314+
where
315+
Fpk: FnMut(&P) -> Result<Q, E>,
316+
{
317+
self.translate_pk(translatefpk, |h| Ok(P::hash_to_hash160(h, pk_ctx)))
318+
}
319+
320+
/// Translate a struct from one generic to another where the
321+
/// translation for Pk is provided by translatefpk
322+
fn translate_pk3_infallible<Fpk: FnMut(&P) -> Q>(
323+
&self,
324+
translatefpk: Fpk,
325+
pk_ctx: PkCtx,
326+
) -> <Self as PkTranslate<P, Q>>::Output {
327+
self.translate_pk_infallible(translatefpk, |h| P::hash_to_hash160(h, pk_ctx))
328+
}
329+
}
330+
impl<
331+
P: MiniscriptKey + ToPublicKey<PkCtx>,
332+
PkCtx: Copy,
333+
Q: MiniscriptKey<Hash = hash160::Hash>,
334+
T: PkTranslate<P, Q>,
335+
> PkTranslate3<P, PkCtx, Q> for T
336+
{
337+
}
338+
298339
// There are some additional convenience functions we could add to `PkTranslate`
299340
// for the common special cases where `P::Hash == Q::Hash`, or when `P == P::Hash`,
300341
// but these are blocked on https://github.com/rust-lang/rust/issues/20041 which

0 commit comments

Comments
 (0)