Skip to content

Commit

Permalink
miniscript: translate_pkk to take and return both pk and pkh
Browse files Browse the repository at this point in the history
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 <darosior@protonmail.com>
  • Loading branch information
darosior committed Sep 25, 2020
1 parent 80a76d7 commit 47809cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
) -> Result<Descriptor<Q>, E>
where
Fpk: FnMut(&Pk) -> Result<Q, E>,
Fpkh: FnMut(&Pk::Hash) -> Result<Q::Hash, E>,
Fpkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), E>,
Q: MiniscriptKey,
{
match *self {
Expand Down Expand Up @@ -684,8 +684,11 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
}

impl Descriptor<DescriptorPublicKey> {
/// Derives all wildcard keys in the descriptor using the supplied `child_number`
pub fn derive(&self, child_number: bip32::ChildNumber) -> Descriptor<DescriptorPublicKey> {
/// Derives all wildcard keys in the descriptor using the supplied `path`
pub fn derive(
&self,
child_number: bip32::ChildNumber,
) -> Result<Descriptor<DescriptorPublicKey>, Error> {
self.translate_pk(
|pk| Result::Ok::<DescriptorPublicKey, ()>(pk.clone().derive(child_number)),
|pk, _| match *pk {
Expand All @@ -700,7 +703,7 @@ impl Descriptor<DescriptorPublicKey> {
None => Err(()),
},
)
.expect("Translation fn can't fail.")
.map_err(|_| Error::BadDescriptor)
}
}

Expand Down Expand Up @@ -1522,8 +1525,9 @@ pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHW
pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
let policy: policy::concrete::Policy<DescriptorPublicKey> = descriptor_str.parse().unwrap();
let descriptor = Descriptor::Sh(policy.compile().unwrap());
let derived_descriptor =
descriptor.derive(bip32::ChildNumber::from_normal_idx(42).unwrap());
let derived_descriptor = descriptor
.derive(bip32::ChildNumber::from_normal_idx(42).unwrap())
.expect("Deriving descriptor");

let res_descriptor_str = "thresh(2,\
pk([d34db33f/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/42),\
Expand Down
10 changes: 5 additions & 5 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
) -> Result<Terminal<Q, Ctx>, Error>
where
FPk: FnMut(&Pk) -> Result<Q, Error>,
FPkh: FnMut(&Pk::Hash) -> Result<Q::Hash, Error>,
FPkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), Error>,
Q: MiniscriptKey,
{
let frag = match *self {
Terminal::PkK(ref p) => Terminal::PkK(translatefpk(p)?),
Terminal::PkH(ref pk, ref pkh) => match pk {
Some(pk) => Terminal::PkH(Some(translatefpk(pk)?), translatefpkh(pkh)?),
None => Terminal::PkH(None, translatefpkh(pkh)?),
},
Terminal::PkH(ref pk, ref pkh) => {
let (pk, pkh) = translatefpkh(pk, pkh)?;
Terminal::PkH(pk, pkh)
}
Terminal::After(n) => Terminal::After(n),
Terminal::Older(n) => Terminal::Older(n),
Terminal::Sha256(x) => Terminal::Sha256(x),
Expand Down
4 changes: 2 additions & 2 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
) -> Result<Miniscript<Q, Ctx>, FuncError>
where
FPk: FnMut(&Pk) -> Result<Q, FuncError>,
FPkh: FnMut(&Pk::Hash) -> Result<Q::Hash, FuncError>,
FPkh: FnMut(&Option<Pk>, &Pk::Hash) -> Result<(Option<Q>, Q::Hash), FuncError>,
Q: MiniscriptKey,
{
let inner = self.node.translate_pk(translatefpk, translatefpkh)?;
Expand Down Expand Up @@ -373,7 +373,7 @@ mod tests {
assert_eq!(roundtrip, script);

let translated: Result<_, ()> =
script.translate_pk(&mut |k| Ok(k.clone()), &mut |h| Ok(h.clone()));
script.translate_pk(&mut |k| Ok(k.clone()), &mut |_, h| Ok((None, h.clone())));
assert_eq!(translated, Ok(script));
}

Expand Down

0 comments on commit 47809cb

Please sign in to comment.