{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://makewine.app/spec/provenance/v1/bundle.schema.json",
  "title": "MakeWine provenance verification bundle, merkle-v1 (DRAFT 1.0.0-draft)",
  "description": "The JSON file a visitor downloads from a signed bottle page (SPEC.md section 10.1). Hidden leaves carry their digest only. certificateLeaves (the certificate sub-tree) is present exactly when the certificates leaf has its text; that rule and the sub-tree size are checked by the steps of SPEC.md section 11, not by this schema. A schema check is not a verification.",
  "type": "object",
  "required": ["kind", "format", "specVersion", "publicToken", "wineId", "signerUserId", "signedAt", "signing",
    "keyId", "publicKeyPem", "previousRoot", "root", "message", "signature", "leaves"],
  "additionalProperties": false,
  "properties": {
    "kind": { "const": "verification" },
    "format": { "$ref": "#/$defs/format" },
    "specVersion": { "$ref": "#/$defs/specVersion" },
    "publicToken": { "$ref": "#/$defs/publicToken" },
    "wineId": { "$ref": "#/$defs/decimalId" },
    "signerUserId": { "$ref": "#/$defs/decimalId" },
    "signedAt": { "$ref": "#/$defs/signedAt" },
    "signing": { "$ref": "#/$defs/signing" },
    "keyId": { "$ref": "#/$defs/hex64" },
    "publicKeyPem": { "$ref": "#/$defs/publicKeyPem" },
    "previousRoot": { "$ref": "#/$defs/previousRoot" },
    "root": { "$ref": "#/$defs/hex64" },
    "message": { "$ref": "#/$defs/message" },
    "signature": { "$ref": "#/$defs/signature" },
    "leaves": { "$ref": "#/$defs/leaves" },
    "certificateLeaves": { "$ref": "#/$defs/certificateLeaves" }
  },
  "$defs": {
    "format": { "const": "merkle-v1" },
    "specVersion": { "type": "string", "pattern": "^1\\.[0-9]+\\.[0-9]+(-[0-9A-Za-z.-]+)?$" },
    "publicToken": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,128}$" },
    "decimalId": { "type": "string", "pattern": "^[1-9][0-9]{0,18}$" },
    "signedAt": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$" },
    "signing": { "type": "integer", "minimum": 1 },
    "hex64": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
    "previousRoot": { "type": "string", "pattern": "^([0-9a-f]{64})?$" },
    "signature": { "type": "string", "pattern": "^[0-9a-f]{128}$" },
    "publicKeyPem": { "type": "string", "pattern": "^-----BEGIN PUBLIC KEY-----\\n[A-Za-z0-9+/=\\n]+-----END PUBLIC KEY-----\\n?$" },
    "message": { "type": "string", "pattern": "^mw-prov\\|merkle-v1\\|" },
    "leafName": {
      "enum": ["core", "bottling", "bottling.lotNumber", "vineyard", "fermentation", "fermentation.gravity",
        "fermentation.abv", "fermentation.lotNumber", "fermentation.notes", "cellar", "cellar.gravity", "cellar.abv",
        "cellar.lotNumber", "additions", "additions.technical", "officialTasting", "nutrition", "certificates"]
    },
    "certificateLeafName": { "type": "string", "pattern": "^certificates\\[(0|[1-9][0-9]*)\\]$" },
    "leaf": {
      "type": "object",
      "required": ["index", "name", "digest"],
      "additionalProperties": false,
      "properties": {
        "index": { "type": "integer", "minimum": 0, "maximum": 17 },
        "name": { "$ref": "#/$defs/leafName" },
        "digest": { "$ref": "#/$defs/hex64" },
        "text": { "type": "string", "pattern": "^leaf:s:[0-9]+:" }
      }
    },
    "leaves": { "type": "array", "minItems": 18, "maxItems": 18, "items": { "$ref": "#/$defs/leaf" } },
    "certificateLeaf": {
      "type": "object",
      "required": ["index", "name", "digest"],
      "additionalProperties": false,
      "properties": {
        "index": { "type": "integer", "minimum": 0 },
        "name": { "$ref": "#/$defs/certificateLeafName" },
        "digest": { "$ref": "#/$defs/hex64" },
        "text": { "type": "string", "pattern": "^leaf:s:[0-9]+:certificates\\[" }
      }
    },
    "certificateLeaves": { "type": "array", "minItems": 8, "items": { "$ref": "#/$defs/certificateLeaf" } },
    "revealedName": { "anyOf": [{ "$ref": "#/$defs/leafName" }, { "$ref": "#/$defs/certificateLeafName" }] }
  }
}
