chio-governance 0.1.2

Chio generic governance charters and case evaluation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use serde::{Deserialize, Serialize};

use crate::canonical_json_bytes;
use crate::crypto::sha256_hex;
use crate::listing::{
    normalize_namespace, GenericListingActorKind, GenericRegistryPublisher, SignedGenericListing,
    SignedGenericTrustActivation,
};
use crate::receipt::lineage::SignedExportEnvelope;
use crate::validation::{is_sha256_hex, validate_non_empty};

pub const GENERIC_GOVERNANCE_CHARTER_ARTIFACT_SCHEMA: &str = "chio.registry.governance-charter.v1";
pub const GENERIC_GOVERNANCE_CASE_ARTIFACT_SCHEMA: &str = "chio.registry.governance-case.v1";

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GenericGovernanceCaseKind {
    Dispute,
    Freeze,
    Sanction,
    Appeal,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GenericGovernanceCaseState {
    Open,
    Escalated,
    Enforced,
    Resolved,
    Denied,
    Superseded,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GenericGovernanceEffectiveState {
    Clear,
    Disputed,
    Frozen,
    Sanctioned,
    Appealed,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GenericGovernanceEvidenceKind {
    Listing,
    TrustActivation,
    Certification,
    RegistrySearch,
    OperatorReport,
    External,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GenericGovernanceFindingCode {
    ListingUnverifiable,
    ActivationUnverifiable,
    CharterUnverifiable,
    CaseUnverifiable,
    PriorCaseUnverifiable,
    CharterExpired,
    CaseExpired,
    CharterScopeMismatch,
    CharterKindUnsupported,
    CaseMismatch,
    MissingActivation,
    ActivationMismatch,
    AppealTargetMissing,
    AppealTargetInvalid,
    SupersessionTargetMissing,
    SupersessionTargetInvalid,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceAuthorityScope {
    pub namespace: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub allowed_listing_operator_ids: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub allowed_actor_kinds: Vec<GenericListingActorKind>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub policy_reference: Option<String>,
}

impl GenericGovernanceAuthorityScope {
    pub fn validate(&self) -> Result<(), String> {
        validate_non_empty(&self.namespace, "authority_scope.namespace")?;
        for (index, operator_id) in self.allowed_listing_operator_ids.iter().enumerate() {
            validate_non_empty(
                operator_id,
                &format!("authority_scope.allowed_listing_operator_ids[{index}]"),
            )?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceEvidenceReference {
    pub kind: GenericGovernanceEvidenceKind,
    pub reference_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
}

impl GenericGovernanceEvidenceReference {
    pub fn validate(&self, field: &str) -> Result<(), String> {
        validate_non_empty(&self.reference_id, &format!("{field}.reference_id"))?;
        if let Some(uri) = self.uri.as_deref() {
            validate_non_empty(uri, &format!("{field}.uri"))?;
        }
        if let Some(sha256) = self.sha256.as_deref() {
            let sha256_field = format!("{field}.sha256");
            if !is_sha256_hex(sha256) {
                return Err(format!(
                    "{sha256_field} must be a 64-character SHA-256 hex digest"
                ));
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceCharterArtifact {
    pub schema: String,
    pub charter_id: String,
    pub governing_operator_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub governing_operator_name: Option<String>,
    pub authority_scope: GenericGovernanceAuthorityScope,
    pub allowed_case_kinds: Vec<GenericGovernanceCaseKind>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub escalation_operator_ids: Vec<String>,
    pub issued_at: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,
    pub issued_by: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

impl GenericGovernanceCharterArtifact {
    pub fn validate(&self) -> Result<(), String> {
        if self.schema != GENERIC_GOVERNANCE_CHARTER_ARTIFACT_SCHEMA {
            return Err(format!(
                "unsupported generic governance charter schema: {}",
                self.schema
            ));
        }
        validate_non_empty(&self.charter_id, "charter_id")?;
        validate_non_empty(&self.governing_operator_id, "governing_operator_id")?;
        validate_non_empty(&self.issued_by, "issued_by")?;
        self.authority_scope.validate()?;
        if normalize_namespace(&self.authority_scope.namespace).is_empty() {
            return Err("authority_scope.namespace must not be empty".to_string());
        }
        if self.allowed_case_kinds.is_empty() {
            return Err("allowed_case_kinds must not be empty".to_string());
        }
        for (index, operator_id) in self.escalation_operator_ids.iter().enumerate() {
            validate_non_empty(operator_id, &format!("escalation_operator_ids[{index}]"))?;
        }
        if let Some(expires_at) = self.expires_at {
            if expires_at <= self.issued_at {
                return Err("expires_at must be greater than issued_at".to_string());
            }
        }
        Ok(())
    }
}

pub type SignedGenericGovernanceCharter = SignedExportEnvelope<GenericGovernanceCharterArtifact>;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceCaseArtifact {
    pub schema: String,
    pub case_id: String,
    pub charter_id: String,
    pub governing_operator_id: String,
    pub kind: GenericGovernanceCaseKind,
    pub state: GenericGovernanceCaseState,
    pub namespace: String,
    pub listing_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub activation_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject_operator_id: Option<String>,
    pub opened_at: u64,
    pub updated_at: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub escalated_to_operator_ids: Vec<String>,
    pub evidence_refs: Vec<GenericGovernanceEvidenceReference>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub appeal_of_case_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub supersedes_case_id: Option<String>,
    pub issued_by: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

impl GenericGovernanceCaseArtifact {
    pub fn validate(&self) -> Result<(), String> {
        if self.schema != GENERIC_GOVERNANCE_CASE_ARTIFACT_SCHEMA {
            return Err(format!(
                "unsupported generic governance case schema: {}",
                self.schema
            ));
        }
        validate_non_empty(&self.case_id, "case_id")?;
        validate_non_empty(&self.charter_id, "charter_id")?;
        validate_non_empty(&self.governing_operator_id, "governing_operator_id")?;
        validate_non_empty(&self.namespace, "namespace")?;
        validate_non_empty(&self.listing_id, "listing_id")?;
        validate_non_empty(&self.issued_by, "issued_by")?;
        if self.updated_at < self.opened_at {
            return Err("updated_at must be greater than or equal to opened_at".to_string());
        }
        if let Some(expires_at) = self.expires_at {
            if expires_at <= self.opened_at {
                return Err("expires_at must be greater than opened_at".to_string());
            }
        }
        for (index, operator_id) in self.escalated_to_operator_ids.iter().enumerate() {
            validate_non_empty(operator_id, &format!("escalated_to_operator_ids[{index}]"))?;
        }
        if self.evidence_refs.is_empty() {
            return Err("evidence_refs must not be empty".to_string());
        }
        for (index, evidence_ref) in self.evidence_refs.iter().enumerate() {
            evidence_ref.validate(&format!("evidence_refs[{index}]"))?;
        }
        if matches!(self.kind, GenericGovernanceCaseKind::Appeal) {
            if self.appeal_of_case_id.as_deref().is_none() {
                return Err("appeal case requires appeal_of_case_id".to_string());
            }
        } else if self.appeal_of_case_id.is_some() {
            return Err("appeal_of_case_id is only valid for appeal cases".to_string());
        }
        if matches!(self.state, GenericGovernanceCaseState::Escalated)
            && self.escalated_to_operator_ids.is_empty()
        {
            return Err("escalated case requires escalated_to_operator_ids".to_string());
        }
        Ok(())
    }
}

pub type SignedGenericGovernanceCase = SignedExportEnvelope<GenericGovernanceCaseArtifact>;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceCharterIssueRequest {
    pub authority_scope: GenericGovernanceAuthorityScope,
    pub allowed_case_kinds: Vec<GenericGovernanceCaseKind>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub escalation_operator_ids: Vec<String>,
    pub issued_by: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub issued_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

impl GenericGovernanceCharterIssueRequest {
    pub fn validate(&self) -> Result<(), String> {
        self.authority_scope.validate()?;
        validate_non_empty(&self.issued_by, "issued_by")?;
        if self.allowed_case_kinds.is_empty() {
            return Err("allowed_case_kinds must not be empty".to_string());
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceCaseIssueRequest {
    pub charter: SignedGenericGovernanceCharter,
    pub listing: SignedGenericListing,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub activation: Option<SignedGenericTrustActivation>,
    pub kind: GenericGovernanceCaseKind,
    pub state: GenericGovernanceCaseState,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject_operator_id: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub escalated_to_operator_ids: Vec<String>,
    pub evidence_refs: Vec<GenericGovernanceEvidenceReference>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub appeal_of_case_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub supersedes_case_id: Option<String>,
    pub issued_by: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub opened_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

impl GenericGovernanceCaseIssueRequest {
    pub fn validate(&self) -> Result<(), String> {
        self.listing.body.validate()?;
        if !self
            .listing
            .verify_signature()
            .map_err(|error| error.to_string())?
        {
            return Err("governance case listing signature is invalid".to_string());
        }
        if !self
            .charter
            .verify_signature()
            .map_err(|error| error.to_string())?
        {
            return Err("governance charter signature is invalid".to_string());
        }
        self.charter.body.validate()?;
        if let Some(activation) = self.activation.as_ref() {
            if !activation
                .verify_signature()
                .map_err(|error| error.to_string())?
            {
                return Err("trust activation signature is invalid".to_string());
            }
            activation
                .body
                .validate()
                .map_err(|error| error.to_string())?;
        }
        validate_non_empty(&self.issued_by, "issued_by")?;
        if self.evidence_refs.is_empty() {
            return Err("evidence_refs must not be empty".to_string());
        }
        for (index, evidence_ref) in self.evidence_refs.iter().enumerate() {
            evidence_ref.validate(&format!("evidence_refs[{index}]"))?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceCaseEvaluationRequest {
    pub listing: SignedGenericListing,
    pub current_publisher: GenericRegistryPublisher,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub activation: Option<SignedGenericTrustActivation>,
    pub charter: SignedGenericGovernanceCharter,
    pub case: SignedGenericGovernanceCase,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prior_case: Option<SignedGenericGovernanceCase>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub evaluated_at: Option<u64>,
}

impl GenericGovernanceCaseEvaluationRequest {
    pub fn validate(&self) -> Result<(), String> {
        self.listing.body.validate()?;
        self.current_publisher.validate()?;
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceFinding {
    pub code: GenericGovernanceFindingCode,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GenericGovernanceCaseEvaluation {
    pub listing_id: String,
    pub namespace: String,
    pub charter_id: String,
    pub case_id: String,
    pub governing_operator_id: String,
    pub kind: GenericGovernanceCaseKind,
    pub state: GenericGovernanceCaseState,
    pub effective_state: GenericGovernanceEffectiveState,
    pub evaluated_at: u64,
    pub blocks_admission: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub findings: Vec<GenericGovernanceFinding>,
}

pub fn build_generic_governance_charter_artifact(
    local_operator_id: &str,
    local_operator_name: Option<String>,
    request: &GenericGovernanceCharterIssueRequest,
    issued_at: u64,
) -> Result<GenericGovernanceCharterArtifact, String> {
    request.validate()?;
    validate_non_empty(local_operator_id, "local_operator_id")?;
    let issued_at = request.issued_at.unwrap_or(issued_at);
    let charter_id = format!(
        "charter-{}",
        sha256_hex(
            &canonical_json_bytes(&(
                local_operator_id,
                normalize_namespace(&request.authority_scope.namespace),
                &request.allowed_case_kinds,
                issued_at,
            ))
            .map_err(|error| error.to_string())?
        )
    );
    let artifact = GenericGovernanceCharterArtifact {
        schema: GENERIC_GOVERNANCE_CHARTER_ARTIFACT_SCHEMA.to_string(),
        charter_id,
        governing_operator_id: local_operator_id.to_string(),
        governing_operator_name: local_operator_name,
        authority_scope: request.authority_scope.clone(),
        allowed_case_kinds: request.allowed_case_kinds.clone(),
        escalation_operator_ids: request.escalation_operator_ids.clone(),
        issued_at,
        expires_at: request.expires_at,
        issued_by: request.issued_by.clone(),
        note: request.note.clone(),
    };
    artifact.validate()?;
    Ok(artifact)
}

pub fn build_generic_governance_case_artifact(
    local_operator_id: &str,
    request: &GenericGovernanceCaseIssueRequest,
    issued_at: u64,
) -> Result<GenericGovernanceCaseArtifact, String> {
    request.validate()?;
    validate_non_empty(local_operator_id, "local_operator_id")?;
    if request.charter.body.governing_operator_id != local_operator_id {
        return Err("governance case must be issued by the charter governing operator".to_string());
    }
    if request
        .activation
        .as_ref()
        .is_some_and(|activation| activation.body.local_operator_id != local_operator_id)
    {
        return Err(
            "governance cases must use a trust activation issued by the governing operator"
                .to_string(),
        );
    }
    let opened_at = request.opened_at.unwrap_or(issued_at);
    let updated_at = request.updated_at.unwrap_or(opened_at);
    let case_id = format!(
        "case-{}",
        sha256_hex(
            &canonical_json_bytes(&(
                local_operator_id,
                &request.charter.body.charter_id,
                &request.listing.body.listing_id,
                request.kind,
                request.state,
                opened_at,
                &request.appeal_of_case_id,
                &request.supersedes_case_id,
            ))
            .map_err(|error| error.to_string())?
        )
    );
    let artifact = GenericGovernanceCaseArtifact {
        schema: GENERIC_GOVERNANCE_CASE_ARTIFACT_SCHEMA.to_string(),
        case_id,
        charter_id: request.charter.body.charter_id.clone(),
        governing_operator_id: local_operator_id.to_string(),
        kind: request.kind,
        state: request.state,
        namespace: request.listing.body.namespace.clone(),
        listing_id: request.listing.body.listing_id.clone(),
        activation_id: request
            .activation
            .as_ref()
            .map(|activation| activation.body.activation_id.clone()),
        subject_operator_id: request.subject_operator_id.clone(),
        opened_at,
        updated_at,
        expires_at: request.expires_at,
        escalated_to_operator_ids: request.escalated_to_operator_ids.clone(),
        evidence_refs: request.evidence_refs.clone(),
        appeal_of_case_id: request.appeal_of_case_id.clone(),
        supersedes_case_id: request.supersedes_case_id.clone(),
        issued_by: request.issued_by.clone(),
        note: request.note.clone(),
    };
    artifact.validate()?;
    Ok(artifact)
}