telltale_machine/
output_condition.rs1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub enum OutputConditionPolicy {
11 Disabled,
13 AllowAll,
15 DenyAll,
17 PredicateAllowList(Vec<String>),
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct OutputConditionMeta {
24 pub predicate_ref: String,
26 pub witness_ref: Option<String>,
28 pub output_digest: String,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct OutputConditionCheck {
35 pub meta: OutputConditionMeta,
37 pub passed: bool,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub struct OutputConditionHint {
44 pub predicate_ref: String,
46 pub witness_ref: Option<String>,
48}
49
50impl OutputConditionMeta {
51 #[must_use]
53 pub fn from_hint(hint: OutputConditionHint, output_digest: String) -> Self {
54 Self {
55 predicate_ref: hint.predicate_ref,
56 witness_ref: hint.witness_ref,
57 output_digest,
58 }
59 }
60
61 #[must_use]
63 pub fn default_observable(output_digest: String) -> Self {
64 Self {
65 predicate_ref: "protocol_machine.observable_output".to_string(),
66 witness_ref: None,
67 output_digest,
68 }
69 }
70}
71
72#[must_use]
74pub fn verify_output_condition(policy: &OutputConditionPolicy, meta: &OutputConditionMeta) -> bool {
75 match policy {
76 OutputConditionPolicy::Disabled | OutputConditionPolicy::AllowAll => true,
77 OutputConditionPolicy::DenyAll => false,
78 OutputConditionPolicy::PredicateAllowList(allowed) => {
79 allowed.iter().any(|p| p == &meta.predicate_ref)
80 }
81 }
82}