Payments (x402)

A route can charge per request with x402, the open protocol built on HTTP 402 Payment Required. Dash implements the server side of x402 v2, scheme exact: it names a price, hands the signed payment to a facilitator, and runs the route once the facilitator confirms. Dash holds no payment keys and does no chain work. The extension pays as well, from a wallet of its own (see "Wallet"): for the person, not for agents.

Using it

ts
import { createPaywall, createRPCRoute } from "@dash/server";

const paywall = createPaywall({
  facilitator: "https://x402.org/facilitator", // testnet only; use a production one for mainnet
  network: "eip155:84532", // CAIP-2 id
  asset: "0x…", // the token contract
  extra: { name: "USDC", version: "2" }, // the token's EIP-712 name() and version()
  payTo: "0x…", // your wallet
});

// src/app/api/rpc/report.ts
export const POST = paywall.charge("0.001", createRPCRoute(schema, handler));
charge(price, handler) returns a handler of the same kind, so it wraps an RPC route (its schema is kept), a plain route, or a handler that takes (req, payment). payment is { payer, amount, asset, network }, known only after verification.price is a decimal in whole units ("0.001", converted with decimals, default 6). It can also be { price | amount, description, mimeType } (amount in atomic units), or a function of the request, for a price that depends on what is asked for.Nothing is built in for tokens or networks. asset, network and extra are yours to set from the network's published values, because a wrong token address sends money to the wrong place. Read name() and version() from the token contract, and the facilitator's GET /supported lists what it handles.

The flow

  • No PAYMENT-SIGNATURE header: 402 with the requirements in a PAYMENT-REQUIRED header (base64 JSON, also repeated as the body).
  • The caller picks the one option, signs it, and retries with PAYMENT-SIGNATURE (base64 JSON PaymentPayload).
  • The caller's accepted must equal our own requirements (scheme, network, amount, asset, recipient), or it is refused with invalid_payment_requirements. The facilitator is always sent our requirements, never the caller's.
  • POST {facilitator}/verify; if invalid, 402 with the facilitator's reason.
  • The route runs; if it answers 400 or above, nothing is settled and the same payment may be tried again.
  • POST {facilitator}/settle; on success the response gets a PAYMENT-RESPONSE header (base64 JSON with the transaction), on failure the route's answer is withheld and the caller gets 402.
settle: "before" swaps steps 5 and 6: the route never runs unpaid, and a failure afterwards is not refunded. The default after costs the caller nothing when the route fails.A facilitator that cannot be reached is 503; the route does not run. A facilitator that needs authentication takes facilitator: { url, headers(endpoint) }.

Things to know

  • Reuse. A payment being used or already spent is refused (payment_already_used) without running the route, so one signature does not get several answers. That memory is per process; across several instances the facilitator's settlement is what stops a second spend, and the route may have run on both.
  • Abuse. Every payment attempt costs a /verify call to the facilitator. Put the paid routes under rateLimit.
  • Browsers. The CORS definition allows PAYMENT-SIGNATURE and exposes PAYMENT-REQUIRED and PAYMENT-RESPONSE. Dash's own web and terminal clients do not pay: they show the 402 as an error.
  • Identity is separate. A payment says who paid, not who they are. A route can be both signed-in-only and paid.

Checked

Exercised with a mock facilitator (unpaid, malformed, mismatched requirements, valid, spent, route failure, failed settlement, facilitator down, simultaneous use, both settle orders, dynamic price, RPC wrapping, facilitator headers) and against the x402.org testnet facilitator, which accepted the request and refused a made-up signature with invalid_exact_evm_signature. Not done: a payment made with a real wallet through a real facilitator and chain.

Wallet

The extension is the person's wallet for x402: one wallet derived from the identity, EVM scheme exact with one configured token. Agents do not spend from it (see "Agents").

Networks

The wallet pays in USDC on Base, on mainnet unless the person ticks "Use testnet":
Network
Chain
Token contract
EIP-712 name, version
TestnetBase Sepolia, eip155:845320x036CbD53842c5426634e7929541eC2318f3dCF7eUSDC, 2
Mainnet (the default)Base, eip155:84530x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913USD Coin, 2
The addresses are Circle's published ones. The name and version were read from the contracts, and each contract's DOMAIN_SEPARATOR equals the one computed from these values and our encoding, so a signature made here is for the right token on the right chain.The Wallet tab shows which network is in use. The Use testnet checkbox is in the Settings tab; ticking it switches at once, unticking goes back to mainnet. Allowances and the 24-hour totals are kept per network, so an allowance set on the testnet never applies on mainnet, and the log shows only the network in use. The address is the same on both. A payment that is waiting keeps the network it was asked on. There is no build option: every build has the wallet, and the wallet code is only in the background worker's bundle.

Key

secret = HKDF-SHA256(master, info = "dash-wallet-v1 evm " ‖ counter), taking the first counter (from 0) whose 32 bytes are a valid secp256k1 scalar. It sits under the same passphrase, passkey and lock as the site keys, and the same 24 words and passphrase recover it. It is not a BIP-44 wallet: loading the phrase into another wallet gives unrelated keys, so funds are reachable only through Dash. The remedy is Private key in the Wallet tab, which shows the key after the same proof as showing the recovery phrase (passphrase or passkey) and hides it again. The address is the last 20 bytes of keccak-256 of the uncompressed public key, with its EIP-55 checksum; it is remembered, so the tab shows it while the identity is locked. There is no balance (that needs a chain call). Treat the wallet as pocket money.

Signing (EIP-3009, scheme exact)

No chain access is needed: the payer signs an authorization and the facilitator submits it, paying the gas.
  • Domain: { name, version, chainId, verifyingContract }, where chainId comes from the CAIP-2 network, verifyingContract is the configured token, and name and version are the configured values, never the server's.
  • Message: TransferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce), with from the wallet, to = payTo, value = amount, validAfter = now − 600 s, validBefore = now + maxTimeoutSeconds, and a random 32-byte nonce.
  • Signature: ECDSA over the EIP-712 digest, 65 bytes r ‖ s ‖ v with v 27 or 28, low-s.
  • The reply is the PaymentPayload of the flow above, accepted echoing the chosen requirement.

Rules the extension enforces (the page enforces none)

  • Selected token only. Refused: scheme not exact, a network or asset other than the one selected (so on the testnet a mainnet price is not paid, and the reverse), an extra.name or extra.version that differs from ours, a zero amount, a recipient that is not an address.
  • Same site. resource.url must be on the origin that asked (the browser's own record of the sender). A site cannot make the extension pay for another site's resource.
  • Short life. maxTimeoutSeconds above 300 is refused, so an unspent authorization expires soon.
  • Allowance per site. Default none. A site may be given a per-payment and a per-24-hours limit in the token. Inside it the payment is signed without a prompt; outside it, the card in the popup. The 24-hour total counts what was signed, not what settled, which errs towards spending less, and is reserved before signing so two payments at once cannot use the same room.
  • The card shows fields, not a sentence: site, resource URL, description, amount in the token and atomic units, asset, network, payTo, validity, and what was signed on the site in 24 hours. Pay or Cancel, and optionally "Pay without asking here, up to…" with the limits filled in (the payment, and twenty times it per day) and editable. A locked identity asks for the passphrase or passkey on the card, even inside an allowance.
  • The toolbar button is the notification. A payment outside the allowance needs the page to have just been used (the same rule as sign-in); otherwise the page keeps its 402 and nothing happens. When it does need the person, the toolbar button shows a number and nothing is drawn on the page and no window opens, so there is nothing for a page to imitate or cover. Opening the popup shows only the card; the menu comes back once it is decided. The request ends when its tab goes or navigates, or after three minutes. A payment inside the allowance needs no click and no number, which is what an allowance is for.
  • The site names the recipient, so a hostile or compromised site, or a script on it, can ask for any payTo. The allowance and the card bound the loss to what the person allowed there. The allowance is the real control; there is no recipient allow-list.

Log

Every signed authorization is recorded: time, site, URL, amount, payTo, nonce, and the transaction from PAYMENT-RESPONSE once the page reports it (for display only; the page is not trusted). The last 500 are kept locally and are not part of the backup. The Wallet tab lists the latest 50.

Where it hooks in

The extension's page script (boot.js, in the page's own world) wraps fetch. A request to the same origin that is answered 402 with a PAYMENT-REQUIRED header is passed, through the content script, to the background worker; if the worker signs, the request is repeated once with PAYMENT-SIGNATURE. A refusal, or a second 402, leaves the page with what it got. Requests with a streamed body are not repeated. Only the worker holds the key (keys are derived when needed and dropped); the popup asks the worker for the address, the log and the key.

Library

secp256k1 and keccak are not in WebCrypto. The wallet uses @noble/curves and @noble/hashes, both pinned exactly at 2.3.0 with npm provenance. The projects report audits by Trail of Bits (curves, everything, 2.3.0; secp256k1 also in 2023) and Cure53 (hashes, 2022, SHA-3 and Keccak included). Their own documentation says constant time is a goal JavaScript cannot guarantee; the key signs locally in the worker with no remote observer. Both have a single maintainer (noted in dependency-policy.json). Our own code is the EIP-712 encoding. The key and signature code was cross-checked against viem: addresses, digests and signatures are identical for two token domains and values from 0 to the maximum, and a signature recovers to the wallet's address.

Agents

Agents do not spend from this wallet. An agent keeps its own funded wallet, and the balance it holds is its cap. The agent skill (section 11, "Paying for a resource") gives the x402 client steps in pseudocode, rules (a wallet funded for this only, check every field, nothing over 300 seconds, single-use authorizations), and a deterministic test vector (section 12). Following only that text reproduces the vector. Dash holds no agent payment keys and adds no agent payment routes. Spending from the person's wallet under a limit, without trusting the agent, needs an on-chain allowance (a smart account with a session key); that is a separate design.

Checked

With the extension in Chrome, a Dash route behind createPaywall, and a mock facilitator that recovers the signer from the authorization itself (viem): the toolbar button shows a number, nothing is drawn on the page and no popup opens by itself; the popup shows only the card with the site's raw fields, and Pay pays (Cancel refuses; ticking the allowance keeps it and the next payment is silent, with no number; the menu comes back afterwards), and the facilitator verified a signature by the wallet's address and settled once for the right amount to the right recipient within 60 s; the receipt reaches the page and the log; inside an allowance payments are silent; the payment that would pass the daily limit asks; a payment over the per-payment limit asks; a resource on another site, another network and a one-hour authorization are never signed and show no number; outside the allowance with no click, nothing is shown; with the identity locked the card asks for the passphrase, a wrong one leaves the card open and the right one pays; the private key is refused for a wrong passphrase and is the key of the shown address. It starts on mainnet; ticking "Use testnet" switches at once; after that the allowance and log are empty on the other network and a mainnet price is asked, labelled mainnet; a testnet price is not paid while on mainnet even though an allowance exists on the testnet; and the testnet allowance is still there after switching back. Not done: a payment from a real wallet funded on a real chain through a real facilitator (the signature is checked by the mock facilitator and against the token contracts' domain separators, not settled on chain), and a passkey-locked identity approving a payment in the real toolbar popup.

Not yet

  • Other schemes. upto and batch-settlement (usage-based and batched charges) are not supported; offers list one option.
  • Shared reuse memory across instances, and a log of payments received on the server.
  • Solana and other networks. Only EVM exact is signed. Solana's scheme builds a transaction (blockhash, token accounts) and needs an RPC; it would not need a new library.
  • More networks and tokens in the switch (the table above is two entries), a recipient allow-list, and a balance in the popup.