# The buyer gate

> Compare the served bytes with the hash before anything is signed.

Source: https://lcp.integraledger.com/concepts/buyer-gate

Updated 2026-11-18. Documents @integraledger/lcp 0.6.3 and @integraledger/profile-facilitator 0.5.1. Every page of this site is listed in https://lcp.integraledger.com/llms.txt.

The **buyer gate** is the buyer-side check that compares the served bytes with H before anything is signed. Where the
payment carries H, it is the one step that makes paying mean agreeing: the buyer signs only a payment bound to a record
it has fetched, hashed and kept. Where the buyer's signature does not carry H, the pairing's record says so.

```mermaid
sequenceDiagram
  participant B as Buyer
  participant S as Seller
  B->>S: request the resource
  S-->>B: 402 challenge carrying H and the link
  B->>S: fetch the link
  S-->>B: the ATR's exact bytes
  Note over B: SHA-256 over the bytes received, compared with H
  alt the hashes differ, or the fetch fails
    Note over B: decline, and sign nothing
  else the hashes are equal
    Note over B: build the payment and sign it, on most pairings with H in the field it signs
    B->>S: the payment, carrying H where the pairing's claims is true
  end
```

## The steps

1. **Read.** The pairing's `read(doc)` gives H, the link, and the options this pairing can pay. A document without a
   well-formed hash and `https` link is refused before anything is fetched.
2. **Fetch.** Fetch the link and keep the bytes exactly as received. Bound the fetch: this package's shared vectors
   fix a limit of 1 MiB on the body, a 10-second deadline, and no redirects. Ask for the bytes as they are
   (`Accept-Encoding: identity`) and decline an answer in any other content coding, such as `gzip` or `br`: the hash is
   over the bytes served, never over bytes a decoder produced.
3. **Compare.** `hashEquals(await hash(bytes), h)`. On any difference, stop: nothing is signed.
4. **Build and sign.** The pairing's `build(choice, h)` returns what the signer signs, with H in its place. Pass the H
   you compared, never a value read again from the document.
5. **Finish.** `complete(signature)` gives the payment. Before sending it, check that `bound(payment)` equals the H you
   compared. Where the pairing's `data.coverage` is `none`, no public act the payer signs carries H, whatever `bound`
   returns, and the agreement payment is the proof.
6. **Keep the bytes.** The buyer's copy of the ATR is its record of what it agreed to.

## One changed byte

The comparison is over bytes, not meaning. Re-serialising the same JSON, or changing one byte, gives another hash:

```ts
import { hash, hashEquals } from "@integraledger/lcp";

const utf8 = new TextEncoder();
const served = utf8.encode('{"atrVersion":"1","id":"0f8fad5b-d9cb-469f-a165-70867728950e","terms":"10000 units"}');
const h = await hash(served);

const reformatted = utf8.encode(JSON.stringify(JSON.parse(new TextDecoder().decode(served)), null, 1));
const edited = served.slice();
edited[served.length - 3] = 0x31;

console.log(hashEquals(await hash(served), h));
console.log(hashEquals(await hash(reformatted), h));
console.log(hashEquals(await hash(edited), h));
```

```text
true
false
false
```

## The gate as a package

This package gives the gate's pieces: `read`, `hash`, `hashEquals`, `build` and `bound`. The
[buyer packages](https://agenticterms.integraledger.com/concepts) assemble them into one gate with a bounded fetch
and named declines, for TypeScript, Python and MCP clients. They follow the rows of
[`vectors/buyer.json`](https://lcp.integraledger.com/vectors/buyer.json), which this package ships.

The function names in this table (`transact`, `agree`, `confirm`, `within`, `finish`) are the buyer packages' gate
functions, not this package's; the ids in parentheses are the `name`s of the rows in `vectors/buyer.json` that fix each
case.

| Row outcome | When |
|---|---|
| `hash-mismatch` | The served bytes do not hash to H: one changed byte, the same JSON written another way, or an agreement resource advertising another ATR's hash. |
| `signed-not-bound` | What the signer returned does not carry the H the buyer compared. |
| `offer-unreadable` | The pairing's `read` refused the document, or its `build` refused the option with the buyer's inputs; the row carries the refusal code, and nothing is signed. |
| `link-not-https` | The link or the agreement URL that `read` gave is not an `https` link, and it is not fetched. Each pairing's own `read` refuses a link or agreement URL of another scheme, which declines `offer-unreadable` with the refusal's code. An agreement URL refused at the agreement step, by `transact` or by `agree` called with it alone, carries this package's code for the value as its detail: `x402/link-not-https` for a URL of another scheme, `x402/legal-context-malformed` for any other value (`BA5`). |
| `atr-unfetchable` | The fetch redirected, returned an error status, failed, did not answer in time, or answered in a content coding other than `identity`. |
| `atr-too-large` | The body is larger than 1 MiB. The fetch is cancelled. |
| `signer-failed` | The signer rejected. |
| `no-payable-option` | No option is payable by the buyer's accounts. The same decline, with the detail `<namespace>/input-malformed`, answers inputs the caller gave `confirm`, `agree` or `within` that are not an object, and a refund's own Solana value that is malformed (`B20`, `BA16`, `BW3`, `BW4`). Nothing is fetched or signed. |
| `pairing-not-supported` | The pairing id is not one the gate implements, or `finish` is handed a chosen payment of another pairing than its binding's (`B19`), before anything is rebuilt. |
| `agreement-pending` | An agreement payment for this ATR is settling, and the full payment is not signed: the gate made the agreement payment and its receipt did not arrive in time (`BA3`), or the agreement URL answered the unpaid request `202` (`BA14`), and nothing is signed. After that unpaid `202`, call again later without approval: once the agreement payment is recorded, the agreement URL answers its receipt. Never sign a second agreement payment. |
| `agreement-failed` | The agreement's receipt names another ATR's hash, an agreement answer is in a content coding other than `identity`, `PAYMENT-REQUIRED` is not strict base64, padded, in the standard alphabet and with its pad bits zero (`BA13`, `BA18`, `BA19`), or the JSON text of `PAYMENT-REQUIRED` or of the receipt is led by a byte-order mark (`BA17`, `BA18`); the full payment is not signed. |
| `agreement-not-offered` | The chosen pairing's payment is not a public proof of H (its `data.coverage` is `none`) and the offer names no agreement URL. The link is fetched and compared first; nothing is signed. |

## The agreement URL

Where the pairing's `data.coverage` is `none`, the seller advertises an **agreement URL** (`legalContextAgreementUrl`)
beside the link; the buyer gate declines such an offer without one. The buyer first pays that URL. That payment is
itself a public proof of H: it carries H or, on a pairing whose single-use value is H itself or H's low bits (the
EIP-3009 and Permit2 `nonce` among them), [the agreement's own value](https://lcp.integraledger.com/concepts/atr-hash.md#the-agreements-own-value),
`agreementValue(h)`, so the agreement and the full payment never share a single-use value. The pairing's
`build(choice, h, "agreement")` writes it, and `bound(presented, { act: "agreement", h })` checks it. The buyer pays
the full payment only after the agreement's `200` receipt names the same H. Where the chosen pairing's payment is
itself a public proof of H (`data.coverage` is `full`), the buyer does not pay the agreement URL.

The agreement payment is a payment like any other, so the buyer's agent approves it before anything is signed:

1. **Fetch** the agreement URL's `402` and check that it advertises the same H. A `202` to this unpaid request means an
   agreement payment for this ATR is settling: the gate declines `agreement-pending` and signs nothing (`BA14`).
2. **Choose** its option with the main payment's rule: the options in order, the first the signer can pay whose
   pairing's payment is itself a public proof of H.
3. **Hand the agent** that option's amount, token (`asset`), payee (`payTo`) and network, and sign only once it
   approves.
4. **Pay and wait** for the `200` receipt. The exchange is bounded by the chosen option's `maxTimeoutSeconds` plus
   180 seconds; `maxTimeoutSeconds` is a JSON number with an integral value, so `60.0` is `60`. After a `202` the gate
   sends the same payment again after `Retry-After`'s delay-seconds (RFC 9110 section 10.2.3) of 1 to 9 digits, at least
   1 second, and after 2 seconds when the header is absent or in any other form, an HTTP-date included (`BA15`).

The buyer vector rows `BA1` to `BA19` fix this exchange: its order, the approval and the bounds.

## What the gate checks, and what it does not

The gate compares the served bytes with H before anything is signed. It never parses the ATR.

- **The bytes it compared are the record.** Evaluate, and show a person, those bytes, never a second fetch of the
  link: `LCP §12.7` requires that "the document presented for human review is byte-identical to the document whose
  hash is signed".
- **The ATR's text is the seller's statement.** The gate acts on nothing a party slot says. An agent that reads the
  slots takes their text as the seller's statement, not as instructions to it: `LCP §12.7` says free-text prose in the
  terms body should not "drive policy decisions directly".
- **The binding slot is the seller's statement too.** The gate compares the bytes with H; it does not compare the
  options and request the binding slot records with the challenge it received.
- **The link is the seller's choice.** `isHttpsLink` accepts loopback, private and link-local hosts, such as
  `127.0.0.1`, `10.0.0.1`, `169.254.169.254`, `localhost` and `[::1]`. A buyer that fetches from inside a private
  network refuses such destinations itself.

## Next

- [Buyer](https://lcp.integraledger.com/guides/buyer.md): the gate in code, step by step.
- [Vectors](https://lcp.integraledger.com/concepts/vectors.md): running the shared rows.
