{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aiwg.io/schemas/effects/EffectKeyring.v1.schema.json",
  "title": "AIWG effect ledger keyring v1",
  "description": "Public Ed25519 ledger keys with validity windows, and key rotation records. Each rotation carries a signature by the prior key and by the successor key over the DSSE PAE of the rotation without its signatures member. Private keys never appear here. See docs/contracts/effect-ledger.v1.md.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "scope", "keys", "rotations"],
  "properties": {
    "schemaVersion": { "const": "aiwg.effect.keyring.v1" },
    "scope": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/scope" },
    "keys": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/key" }
    },
    "rotations": {
      "type": "array",
      "items": { "$ref": "#/$defs/rotation" }
    }
  },
  "$defs": {
    "key": {
      "type": "object",
      "additionalProperties": false,
      "required": ["keyid", "algorithm", "publicKey", "validFrom", "status"],
      "properties": {
        "keyid": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/keyid" },
        "algorithm": { "const": "ed25519" },
        "publicKey": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/ed25519PublicKey" },
        "validFrom": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/timestamp" },
        "validUntil": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/timestamp" },
        "status": { "enum": ["active", "retired", "revoked"] },
        "revokedAt": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/timestamp" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "revoked" } } },
          "then": { "required": ["revokedAt"], "properties": { "revokedAt": true } },
          "else": { "properties": { "revokedAt": false } }
        },
        {
          "if": { "properties": { "status": { "const": "retired" } } },
          "then": { "required": ["validUntil"], "properties": { "validUntil": true } }
        }
      ]
    },
    "rotationSignature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["role", "keyid", "sig"],
      "properties": {
        "role": { "enum": ["prior", "successor"] },
        "keyid": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/keyid" },
        "sig": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/ed25519Signature" }
      }
    },
    "rotation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "sequence", "from", "to", "effectiveAt", "reason", "signatures"],
      "properties": {
        "schemaVersion": { "const": "aiwg.effect.key-rotation.v1" },
        "sequence": { "type": "integer", "minimum": 1 },
        "from": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/keyid" },
        "to": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/keyid" },
        "effectiveAt": { "$ref": "https://aiwg.io/schemas/effects/EffectRecord.v1.schema.json#/$defs/timestamp" },
        "reason": { "enum": ["scheduled", "custody-change", "compromise"] },
        "signatures": {
          "type": "array",
          "minItems": 2,
          "maxItems": 2,
          "items": { "$ref": "#/$defs/rotationSignature" },
          "allOf": [
            {
              "contains": {
                "type": "object",
                "required": ["role"],
                "properties": { "role": { "const": "prior" } }
              },
              "minContains": 1,
              "maxContains": 1
            },
            {
              "contains": {
                "type": "object",
                "required": ["role"],
                "properties": { "role": { "const": "successor" } }
              },
              "minContains": 1,
              "maxContains": 1
            }
          ]
        }
      }
    }
  }
}
