Skip to content

Commit d082cc4

Browse files
authored
Merge pull request #234 from LLFourn/fix_policy_translate_pk
Workaround rust compiler goof in Policy::translate_pk
2 parents 42367cf + ef1d69c commit d082cc4

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/policy/concrete.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,34 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
166166
impl<Pk: MiniscriptKey> Policy<Pk> {
167167
/// Convert a policy using one kind of public key to another
168168
/// type of public key
169+
///
170+
/// # Example
171+
///
172+
/// ```
173+
/// use miniscript::{bitcoin::PublicKey, policy::concrete::Policy};
174+
/// use std::str::FromStr;
175+
/// let alice_key = "0270cf3c71f65a3d93d285d9149fddeeb638f87a2d4d8cf16c525f71c417439777";
176+
/// let bob_key = "02f43b15c50a436f5335dbea8a64dd3b4e63e34c3b50c42598acb5f4f336b5d2fb";
177+
/// let placeholder_policy = Policy::<String>::from_str("and(pk(alice_key),pk(bob_key))").unwrap();
178+
///
179+
/// let real_policy = placeholder_policy.translate_pk(|placeholder: &String| match placeholder.as_str() {
180+
/// "alice_key" => PublicKey::from_str(alice_key),
181+
/// "bob_key" => PublicKey::from_str(bob_key),
182+
/// _ => panic!("unknown key!")
183+
/// }).unwrap();
184+
///
185+
/// let expected_policy = Policy::from_str(&format!("and(pk({}),pk({}))", alice_key, bob_key)).unwrap();
186+
/// assert_eq!(real_policy, expected_policy);
187+
/// ```
169188
pub fn translate_pk<Fpk, Q, E>(&self, mut translatefpk: Fpk) -> Result<Policy<Q>, E>
189+
where
190+
Fpk: FnMut(&Pk) -> Result<Q, E>,
191+
Q: MiniscriptKey,
192+
{
193+
self._translate_pk(&mut translatefpk)
194+
}
195+
196+
fn _translate_pk<Fpk, Q, E>(&self, translatefpk: &mut Fpk) -> Result<Policy<Q>, E>
170197
where
171198
Fpk: FnMut(&Pk) -> Result<Q, E>,
172199
Q: MiniscriptKey,
@@ -184,18 +211,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
184211
Policy::Threshold(k, ref subs) => {
185212
let new_subs: Result<Vec<Policy<Q>>, _> = subs
186213
.iter()
187-
.map(|sub| sub.translate_pk(&mut translatefpk))
214+
.map(|sub| sub._translate_pk(translatefpk))
188215
.collect();
189216
new_subs.map(|ok| Policy::Threshold(k, ok))
190217
}
191218
Policy::And(ref subs) => Ok(Policy::And(
192219
subs.iter()
193-
.map(|sub| sub.translate_pk(&mut translatefpk))
220+
.map(|sub| sub._translate_pk(translatefpk))
194221
.collect::<Result<Vec<Policy<Q>>, E>>()?,
195222
)),
196223
Policy::Or(ref subs) => Ok(Policy::Or(
197224
subs.iter()
198-
.map(|&(ref prob, ref sub)| Ok((*prob, sub.translate_pk(&mut translatefpk)?)))
225+
.map(|&(ref prob, ref sub)| Ok((*prob, sub._translate_pk(translatefpk)?)))
199226
.collect::<Result<Vec<(usize, Policy<Q>)>, E>>()?,
200227
)),
201228
}

src/policy/semantic.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,34 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
8080
impl<Pk: MiniscriptKey> Policy<Pk> {
8181
/// Convert a policy using one kind of public key to another
8282
/// type of public key
83+
///
84+
/// # Example
85+
///
86+
/// ```
87+
/// use miniscript::{bitcoin::{hashes::hash160, PublicKey}, policy::semantic::Policy};
88+
/// use std::str::FromStr;
89+
/// let alice_pkh = "236ada020df3208d2517f4b0db03e16f92cd8cf1";
90+
/// let bob_pkh = "3e89b972416ae33870b4634d03b8cdc773200cac";
91+
/// let placeholder_policy = Policy::<String>::from_str("and(pkh(alice_pkh),pkh(bob_pkh))").unwrap();
92+
///
93+
/// let real_policy = placeholder_policy.translate_pkh(|placeholder| match placeholder.as_str() {
94+
/// "alice_pkh" => hash160::Hash::from_str(alice_pkh),
95+
/// "bob_pkh" => hash160::Hash::from_str(bob_pkh),
96+
/// _ => panic!("unknown key hash!")
97+
/// }).unwrap();
98+
///
99+
/// let expected_policy = Policy::<PublicKey>::from_str(&format!("and(pkh({}),pkh({}))", alice_pkh, bob_pkh)).unwrap();
100+
/// assert_eq!(real_policy, expected_policy);
101+
/// ```
83102
pub fn translate_pkh<Fpkh, Q, E>(&self, mut translatefpkh: Fpkh) -> Result<Policy<Q>, E>
103+
where
104+
Fpkh: FnMut(&Pk::Hash) -> Result<Q::Hash, E>,
105+
Q: MiniscriptKey,
106+
{
107+
self._translate_pkh(&mut translatefpkh)
108+
}
109+
110+
fn _translate_pkh<Fpkh, Q, E>(&self, translatefpkh: &mut Fpkh) -> Result<Policy<Q>, E>
84111
where
85112
Fpkh: FnMut(&Pk::Hash) -> Result<Q::Hash, E>,
86113
Q: MiniscriptKey,
@@ -98,7 +125,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
98125
Policy::Threshold(k, ref subs) => {
99126
let new_subs: Result<Vec<Policy<Q>>, _> = subs
100127
.iter()
101-
.map(|sub| sub.translate_pkh(&mut translatefpkh))
128+
.map(|sub| sub._translate_pkh(translatefpkh))
102129
.collect();
103130
new_subs.map(|ok| Policy::Threshold(k, ok))
104131
}

0 commit comments

Comments
 (0)