{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://usedeed.app/contracts/deed.production_attestation_export.v1.schema.json",
  "title": "DEED production attestation export v1",
  "type": "object",
  "required": ["schema", "environment", "source", "synthetic", "exportedAt", "contractVersion", "emitterContract", "trace", "signers", "expectedSignerPublicKeySha256", "custodyBoundary", "exportSignature"],
  "properties": {
    "schema": { "const": "https://usedeed.app/contracts/deed.production_attestation_export.v1.schema.json" },
    "environment": { "const": "production" },
    "source": { "$ref": "#/$defs/source" },
    "synthetic": { "const": false },
    "exportedAt": { "type": "string", "format": "date-time" },
    "contractVersion": { "type": "string", "minLength": 1 },
    "emitterContract": { "$ref": "#/$defs/emitterContract" },
    "trace": {
      "type": "object",
      "required": ["sha256", "byteLength", "recordCount", "firstTs", "lastTs", "durationMs", "maxWindowMs", "continuity", "branchHistogram", "requiredBranches"],
      "properties": {
        "sha256": { "$ref": "#/$defs/sha256" },
        "byteLength": { "type": "integer", "minimum": 1 },
        "recordCount": { "type": "integer", "minimum": 1 },
        "firstTs": { "type": "string", "format": "date-time" },
        "lastTs": { "type": "string", "format": "date-time" },
        "durationMs": { "type": "integer", "minimum": 0, "maximum": 86400000 },
        "maxWindowMs": { "const": 86400000 },
        "continuity": {
          "type": "object",
          "required": ["stateHashAlgorithm", "adjacentStateEquality", "firstPrevStateSha256", "lastNextStateSha256"],
          "properties": {
            "stateHashAlgorithm": { "const": "sha256-canonical-frontier-state-v1" },
            "adjacentStateEquality": { "const": "record[i].prevState == record[i-1].nextState" },
            "firstPrevStateSha256": { "$ref": "#/$defs/sha256" },
            "lastNextStateSha256": { "$ref": "#/$defs/sha256" }
          },
          "additionalProperties": false
        },
        "branchHistogram": { "$ref": "#/$defs/branchHistogram" },
        "requiredBranches": { "$ref": "#/$defs/requiredBranches" }
      },
      "additionalProperties": false
    },
    "signers": { "$ref": "#/$defs/signers" },
    "expectedSignerPublicKeySha256": { "$ref": "#/$defs/hashList" },
    "custodyBoundary": { "type": "string", "minLength": 1 },
    "exportSignature": { "$ref": "#/$defs/signature" }
  },
  "$defs": {
    "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
    "source": {
      "type": "object",
      "required": ["type", "id", "deployment", "exporter"],
      "properties": {
        "type": { "const": "production-agent" },
        "id": { "type": "string", "minLength": 1 },
        "deployment": { "type": "string", "minLength": 1 },
        "exporter": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "emitterContract": {
      "type": "object",
      "required": ["path", "hashAlgorithm", "sha256"],
      "properties": {
        "path": { "const": "runtime-frontier-contract.js" },
        "hashAlgorithm": { "const": "sha256-source-v1" },
        "sha256": { "$ref": "#/$defs/sha256" }
      },
      "additionalProperties": false
    },
    "branchHistogram": {
      "type": "object",
      "required": ["observe", "constrain", "verify"],
      "properties": {
        "observe": { "type": "integer", "minimum": 0 },
        "constrain": { "type": "integer", "minimum": 0 },
        "verify": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "requiredBranches": { "const": ["observe", "constrain", "verify"] },
    "hashList": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/sha256" } },
    "signers": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["keyId", "publicKeySha256"],
        "properties": { "keyId": { "type": "string", "minLength": 1 }, "publicKeySha256": { "$ref": "#/$defs/sha256" } },
        "additionalProperties": false
      }
    },
    "signature": {
      "type": "object",
      "required": ["alg", "keyId", "publicKeyJwk", "value"],
      "properties": {
        "alg": { "const": "ECDSA-P256-SHA256" },
        "keyId": { "type": "string", "minLength": 1 },
        "publicKeyJwk": {
          "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 },
            "ext": { "type": "boolean" }, "key_ops": { "type": "array", "items": { "type": "string" } }, "kid": { "type": "string" }
          },
          "additionalProperties": false
        },
        "value": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
