{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:sitla:wire:1:schema:evidence-envelope",
  "title": "evidence-envelope",
  "$comment": "SPDX-License-Identifier: CC0-1.0; structural validation is not cryptographic verification",
  "type": "object",
  "properties": {
    "protocol": {
      "const": "sitla-wire/1"
    },
    "profile": {
      "const": "core-evidence-v1"
    },
    "operator_id": {
      "type": "string",
      "pattern": "^https://[^/?#]+(?:/[^?#]*)?$"
    },
    "request_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "request_digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "mode": {
      "enum": [
        "online",
        "disaster-offline"
      ]
    },
    "audience": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "purpose": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "challenge": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]+$",
      "minLength": 43,
      "maxLength": 86
    },
    "issued_at": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
    },
    "expires_at": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
    },
    "evidence_level": {
      "enum": [
        "service-json",
        "service-signed",
        "user-signed",
        "issuer-credential",
        "zkp"
      ]
    },
    "verified_by": {
      "type": "object",
      "properties": {
        "authority": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        },
        "method": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        },
        "implementation": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        }
      },
      "required": [
        "authority",
        "method",
        "implementation"
      ],
      "additionalProperties": false
    },
    "verification_result": {
      "enum": [
        "not_independently_verified",
        "valid",
        "invalid",
        "indeterminate"
      ]
    },
    "acceptance_decision": {
      "enum": [
        "not_evaluated",
        "accepted",
        "rejected"
      ]
    },
    "policy_digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "status_observations": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "authority": {
            "type": "string",
            "minLength": 1,
            "maxLength": 2048
          },
          "reference": {
            "type": "string",
            "minLength": 1,
            "maxLength": 2048
          },
          "state": {
            "enum": [
              "valid",
              "revoked",
              "unknown",
              "stale"
            ]
          },
          "status_as_of": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
              },
              {
                "type": "null"
              }
            ]
          },
          "expires_at": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
              },
              {
                "type": "null"
              }
            ]
          },
          "clock_confidence": {
            "enum": [
              "trusted",
              "bounded",
              "unknown"
            ]
          }
        },
        "required": [
          "authority",
          "reference",
          "state",
          "status_as_of",
          "expires_at",
          "clock_confidence"
        ],
        "additionalProperties": false
      },
      "maxItems": 64
    },
    "limitations": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 2048
      },
      "maxItems": 64
    },
    "evidence_digest": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "protocol",
    "profile",
    "operator_id",
    "request_id",
    "request_digest",
    "mode",
    "audience",
    "purpose",
    "challenge",
    "issued_at",
    "expires_at",
    "evidence_level",
    "verified_by",
    "verification_result",
    "acceptance_decision",
    "policy_digest",
    "status_observations",
    "limitations",
    "evidence_digest"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "evidence_level": {
            "const": "service-json"
          }
        }
      },
      "then": {
        "properties": {
          "verification_result": {
            "const": "not_independently_verified"
          },
          "evidence_digest": {
            "type": "null"
          }
        }
      },
      "else": {
        "properties": {
          "evidence_digest": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "verification_result": {
            "enum": [
              "valid",
              "invalid",
              "indeterminate"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "verification_result": {
            "const": "invalid"
          }
        }
      },
      "then": {
        "properties": {
          "acceptance_decision": {
            "enum": [
              "not_evaluated",
              "rejected"
            ]
          }
        }
      }
    }
  ]
}
