{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.bitcoinuniverse.dev/witc/v1/state-snapshot.schema.json",
  "title": "Witness Circles canonical state snapshot",
  "$comment": "Arrays use the canonical order defined by SPECIFICATION.md. The state hash is SHA-256 of the UTF-8 RFC 8785 serialization of this exact object.",
  "type": "object",
  "additionalProperties": false,
  "required": ["protocol", "version", "revision", "lineages", "shards", "circles", "edges"],
  "properties": {
    "protocol": { "const": "witc" },
    "version": { "const": 1 },
    "revision": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
    "lineages": {
      "type": "array",
      "items": { "$ref": "#/$defs/lineage" }
    },
    "shards": {
      "type": "array",
      "items": { "$ref": "#/$defs/shard" }
    },
    "circles": {
      "type": "array",
      "items": { "$ref": "#/$defs/circle" }
    },
    "edges": {
      "type": "array",
      "items": { "$ref": "#/$defs/edge" }
    }
  },
  "$defs": {
    "hex64": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "outpoint": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}:(?:0|[1-9][0-9]{0,8}|[1-3][0-9]{9}|4[01][0-9]{8}|42[0-8][0-9]{7}|429[0-3][0-9]{6}|4294[0-8][0-9]{5}|42949[0-5][0-9]{4}|429496[0-6][0-9]{3}|4294967[0-1][0-9]{2}|42949672[0-8][0-9]|429496729[0-5])$"
    },
    "sats": {
      "type": "string",
      "pattern": "^(?:0|[1-9][0-9]{0,14}|1[0-9]{15}|20[0-9]{14}|2100000000000000)$"
    },
    "lineage": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "lineageId",
        "genesisOutpoint",
        "currentOutpoint",
        "status",
        "firstHeight",
        "lastHeight",
        "circleCount",
        "closedByTxid"
      ],
      "properties": {
        "lineageId": { "$ref": "#/$defs/hex64" },
        "genesisOutpoint": { "$ref": "#/$defs/outpoint" },
        "currentOutpoint": {
          "oneOf": [{ "$ref": "#/$defs/outpoint" }, { "type": "null" }]
        },
        "status": { "enum": ["active", "closed"] },
        "firstHeight": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
        "lastHeight": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
        "circleCount": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
        "closedByTxid": {
          "oneOf": [{ "$ref": "#/$defs/hex64" }, { "type": "null" }]
        }
      }
    },
    "shard": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "outpoint",
        "lineageId",
        "scriptPubKey",
        "valueSats",
        "createdByCircle",
        "previousOutpoint",
        "createdHeight",
        "spentByTxid",
        "spentHeight"
      ],
      "properties": {
        "outpoint": { "$ref": "#/$defs/outpoint" },
        "lineageId": { "$ref": "#/$defs/hex64" },
        "scriptPubKey": { "type": "string", "pattern": "^5120[0-9a-f]{64}$" },
        "valueSats": { "$ref": "#/$defs/sats" },
        "createdByCircle": { "$ref": "#/$defs/hex64" },
        "previousOutpoint": { "$ref": "#/$defs/outpoint" },
        "createdHeight": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
        "spentByTxid": {
          "oneOf": [{ "$ref": "#/$defs/hex64" }, { "type": "null" }]
        },
        "spentHeight": {
          "oneOf": [
            { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
            { "type": "null" }
          ]
        }
      }
    },
    "member": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slot",
        "lineageId",
        "inputOutpoint",
        "outputOutpoint",
        "inputValueSats",
        "outputValueSats",
        "feeShareSats",
        "wasExistingLineage"
      ],
      "properties": {
        "slot": { "type": "integer", "minimum": 0, "maximum": 15 },
        "lineageId": { "$ref": "#/$defs/hex64" },
        "inputOutpoint": { "$ref": "#/$defs/outpoint" },
        "outputOutpoint": { "$ref": "#/$defs/outpoint" },
        "inputValueSats": { "$ref": "#/$defs/sats" },
        "outputValueSats": { "$ref": "#/$defs/sats" },
        "feeShareSats": { "$ref": "#/$defs/sats" },
        "wasExistingLineage": { "type": "boolean" }
      }
    },
    "circle": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "txid",
        "wtxid",
        "contextHash",
        "participantCount",
        "feeSats",
        "blockHeight",
        "blockHash",
        "transactionIndex",
        "members"
      ],
      "properties": {
        "txid": { "$ref": "#/$defs/hex64" },
        "wtxid": { "$ref": "#/$defs/hex64" },
        "contextHash": { "$ref": "#/$defs/hex64" },
        "participantCount": { "type": "integer", "minimum": 2, "maximum": 16 },
        "feeSats": { "$ref": "#/$defs/sats" },
        "blockHeight": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
        "blockHash": { "$ref": "#/$defs/hex64" },
        "transactionIndex": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        "members": {
          "type": "array",
          "minItems": 2,
          "maxItems": 16,
          "items": { "$ref": "#/$defs/member" }
        }
      }
    },
    "edge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["fromCircle", "toCircle", "lineageId", "viaOutpoint"],
      "properties": {
        "fromCircle": { "$ref": "#/$defs/hex64" },
        "toCircle": { "$ref": "#/$defs/hex64" },
        "lineageId": { "$ref": "#/$defs/hex64" },
        "viaOutpoint": { "$ref": "#/$defs/outpoint" }
      }
    }
  }
}
