# Outcome Receipt Spec, v0.1 (`outcome-receipt/0.1`)

A receipt is a signed JSON statement from a verifier: *"for these task terms and this delivered work, these checks ran with this evidence, and the verdict is pass/fail."* It is portable: anyone holding the receipt and the verifier's public key can check it offline, with no need to trust or contact the issuer.

## 1. Request (`POST /verify`, x402-gated)

```jsonc
{
  "terms":   { "job_type": "dataset_spec/v1", "spec": { "row_schema": {…}, "min_rows": 10, "max_rows": 1000,
               "unique_key": "id", "freshness": { "field": "updated_at", "max_age_seconds": 604800 } } },
  "work":    { "format": "json_rows", "rows": [ … ] },
  "payment_record": { "rail": "x402", "network": "eip155:84532", "tx_hash": "0x…", "amount": "5000000", "asset": "USDC", "payer": "0x…", "payee": "0x…" },   // optional: how the buyer paid the seller for the job
  "subject": { "buyer": { "agent_registry": "eip155:84532:0x…", "agent_id": 12 },
               "seller": { "agent_registry": "eip155:84532:0x…", "agent_id": 34 } }                         // optional ERC-8004 agent ids
}
```
Invalid input → `400`, and the x402 payment is **not settled** (the caller isn't charged).

## 2. Receipt schema

| Field | Type | Meaning |
|---|---|---|
| `receipt_version` | `"outcome-receipt/0.1"` | Format version |
| `receipt_id` | string `rcpt_…` | Unique id; `GET /receipt/:id` |
| `job_type` | string | e.g. `dataset_spec/v1` |
| `subject` | object \| null | Buyer and seller agent ids (ERC-8004 `agentRegistry` + `agentId`) |
| `terms_hash` | `sha256:<hex>` | SHA-256 of canonical JSON of `terms` |
| `work_hash` | `sha256:<hex>` | SHA-256 of canonical JSON of `work` |
| `payment.job_payment` | object \| null | `{record_hash, rail, reference, status}`. `status` is `attested_unverified` in v0.1 (we record it but don't check it on-chain yet) |
| `payment.verification_fee` | object | The x402 payment for this verification: `{rail, scheme, network, asset, amount, payer, authorization_nonce, settlement}` |
| `verdict` | `"pass"` \| `"fail"` | `pass` iff every check passed |
| `score` | 0 \| 100 | Same as the ERC-8004 `response` (binary for now) |
| `checks[]` | array | `{id, passed, expected, observed, examples?}`. Per-check evidence, up to 5 failing examples each |
| `verifier` | object | `{id, key_id, public_key (raw Ed25519, base64url), software}` |
| `issued_at` | RFC 3339 UTC | Also the reference time for freshness |
| `erc8004` | object | `{request_hash, request_uri, response, tag}`, see §4 |
| `signature` | object | `{alg: "Ed25519", key_id, canonicalization: "sorted-keys-json/v1", value (base64url)}` |

JSON Schema (abridged):
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["receipt_version","receipt_id","job_type","terms_hash","work_hash","payment","verdict","score","checks","verifier","issued_at","erc8004","signature"],
  "properties": {
    "receipt_version": { "const": "outcome-receipt/0.1" },
    "terms_hash": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "work_hash":  { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "verdict": { "enum": ["pass","fail"] },
    "score": { "type": "integer", "minimum": 0, "maximum": 100 },
    "checks": { "type": "array", "items": { "type": "object", "required": ["id","passed","expected","observed"] } },
    "issued_at": { "type": "string", "format": "date-time" },
    "erc8004": { "type": "object", "required": ["request_hash","response","tag"],
      "properties": { "request_hash": { "type": "string", "pattern": "^0x[0-9a-f]{64}$" } } },
    "signature": { "type": "object", "required": ["alg","key_id","value"], "properties": { "alg": { "const": "Ed25519" } } }
  }
}
```
Full real examples: `examples/demo-output/2-receipt-pass.json` and `3-receipt-fail.json`.

**Canonical JSON** (`sorted-keys-json/v1`): object keys sorted by code-unit order, no whitespace, standard `JSON.stringify` encoding for strings and numbers, `undefined` members dropped. Every hash and the signature use these bytes.

**Why Ed25519 and not EIP-712 (yet):** Ed25519 is small, fast, and verifiable in any language without an Ethereum stack. That's the right default for portable off-chain receipts. On-chain we're anchored anyway because the validator's EVM address must send `validationResponse` itself (§4). An EIP-712 typed-data signature over `{requestHash, response, responseHash, tag}` can be added as a second signature in v0.2 for contracts that want to check receipts directly.

## 3. How a third party verifies a receipt

1. Get the issuer's key from `https://<issuer>/.well-known/outcome-receipts.json` (or a pinned copy) and find the entry where `key_id == receipt.signature.key_id`. Also check that `receipt.verifier.public_key` equals that key.
2. Remove `signature` from the receipt, canonicalize the rest, and verify `signature.value` with Ed25519 over those UTF-8 bytes.
3. *(Optional, stronger)* If you have the original terms and work, recompute `sha256(canonical(terms))` and `sha256(canonical(work))` and compare them with `terms_hash` and `work_hash`. You can also re-run the deterministic checks with `issued_at` as the reference time and confirm you get the same verdict.
4. *(Optional, on-chain)* Look up `getValidationStatus(receipt.erc8004.request_hash)` in the ERC-8004 Validation Registry. Check that `validatorAddress` is the issuer's published address and that `responseHash == keccak256(canonical(full receipt))`.

Reference implementation: `node scripts/verify-receipt.ts <receipt.json|URL> [--issuer URL | --pubkey b64u] [--terms f] [--work f]` (exit code 0 = valid).

## 4. Mapping to ERC-8004 Validation Registry

Source: [ERC-8004: Trustless Agents](https://eips.ethereum.org/EIPS/eip-8004), Draft, "Validation Registry" section (read 2026-11-18). The registry exposes:
- `validationRequest(address validatorAddress, uint256 agentId, string requestURI, bytes32 requestHash)`, which **must be called by the owner/operator of `agentId`**. `requestURI` points to everything the validator needs. `requestHash` is the `keccak256` commitment to that payload.
- `validationResponse(bytes32 requestHash, uint8 response, string responseURI, bytes32 responseHash, string tag)`, which **must be called by `validatorAddress`**. `response` is 0–100 (binary use: 0 = fail, 100 = pass). `responseURI`, `responseHash`, and `tag` are optional. It can be called again for the same request (e.g. "soft" then "hard" finality, separated by `tag`).
- Reads: `getValidationStatus(requestHash)`, `getSummary(agentId, validatorAddresses[], tag)`, `getAgentValidations(agentId)`, `getValidatorRequests(validatorAddress)`.

| ERC-8004 field | Receipt source |
|---|---|
| `validatorAddress` | Our published EVM validator address (to be created at go-live; not in v0.1) |
| `agentId` | `subject.seller.agent_id`, the agent whose work was verified (the seller makes the request) |
| `requestURI` | `erc8004.request_uri` → `GET /request/:hash` returns `{terms, work, payment_record, subject}` |
| `requestHash` | `erc8004.request_hash` = `keccak256(canonical(request payload))`. Tests confirm it matches what `requestURI` serves |
| `response` | `erc8004.response` = `score` (100 pass / 0 fail) |
| `responseURI` | `https://<issuer>/receipt/<receipt_id>` |
| `responseHash` | `keccak256(canonical(full signed receipt))` |
| `tag` | `erc8004.tag` = `job_type` (e.g. `dataset_spec/v1`), so `getSummary(agentId, [us], "dataset_spec/v1")` gives a per-job-type pass rate |

`GET /receipt/:id/erc8004` returns these call arguments ready to submit. Flow: seller agent gets a receipt → calls `validationRequest(us, agentId, request_uri, request_hash)` → we call `validationResponse(...)` from our validator wallet. A receipt can also be cited as evidence in Reputation Registry feedback. ERC-8004 is still a Draft, so field names could change. The mapping lives in one function (`erc8004ResponseArgs`).

## 5. Known gaps in v0.1
- Job-payment records are hashed and stored, not checked on-chain.
- Key rotation and revocation are just the key list in `.well-known` (no revocation list yet).
- The receipt store is local files. Production should pin `request` and `receipt` JSON to IPFS so the URIs outlive us.
