{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://theloopbreaker.com/schemas/v3-x402/receipt-status.v1.schema.json",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "schemaVersion",
    "protocolVersion",
    "receiptId",
    "requestId",
    "state",
    "routeId",
    "route",
    "resource",
    "method",
    "paymentRequirement",
    "bindingDigest",
    "createdAt",
    "updatedAt",
    "version",
    "auditIntegrity",
    "transactionEvidence",
    "evidenceLabels"
  ],
  "properties": {
    "schema": {
      "const": "vaultfire.v3.x402.receipt-status.v1"
    },
    "schemaVersion": {
      "const": "1.0.0"
    },
    "protocolVersion": {
      "const": "3.0.0"
    },
    "receiptId": {
      "type": "string",
      "pattern": "^x402r-[0-9a-f-]{36}$"
    },
    "requestId": {
      "type": "string",
      "pattern": "^v3r_[0-9a-f]{32}$"
    },
    "state": {
      "enum": [
        "claimed",
        "pending_confirmation",
        "confirmed",
        "finalized",
        "reorged",
        "expired",
        "failed"
      ]
    },
    "routeId": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{1,79}$"
    },
    "route": {
      "type": "string",
      "pattern": "^/api/v3/(?:[A-Za-z0-9._-]+|:[A-Za-z][A-Za-z0-9]*)(?:/(?:[A-Za-z0-9._-]+|:[A-Za-z][A-Za-z0-9]*))*$"
    },
    "resource": {
      "type": "string",
      "pattern": "^/api/v3/[A-Za-z0-9._/-]+$"
    },
    "method": {
      "enum": [
        "GET",
        "POST"
      ]
    },
    "paymentRequirement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "network",
        "chainId",
        "asset",
        "amount",
        "scheme",
        "payee",
        "requiredFinality",
        "minConfirmations"
      ],
      "properties": {
        "network": {
          "type": "string",
          "pattern": "^eip155:[1-9][0-9]{0,14}$"
        },
        "chainId": {
          "type": "integer",
          "minimum": 1
        },
        "asset": {
          "type": "string",
          "pattern": "^0x[0-9a-f]{40}$"
        },
        "amount": {
          "type": "string",
          "pattern": "^[1-9][0-9]*$"
        },
        "scheme": {
          "const": "exact"
        },
        "payee": {
          "type": "string",
          "pattern": "^0x[0-9a-f]{40}$"
        },
        "requiredFinality": {
          "enum": [
            "soft",
            "safe",
            "finalized"
          ]
        },
        "minConfirmations": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10000
        }
      }
    },
    "bindingDigest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "createdAt": {
      "type": "integer",
      "minimum": 0
    },
    "updatedAt": {
      "type": "integer",
      "minimum": 0
    },
    "version": {
      "type": "integer",
      "minimum": 1
    },
    "auditIntegrity": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "sequence",
        "previousDigest",
        "eventDigest",
        "verified",
        "historyVerifiedFromGenesis",
        "eventCount"
      ],
      "properties": {
        "sequence": {
          "type": "integer",
          "minimum": 1
        },
        "previousDigest": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "eventDigest": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "verified": {
          "const": true
        },
        "historyVerifiedFromGenesis": {
          "const": true
        },
        "eventCount": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "transactionEvidence": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "network",
            "chainId",
            "txHash",
            "blockNumber",
            "blockHash",
            "headBlockNumber",
            "observedFinality",
            "observedAt",
            "canonicalBlock"
          ],
          "properties": {
            "network": {
              "type": "string",
              "pattern": "^eip155:[1-9][0-9]{0,14}$"
            },
            "chainId": {
              "type": "integer",
              "minimum": 1
            },
            "txHash": {
              "type": "string",
              "pattern": "^0x[0-9a-f]{64}$"
            },
            "blockNumber": {
              "type": "integer",
              "minimum": 0
            },
            "blockHash": {
              "type": "string",
              "pattern": "^0x[0-9a-f]{64}$"
            },
            "headBlockNumber": {
              "type": "integer",
              "minimum": 0
            },
            "observedFinality": {
              "enum": [
                "soft",
                "safe",
                "finalized"
              ]
            },
            "observedAt": {
              "type": "integer",
              "minimum": 0
            },
            "canonicalBlock": {
              "const": true
            }
          }
        }
      ]
    },
    "evidenceLabels": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "receiptPersisted",
        "transactionObserved",
        "settlementVerified",
        "bazaarIndexingVerified"
      ],
      "properties": {
        "receiptPersisted": {
          "const": true
        },
        "transactionObserved": {
          "type": "boolean"
        },
        "settlementVerified": {
          "type": "boolean"
        },
        "bazaarIndexingVerified": {
          "const": false
        }
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "state": { "enum": ["confirmed", "finalized", "reorged"] } } },
      "then": {
        "properties": {
          "transactionEvidence": { "type": "object" },
          "evidenceLabels": {
            "properties": { "transactionObserved": { "const": true } }
          }
        }
      }
    },
    {
      "if": { "properties": { "state": { "enum": ["confirmed", "finalized"] } } },
      "then": {
        "properties": {
          "evidenceLabels": { "properties": { "settlementVerified": { "const": true } } }
        }
      }
    },
    {
      "if": { "properties": { "state": { "enum": ["claimed", "pending_confirmation", "reorged", "expired", "failed"] } } },
      "then": {
        "properties": {
          "evidenceLabels": { "properties": { "settlementVerified": { "const": false } } }
        }
      }
    }
  ]
}
