qualia_client_core/wellfair/api/
accountability.rs1use sha2::{Digest, Sha256};
4
5use super::*;
6
7impl WebizenHostApi {
8 pub(crate) fn accountability_store(
20 &self,
21 ) -> Result<crate::accountability_store::AccountabilityStore, String> {
22 crate::accountability_store::AccountabilityStore::open(&self.storage_root)
23 .map_err(|e| e.to_string())
24 }
25
26 pub fn ledger_append(
28 &self,
29 kind: &str,
30 payload_json: &str,
31 ) -> Result<crate::accountability_ledger::LedgerEntry, String> {
32 self.accountability_store()?
33 .append_ledger(kind, payload_json, &self.signing_key, Self::now_unix())
34 .map_err(|e| e.to_string())
35 }
36
37 pub fn ledger_verify(
39 &self,
40 ) -> Result<Option<crate::accountability_ledger::LedgerTamper>, String> {
41 let verdict = self
42 .accountability_store()?
43 .verify_ledger()
44 .map_err(|e| e.to_string())?;
45 Ok(verdict.err())
46 }
47
48 pub fn ledger_entries(
50 &self,
51 limit: usize,
52 ) -> Result<Vec<crate::accountability_ledger::LedgerEntry>, String> {
53 self.accountability_store()?
54 .ledger_entries(limit)
55 .map_err(|e| e.to_string())
56 }
57
58 pub fn grant_consent_credential(
62 &self,
63 agent_did: &str,
64 scope: &str,
65 purpose: &str,
66 commitment_hex: &str,
67 wrapped_key_hex: &str,
68 expiry_unix: Option<u64>,
69 ) -> Result<crate::consent_credential::ConsentCredential, String> {
70 let commitment = crate::accountability_store::parse_commitment_hex(commitment_hex)?;
71 let wrapped_key =
72 hex::decode(wrapped_key_hex.trim()).map_err(|e| format!("wrapped key not hex: {e}"))?;
73 let now = Self::now_unix();
74 let id = {
75 let digest = Sha256::digest(format!("{agent_did}:{scope}:{now}").as_bytes());
76 format!("cc-{}", hex::encode(&digest[..6]))
77 };
78 let cred = crate::consent_credential::ConsentCredential::grant(
79 id,
80 &self.owner_did,
81 agent_did,
82 scope,
83 purpose,
84 commitment,
85 wrapped_key,
86 now,
87 expiry_unix,
88 );
89 self.accountability_store()?
90 .grant_credential(cred, &self.signing_key, now)
91 .map_err(|e| e.to_string())
92 }
93
94 pub fn revoke_consent_credential(&self, credential_id: &str) -> Result<bool, String> {
97 self.accountability_store()?
98 .revoke_credential(credential_id, &self.signing_key, Self::now_unix())
99 .map_err(|e| e.to_string())
100 }
101
102 pub fn list_consent_credentials(
104 &self,
105 ) -> Result<Vec<crate::consent_credential::ConsentCredential>, String> {
106 self.accountability_store()?
107 .list_credentials()
108 .map_err(|e| e.to_string())
109 }
110
111 pub fn record_conduct(
114 &self,
115 agent_did: &str,
116 credential_id: &str,
117 action: &str,
118 reason: &str,
119 commitment_hex: &str,
120 ) -> Result<crate::consent_credential::ConductRecord, String> {
121 let commitment = crate::accountability_store::parse_commitment_hex(commitment_hex)?;
122 self.accountability_store()?
123 .record_conduct(
124 agent_did,
125 credential_id,
126 action,
127 reason,
128 commitment,
129 &self.signing_key,
130 Self::now_unix(),
131 )
132 .map_err(|e| e.to_string())
133 }
134
135 pub fn conduct_audit_trail(
137 &self,
138 credential_id: &str,
139 ) -> Result<Vec<crate::consent_credential::ConductRecord>, String> {
140 self.accountability_store()?
141 .audit_trail(credential_id)
142 .map_err(|e| e.to_string())
143 }
144
145 pub fn record_guardian_notifications(
150 &self,
151 notifications: &[super::super::ingest_guardian::GuardianNotification],
152 ) -> Result<(), String> {
153 let store = self.accountability_store()?;
154 for n in notifications {
155 let payload = serde_json::to_string(n).map_err(|e| e.to_string())?;
156 store
157 .append_ledger(
158 "guardian_notified",
159 &payload,
160 &self.signing_key,
161 Self::now_unix(),
162 )
163 .map_err(|e| e.to_string())?;
164 }
165 Ok(())
166 }
167}