Skip to content

Commit 3e218b3

Browse files
committed
miniscript: translate_pkk to take and return both pk and pkh
This adds the optional Pk to the translation methods, which allows Descriptor with extended public keys to derive keys behind hashes. Signed-off-by: Antoine Poinsot <[email protected]>
1 parent 7797e60 commit 3e218b3

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/descriptor/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
336336
) -> Result<Descriptor<Q>, E>
337337
where
338338
Fpk: FnMut(&Pk) -> Result<Q, E>,
339-
Fpkh: FnMut(&Pk::Hash) -> Result<Q::Hash, E>,
339+
Fpkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), E>,
340340
Q: MiniscriptKey,
341341
{
342342
match *self {
@@ -684,8 +684,11 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
684684
}
685685

686686
impl Descriptor<DescriptorPublicKey> {
687-
/// Derives all wildcard keys in the descriptor using the supplied `child_number`
688-
pub fn derive(&self, child_number: bip32::ChildNumber) -> Descriptor<DescriptorPublicKey> {
687+
/// Derives all wildcard keys in the descriptor using the supplied `path`
688+
pub fn derive(
689+
&self,
690+
child_number: bip32::ChildNumber,
691+
) -> Result<Descriptor<DescriptorPublicKey>, Error> {
689692
self.translate_pk(
690693
|pk| Result::Ok::<DescriptorPublicKey, ()>(pk.clone().derive(child_number)),
691694
|pk, _| match *pk {
@@ -700,7 +703,7 @@ impl Descriptor<DescriptorPublicKey> {
700703
None => Err(()),
701704
},
702705
)
703-
.expect("Translation fn can't fail.")
706+
.map_err(|_| Error::BadDescriptor)
704707
}
705708
}
706709

@@ -1522,8 +1525,9 @@ pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHW
15221525
pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
15231526
let policy: policy::concrete::Policy<DescriptorPublicKey> = descriptor_str.parse().unwrap();
15241527
let descriptor = Descriptor::Sh(policy.compile().unwrap());
1525-
let derived_descriptor =
1526-
descriptor.derive(bip32::ChildNumber::from_normal_idx(42).unwrap());
1528+
let derived_descriptor = descriptor
1529+
.derive(bip32::ChildNumber::from_normal_idx(42).unwrap())
1530+
.expect("Deriving descriptor");
15271531

15281532
let res_descriptor_str = "thresh(2,\
15291533
pk([d34db33f/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/42),\

src/miniscript/astelem.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
7070
) -> Result<Terminal<Q, Ctx>, Error>
7171
where
7272
FPk: FnMut(&Pk) -> Result<Q, Error>,
73-
FPkh: FnMut(&Pk::Hash) -> Result<Q::Hash, Error>,
73+
FPkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), Error>,
7474
Q: MiniscriptKey,
7575
{
7676
let frag = match *self {
7777
Terminal::PkK(ref p) => Terminal::PkK(translatefpk(p)?),
78-
Terminal::PkH(ref pk, ref pkh) => match pk {
79-
Some(pk) => Terminal::PkH(Some(translatefpk(pk)?), translatefpkh(pkh)?),
80-
None => Terminal::PkH(None, translatefpkh(pkh)?),
81-
},
78+
Terminal::PkH(ref pk, ref pkh) => {
79+
let (pk, pkh) = translatefpkh(pk, pkh)?;
80+
Terminal::PkH(pk, pkh)
81+
}
8282
Terminal::After(n) => Terminal::After(n),
8383
Terminal::Older(n) => Terminal::Older(n),
8484
Terminal::Sha256(x) => Terminal::Sha256(x),

src/miniscript/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
219219
) -> Result<Miniscript<Q, Ctx>, FuncError>
220220
where
221221
FPk: FnMut(&Pk) -> Result<Q, FuncError>,
222-
FPkh: FnMut(&Pk::Hash) -> Result<Q::Hash, FuncError>,
222+
FPkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), FuncError>,
223223
Q: MiniscriptKey,
224224
{
225225
let inner = self.node.translate_pk(translatefpk, translatefpkh)?;
@@ -373,7 +373,7 @@ mod tests {
373373
assert_eq!(roundtrip, script);
374374

375375
let translated: Result<_, ()> =
376-
script.translate_pk(&mut |k| Ok(k.clone()), &mut |h| Ok(h.clone()));
376+
script.translate_pk(&mut |k| Ok(k.clone()), &mut |_, h| Ok((None, h.clone())));
377377
assert_eq!(translated, Ok(script));
378378
}
379379

0 commit comments

Comments
 (0)