exochain-avc 0.2.1-beta

EXOCHAIN Autonomous Volition Credential — portable signed credential for autonomous agent intent, authority, constraints, delegation, revocation, and trust receipts
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
// 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

//! Deterministic LiveSafe public-output AVC credential ceremony.
//!
//! The ceremony emits a signed, narrow AVC credential plus the JSON-shaped
//! registration and authorization material an operator can submit to the node.
//! It never generates signing keys, reads clocks, accepts bearer tokens, or
//! serializes raw evidence bytes into the output package.

use exo_authority::permission::Permission;
use exo_core::{Did, Hash256, Signature, Timestamp, hash::hash_structured};
use serde::{Deserialize, Serialize};

use crate::{
    AVC_SCHEMA_VERSION, AuthorityScope, AutonomousVolitionCredential, AutonomyLevel,
    AvcConstraints, AvcDraft, AvcSubjectKind, DataClass, DelegatedIntent, TimeWindow,
    error::AvcError,
    issue_avc,
    public_output_authorization::{
        LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_AUDIENCE,
        LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_DOMAIN,
        LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_SUBJECT,
        livesafe_public_adapter_output_authorization_idempotency_hash,
    },
};

/// The only AVC subject DID admitted by the LiveSafe public-output ceremony.
pub const LIVESAFE_PUBLIC_ADAPTER_OUTPUT_CREDENTIAL_SUBJECT_DID: &str =
    "did:exo:livesafe-public-adapter";
/// Domain for the deterministic ceremony intent hash.
pub const LIVESAFE_PUBLIC_OUTPUT_CREDENTIAL_CEREMONY_DOMAIN: &str =
    "livesafe.public_output_credential_ceremony.v1";

const CEREMONY_PURPOSE: &str =
    "Authorize narrow LiveSafe public adapter output for redacted public trust status";
const FORBIDDEN_CLAIM_FRAGMENTS: &[&str] = &[
    "custody",
    "consent",
    "emergency",
    "legal",
    "medical",
    "exochain.constitutional",
    "exochain.core",
];

/// Canonical LiveSafe evidence-summary hash supplied to the ceremony.
///
/// The hash is produced outside this Rust ceremony by the LiveSafe evidence
/// contract and is consumed here only as already-canonical bytes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LivesafePublicOutputCredentialCeremonyEvidence {
    pub sha256_hash: Hash256,
}

/// Deterministic inputs for the LiveSafe public-output credential ceremony.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LivesafePublicOutputCredentialCeremonyInput {
    pub issuer_did: Did,
    pub issuer_authority_scope: AuthorityScope,
    pub credential_subject_did: Did,
    pub public_subject: String,
    pub public_audience: String,
    pub allowed_claim_names: Vec<String>,
    pub evidence: LivesafePublicOutputCredentialCeremonyEvidence,
    pub not_before: Timestamp,
    pub expires_at: Timestamp,
    pub idempotency_key: String,
}

/// Node `/api/v1/avc/issue` request body emitted by the ceremony.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LivesafePublicOutputCredentialIssueRequest {
    pub credential: AutonomousVolitionCredential,
}

/// Node public-output authorization request material emitted by the ceremony.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LivesafePublicOutputAuthorizationRequestMaterial {
    #[serde(with = "sha256_hash_serde")]
    pub credential_id: Hash256,
    pub subject: String,
    pub audience: String,
    #[serde(with = "sha256_hash_serde")]
    pub evidence_hash: Hash256,
    pub idempotency_key: String,
    pub idempotency_key_hash: Hash256,
    pub expires_at: Timestamp,
}

/// Redacted ceremony output suitable for operator review and registration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LivesafePublicOutputCredentialCeremonyOutput {
    pub schema_version: u16,
    pub ceremony_domain: String,
    pub credential_id: Hash256,
    pub credential: AutonomousVolitionCredential,
    pub issue_request: LivesafePublicOutputCredentialIssueRequest,
    pub authorization_request: LivesafePublicOutputAuthorizationRequestMaterial,
    pub evidence_hash: Hash256,
    pub not_before: Timestamp,
    pub expires_at: Timestamp,
}

#[derive(Serialize)]
struct CeremonyIntentPayload<'a> {
    domain: &'static str,
    issuer_did: &'a Did,
    credential_subject_did: &'a Did,
    public_subject: &'a str,
    public_audience: &'a str,
    allowed_claim_names: &'a [String],
    evidence_hash: &'a Hash256,
    not_before: &'a Timestamp,
    expires_at: &'a Timestamp,
}

/// Borrowed inputs for the signed LiveSafe public-output ceremony intent hash.
pub(crate) struct LivesafePublicOutputCredentialCeremonyIntentInput<'a> {
    pub(crate) issuer_did: &'a Did,
    pub(crate) credential_subject_did: &'a Did,
    pub(crate) public_subject: &'a str,
    pub(crate) public_audience: &'a str,
    pub(crate) allowed_claim_names: &'a [String],
    pub(crate) evidence_hash: &'a Hash256,
    pub(crate) not_before: &'a Timestamp,
    pub(crate) expires_at: &'a Timestamp,
}

mod sha256_hash_serde {
    use exo_core::Hash256;
    use serde::{Deserialize, Deserializer, Serializer, de};

    pub fn serialize<S>(hash: &Hash256, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&format!("sha256:{hash}"))
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Hash256, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        super::parse_livesafe_public_output_evidence_sha256(&value).map_err(de::Error::custom)
    }
}

/// Parse a LiveSafe public-output evidence hash from exact `sha256:` lowercase
/// hex.
///
/// # Errors
/// Returns [`AvcError::InvalidInput`] unless the value is exactly
/// `sha256:<64 lowercase hex characters>`.
pub fn parse_livesafe_public_output_evidence_sha256(value: &str) -> Result<Hash256, AvcError> {
    const PREFIX: &str = "sha256:";
    let Some(hex) = value.strip_prefix(PREFIX) else {
        return Err(AvcError::InvalidInput {
            reason: "LiveSafe public-output evidence hash must be sha256:<64 lowercase hex>".into(),
        });
    };
    if hex.len() != 64
        || !hex.as_bytes().iter().all(u8::is_ascii_hexdigit)
        || hex.as_bytes().iter().any(u8::is_ascii_uppercase)
    {
        return Err(AvcError::InvalidInput {
            reason: "LiveSafe public-output evidence hash must be sha256:<64 lowercase hex>".into(),
        });
    }

    let mut bytes = [0u8; 32];
    for (index, chunk) in hex.as_bytes().chunks_exact(2).enumerate() {
        let high = hex_nibble(chunk[0])?;
        let low = hex_nibble(chunk[1])?;
        bytes[index] = (high << 4) | low;
    }
    Ok(Hash256::from_bytes(bytes))
}

/// Compute the signed ceremony intent id that binds a credential to LiveSafe's
/// canonical evidence hash and fixed public-output claim identity.
///
/// # Errors
/// Returns [`AvcError::Serialization`] if canonical intent hashing fails.
pub(crate) fn livesafe_public_output_credential_ceremony_intent_id(
    input: &LivesafePublicOutputCredentialCeremonyIntentInput<'_>,
) -> Result<Hash256, AvcError> {
    let mut normalized_allowed_claim_names = input.allowed_claim_names.to_vec();
    normalized_allowed_claim_names.sort();
    normalized_allowed_claim_names.dedup();
    hash_structured(&CeremonyIntentPayload {
        domain: LIVESAFE_PUBLIC_OUTPUT_CREDENTIAL_CEREMONY_DOMAIN,
        issuer_did: input.issuer_did,
        credential_subject_did: input.credential_subject_did,
        public_subject: input.public_subject,
        public_audience: input.public_audience,
        allowed_claim_names: &normalized_allowed_claim_names,
        evidence_hash: input.evidence_hash,
        not_before: input.not_before,
        expires_at: input.expires_at,
    })
    .map_err(AvcError::from)
}

/// Issue the narrow LiveSafe public-output AVC credential and redacted
/// registration package.
///
/// # Errors
/// Returns [`AvcError`] if any input widens beyond the public-output ceremony
/// contract or if AVC issuance fails.
pub fn issue_livesafe_public_output_credential_ceremony<F>(
    input: LivesafePublicOutputCredentialCeremonyInput,
    sign: F,
) -> Result<LivesafePublicOutputCredentialCeremonyOutput, AvcError>
where
    F: FnOnce(&[u8]) -> Signature,
{
    validate_input(&input)?;
    let evidence_hash = input.evidence.sha256_hash;
    let mut allowed_claim_names = input.allowed_claim_names.clone();
    allowed_claim_names.sort();
    allowed_claim_names.dedup();
    let intent_id = livesafe_public_output_credential_ceremony_intent_id(
        &LivesafePublicOutputCredentialCeremonyIntentInput {
            issuer_did: &input.issuer_did,
            credential_subject_did: &input.credential_subject_did,
            public_subject: &input.public_subject,
            public_audience: &input.public_audience,
            allowed_claim_names: &allowed_claim_names,
            evidence_hash: &evidence_hash,
            not_before: &input.not_before,
            expires_at: &input.expires_at,
        },
    )?;
    let jurisdiction = input
        .issuer_authority_scope
        .jurisdictions
        .first()
        .cloned()
        .ok_or(AvcError::InvalidInput {
            reason: "LiveSafe public-output issuer authority must declare a jurisdiction".into(),
        })?;
    let authority_scope = AuthorityScope {
        permissions: vec![Permission::Read],
        tools: vec![LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_DOMAIN.into()],
        data_classes: vec![DataClass::Public],
        counterparties: vec![],
        jurisdictions: vec![jurisdiction],
    };
    let draft = AvcDraft {
        schema_version: AVC_SCHEMA_VERSION,
        issuer_did: input.issuer_did.clone(),
        principal_did: input.issuer_did.clone(),
        subject_did: input.credential_subject_did,
        holder_did: None,
        subject_kind: AvcSubjectKind::Service {
            service_id: LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_SUBJECT.into(),
        },
        created_at: input.not_before,
        expires_at: Some(input.expires_at),
        delegated_intent: DelegatedIntent {
            intent_id,
            purpose: CEREMONY_PURPOSE.into(),
            allowed_objectives: allowed_claim_names,
            prohibited_objectives: forbidden_claims(),
            autonomy_level: AutonomyLevel::ExecuteWithinBounds,
            delegation_allowed: false,
        },
        authority_scope,
        constraints: AvcConstraints {
            allowed_time_window: Some(TimeWindow {
                not_before: input.not_before,
                not_after: input.expires_at,
            }),
            ..AvcConstraints::permissive()
        },
        authority_chain: None,
        consent_refs: vec![],
        policy_refs: vec![],
        parent_avc_id: None,
    };
    let credential = issue_avc(draft, sign)?;
    let credential_id = credential.id()?;
    let idempotency_key_hash =
        livesafe_public_adapter_output_authorization_idempotency_hash(&input.idempotency_key)?;
    let authorization_request = LivesafePublicOutputAuthorizationRequestMaterial {
        credential_id,
        subject: LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_SUBJECT.into(),
        audience: LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_AUDIENCE.into(),
        evidence_hash,
        idempotency_key: input.idempotency_key,
        idempotency_key_hash,
        expires_at: input.expires_at,
    };
    Ok(LivesafePublicOutputCredentialCeremonyOutput {
        schema_version: AVC_SCHEMA_VERSION,
        ceremony_domain: LIVESAFE_PUBLIC_OUTPUT_CREDENTIAL_CEREMONY_DOMAIN.into(),
        credential_id,
        credential: credential.clone(),
        issue_request: LivesafePublicOutputCredentialIssueRequest { credential },
        authorization_request,
        evidence_hash,
        not_before: input.not_before,
        expires_at: input.expires_at,
    })
}

fn validate_input(input: &LivesafePublicOutputCredentialCeremonyInput) -> Result<(), AvcError> {
    validate_issuer_authority(&input.issuer_authority_scope)?;
    validate_public_claims(input)?;
    if input.expires_at <= input.not_before {
        return Err(AvcError::InvalidTimestamp {
            reason: "LiveSafe public-output ceremony expires_at must be after not_before".into(),
        });
    }
    livesafe_public_adapter_output_authorization_idempotency_hash(&input.idempotency_key)?;
    Ok(())
}

fn validate_issuer_authority(scope: &AuthorityScope) -> Result<(), AvcError> {
    let mut permissions = scope.permissions.clone();
    permissions.sort();
    permissions.dedup();
    let mut tools = scope.tools.clone();
    tools.sort();
    tools.dedup();
    let mut data_classes = scope.data_classes.clone();
    data_classes.sort();
    data_classes.dedup();

    if permissions != vec![Permission::Read]
        || tools != vec![LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_DOMAIN.to_owned()]
        || data_classes != vec![DataClass::Public]
        || !scope.counterparties.is_empty()
    {
        return Err(AvcError::InvalidInput {
            reason: format!(
                "LiveSafe public-output issuer authority must be exactly Permission::Read, tool {}, DataClass::Public, and no counterparties",
                LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_DOMAIN
            ),
        });
    }
    if scope
        .jurisdictions
        .iter()
        .any(|jurisdiction| jurisdiction.trim().is_empty())
        || scope.jurisdictions.is_empty()
    {
        return Err(AvcError::InvalidInput {
            reason: "LiveSafe public-output issuer authority must declare a jurisdiction".into(),
        });
    }
    Ok(())
}

fn validate_public_claims(
    input: &LivesafePublicOutputCredentialCeremonyInput,
) -> Result<(), AvcError> {
    let expected_subject_did = Did::new(LIVESAFE_PUBLIC_ADAPTER_OUTPUT_CREDENTIAL_SUBJECT_DID)
        .map_err(|error| AvcError::InvalidInput {
            reason: format!("LiveSafe public-output credential subject DID is invalid: {error}"),
        })?;
    if input.credential_subject_did != expected_subject_did {
        return Err(AvcError::InvalidInput {
            reason: format!(
                "LiveSafe public-output credential subject DID must be exactly {}",
                LIVESAFE_PUBLIC_ADAPTER_OUTPUT_CREDENTIAL_SUBJECT_DID
            ),
        });
    }
    if input.public_subject != LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_SUBJECT {
        return Err(AvcError::InvalidInput {
            reason: format!(
                "LiveSafe public-output subject must be exactly {}",
                LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_SUBJECT
            ),
        });
    }
    if input.public_audience != LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_AUDIENCE {
        return Err(AvcError::InvalidInput {
            reason: format!(
                "LiveSafe public-output audience must be exactly {}",
                LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_AUDIENCE
            ),
        });
    }
    let mut claims = input.allowed_claim_names.clone();
    claims.sort();
    claims.dedup();
    if claims.is_empty() {
        return Err(AvcError::InvalidInput {
            reason: "LiveSafe public-output allowed claim names must not be empty".into(),
        });
    }
    for claim in &claims {
        if contains_forbidden_claim_fragment(claim) {
            return Err(AvcError::InvalidInput {
                reason: format!("LiveSafe public-output forbidden claim cap rejected: {claim}"),
            });
        }
    }
    if claims != vec![LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_DOMAIN.to_owned()] {
        return Err(AvcError::InvalidInput {
            reason: format!(
                "LiveSafe public-output allowed claim names must be exactly {}",
                LIVESAFE_PUBLIC_ADAPTER_OUTPUT_AUTHORIZATION_DOMAIN
            ),
        });
    }
    Ok(())
}

fn contains_forbidden_claim_fragment(claim: &str) -> bool {
    let lower = claim.to_ascii_lowercase();
    FORBIDDEN_CLAIM_FRAGMENTS
        .iter()
        .any(|fragment| lower.contains(fragment))
}

fn forbidden_claims() -> Vec<String> {
    FORBIDDEN_CLAIM_FRAGMENTS
        .iter()
        .map(|fragment| format!("forbid:{fragment}"))
        .collect()
}

fn hex_nibble(byte: u8) -> Result<u8, AvcError> {
    match byte {
        b'0'..=b'9' => Ok(byte - b'0'),
        b'a'..=b'f' => Ok(byte - b'a' + 10),
        _ => Err(AvcError::InvalidInput {
            reason: "LiveSafe public-output evidence hash must be sha256:<64 lowercase hex>".into(),
        }),
    }
}