use std::collections::HashMap;
use std::env;
pub fn check_consensus(
conn: &rusqlite::Connection,
transition_id: &str,
inputs: &[String],
place_colors: &HashMap<String, String>,
clm_root: &str,
) -> Result<bool, String> {
let safe_tid = transition_id.replace(['-', '.'], "_");
let consensus_var_name = format!("CLM_TOPO_TRANS_CONSENSUS_{}", safe_tid);
let required_dids_str = env::var(&consensus_var_name).unwrap_or_default();
if required_dids_str.is_empty() {
return Ok(true);
}
let required_dids: Vec<&str> = required_dids_str.split_whitespace().collect();
let mut token_signers = Vec::new();
for inp in inputs {
if let Some(token_hash) = place_colors.get(inp) {
if token_hash.is_empty() || token_hash == "null" {
continue;
}
let mut token_content = String::new();
if let Ok(Some(content)) = crate::db::resolve_handle_content("content_memory", clm_root, token_hash) {
token_content = content;
} else if let Ok(Some(content)) = crate::db::resolve_handle_content("execution_log", clm_root, token_hash) {
token_content = content;
} else {
let card_content: Option<String> = conn
.query_row(
"SELECT content FROM card WHERE hash = ?",
rusqlite::params![token_hash],
|row| row.get(0),
)
.ok();
if let Some(c) = card_content {
token_content = c;
}
}
if token_content.is_empty() {
continue;
}
if let Ok(v) = serde_yaml::from_str::<serde_json::Value>(&token_content)
&& let Some(vcard) = v.get("vcard_token")
&& let Some(did) = vcard.get("agent_did").and_then(|d| d.as_str())
.or_else(|| vcard.get("signer_did").and_then(|d| d.as_str()))
{
token_signers.push(did.to_string());
}
}
}
for req in required_dids {
let mut satisfied = false;
for i in 0..token_signers.len() {
if token_signers[i] == req {
satisfied = true;
token_signers.remove(i);
break;
}
}
if !satisfied {
eprintln!(
" ❌ [ZTA] Transition '{}' blocked: required consensus from '{}' not satisfied by input tokens",
transition_id, req
);
return Ok(false);
}
}
Ok(true)
}
pub fn check_transition_acl(
transition_id: &str,
agent_did: &str,
req_class_opt: Option<&str>,
default_req_class: Option<&str>,
) -> bool {
let safe_tid = transition_id.replace(['-', '.'], "_");
let req_class_var = format!("CLM_TOPO_TRANS_AGENT_CLASS_{}", safe_tid);
let req_class = env::var(&req_class_var)
.ok()
.or_else(|| req_class_opt.map(|s| s.to_string()))
.or_else(|| default_req_class.map(|s| s.to_string()))
.unwrap_or_default();
if req_class.is_empty() || req_class == "any" {
return true;
}
let actual_class = if agent_did.starts_with("did:key:") {
"EOA"
} else {
"CA"
};
if actual_class != req_class {
eprintln!(
" ❌ [ZTA] Transition '{}' requires agent_class='{}' but firer '{}' is class='{}'",
transition_id, req_class, agent_did, actual_class
);
false
} else {
true
}
}
pub fn authorize_kenotic_sponsor(
sponsor_did: &str,
authorized_sponsors: &[String],
) -> Result<(), String> {
if authorized_sponsors.is_empty() {
return Ok(());
}
for pattern in authorized_sponsors {
match pattern.as_str() {
"*" => return Ok(()),
"did:key:*" => {
if sponsor_did.starts_with("did:key:") {
return Ok(());
}
}
"did:clm:*" => {
if sponsor_did.starts_with("did:clm:") {
return Ok(());
}
}
"did:pkc:ca:*" => {
if sponsor_did.starts_with("did:pkc:ca:") {
return Ok(());
}
}
exact => {
if sponsor_did == exact {
return Ok(());
}
}
}
}
eprintln!(
" ❌ [ZTA] Sponsor DID '{}' not in authorized_sponsors: {:?}",
sponsor_did, authorized_sponsors
);
Err("Sponsor not authorized".to_string())
}