@rail402.dev/sdk, rail402-mcp and rail402 passes the same controls. A spending
policy says what may be paid; each call brings its own cap; a budget ledger holds what has been spent and
what is pending; and a signing guard wraps the key so that nothing is signed unless the exact bytes pass
the policy, immediately before the signature. The unmodified @x402/stellar client builds the payment; the
guard is the only way it reaches the key.
The policy
Any other SEP-41 token can be added with its symbol and decimals; both are read from the token contract when
the buyer starts and must match (
payment_policy_invalid), and every asset other than testnet USDC needs
explicit caps. Recipient allow and deny lists compare base accounts, so a muxed M… address matches its G…
account. Amounts are decimal strings converted to base units without rounding: a cap with more decimals than
its asset is refused (payment_cap_invalid), never rounded.
upto is paid only through a pinned settlement contract (uptoContracts, RAIL402_UPTO_CONTRACTS); on
testnet the canonical deployment CBTVKCHLSGQ3WSV3NKSU6FZR4FZMC6ENMDMXUS5XQ66HYVUUHYE3TU7U is pinned by default.
The contract is never taken from a seller. uptoFacilitators (RAIL402_UPTO_FACILITATORS) optionally limits
which settlement authorities (extra.facilitatorAddress) may settle upto payments; without it any G…
account other than the payer and the recipient is accepted, bound exactly to the option the call selected.
The settlement authority is the source of the settlement transaction, so it must be a G… account: the
policy accepts a C… address (also in uptoFacilitators), but the upto client then refuses to build the
payment, and the call fails with payment_simulation_failed (“Building the payment failed:
extra.facilitatorAddress must be the facilitator’s G… settlement authority”) before anything is signed.
The longest authorization is maxAuthorizationSeconds, which the SDK’s spending policy accepts up to 3600
seconds. The upto client signs for at most 300 seconds whatever the policy allows: an upto option with a
longer maxTimeoutSeconds fails the same way, with payment_simulation_failed (“Building the payment
failed: maxTimeoutSeconds … exceeds the 300 seconds this client signs for”), before anything is signed.
A buyer that can sign never runs without caps. Configuration errors are refused when the buyer, the MCP server
or the CLI starts, with payment_policy_invalid and the offending setting in details.setting.
Checks, in order
Test files, relative topackages/sdk/test/: policy.test.ts (the option and policy matrix), decide.test.ts
(the signing decision on raw preimages), guard.test.ts (the guard around a spy signer), ledger.test.ts and
sqlite-ledger.test.ts (budget windows), http-flow.test.ts and mcp-flow.test.ts (the payment flows),
discovery.test.ts (listings), upto.test.ts (the upto scheme: the signed tree, option checks, settlement
proof and budget accounting), buyer.integration.test.ts, guard.integration.test.ts and
upto.integration.test.ts (the private Stellar network).
1. Before anything is fetched
2. Choosing a payment option
The unpaid request returns the seller’sPAYMENT-REQUIRED (x402 version 2 only, at most 64 KiB, otherwise
payment_requirements_invalid). Each option is then judged:
Among acceptable options the preferred scheme wins, then the lowest price (an
upto option counts at its
cap), then exact, then the seller’s order. When none is acceptable, a single option’s own refusal is returned; with several,
payment_no_acceptable_option lists each option and why it was refused.
3. Before signing
Contract accounts and custom tokens, whose balances cannot be decided this way, proceed to the guard.
4. The signing guard
The guard is armed with the one option the call selected and signs at most once. It decodes the exact bytes it is asked to sign and refuses, in this order:
The latest ledger is read just before deciding. The decision and the reservation run in one synchronous step,
so concurrent calls, including calls from several processes sharing the SQLite ledger, can never jointly exceed
a cap. A spy signer in
guard.test.ts is never called on any refusal and is called exactly once on success,
including against malicious scheme doubles that change the amount by one, the recipient, the asset, the
network, add an approval or a sub-invocation, push the expiry out, or ask for a second signature. For
upto, doubles that sign for another settlement contract (including one a seller’s 402 advertises), another
settlement authority, another recipient or a higher cap are refused the same way, with nothing signed
(upto.test.ts, upto.integration.test.ts). For a
G… signer the produced Ed25519 signature is verified; a key or wallet failure is payment_signing_failed
and releases the reservation.
After signing, the payload the client built is checked against what was signed (payment_authorization_rejected).
5. After the payment is sent
A reported settlement is checked on chain: the transaction must have succeeded and consumed this payment’s own
authorization, meaning the envelope carries the payer’s entry with the signed nonce and the host consumed that
nonce (
verifiedOnChain). That proves the exact price moved, whether the transfer was the transaction’s own
call or was made from inside another contract. For upto the amount is the one the pinned settlement contract
recorded for the signed terms in its upto_settled event, at most the cap; when no such event can be read the
whole cap counts. That amount is what the receipt reports as settled. A malformed hash is not recorded. A
hash that another payment also reported is still only a claim against each payment that reported it.
The budget ledger
Each signature reserves its amount as pending. A verified settlement moves it to spent. A reported settlement that is not verified yet counts as spent. A payment whose outcome is unknown stays pending until reconciliation proves it. Proof comes from the payer’s nonce recorded at signing. The host keeps every nonce it consumes in a temporary ledger entry keyed by the payer and the nonce, and only the entry the payer signed can consume it. Reconciliation checks every transaction that sent the payer’s asset to the recipient between the signing ledger and the authorization’s expiry, plus the hash the seller reported:- A transaction consumed the nonce. The payment is spent, whatever shape the transaction has: a direct settlement, a call through a wrapper or router contract, or a batch that settles several payments (of one payer or of several payers sharing a ledger file).
- No transaction consumed it and the authorization has expired. The payment is released (
expired_unused), orclaim_falsewhen a settlement was reported.
upto payment reserves its cap. The amount the seller reports counts as spent at once, and the rest of the
cap stays pending until the chain proves the settlement: then the proven amount is spent and the rest returns
to the window (a released event with reason settled_below_cap). A zero settlement proven on chain returns
the whole cap; a zero settled off chain (no transaction) returns it once the authorization expires with nothing
consumed.
The RPC keeps events and transactions for a limited time (about 7 days on the public RPCs). An expired payment
whose ledgers the RPC no longer holds can never be proven either way. It is counted as spent at its whole
amount, reported once (an unprovable event), and no longer reconciled. It never blocks later windows and
never returns budget that may have been paid. To avoid this, reconcile within the RPC’s retention.
Reconciliation runs:
- for a few rows before each new payment;
- for its own payment when a call is retried with the same idempotency key;
- on demand:
buyer.reconcile(),rail402 session reconcile, or MCPget_budgetwithreconcile: true.
autoRenew (RAIL402_SESSION_AUTO_RENEW=true) says so. Named
sessions (rail402 session start --name … --max-total … --ttl …) do not renew and end with
payment_session_expired. The in-memory ledger serves one process; the SQLite ledger
(@rail402.dev/sdk/sqlite, the CLI and MCP default) is shared by every process that opens the same file.