{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "ddibb inbox · message v1/v2/v3",
  "type": "object",
  "additionalProperties": false,
  "required": ["v", "message_id", "conversation_id", "kind", "sender", "recipient", "recipient_keyset_version", "in_reply_to", "jwe", "sender_signature"],
  "properties": {
    "v": { "enum": [1, 2, 3] },
    "delivery_address": {
      "type": "string", "maxLength": 317,
      "pattern": "^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)?@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\\+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|(?:\\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+)?$",
      "not": { "anyOf": [
        { "pattern": "^@[^+]*\\." }, { "pattern": "@[0-9.]+\\.[0-9.]+$" },
        { "pattern": "@.{254}" },
        { "pattern": "^(?![0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}@)[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}@" },
        { "pattern": "^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}@[^+]*\\." }
      ] }
    },
    "membership_id": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/uuid" }] },
    "message_id": { "$ref": "#/$defs/uuid" },
    "conversation_id": { "$ref": "#/$defs/uuid" },
    "kind": { "enum": ["request", "reply"] },
    "sender": { "$ref": "#/$defs/principal" },
    "recipient": { "$ref": "#/$defs/principal" },
    "recipient_keyset_version": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
    "in_reply_to": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/uuid" }] },
    "jwe": { "$ref": "#/$defs/jwe" },
    "sender_signature": {
      "type": "string",
      "description": "Detached Compact JWS (ES256): 보호 헤더·빈 페이로드 구간·64바이트 서명. 발신 계정의 서명키로 JCS 봉투(sender_signature 제외)를 별도 검증한다.",
      "pattern": "^[A-Za-z0-9_-]+\\.\\.[A-Za-z0-9_-]{86}$"
    }
  },
  "oneOf": [
    {
      "properties": { "v": { "const": 1 }, "delivery_address": false, "membership_id": false },
      "if": { "properties": { "kind": { "const": "request" } } },
      "then": { "properties": { "recipient": { "$ref": "#/$defs/agent" }, "in_reply_to": { "type": "null" } } },
      "else": { "properties": { "sender": { "$ref": "#/$defs/agent" }, "in_reply_to": { "$ref": "#/$defs/uuid" } } }
    },
    {
      "required": ["delivery_address", "membership_id"],
      "properties": { "v": { "const": 2 }, "kind": { "const": "request" }, "in_reply_to": { "type": "null" } },
      "allOf": [
        { "if": { "properties": { "delivery_address": { "pattern": "^@" } } },
          "then": { "properties": { "recipient": { "pattern": "^account:" } } },
          "else": { "properties": { "recipient": { "$ref": "#/$defs/agent" } } } },
        { "if": { "properties": { "delivery_address": { "pattern": "\\+" } } },
          "then": { "properties": { "membership_id": { "$ref": "#/$defs/uuid" } } },
          "else": { "properties": { "membership_id": { "type": "null" } } } }
      ]
    },
    {
      "properties": {
        "v": { "const": 3 }, "kind": { "const": "reply" }, "sender": { "pattern": "^account:" },
        "in_reply_to": { "$ref": "#/$defs/uuid" }, "delivery_address": false, "membership_id": false
      }
    }
  ],
  "$defs": {
    "uuid": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" },
    "principal": { "type": "string", "pattern": "^(account|agent):[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" },
    "agent": { "type": "string", "pattern": "^agent:" },
    "b64u": { "type": "string", "pattern": "^[A-Za-z0-9_-]+$" },
    "word256": { "type": "string", "pattern": "^[A-Za-z0-9_-]{43}$" },
    "epk": {
      "type": "object", "additionalProperties": false, "required": ["kty", "crv", "x", "y"],
      "properties": { "kty": { "const": "EC" }, "crv": { "const": "P-256" }, "x": { "$ref": "#/$defs/word256" }, "y": { "$ref": "#/$defs/word256" } }
    },
    "wrap": {
      "type": "object", "additionalProperties": false, "required": ["header", "encrypted_key"],
      "properties": {
        "header": { "type": "object", "additionalProperties": false, "required": ["kid"], "properties": { "kid": { "$ref": "#/$defs/word256" }, "epk": { "$ref": "#/$defs/epk" } } },
        "encrypted_key": { "type": "string", "pattern": "^[A-Za-z0-9_-]{54}$" }
      }
    },
    "jwe": {
      "type": "object", "additionalProperties": false,
      "required": ["protected", "recipients", "aad", "iv", "ciphertext", "tag"],
      "properties": {
        "protected": { "$ref": "#/$defs/b64u" }, "aad": { "$ref": "#/$defs/b64u" }, "ciphertext": { "$ref": "#/$defs/b64u" },
        "iv": { "type": "string", "pattern": "^[A-Za-z0-9_-]{16}$" }, "tag": { "type": "string", "pattern": "^[A-Za-z0-9_-]{22}$" },
        "recipients": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/wrap" } }
      }
    }
  }
}
