Skip to content

Commit 0c3850d

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 e777261 commit 0c3850d

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
@@ -328,7 +328,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
328328
) -> Result<Descriptor<Q>, E>
329329
where
330330
Fpk: FnMut(&Pk) -> Result<Q, E>,
331-
Fpkh: FnMut(&Pk::Hash) -> Result<Q::Hash, E>,
331+
Fpkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), E>,
332332
Q: MiniscriptKey,
333333
{
334334
match *self {
@@ -676,8 +676,11 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
676676
}
677677

678678
impl Descriptor<DescriptorPublicKey> {
679-
/// Derives all wildcard keys in the descriptor using the supplied `child_number`
680-
pub fn derive(&self, child_number: bip32::ChildNumber) -> Descriptor<DescriptorPublicKey> {
679+
/// Derives all wildcard keys in the descriptor using the supplied `path`
680+
pub fn derive(
681+
&self,
682+
child_number: bip32::ChildNumber,
683+
) -> Result<Descriptor<DescriptorPublicKey>, Error> {
681684
self.translate_pk(
682685
|pk| Result::Ok::<DescriptorPublicKey, ()>(pk.clone().derive(child_number)),
683686
|pk, _| match *pk {
@@ -692,7 +695,7 @@ impl Descriptor<DescriptorPublicKey> {
692695
None => Err(()),
693696
},
694697
)
695-
.expect("Translation fn can't fail.")
698+
.map_err(|_| Error::BadDescriptor)
696699
}
697700
}
698701

@@ -1514,8 +1517,9 @@ pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHW
15141517
pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
15151518
let policy: policy::concrete::Policy<DescriptorPublicKey> = descriptor_str.parse().unwrap();
15161519
let descriptor = Descriptor::Sh(policy.compile().unwrap());
1517-
let derived_descriptor =
1518-
descriptor.derive(bip32::ChildNumber::from_normal_idx(42).unwrap());
1520+
let derived_descriptor = descriptor
1521+
.derive(bip32::ChildNumber::from_normal_idx(42).unwrap())
1522+
.expect("Deriving descriptor");
15191523

15201524
let res_descriptor_str = "thresh(2,\
15211525
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)