Skip to content

Commit c68f410

Browse files
committed
descriptor: add infallible variant of translate_pk
1 parent e705948 commit c68f410

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/descriptor/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,28 @@ pub trait PkTranslate<P: MiniscriptKey, Q: MiniscriptKey> {
224224
Fpk: FnMut(&P) -> Result<Q, E>,
225225
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
226226
Q: MiniscriptKey;
227+
228+
/// Calls `translate_pk` with conversion functions that cannot fail
229+
fn translate_pk_infallible<Fpk, Fpkh>(
230+
&self,
231+
mut translatefpk: Fpk,
232+
mut translatefpkh: Fpkh,
233+
) -> Self::Output
234+
where
235+
Fpk: FnMut(&P) -> Q,
236+
Fpkh: FnMut(&P::Hash) -> Q::Hash,
237+
Q: MiniscriptKey,
238+
{
239+
self.translate_pk::<_, _, ()>(|pk| Ok(translatefpk(pk)), |pkh| Ok(translatefpkh(pkh)))
240+
.expect("infallible translation function")
241+
}
227242
}
228243

244+
// There are some additional convenience functions we could add to `PkTranslate`
245+
// for the common special cases where `P::Hash == Q::Hash`, or when `P == P::Hash`,
246+
// but these are blocked on https://github.com/rust-lang/rust/issues/20041 which
247+
// is unlikely to arrive in Rust very soon, as of Jan 2021. Should revisit this.
248+
229249
/// Script descriptor
230250
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
231251
pub enum Descriptor<Pk: MiniscriptKey> {
@@ -548,11 +568,10 @@ where
548568
impl Descriptor<DescriptorPublicKey> {
549569
/// Derives all wildcard keys in the descriptor using the supplied `child_number`
550570
pub fn derive(&self, child_number: bip32::ChildNumber) -> Descriptor<DescriptorPublicKey> {
551-
self.translate_pk(
552-
|pk| Result::Ok::<DescriptorPublicKey, ()>(pk.clone().derive(child_number)),
553-
|pk| Result::Ok::<DescriptorPublicKey, ()>(pk.clone().derive(child_number)),
571+
self.translate_pk_infallible(
572+
|pk| pk.clone().derive(child_number),
573+
|pk| pk.clone().derive(child_number),
554574
)
555-
.expect("Translation fn can't fail.")
556575
}
557576

558577
/// Parse a descriptor that may contain secret keys

0 commit comments

Comments
 (0)