Skip to content
Closed
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bitcoin = { version = "0.29.2", optional = true }
bitcoin_hashes = "0.11"
byteorder = "1.3"
elements = { version = "0.21.1", optional = true }
elements-miniscript = { git = "https://github.com/ElementsProject/elements-miniscript", rev = "955f380" }
elements-miniscript = { git = "https://github.com/apoelstra/elements-miniscript", tag = "2023-07--rust-simplicity-patch" }
simplicity-sys = { version = "0.1.0", path = "./simplicity-sys" }
actual-serde = { package = "serde", version = "1.0.103", features = ["derive"], optional = true }

Expand Down
31 changes: 30 additions & 1 deletion src/policy/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::sync::Arc;
use std::{fmt, iter, mem};

use crate::jet::Elements;
use crate::miniscript::{MiniscriptKey, ToPublicKey, Translator};
use crate::miniscript::{ForEachKey, MiniscriptKey, ToPublicKey, Translator};
use crate::node::{ConstructNode, NoWitness};
use crate::FailEntropy;

Expand Down Expand Up @@ -217,6 +217,35 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
}
}

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
{
let mut stack = vec![self];

while let Some(top) = stack.pop() {
match top {
Policy::Key(key) => {
if !pred(key) {
return false;
}
}
Policy::And { left, right } | Policy::Or { left, right } => {
stack.push(right);
stack.push(left);
}
Policy::Threshold(_, sub_policies) => {
stack.extend(sub_policies.iter());
}
_ => {}
}
}

true
}
}

impl<Pk: MiniscriptKey> fmt::Debug for Policy<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down
Loading