{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.sorane.dev/schemas/delivery-protocol-v2.schema.json",
  "title": "Distributed Document Delivery v2 Protocol Data Models",
  "description": "Machine-readable schema for EndpointManifest (sorane.delivery.manifest.v2), DeliveryPayload, AliasBindingRecord, DeliveryThreadRecord, DeliveryThreadMessage, and cryptographic algorithm identifiers.",
  "$defs": {
    "DidJwk": {
      "type": "string",
      "pattern": "^did:jwk:[A-Za-z0-9_-]+$"
    },
    "KeyHash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$",
      "description": "Lowercase hex SHA-256 of canonical public JWK."
    },
    "EnvelopeString": {
      "type": "string",
      "pattern": "^(sdd1|spot1):[A-Za-z0-9_-]+$"
    },
    "EncryptionAlg": {
      "type": "string",
      "enum": ["X-Wing", "ECDH-ES+HKDF-256"]
    },
    "SigningAlg": {
      "type": "string",
      "enum": ["ES256", "SLH-DSA-SHA2-128f", "FN-DSA-512"]
    },
    "CoseAlgId": {
      "type": "integer",
      "enum": [-7, 3, -29, -65537, -65538, -65539],
      "description": "-7: ES256, 3: A256GCM, -29: ECDH-ES+HKDF-256, -65537: X-Wing, -65538: SLH-DSA-SHA2-128f, -65539: FN-DSA-512"
    },
    "RelayProvider": {
      "type": "string",
      "enum": [
        "cloudflare",
        "firebase",
        "github-drop",
        "gitlab-drop",
        "googledrive-drop",
        "govcloud",
        "custom"
      ]
    },
    "RelayEndpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": ["priority", "url", "provider"],
      "properties": {
        "priority": { "type": "integer", "minimum": 0 },
        "url": { "type": "string", "minLength": 1 },
        "provider": { "$ref": "#/$defs/RelayProvider" }
      }
    },
    "P256PublicJwk": {
      "type": "object",
      "required": ["kty", "crv", "x", "y"],
      "properties": {
        "kty": { "const": "EC" },
        "crv": { "const": "P-256" },
        "x": { "type": "string", "minLength": 1 },
        "y": { "type": "string", "minLength": 1 },
        "kid": { "type": "string" }
      }
    },
    "AkpPublicJwk": {
      "type": "object",
      "required": ["kty", "alg", "pub"],
      "properties": {
        "kty": { "const": "AKP" },
        "alg": { "enum": ["X-Wing", "SLH-DSA-SHA2-128f", "FN-DSA-512"] },
        "pub": { "type": "string", "minLength": 1 },
        "kid": { "type": "string" }
      }
    },
    "ManifestEncryptionKey": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "alg", "publicKeyJwk"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "alg": { "type": "string", "minLength": 1 },
        "status": { "enum": ["active", "retiring"] },
        "decrypt_only_until": { "type": "integer", "minimum": 0 },
        "holder_label": { "type": "string", "minLength": 1 },
        "role": { "enum": ["primary", "duty_officer", "escrow"] },
        "publicKeyJwk": { "type": "object" }
      }
    },
    "OrgOpenAuditEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema",
        "envelope_id",
        "doc_id",
        "recipient_did",
        "opened_by_did",
        "opened_by_kid",
        "opened_at",
        "proof"
      ],
      "properties": {
        "schema": { "const": "sorane.delivery.audit.v1" },
        "envelope_id": { "$ref": "#/$defs/KeyHash" },
        "doc_id": { "type": "string", "minLength": 1 },
        "recipient_did": { "$ref": "#/$defs/DidJwk" },
        "opened_by_did": { "$ref": "#/$defs/DidJwk" },
        "opened_by_kid": { "type": "string", "minLength": 1 },
        "opened_at": { "type": "integer", "minimum": 0 },
        "proof": { "type": "string", "minLength": 1 }
      }
    },
    "ManifestSigningKey": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "alg", "publicKeyJwk"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "alg": { "$ref": "#/$defs/SigningAlg" },
        "expires_at": { "type": "integer", "minimum": 0 },
        "publicKeyJwk": { "type": "object" }
      }
    },
    "KeySuccessionCert": {
      "type": "object",
      "additionalProperties": false,
      "required": ["old_did", "new_did", "issued_at", "old_sig", "new_sig"],
      "properties": {
        "old_did": { "$ref": "#/$defs/DidJwk" },
        "new_did": { "$ref": "#/$defs/DidJwk" },
        "issued_at": { "type": "integer", "minimum": 0 },
        "old_sig": { "type": "string", "minLength": 1 },
        "new_sig": { "type": "string", "minLength": 1 }
      }
    },
    "SubmissionPolicy": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "min_trust_level": { "$ref": "#/$defs/TrustLevel" },
        "pow_difficulty": { "type": "integer", "minimum": 0, "maximum": 32 },
        "quarantine_unknown_senders": { "type": "boolean" }
      }
    },
    "EndpointManifest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema",
        "subject_did",
        "seq",
        "updated_at",
        "expires_at",
        "encryption_keys",
        "relays",
        "proof"
      ],
      "properties": {
        "schema": { "const": "sorane.delivery.manifest.v2" },
        "subject_did": { "$ref": "#/$defs/DidJwk" },
        "seq": { "type": "integer", "minimum": 1 },
        "updated_at": { "type": "integer", "minimum": 0 },
        "expires_at": { "type": "integer", "minimum": 0 },
        "encryption_keys": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/ManifestEncryptionKey" }
        },
        "signing_keys": {
          "type": "array",
          "items": { "$ref": "#/$defs/ManifestSigningKey" }
        },
        "successor": { "$ref": "#/$defs/KeySuccessionCert" },
        "submission_policy": { "$ref": "#/$defs/SubmissionPolicy" },
        "relays": {
          "type": "array",
          "items": { "$ref": "#/$defs/RelayEndpoint" }
        },
        "proof": { "type": "string", "minLength": 1 }
      }
    },
    "BlobRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["blob_id", "plain_sha256", "blob_key", "blob_iv", "filename", "mime_type", "size"],
      "properties": {
        "blob_id": { "$ref": "#/$defs/KeyHash" },
        "blob_url": { "type": "string" },
        "plain_sha256": { "$ref": "#/$defs/KeyHash" },
        "blob_key": { "type": "string", "minLength": 1 },
        "blob_iv": { "type": "string", "minLength": 1 },
        "filename": { "type": "string", "minLength": 1 },
        "mime_type": { "type": "string", "minLength": 1 },
        "size": { "type": "integer", "minimum": 1 }
      }
    },
    "DeliveryPayload": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "type": "string", "minLength": 1 },
        "filename": { "type": "string" },
        "text": { "type": "string" },
        "body_bytes": { "type": "string" },
        "blob_ref": { "$ref": "#/$defs/BlobRef" }
      }
    },
    "AliasKey": {
      "type": "string",
      "oneOf": [
        { "pattern": "^github:[a-z0-9._-]+$" },
        { "pattern": "^gitlab:[a-z0-9.-]+:[a-z0-9._-]+$" },
        { "pattern": "^google:email:[^\\s@]+@[^\\s@]+\\.[^\\s@]+$" },
        { "pattern": "^google:hd:[a-z0-9.-]+$" },
        { "pattern": "^corp:jp:[0-9]{13}$" },
        { "pattern": "^person:blind:[a-f0-9]{64}$" },
        { "pattern": "^[a-z][a-z0-9_-]{1,31}:.+$" }
      ]
    },
    "TrustLevel": {
      "type": "string",
      "enum": ["vc_attested", "idp_verified", "self_asserted"]
    },
    "VcFormat": {
      "type": "string",
      "enum": ["sd-jwt", "mdoc", "domain-linkage", "corp-registry", "jwt_vc"]
    },
    "AliasAttestation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "verified_at"],
      "properties": {
        "kind": { "enum": ["git_oauth", "oidc", "vc", "self_asserted"] },
        "provider": { "type": "string" },
        "issuer": { "type": "string" },
        "subject": { "type": "string" },
        "format": { "$ref": "#/$defs/VcFormat" },
        "verified_at": { "type": "integer", "minimum": 0 },
        "credential_digest": { "type": "string" }
      }
    },
    "AliasBindingRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "alias_key",
        "did",
        "key_hash",
        "trust_level",
        "display_name",
        "attestation",
        "updated_at"
      ],
      "properties": {
        "alias_key": { "$ref": "#/$defs/AliasKey" },
        "did": { "$ref": "#/$defs/DidJwk" },
        "key_hash": { "$ref": "#/$defs/KeyHash" },
        "trust_level": { "$ref": "#/$defs/TrustLevel" },
        "display_name": { "type": "string", "minLength": 1 },
        "org_domain": { "type": "string" },
        "tenant_id": { "type": "string" },
        "attestation": { "$ref": "#/$defs/AliasAttestation" },
        "updated_at": { "type": "integer", "minimum": 0 }
      }
    },
    "DeliveryThreadStatus": {
      "type": "string",
      "enum": ["submitted", "needs_clarification", "escalated", "resubmitted", "accepted"]
    },
    "DeliveryThreadMessageKind": {
      "type": "string",
      "enum": ["submission", "validation_report", "clarification_request", "handoff", "acceptance"]
    },
    "DeliveryThreadRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": ["thread_id", "procedure", "participants", "status", "created_at", "updated_at"],
      "properties": {
        "thread_id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{4,128}$" },
        "procedure": { "type": "string", "minLength": 1 },
        "participants": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "items": { "$ref": "#/$defs/KeyHash" }
        },
        "status": { "$ref": "#/$defs/DeliveryThreadStatus" },
        "created_at": { "type": "integer", "minimum": 0 },
        "updated_at": { "type": "integer", "minimum": 0 }
      }
    },
    "DeliveryThreadMessage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["message_id", "thread_id", "sender_bucket", "kind", "envelope", "created_at"],
      "properties": {
        "message_id": { "type": "string", "minLength": 1 },
        "thread_id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{4,128}$" },
        "sender_bucket": { "$ref": "#/$defs/KeyHash" },
        "kind": { "$ref": "#/$defs/DeliveryThreadMessageKind" },
        "envelope": { "type": "string", "minLength": 1, "maxLength": 262144 },
        "created_at": { "type": "integer", "minimum": 0 }
      }
    }
  }
}
