oas-document 1.1.1

OAS Identity Document types and validation for the Open Agent Specification
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
// OAS Spec §5 — OAS Identity Document
//! The core [`OasDocument`] type representing an OAS Identity Document.
//!
//! An OAS Identity Document is a W3C DID Document extended with OAS-specific
//! properties: lineage, capabilities, conformance level, and metadata.

use serde::{Deserialize, Serialize};

use crate::calendar::CalendarSection;
use crate::compliance::ComplianceSection;
use crate::conformance::ConformanceLevel;
use crate::contact::ContactSection;
use crate::error::DocumentError;
use crate::governance::GovernanceSection;
use crate::interop::InteroperabilitySection;
use crate::lifecycle::LifecycleStatus;
use crate::lineage_section::LineageSection;
use crate::operational::OperationalSection;
use crate::pricing::PricingSection;
use crate::profile::ProfileSection;
use crate::proof_format::DocumentProof;
use crate::relationships::RelationshipsSection;
use crate::reputation::ReputationSection;
use crate::service::ServiceEndpoint;
use crate::verification_method::VerificationMethod;

/// The OAS JSON-LD context URI.
pub const OAS_CONTEXT: &str = "https://openagent.id/ns/oas/v1";

/// The W3C DID context URI.
pub const DID_CONTEXT: &str = "https://www.w3.org/ns/did/v1";

/// The Ed25519 2020 suite context URI.
pub const ED25519_CONTEXT: &str = "https://w3id.org/security/suites/ed25519-2020/v1";

/// The current OAS specification version.
pub const OAS_VERSION: &str = "1.0.0";

/// Metadata section of an OAS Identity Document.
///
/// # Examples
///
/// ```
/// use oas_document::document::DocumentMetadata;
///
/// let meta = DocumentMetadata {
///     created: "2026-01-15T00:00:00Z".to_string(),
///     updated: None,
///     version: None,
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentMetadata {
    /// ISO 8601 creation timestamp (required).
    pub created: String,

    /// ISO 8601 last update timestamp (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated: Option<String>,

    /// Semantic version of this entity's definition (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

/// An OAS Identity Document as defined in OAS Specification §5.
///
/// This is the canonical representation of an autonomous entity's identity,
/// extending the W3C DID Document with OAS-specific properties.
///
/// # Examples
///
/// ```
/// use oas_document::OasDocument;
///
/// let json = r#"{
///   "@context": ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1", "https://openagent.id/ns/oas/v1"],
///   "id": "did:oas:test:hmr:alice",
///   "controller": "did:oas:test:hmr:alice",
///   "verificationMethod": [{"id": "did:oas:test:hmr:alice#key-1", "type": "Ed25519VerificationKey2020", "controller": "did:oas:test:hmr:alice", "publicKeyMultibase": "zTest"}],
///   "authentication": ["did:oas:test:hmr:alice#key-1"],
///   "oasVersion": "1.0.0",
///   "kind": "hmr",
///   "conformanceLevel": "L1",
///   "sequence": 1,
///   "metadata": {"created": "2026-01-15T00:00:00Z"}
/// }"#;
/// let doc: OasDocument = serde_json::from_str(json).unwrap();
/// assert_eq!(doc.id, "did:oas:test:hmr:alice");
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OasDocument {
    /// JSON-LD contexts. MUST include the OAS context.
    #[serde(rename = "@context")]
    pub context: Vec<String>,

    /// The `did:oas` identifier.
    pub id: String,

    /// DID of the controlling entity.
    pub controller: String,

    /// At least one Ed25519VerificationKey2020.
    pub verification_method: Vec<VerificationMethod>,

    /// References to keys authorized for authentication.
    pub authentication: Vec<String>,

    /// References to keys authorized for assertions.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub assertion_method: Vec<String>,

    /// References to keys authorized for capability invocation.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub capability_invocation: Vec<String>,

    /// References to keys authorized for capability delegation.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub capability_delegation: Vec<String>,

    /// Service endpoints.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub service: Vec<ServiceEndpoint>,

    /// OAS specification version (currently `"1.0.0"`).
    pub oas_version: String,

    /// Entity kind from the taxonomy.
    pub kind: String,

    /// Human-readable name (max 256 chars).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Human-readable description (max 4096 chars).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Lineage information (required for L1+ non-root entities).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lineage: Option<LineageSection>,

    /// Governance section (ENR entities only).
    /// Tracks which MHR governs this enterprise identity, epoch, and transition history.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub governance: Option<GovernanceSection>,

    /// Conformance level: L0, L1, or L2.
    pub conformance_level: ConformanceLevel,

    /// Lifecycle status.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lifecycle_status: Option<LifecycleStatus>,

    /// Monotonically increasing version counter (starting at 1).
    pub sequence: u64,

    /// Document metadata.
    pub metadata: DocumentMetadata,

    /// Document proof (Ed25519Signature2020).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proof: Option<DocumentProof>,

    /// Whether this identity has been revoked.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revoked: Option<bool>,

    /// Timestamp of revocation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revoked_at: Option<String>,

    // ── Extended Identity Sections (OAS §5.8–§5.16) ──────────────────
    /// Profile / presentation data (§5.8): avatar, tagline, categories.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profile: Option<ProfileSection>,

    /// Contact directory (§5.9): phones, emails, socials, websites.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contact: Option<ContactSection>,

    /// Calendar & scheduling (§5.10): booking links, office hours.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub calendar: Option<CalendarSection>,

    /// Operational specification (§5.11): SLA, rate limits, formats.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub operational: Option<OperationalSection>,

    /// Pricing & economics (§5.12): cost model, billing, free tier.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pricing: Option<PricingSection>,

    /// Interoperability (§5.13): protocols, API schemas, auth methods.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interoperability: Option<InteroperabilitySection>,

    /// Reputation summary (§5.14): interactions, endorsements, uptime.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reputation: Option<ReputationSection>,

    /// Compliance & jurisdiction (§5.15): certifications, policy URLs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub compliance: Option<ComplianceSection>,

    /// Relationships & affiliations (§5.16): org structure, partnerships.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub relationships: Option<RelationshipsSection>,
}

impl OasDocument {
    /// Returns the default JSON-LD context array for OAS documents.
    pub fn default_context() -> Vec<String> {
        vec![
            DID_CONTEXT.to_string(),
            ED25519_CONTEXT.to_string(),
            OAS_CONTEXT.to_string(),
        ]
    }

    /// Returns true if this document represents a root entity (HMR, MHR, or ENR).
    pub fn is_root(&self) -> bool {
        self.kind == "hmr" || self.kind == "mhr" || self.kind == "enr"
    }

    /// Returns true if this document has been revoked.
    pub fn is_revoked(&self) -> bool {
        self.revoked.unwrap_or(false)
    }

    /// Computes the canonical BLAKE3 commitment for this complete document.
    ///
    /// The digest covers the RFC 8785 canonical JSON representation, including
    /// the document proof and all status, relationship, key, and lineage fields.
    /// Lineage proofs bind this value to the authenticated parent state used at
    /// issuance time.
    ///
    /// # Returns
    ///
    /// A lowercase `blake3:`-prefixed digest.
    ///
    /// # Errors
    ///
    /// Returns [`DocumentError::Crypto`] if canonicalization fails.
    pub fn canonical_digest(&self) -> Result<String, DocumentError> {
        let value = serde_json::to_value(self)?;
        let canonical = oas_crypto::jcs::canonicalize(&value)?;
        Ok(format!(
            "blake3:{}",
            oas_crypto::hashing::blake3_hash_hex(&canonical)
        ))
    }

    /// Returns the primary public key multibase string, if available.
    ///
    /// Looks for the first verification method's public key.
    pub fn primary_public_key_multibase(&self) -> Option<&str> {
        self.verification_method
            .first()
            .map(|vm| vm.public_key_multibase.as_str())
    }

    /// Finds a verification method by its full ID or fragment.
    pub fn find_verification_method(&self, id_or_fragment: &str) -> Option<&VerificationMethod> {
        self.verification_method.iter().find(|vm| {
            vm.id == id_or_fragment
                || vm.key_id() == id_or_fragment
                || vm.id.ends_with(&format!("#{id_or_fragment}"))
        })
    }
}

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

    fn make_minimal_doc(kind: &str) -> OasDocument {
        OasDocument {
            context: OasDocument::default_context(),
            id: format!("did:oas:test:{kind}:test-1"),
            controller: format!("did:oas:test:{kind}:test-1"),
            verification_method: vec![VerificationMethod::new(
                &format!("did:oas:test:{kind}:test-1"),
                1,
                "zTestPubKey",
            )],
            authentication: vec![format!("did:oas:test:{kind}:test-1#key-1")],
            assertion_method: vec![],
            capability_invocation: vec![],
            capability_delegation: vec![],
            service: vec![],
            oas_version: OAS_VERSION.to_string(),
            kind: kind.to_string(),
            name: None,
            description: None,
            lineage: None,
            governance: None,
            conformance_level: ConformanceLevel::L0,
            lifecycle_status: None,
            sequence: 1,
            metadata: DocumentMetadata {
                created: "2026-01-15T00:00:00Z".to_string(),
                updated: None,
                version: None,
            },
            proof: None,
            revoked: None,
            revoked_at: None,
            profile: None,
            contact: None,
            calendar: None,
            operational: None,
            pricing: None,
            interoperability: None,
            reputation: None,
            compliance: None,
            relationships: None,
        }
    }

    #[test]
    fn is_root_for_hmr() {
        let doc = make_minimal_doc("hmr");
        assert!(doc.is_root());
    }

    #[test]
    fn is_root_for_mhr() {
        let doc = make_minimal_doc("mhr");
        assert!(doc.is_root());
    }

    #[test]
    fn is_root_for_enr() {
        let doc = make_minimal_doc("enr");
        assert!(doc.is_root());
    }

    #[test]
    fn is_not_root_for_agent() {
        let doc = make_minimal_doc("agent");
        assert!(!doc.is_root());
    }

    #[test]
    fn default_context_has_three_uris() {
        let ctx = OasDocument::default_context();
        assert_eq!(ctx.len(), 3);
        assert!(ctx.contains(&DID_CONTEXT.to_string()));
        assert!(ctx.contains(&OAS_CONTEXT.to_string()));
    }

    #[test]
    fn find_verification_method_by_fragment() {
        let doc = make_minimal_doc("hmr");
        let vm = doc.find_verification_method("key-1");
        assert!(vm.is_some());
    }

    #[test]
    fn is_revoked_default_false() {
        let doc = make_minimal_doc("hmr");
        assert!(!doc.is_revoked());
    }

    #[test]
    fn canonical_digest_is_deterministic_and_state_binding() {
        let doc = make_minimal_doc("hmr");
        let first = doc.canonical_digest().unwrap_or_default();
        let second = doc.canonical_digest().unwrap_or_default();
        assert_eq!(first, second);
        assert!(first.starts_with("blake3:") && first.len() == 71);

        let mut changed = doc;
        changed.sequence += 1;
        assert_ne!(first, changed.canonical_digest().unwrap_or_default());
    }

    #[test]
    fn serde_round_trip() {
        let doc = make_minimal_doc("agent");
        let json = serde_json::to_string_pretty(&doc).unwrap_or_default();
        let parsed: Result<OasDocument, _> = serde_json::from_str(&json);
        assert!(parsed.is_ok());
    }

    #[test]
    fn backward_compat_no_extended_sections() {
        // Documents created before §5.8–§5.16 (no profile, contact, etc.)
        // must deserialize with all new fields as None.
        let json = r#"{
          "@context": ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1", "https://openagent.id/ns/oas/v1"],
          "id": "did:oas:test:agent:legacy",
          "controller": "did:oas:test:hmr:alice",
          "verificationMethod": [{"id": "did:oas:test:agent:legacy#key-1", "type": "Ed25519VerificationKey2020", "controller": "did:oas:test:hmr:alice", "publicKeyMultibase": "zTest"}],
          "authentication": ["did:oas:test:agent:legacy#key-1"],
          "oasVersion": "1.0.0",
          "kind": "agent",
          "conformanceLevel": "L0",
          "sequence": 1,
          "metadata": {"created": "2026-01-15T00:00:00Z"}
        }"#;
        let doc: Result<OasDocument, _> = serde_json::from_str(json);
        assert!(
            doc.is_ok(),
            "Legacy document without extended sections must deserialize"
        );
        let doc = doc.unwrap_or_else(|_| make_minimal_doc("agent"));
        assert!(doc.profile.is_none());
        assert!(doc.contact.is_none());
        assert!(doc.calendar.is_none());
        assert!(doc.operational.is_none());
        assert!(doc.pricing.is_none());
        assert!(doc.interoperability.is_none());
        assert!(doc.reputation.is_none());
        assert!(doc.compliance.is_none());
        assert!(doc.relationships.is_none());
    }

    #[test]
    fn extended_sections_omitted_when_none() {
        let doc = make_minimal_doc("agent");
        let json = serde_json::to_string(&doc).unwrap_or_default();
        // None sections should not appear in serialized output
        assert!(!json.contains("profile"));
        assert!(!json.contains("contact"));
        assert!(!json.contains("calendar"));
        assert!(!json.contains("operational"));
        assert!(!json.contains("pricing"));
        assert!(!json.contains("interoperability"));
        assert!(!json.contains("reputation"));
        assert!(!json.contains("compliance"));
        assert!(!json.contains("relationships"));
    }
}