{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://emissar.ai/specs/schemas/receipt-v0.1.schema.json",
  "title": "Emissar Receipt v0.1 document",
  "description": "Emissar Receipt v0.1, a draft for comment. The root schema validates one receipt document (https://emissar.ai/specs/receipt/v0.1 section 5.1). Other objects are under $defs: ProtectedHeader (a decoded signature header, section 7.2), Openings (5.9), Refusal (7.6), CardExtension and RequestMetadata (10.1-10.2), ArtifactMetadata (10.4), and the Retrieval API bodies (section 11). The schema checks shape only; signatures, digests, chains and proofs are checked by the algorithm in section 12.",
  "$ref": "#/$defs/ReceiptDocument",
  "$defs": {
    "Timestamp": {
      "description": "UTC instant with exactly millisecond precision (section 5.8). Compare instants, not strings.",
      "type": "string",
      "format": "date-time",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
    },
    "Digest": {
      "description": "RFC 6920 ni URI without authority. Allowed algorithms in v0.1: sha-256 (MUST), sha-384, sha-512. Truncated suites such as sha-256-128 are not allowed (section 6.2).",
      "type": "string",
      "pattern": "^ni:///(sha-256;[A-Za-z0-9_-]{43}|sha-384;[A-Za-z0-9_-]{64}|sha-512;[A-Za-z0-9_-]{86})$"
    },
    "DigestAlg": { "enum": ["sha-256", "sha-384", "sha-512"] },
    "Base64url": { "type": "string", "pattern": "^[A-Za-z0-9_-]+$" },
    "Hash256": { "description": "Base64url SHA-256 output (43 characters).", "type": "string", "pattern": "^[A-Za-z0-9_-]{43}$" },
    "Salt": { "description": "Base64url of at least 16 random octets (section 6.4).", "type": "string", "pattern": "^[A-Za-z0-9_-]{22,}$" },
    "Uuid": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" },
    "HttpsUrl": { "type": "string", "format": "uri", "pattern": "^https://", "maxLength": 2048 },
    "Id": { "description": "An A2A identifier (taskId, contextId, messageId, artifactId) as the protocol carried it.", "type": "string", "minLength": 1, "maxLength": 256 },
    "Kid": { "type": "string", "minLength": 1, "maxLength": 256 },
    "Role": { "enum": ["server", "client"] },
    "TerminalState": { "enum": ["TASK_STATE_COMPLETED", "TASK_STATE_FAILED", "TASK_STATE_CANCELED", "TASK_STATE_REJECTED"] },

    "SigningKey": {
      "description": "The key a party signs receipts with (section 5.4). jkt is the RFC 7638 SHA-256 thumbprint and is authoritative; kid selects the key; jku is a hint subject to the verifier's location policy.",
      "type": "object",
      "properties": {
        "kid": { "$ref": "#/$defs/Kid" },
        "jkt": { "$ref": "#/$defs/Hash256" },
        "jku": { "$ref": "#/$defs/HttpsUrl" }
      },
      "required": ["kid", "jkt"],
      "additionalProperties": false
    },
    "AgentCardRef": {
      "type": "object",
      "properties": {
        "url": { "$ref": "#/$defs/HttpsUrl" },
        "digest": { "$ref": "#/$defs/Digest", "description": "Digest of the RFC 8785 form of the card as served, signatures included (section 5.4)." }
      },
      "required": ["url"],
      "additionalProperties": false
    },
    "Principal": {
      "description": "Who the agent acts for (section 5.4). Organizations are named; people are only ever referenced by a salted commitment.",
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "type": { "const": "organization" },
            "id": { "type": "string", "minLength": 1, "maxLength": 256, "description": "A domain name or another public organization identifier." }
          },
          "required": ["type", "id"],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": { "const": "person" },
            "commitment": { "$ref": "#/$defs/Digest" }
          },
          "required": ["type", "commitment"],
          "additionalProperties": false
        }
      ]
    },
    "Authentication": {
      "description": "How the server authenticated the client for this task (section 5.4).",
      "type": "object",
      "properties": {
        "scheme": { "type": "string", "minLength": 1, "maxLength": 128, "description": "Name of the entry in the server Agent Card's securitySchemes that was used." },
        "subject": { "type": "string", "minLength": 1, "maxLength": 256, "description": "The authenticated client identity (for example an OAuth client_id or a certificate thumbprint). Never a person's identifier." }
      },
      "required": ["scheme", "subject"],
      "additionalProperties": false
    },
    "ServerParty": {
      "type": "object",
      "properties": {
        "agentCard": { "$ref": "#/$defs/AgentCardRef" },
        "signingKey": { "$ref": "#/$defs/SigningKey" },
        "principal": { "$ref": "#/$defs/Principal" }
      },
      "required": ["agentCard", "signingKey"],
      "additionalProperties": false
    },
    "ClientParty": {
      "type": "object",
      "properties": {
        "agentCard": { "$ref": "#/$defs/AgentCardRef" },
        "authentication": { "$ref": "#/$defs/Authentication" },
        "signingKey": { "$ref": "#/$defs/SigningKey" },
        "principal": { "$ref": "#/$defs/Principal" }
      },
      "required": ["authentication"],
      "additionalProperties": false
    },
    "TaskRef": {
      "type": "object",
      "properties": {
        "endpoint": { "$ref": "#/$defs/HttpsUrl", "description": "The A2A interface URL (supportedInterfaces[].url) that handled the task." },
        "taskId": { "$ref": "#/$defs/Id" },
        "contextId": { "$ref": "#/$defs/Id" }
      },
      "required": ["endpoint", "taskId"],
      "additionalProperties": false
    },
    "MandateRef": {
      "type": "object",
      "properties": {
        "type": { "type": "string", "pattern": "^[a-z0-9][a-z0-9.+-]{0,63}$", "description": "emissar-mandate, ap2-checkout-mandate, ap2-payment-mandate, verifiable-intent, or another registered value (section 5.5)." },
        "digest": { "$ref": "#/$defs/Digest" },
        "id": { "type": "string", "minLength": 1, "maxLength": 256 }
      },
      "required": ["type", "digest"],
      "additionalProperties": false
    },
    "MessageCommitment": {
      "type": "object",
      "properties": { "messageId": { "$ref": "#/$defs/Id" }, "digest": { "$ref": "#/$defs/Digest" } },
      "required": ["messageId", "digest"],
      "additionalProperties": false
    },
    "ArtifactCommitment": {
      "type": "object",
      "properties": { "artifactId": { "$ref": "#/$defs/Id" }, "digest": { "$ref": "#/$defs/Digest" } },
      "required": ["artifactId", "digest"],
      "additionalProperties": false
    },
    "Request": {
      "type": "object",
      "properties": {
        "messages": { "type": "array", "items": { "$ref": "#/$defs/MessageCommitment" }, "minItems": 1, "maxItems": 1000 }
      },
      "required": ["messages"],
      "additionalProperties": false
    },
    "Outcome": {
      "type": "object",
      "properties": {
        "state": { "$ref": "#/$defs/TerminalState" },
        "statusMessage": { "$ref": "#/$defs/MessageCommitment" },
        "artifacts": { "type": "array", "items": { "$ref": "#/$defs/ArtifactCommitment" }, "maxItems": 1000 }
      },
      "required": ["state", "artifacts"],
      "additionalProperties": false
    },
    "Payment": {
      "type": "object",
      "properties": {
        "protocol": { "enum": ["ap2", "x402", "other"] },
        "reference": { "type": "string", "minLength": 1, "maxLength": 256, "description": "The payment's identifier in its own protocol (AP2 payment_id, x402 transaction)." },
        "receiptDigest": { "$ref": "#/$defs/Digest", "description": "Digest of that protocol's own payment receipt (section 5.7)." },
        "amount": {
          "type": "object",
          "properties": {
            "value": { "type": "string", "pattern": "^-?(0|[1-9][0-9]{0,17})(\\.[0-9]{1,8})?$" },
            "currency": { "type": "string", "pattern": "^[A-Z]{3}$" }
          },
          "required": ["value", "currency"],
          "additionalProperties": false
        }
      },
      "required": ["protocol", "reference"],
      "additionalProperties": false
    },
    "Timestamps": {
      "type": "object",
      "properties": {
        "acceptedAt": { "$ref": "#/$defs/Timestamp" },
        "terminalAt": { "$ref": "#/$defs/Timestamp" },
        "issuedAt": { "$ref": "#/$defs/Timestamp" },
        "countersignBy": { "$ref": "#/$defs/Timestamp" }
      },
      "required": ["acceptedAt", "terminalAt", "issuedAt", "countersignBy"],
      "additionalProperties": false
    },
    "Receipt": {
      "description": "The signed payload (section 5.2). Every signature covers UTF-8(JCS(receipt)).",
      "type": "object",
      "properties": {
        "version": { "const": "0.1" },
        "receiptId": { "$ref": "#/$defs/Uuid" },
        "digestAlg": { "$ref": "#/$defs/DigestAlg" },
        "task": { "$ref": "#/$defs/TaskRef" },
        "parties": {
          "type": "object",
          "properties": { "server": { "$ref": "#/$defs/ServerParty" }, "client": { "$ref": "#/$defs/ClientParty" } },
          "required": ["server", "client"],
          "additionalProperties": false
        },
        "mandates": { "type": "array", "items": { "$ref": "#/$defs/MandateRef" }, "minItems": 1, "maxItems": 16 },
        "request": { "$ref": "#/$defs/Request" },
        "outcome": { "$ref": "#/$defs/Outcome" },
        "payments": { "type": "array", "items": { "$ref": "#/$defs/Payment" }, "minItems": 1, "maxItems": 16 },
        "timestamps": { "$ref": "#/$defs/Timestamps" }
      },
      "required": ["version", "receiptId", "digestAlg", "task", "parties", "request", "outcome", "timestamps"],
      "additionalProperties": false
    },
    "Signature": {
      "description": "One entry of a JWS general JSON serialization (RFC 7515 section 7.2.1) with the unprotected header omitted (section 7.1).",
      "type": "object",
      "properties": { "protected": { "$ref": "#/$defs/Base64url" }, "signature": { "$ref": "#/$defs/Base64url" } },
      "required": ["protected", "signature"],
      "additionalProperties": false
    },
    "ProtectedHeader": {
      "description": "A decoded receipt signature header (section 7.2).",
      "type": "object",
      "properties": {
        "alg": { "enum": ["ES256", "ES384", "Ed25519"] },
        "kid": { "$ref": "#/$defs/Kid" },
        "typ": { "const": "emissar-receipt+json" },
        "jku": { "$ref": "#/$defs/HttpsUrl" },
        "crit": { "const": ["https://emissar.ai/ext/receipt/v1"] },
        "https://emissar.ai/ext/receipt/v1": {
          "type": "object",
          "properties": {
            "role": { "$ref": "#/$defs/Role" },
            "seq": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
            "prev": { "$ref": "#/$defs/Digest" },
            "signedAt": { "$ref": "#/$defs/Timestamp" }
          },
          "required": ["role", "seq", "prev", "signedAt"],
          "additionalProperties": false
        }
      },
      "required": ["alg", "kid", "typ", "crit", "https://emissar.ai/ext/receipt/v1"],
      "additionalProperties": false
    },
    "TreeHead": {
      "type": "object",
      "properties": {
        "log": { "$ref": "#/$defs/HttpsUrl" },
        "treeSize": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
        "rootHash": { "$ref": "#/$defs/Hash256" },
        "timestamp": { "$ref": "#/$defs/Timestamp" }
      },
      "required": ["log", "treeSize", "rootHash", "timestamp"],
      "additionalProperties": false
    },
    "Anchor": {
      "description": "Evidence that the receipt digest is in a transparency log (section 9). Anchors are not covered by the party signatures; each one verifies on its own.",
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "type": { "const": "rfc9162-sha256" },
            "log": { "$ref": "#/$defs/HttpsUrl" },
            "leafIndex": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
            "inclusionPath": { "type": "array", "items": { "$ref": "#/$defs/Hash256" }, "maxItems": 64 },
            "treeHead": { "$ref": "#/$defs/TreeHead" },
            "treeHeadSignature": { "$ref": "#/$defs/Signature" }
          },
          "required": ["type", "log", "leafIndex", "inclusionPath", "treeHead", "treeHeadSignature"],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": { "const": "scitt-cose-receipt" },
            "service": { "$ref": "#/$defs/HttpsUrl" },
            "statement": { "$ref": "#/$defs/Base64url", "description": "The COSE_Sign1 Signed Statement (RFC 9995 hash envelope over the receipt digest)." },
            "receipt": { "$ref": "#/$defs/Base64url", "description": "The COSE Receipt (RFC 9942) returned by the Transparency Service." }
          },
          "required": ["type", "service", "statement", "receipt"],
          "additionalProperties": false
        }
      ]
    },
    "ReceiptDocument": {
      "description": "What parties store, exchange and retrieve (section 5.1). A JWS general JSON serialization with a detached payload (RFC 7515 Appendix F): the payload is rebuilt from `receipt`.",
      "type": "object",
      "properties": {
        "receipt": { "$ref": "#/$defs/Receipt" },
        "signatures": { "type": "array", "items": { "$ref": "#/$defs/Signature" }, "minItems": 1, "maxItems": 2 },
        "anchors": { "type": "array", "items": { "$ref": "#/$defs/Anchor" }, "maxItems": 8 }
      },
      "required": ["receipt", "signatures"],
      "additionalProperties": false
    },
    "Openings": {
      "description": "Salts that open the receipt's commitments (section 5.9). Parties only; never in the receipt document, never in a log.",
      "type": "object",
      "properties": {
        "receiptId": { "$ref": "#/$defs/Uuid" },
        "request": { "type": "object", "additionalProperties": { "$ref": "#/$defs/Salt" }, "minProperties": 1 },
        "outcome": {
          "type": "object",
          "properties": {
            "statusMessage": { "$ref": "#/$defs/Salt" },
            "artifacts": { "type": "object", "additionalProperties": { "$ref": "#/$defs/Salt" } }
          },
          "required": ["artifacts"],
          "additionalProperties": false
        },
        "principals": {
          "type": "object",
          "properties": { "server": { "$ref": "#/$defs/Salt" }, "client": { "$ref": "#/$defs/Salt" } },
          "additionalProperties": false
        }
      },
      "required": ["receiptId", "request", "outcome"],
      "additionalProperties": false
    },
    "Refusal": {
      "description": "A client's signed statement that it will not countersign (section 7.6). Signed like a receipt, over UTF-8(JCS(refusal)), with typ emissar-receipt-refusal+json and no chain parameter.",
      "type": "object",
      "properties": {
        "refusal": {
          "type": "object",
          "properties": {
            "receiptId": { "$ref": "#/$defs/Uuid" },
            "receiptDigest": { "$ref": "#/$defs/Digest" },
            "reason": { "enum": ["request_mismatch", "outcome_mismatch", "identity_mismatch", "timestamp_out_of_range", "unknown_task", "other"] },
            "detail": { "type": "string", "maxLength": 1024 },
            "refusedAt": { "$ref": "#/$defs/Timestamp" }
          },
          "required": ["receiptId", "receiptDigest", "reason", "refusedAt"],
          "additionalProperties": false
        },
        "signatures": { "type": "array", "items": { "$ref": "#/$defs/Signature" }, "minItems": 1, "maxItems": 1 }
      },
      "required": ["refusal", "signatures"],
      "additionalProperties": false
    },
    "CardExtensionParams": {
      "type": "object",
      "properties": {
        "receiptApi": { "$ref": "#/$defs/HttpsUrl", "description": "Base URL of the Retrieval API (section 11)." },
        "jku": { "$ref": "#/$defs/HttpsUrl", "description": "JWK Set holding current and retired receipt-signing keys." },
        "algs": { "type": "array", "items": { "enum": ["ES256", "ES384", "Ed25519"] }, "minItems": 1, "uniqueItems": true },
        "digestAlgs": { "type": "array", "items": { "$ref": "#/$defs/DigestAlg" }, "minItems": 1, "uniqueItems": true },
        "countersignWindowSeconds": { "type": "integer", "minimum": 60, "maximum": 2592000 },
        "logs": { "type": "array", "items": { "$ref": "#/$defs/HttpsUrl" }, "uniqueItems": true }
      },
      "required": ["receiptApi", "algs", "digestAlgs", "countersignWindowSeconds"],
      "additionalProperties": false
    },
    "CardExtension": {
      "description": "The AgentExtension entry in the server's Agent Card (section 10.1).",
      "type": "object",
      "properties": {
        "uri": { "const": "https://emissar.ai/ext/receipt/v1" },
        "description": { "type": "string" },
        "required": { "type": "boolean" },
        "params": { "$ref": "#/$defs/CardExtensionParams" }
      },
      "required": ["uri", "params"],
      "additionalProperties": false
    },
    "RequestMetadata": {
      "description": "The value of message.metadata[\"https://emissar.ai/ext/receipt/v1\"] on a client message (section 10.2).",
      "type": "object",
      "properties": {
        "signingKey": { "$ref": "#/$defs/SigningKey" },
        "agentCard": { "$ref": "#/$defs/HttpsUrl" },
        "principal": { "$ref": "#/$defs/Principal" },
        "countersign": { "type": "boolean" }
      },
      "required": ["countersign"],
      "additionalProperties": false
    },
    "ArtifactMetadata": {
      "description": "The value of artifact.metadata[\"https://emissar.ai/ext/receipt/v1\"] on the receipt artifact (section 10.4).",
      "type": "object",
      "properties": { "receiptId": { "$ref": "#/$defs/Uuid" }, "receiptDigest": { "$ref": "#/$defs/Digest" } },
      "required": ["receiptId", "receiptDigest"],
      "additionalProperties": false
    },
    "ReceiptSummary": {
      "type": "object",
      "properties": {
        "receiptId": { "$ref": "#/$defs/Uuid" },
        "receiptDigest": { "$ref": "#/$defs/Digest" },
        "taskId": { "$ref": "#/$defs/Id" },
        "contextId": { "$ref": "#/$defs/Id" },
        "state": { "$ref": "#/$defs/TerminalState" },
        "issuedAt": { "$ref": "#/$defs/Timestamp" },
        "status": { "enum": ["issued", "countersigned", "refused", "expired"] }
      },
      "required": ["receiptId", "receiptDigest", "taskId", "state", "issuedAt", "status"],
      "additionalProperties": false
    },
    "ReceiptList": {
      "type": "object",
      "properties": {
        "receipts": { "type": "array", "items": { "$ref": "#/$defs/ReceiptSummary" }, "maxItems": 100 },
        "next": { "type": "string", "minLength": 1, "maxLength": 512, "description": "Opaque cursor; absent on the last page." }
      },
      "required": ["receipts"],
      "additionalProperties": false
    },
    "ChainEntry": {
      "description": "One position in a signing key's chain (section 8). Survives deletion of the receipt document (section 13).",
      "type": "object",
      "properties": {
        "kid": { "$ref": "#/$defs/Kid" },
        "jkt": { "$ref": "#/$defs/Hash256" },
        "seq": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
        "prev": { "$ref": "#/$defs/Digest" },
        "head": { "$ref": "#/$defs/Digest" },
        "receiptId": { "$ref": "#/$defs/Uuid" },
        "receiptDigest": { "$ref": "#/$defs/Digest" },
        "signedAt": { "$ref": "#/$defs/Timestamp" },
        "documentDeleted": { "type": "boolean" }
      },
      "required": ["kid", "jkt", "seq", "prev", "head", "receiptId", "receiptDigest", "signedAt"],
      "additionalProperties": false
    },
    "Problem": {
      "description": "RFC 9457 problem details.",
      "type": "object",
      "properties": {
        "type": { "type": "string", "format": "uri" },
        "title": { "type": "string" },
        "status": { "type": "integer", "minimum": 400, "maximum": 599 },
        "detail": { "type": "string" },
        "instance": { "type": "string" }
      },
      "required": ["type", "title", "status"]
    }
  }
}
