exochain-identity 0.2.0-beta

EXOCHAIN constitutional trust fabric — privacy-preserving identity adjudication
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
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! PACE — Primary / Alternate / Contingency / Emergency operator continuity.

use std::{collections::BTreeSet, fmt};

use exo_core::Did;
use serde::{
    Deserialize, Deserializer, Serialize,
    de::{self, SeqAccess, Visitor},
};

use crate::error::IdentityError;

/// Maximum DIDs accepted in any non-primary PACE level.
pub const MAX_PACE_LEVEL_DIDS: usize = 32;

/// Configuration defining the operator hierarchy for PACE continuity.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PaceConfig {
    pub primary: Did,
    #[serde(deserialize_with = "deserialize_pace_alternates")]
    pub alternates: Vec<Did>,
    #[serde(deserialize_with = "deserialize_pace_contingency")]
    pub contingency: Vec<Did>,
    #[serde(deserialize_with = "deserialize_pace_emergency")]
    pub emergency: Vec<Did>,
}

impl fmt::Debug for PaceConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PaceConfig")
            .field("primary", &"<redacted>")
            .field("alternate_count", &self.alternates.len())
            .field("contingency_count", &self.contingency.len())
            .field("emergency_count", &self.emergency.len())
            .finish()
    }
}

fn deserialize_pace_alternates<'de, D>(deserializer: D) -> Result<Vec<Did>, D::Error>
where
    D: Deserializer<'de>,
{
    deserialize_bounded_did_vec(deserializer, "alternates")
}

fn deserialize_pace_contingency<'de, D>(deserializer: D) -> Result<Vec<Did>, D::Error>
where
    D: Deserializer<'de>,
{
    deserialize_bounded_did_vec(deserializer, "contingency")
}

fn deserialize_pace_emergency<'de, D>(deserializer: D) -> Result<Vec<Did>, D::Error>
where
    D: Deserializer<'de>,
{
    deserialize_bounded_did_vec(deserializer, "emergency")
}

fn deserialize_bounded_did_vec<'de, D>(
    deserializer: D,
    field: &'static str,
) -> Result<Vec<Did>, D::Error>
where
    D: Deserializer<'de>,
{
    struct BoundedDidVecVisitor {
        field: &'static str,
    }

    impl<'de> Visitor<'de> for BoundedDidVecVisitor {
        type Value = Vec<Did>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                formatter,
                "at most {MAX_PACE_LEVEL_DIDS} DID values in {}",
                self.field
            )
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut dids = Vec::new();
            while let Some(did) = seq.next_element::<Did>()? {
                if dids.len() >= MAX_PACE_LEVEL_DIDS {
                    return Err(de::Error::custom(format!(
                        "{} must not contain more than {} DIDs",
                        self.field, MAX_PACE_LEVEL_DIDS
                    )));
                }
                dids.push(did);
            }
            Ok(dids)
        }
    }

    deserializer.deserialize_seq(BoundedDidVecVisitor { field })
}

impl PaceConfig {
    /// Validate that all PACE levels are non-empty and contain no duplicate DIDs.
    pub fn validate(&self) -> Result<(), IdentityError> {
        if self.alternates.is_empty() {
            return Err(IdentityError::InvalidPaceConfig(
                "alternates must not be empty".into(),
            ));
        }
        if self.contingency.is_empty() {
            return Err(IdentityError::InvalidPaceConfig(
                "contingency must not be empty".into(),
            ));
        }
        if self.emergency.is_empty() {
            return Err(IdentityError::InvalidPaceConfig(
                "emergency must not be empty".into(),
            ));
        }

        let mut all = BTreeSet::new();
        let all_dids = std::iter::once(&self.primary)
            .chain(self.alternates.iter())
            .chain(self.contingency.iter())
            .chain(self.emergency.iter());

        for did in all_dids {
            if !all.insert(did.as_str().to_owned()) {
                return Err(IdentityError::DuplicatePaceDid(did.clone()));
            }
        }

        Ok(())
    }
}

/// Current operational state in the PACE escalation hierarchy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PaceState {
    Normal,
    AlternateActive,
    ContingencyActive,
    EmergencyActive,
}

/// Resolve the currently active operator DID for the given PACE state.
pub fn resolve_operator<'a>(
    config: &'a PaceConfig,
    state: &PaceState,
) -> Result<&'a Did, IdentityError> {
    match state {
        PaceState::Normal => Ok(&config.primary),
        PaceState::AlternateActive => config
            .alternates
            .first()
            .ok_or_else(|| IdentityError::InvalidPaceConfig("alternates must not be empty".into())),
        PaceState::ContingencyActive => config.contingency.first().ok_or_else(|| {
            IdentityError::InvalidPaceConfig("contingency must not be empty".into())
        }),
        PaceState::EmergencyActive => config
            .emergency
            .first()
            .ok_or_else(|| IdentityError::InvalidPaceConfig("emergency must not be empty".into())),
    }
}

/// Escalate the PACE state to the next higher level, returning the new state.
pub fn escalate(state: &mut PaceState) -> Result<PaceState, IdentityError> {
    let new_state = match *state {
        PaceState::Normal => PaceState::AlternateActive,
        PaceState::AlternateActive => PaceState::ContingencyActive,
        PaceState::ContingencyActive => PaceState::EmergencyActive,
        PaceState::EmergencyActive => return Err(IdentityError::CannotEscalate),
    };
    *state = new_state;
    Ok(new_state)
}

/// De-escalate the PACE state to the next lower level, returning the new state.
pub fn deescalate(state: &mut PaceState) -> Result<PaceState, IdentityError> {
    let new_state = match *state {
        PaceState::EmergencyActive => PaceState::ContingencyActive,
        PaceState::ContingencyActive => PaceState::AlternateActive,
        PaceState::AlternateActive => PaceState::Normal,
        PaceState::Normal => return Err(IdentityError::CannotDeescalate),
    };
    *state = new_state;
    Ok(new_state)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_did(label: &str) -> Did {
        Did::new(&format!("did:exo:{label}")).expect("valid did")
    }

    fn make_config() -> PaceConfig {
        PaceConfig {
            primary: make_did("primary"),
            alternates: vec![make_did("alt1"), make_did("alt2")],
            contingency: vec![make_did("cont1")],
            emergency: vec![make_did("emerg1")],
        }
    }

    #[test]
    fn validate_valid_config() {
        make_config().validate().unwrap();
    }

    #[test]
    fn validate_empty_alternates() {
        let mut config = make_config();
        config.alternates.clear();
        assert!(matches!(
            config.validate().unwrap_err(),
            IdentityError::InvalidPaceConfig(_)
        ));
    }

    #[test]
    fn validate_empty_contingency() {
        let mut config = make_config();
        config.contingency.clear();
        assert!(matches!(
            config.validate().unwrap_err(),
            IdentityError::InvalidPaceConfig(_)
        ));
    }

    #[test]
    fn validate_empty_emergency() {
        let mut config = make_config();
        config.emergency.clear();
        assert!(matches!(
            config.validate().unwrap_err(),
            IdentityError::InvalidPaceConfig(_)
        ));
    }

    #[test]
    fn validate_duplicate_across_levels() {
        let config = PaceConfig {
            primary: make_did("primary"),
            alternates: vec![make_did("alt1")],
            contingency: vec![make_did("primary")],
            emergency: vec![make_did("emerg1")],
        };
        assert!(matches!(
            config.validate().unwrap_err(),
            IdentityError::DuplicatePaceDid(_)
        ));
    }

    #[test]
    fn validate_duplicate_within_level() {
        let config = PaceConfig {
            primary: make_did("primary"),
            alternates: vec![make_did("alt1"), make_did("alt1")],
            contingency: vec![make_did("cont1")],
            emergency: vec![make_did("emerg1")],
        };
        assert!(matches!(
            config.validate().unwrap_err(),
            IdentityError::DuplicatePaceDid(_)
        ));
    }

    #[test]
    fn deserialize_rejects_oversized_pace_levels() {
        let alternates: Vec<String> = (0..=MAX_PACE_LEVEL_DIDS)
            .map(|idx| format!("did:exo:alt-{idx}"))
            .collect();
        let payload = serde_json::json!({
            "primary": "did:exo:primary",
            "alternates": alternates,
            "contingency": ["did:exo:contingency"],
            "emergency": ["did:exo:emergency"]
        });
        let json = serde_json::to_string(&payload).expect("PACE JSON encodes");

        let err = serde_json::from_str::<PaceConfig>(&json)
            .expect_err("oversized PACE level must be rejected during deserialization");

        assert!(
            err.to_string().contains("alternates"),
            "error should identify the oversized PACE level: {err}"
        );
    }

    #[test]
    fn deserialize_accepts_pace_levels_at_bound() {
        let alternates: Vec<String> = (0..MAX_PACE_LEVEL_DIDS)
            .map(|idx| format!("did:exo:alt-{idx}"))
            .collect();
        let payload = serde_json::json!({
            "primary": "did:exo:primary",
            "alternates": alternates,
            "contingency": ["did:exo:contingency"],
            "emergency": ["did:exo:emergency"]
        });
        let json = serde_json::to_string(&payload).expect("PACE JSON encodes");

        let config = serde_json::from_str::<PaceConfig>(&json)
            .expect("PACE levels at the configured bound must deserialize");

        assert_eq!(config.alternates.len(), MAX_PACE_LEVEL_DIDS);
        config
            .validate()
            .expect("bounded non-duplicated PACE config validates");
    }

    #[test]
    fn pace_config_debug_summarizes_operator_lists() {
        let config = make_config();

        let debug = format!("{config:?}");

        assert!(!debug.contains("did:exo:primary"));
        assert!(!debug.contains("did:exo:alt1"));
        assert!(debug.contains("alternate_count"));
        assert!(debug.contains("contingency_count"));
        assert!(debug.contains("emergency_count"));
    }

    #[test]
    fn resolve_operator_normal() {
        let config = make_config();
        assert_eq!(
            resolve_operator(&config, &PaceState::Normal).expect("valid PACE config"),
            &config.primary
        );
    }

    #[test]
    fn resolve_operator_alternate() {
        let config = make_config();
        assert_eq!(
            resolve_operator(&config, &PaceState::AlternateActive).expect("valid PACE config"),
            &config.alternates[0]
        );
    }

    #[test]
    fn resolve_operator_contingency() {
        let config = make_config();
        assert_eq!(
            resolve_operator(&config, &PaceState::ContingencyActive).expect("valid PACE config"),
            &config.contingency[0]
        );
    }

    #[test]
    fn resolve_operator_emergency() {
        let config = make_config();
        assert_eq!(
            resolve_operator(&config, &PaceState::EmergencyActive).expect("valid PACE config"),
            &config.emergency[0]
        );
    }

    #[test]
    fn resolve_operator_rejects_empty_pace_level_without_panicking() {
        let mut config = make_config();
        config.alternates.clear();

        let result =
            std::panic::catch_unwind(|| resolve_operator(&config, &PaceState::AlternateActive));

        assert!(
            result.is_ok(),
            "operator resolution must return a typed error for invalid PACE configs instead of panicking"
        );
        assert!(matches!(
            result.expect("operator resolution must not panic"),
            Err(IdentityError::InvalidPaceConfig(reason)) if reason.contains("alternates")
        ));
    }

    #[test]
    fn escalate_full_path() {
        let mut state = PaceState::Normal;
        assert_eq!(escalate(&mut state).unwrap(), PaceState::AlternateActive);
        assert_eq!(escalate(&mut state).unwrap(), PaceState::ContingencyActive);
        assert_eq!(escalate(&mut state).unwrap(), PaceState::EmergencyActive);
        assert!(matches!(
            escalate(&mut state).unwrap_err(),
            IdentityError::CannotEscalate
        ));
    }

    #[test]
    fn deescalate_full_path() {
        let mut state = PaceState::EmergencyActive;
        assert_eq!(
            deescalate(&mut state).unwrap(),
            PaceState::ContingencyActive
        );
        assert_eq!(deescalate(&mut state).unwrap(), PaceState::AlternateActive);
        assert_eq!(deescalate(&mut state).unwrap(), PaceState::Normal);
        assert!(matches!(
            deescalate(&mut state).unwrap_err(),
            IdentityError::CannotDeescalate
        ));
    }

    #[test]
    fn escalate_and_deescalate_roundtrip() {
        let mut state = PaceState::Normal;
        escalate(&mut state).unwrap();
        escalate(&mut state).unwrap();
        assert_eq!(state, PaceState::ContingencyActive);
        deescalate(&mut state).unwrap();
        assert_eq!(state, PaceState::AlternateActive);
        deescalate(&mut state).unwrap();
        assert_eq!(state, PaceState::Normal);
    }

    #[test]
    fn resolve_changes_with_escalation() {
        let config = make_config();
        let mut state = PaceState::Normal;

        assert_eq!(
            resolve_operator(&config, &state)
                .expect("valid PACE config")
                .as_str(),
            "did:exo:primary"
        );
        escalate(&mut state).unwrap();
        assert_eq!(
            resolve_operator(&config, &state)
                .expect("valid PACE config")
                .as_str(),
            "did:exo:alt1"
        );
        escalate(&mut state).unwrap();
        assert_eq!(
            resolve_operator(&config, &state)
                .expect("valid PACE config")
                .as_str(),
            "did:exo:cont1"
        );
        escalate(&mut state).unwrap();
        assert_eq!(
            resolve_operator(&config, &state)
                .expect("valid PACE config")
                .as_str(),
            "did:exo:emerg1"
        );
    }
}