Skip to content
Integra Protocol

Seller

Assemble the ATR, advertise its hash, check the payment, and read the settlement.

For AI agents: this page as Markdown, and llms.txt, the index of every page.

The seller is the party serving the resource. Its side of the binding has four steps: assemble the ATR for this request, advertise H in the challenge, read H back from the payment it receives, and read the settlement from the rail. This guide walks them on x402 with x402/exact/eip155/eip3009. Every pairing has the same members, so the shape is the same on every surface; eight have no build, because the payer's own client builds their payment (On other surfaces names them). At the end you have a program that runs the seller's whole side, and the exchanges an agreement-first sale adds.

FacilitatorSeller's storageSellerBuyerFacilitatorSeller's storageSellerBuyerrequestCommitment, tie, assembleadvertise H and the link in the 402bound(payment) equals the H it issuedreference, then status through a readerGET /v1/quotewrite the ATR's bytes at the link402 PaymentRequiredthe payment, carrying Hverify and settlethe settlement transaction

The facilitator is the x402 role that verifies and settles. It is the seller's to choose; this package does not call it. This repository's settler is one, for most of the x402 pairings this package implements; What it does not serve lists the others.

1. Assemble the ATR for this request

The ATR's binding slot ties the record to one payment. On x402, tie(accepts, request) records every option the challenge offers, exactly as issued, and the commitment to the request it answers: the method, the path, the query and SHA-256 over the body exactly as received. The party slots carry whatever the parties agreed, as bytes.

Write the ATR's bytes to your own storage before the challenge goes out, and serve them unchanged at an https link. If the write fails, send no challenge: a buyer that cannot fetch the record declines.

The buyer gates decline the link unless it answers 200 with no redirect, in the identity coding (no Content-Encoding, or identity), with at most 1 MiB, within 10 seconds (vectors/buyer.json rows B11 to B14 and BE1). Make the link unguessable: H is in the challenge, and on most pairings in the payment, so build the link from a random token, not from H. Let it expire after a short period, minutes (LCP §12.4), once the buyer has had time to fetch the bytes (LCP §6.1). The binding slot records the request's path and query, and the link serves those bytes to whoever fetches it, so keep secrets out of the query.

3. Advertise H

advertise(doc, h, link, offer) returns a copy of the challenge with extensions.legalContext.info set to {"type":"sha256","value":H,"legalContextUrl":link}, beside its schema, and leaves every other extension and every option as they were. On pairings that carry H in the option itself, such as x402/exact/solana (extra.memo), it also places H there. A challenge, or an option in its accepts, that carries a refused member is refused as x402/option-malformed before anything is placed (Refusals). Send the result as encodeHeader(advertised) in the PAYMENT-REQUIRED header of the 402 (The headers).

4. Check the payment

Read the payment from the retried request's PAYMENT-SIGNATURE header with decodePaymentSignature(header). bound(payment) reads H from what the buyer presented: here, the EIP-3009 authorization's nonce. Match it with hashEquals against the H you issued for this request. The token contract verifies the signature when it executes the transfer; bound does not.

Refuse a payment whose H you did not issue for this request, and one whose H you have already accepted. Those two facts are your records, not this package's.

5. Read the settlement

reference(payment, option) gives the keys for finding this payment on chain, with the payee of option, the option the payment answers: the network, the token (asset), the payer, validBefore, H (h) and presented, the authorization's members as presented. After the facilitator settles, add the transaction its settle answer names as transaction: status reads it beside its own search, and never needs it. status(ref, reader, query) then reads the act query.act names, "payment" here, through a reader you supply, at the finality query.finality asks for. Ask for "finalized": a block above that mark can be replaced. recover({ network, tx, option }, reader) reads H back from the transaction alone.

status answers settled, pending, failed or unreadable, each with one reason from a closed list (the x402 guide gives it). It settles on a transaction that carries H, was signed by the payment's payer and pays the option's payee, read at the finality asked for or above it; below it, the answer is pending below-mark. It never compares the amount: presented on a settled answer says whether the landed authorization is the one presented. A reader call that rejects, or a reader that declares another network, makes the answer unreadable, never failed. Each rail's marks are in Reading settlement.

Before any transaction is named, status reads whether the authorization has been used: at the final mark, and, through a reader that has block, at the latest head. It answers pending instrument-live while the authorization is unused at both and still valid. authorizer(payment) gives the account whose signature authorises the transfer, and authorizationUsed({ network, authorization }, authorizer, block, reader) asks the token whether that authorization had executed by a given block, with authorization built from the reference as the program below builds it. The authorization cannot execute in a block whose timestamp is at or after its validBefore, so once a final block is past that time, an unused authorization never moves money.

The whole flow

This program runs every step above. A Map stands in for the seller's storage, a random key for the buyer's signer, and a reader built from one receipt for the chain. No network is used.

import { , , ,  } from "@integraledger/lcp";
import {
  ,
  ,
  ,
  type EvmReader,
  type ,
} from "@integraledger/lcp/evm";
import {
  ,
  ,
  ,
  ,
  ,
  ,
  type ,
  type ,
} from "@integraledger/lcp/x402";
import type {  } from "viem";
import { ,  } from "viem/accounts";

const :  = {
  : "exact",
  : "eip155:84532",
  : "10000",
  : "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
  : "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
  : 60,
  : { : "USDC", : "2" },
};
const :  = {
  : 2,
  : { : "https://api.seller.example/v1/quote" },
  : [],
};

// 1. Assemble the ATR for this request.
const  = await ({ : "GET", : "/v1/quote", : new () });
if (()) throw new (.);
const  = new ().('{"text":"One quote for 10000 base units of USDC."}');
const  = await ((), (., ), [["terms", ]]);
if (()) throw new (.);

// 2. Store the bytes and link them.
const  = `https://atr.seller.example/${.()}`;
const  = new ([[, .]]);

// 3. Advertise H.
const  = .(, ., , );
if (()) throw new (.);
.("advertised:", .(.?.["legalContext"]?.) === .({
  : "sha256",
  : .,
  : ,
}));
const  = (); // the 402's PAYMENT-REQUIRED header
if (()) throw new (.);

// The buyer's side, in brief: see the buyer guide.
const  = ();
if (()) throw new (.);
const  = (());
const  = await .({ , : , : ., : 1790000000 }, .);
if (()) throw new (.);
const  = .(await .(. as ));
if (()) throw new (.);
const  = (); // the retried request's PAYMENT-SIGNATURE header
if (()) throw new (.);

// 4. Check the payment.
const  = ();
if (()) throw new (.);
const  = await .();
if (typeof  !== "string") throw new (.);
.("bound to the issued H:", (, .), .());

// 5. Read the settlement. The facilitator's settle answer names the transaction.
const  = await .(, );
if (()) throw new (.);
const :  = `0x${"11".(32)}`;
const  = (: string):  => `0x${.(/^0x/, "").().(64, "0")}`;
const : EvmReader = {
  : "eip155:84532",
  : async () => ({
    : 1,
    : 100n,
    : [
      { : . as , : [, (.), ], : "0x" },
      {
        : . as ,
        : [, (.), (.)],
        : ((.).(16)),
      },
    ],
  }),
  : async () => ( === "finalized" ? 100n : 105n),
  : async () => null,
  // The token's authorizationState(authorizer, nonce): true once the authorization is used.
  : async () => ("1"),
};
// Ask about the act "payment" at the final mark; the answer names the reason where it is not settled.
const  = await .({ ...,  }, , { : "payment", : "finalized" });
.("status:", ., . === "settled" ? . : .);
const  = await .();
if (typeof  !== "string") throw new (.);
// The authorization as the token records its use, from the reference: the token, the nonce the payer signed (H), and
// its validBefore.
const  = { : "eip3009", : ., : ., : ., : . } as ;
.("authorization used by block 100:", await ({ : .,  }, , 100n, ));
const  = await .({ : ., : ,  }, );
.("recovered from the chain alone:", typeof  === "string" && (, .));
advertised: true
bound to the issued H: true true
status: settled finalized
authorization used by block 100: true
recovered from the chain alone: true

The agreement URL

Where the chosen pairing's data.coverage is none, the payment is not a public proof of H, and the buyer gates require an agreement URL: pass it to advertise as agreementUrl, and read returns it as agreement. The buyer gates decline an offer that names none (agreement-not-offered). The buyer pays that URL first, with a payment that is itself a public proof of H, and makes the full payment only after the URL's receipt. Build the agreement URL from a random token too, not from H, as Serving the link says of the link: unpaid, it answers with a challenge that advertises the link.

vectors/buyer.json fixes the exchange in fixed.agreement and the BA rows:

  • Unpaid, the URL answers 402 with PAYMENT-REQUIRED: the base64 of an x402 challenge, pad characters included and pad bits zero (RFC 4648 §3.5), as a standard encoder writes it, advertising the same H and link. The buyer gates decline a challenge that advertises another H (BA2), a header without its pad characters (BA13) and a header with a pad bit set (BA19).
  • Settling, a paid request answers 202 with Retry-After, and the buyer sends the same signed payment again, until the agreement option's maxTimeoutSeconds plus 180 seconds have passed (BA1, BA3). The buyer gates decline an unpaid request answered 202 as agreement-pending, and sign nothing (BA14).
  • Recorded, a paid request answers 200 with the receipt {"atrHash": H, "agreed": true, "network": …, "transaction": …}. The buyer gates require all four members: atrHash naming the same H (BA4), agreed true, and network and transaction strings.

Answer in the identity coding: the buyer asks with Accept-Encoding: identity and declines any other Content-Encoding (BE2). The agreement URL gives the buyer's side.

Serving the return

Once the agreement is recorded, the buyer's agent repeats its request for the charge. The LCP profile legal-context-return states the rules; the seller's part is this:

  1. The first response carries H, the link and the agreement URL, and no charge option the payer could pay before the agreement.
  2. Each agreement option names the charges it serves, each <slot>/<i>: the option at index i of the charge options the ATR's binding slot of that name holds (accepts of x402, challenges of mpp, paymentOptions of ack, options of acp, ap2, card and ucp), so a buyer pays an agreement that serves the charge it means to pay. A card sale whose agreement is an x402 option names its card options card/<i>. On x402 the member is extra.legalContextServes (LEGAL_CONTEXT_SERVES), an array of those names; on MPP it is the opaque member of that name, the names joined by one space. It is part of the option as issued, so write it before you record the option's digest. servesOf(option, atr?) reads it, and refuses a malformed one return/serves-malformed, and, given the ATR's bytes, one that names a slot the ATR does not bind or an index past that slot's options.
  3. A return value, where the charge is paid by a bearer value such as a Lightning preimage: draw 32 random bytes for the sale, keep them in your own record, never in the ATR, and send them with H in a Legal-Context response header, which toLegalContextHeader(h, returnValue) writes.
  4. The check. On the returning request, checkLegalContextReturn(value, record) takes the Legal-Context request header's value and your own record for the request, { h, returnValue? }. Serve the charge only where it answers true. It refuses a value that does not parse (return/header-malformed), another H (return/not-this-sale), and a return value that differs from the record's, one given where the record holds none included (return/value-mismatch). Over MCP, the same value arrives in the tool call's params._meta under LEGAL_CONTEXT_META, com.integraledger.lcp/legal-context.
import {
  ,
  ,
  ,
  ,
  ,
  ,
  ,
} from "@integraledger/lcp";

const  = await (new ().("the ATR's bytes"));

// The agreement option serves the charge at index 0 of the ATR's x402 slot.
const  = {
  : "exact",
  : "eip155:84532",
  : "1",
  : "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
  : "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
  : 60,
  : { : "USDC", : "2", : ["x402/0"] },
};
.(());

// The first response: a return value drawn for this sale, kept in the seller's record, sent with H.
const  = { , : .(new ()) };
const  = (., .);
if (()) throw new (.);

// The return: the buyer's agent repeats the request with the same value.
const  = new ("https://api.seller.example/v1/quote", { : { []:  } });
.((..(), ));
.(((), ));
[ 'x402/0' ]
true
{ refused: true, code: 'return/value-mismatch' }

What the seller keeps

Keep H, the link and the reference for each payment, the return value of a sale that has one, and the ATR's bytes in your storage after the link expires; the buyer keeps its own copy.

What this package does not do

It holds no key and signs nothing. It does not call the facilitator, move funds, or decide whether to serve a request. It never judges amount, payee, asset, timing or payer, and never reads the parties' content in the ATR. Beside the binding, it checks only the form the rail or the scheme requires and the bounds within which it reads its input.

On other surfaces

The members are the same everywhere, except build, which is absent where the payer's own client builds the payment and the package builds nothing: on mpp/charge/nearintents, mpp/charge/tempo/split-memo, mpp/charge/tempo/split-memo/push, mpp/session/hedera/stock, mpp/session/solana/stock, mpp/session/xrpl/stock, mpp/subscription/solana and x402/upto/xrpl. Check that build is defined before calling it on a pairing taken from BINDINGS. What tie, advertise and bound handle differs:

Edit on GitHub

Last updated on

On this page