Skip to content

feat(core): mainnet plan rules, Horizon reads and a run ledger (nothing calls them yet) - #43

Merged
dzakwannajmi merged 2 commits into
mainfrom
sow2/w3-optin-prep
Nov 21, 2026
Merged

dzakwannajmi merged 2 commits into
mainfrom
sow2/w3-optin-prep

Conversation

@dzakwannajmi

Copy link
Copy Markdown
Collaborator

Two parts of the mainnet opt-in that hold on their own and can be read before the allow path exists. This version still has no mainnet opt-in: stellar:pubnet is refused before any of this is reached, and the run budget values no mainnet asset.

What

The plan rules of a mainnet run (guard/mainnet-plan.ts, guard/horizon.ts). Exported, called by nothing.

  • MAINNET_USDC: Circle's USDC on Stellar mainnet as a Stellar Asset Contract, CCW67TSZV3SSS2HXMBQ5JFGCKJNXKZM7UQUWUZPUTHXSTZLEO7SJMI75. Checked three ways on 2026-11-21 (Circle's listed issuer, the contract the SDK derives from it, USDC_PUBNET_ADDRESS of @x402/stellar 2.28.0); a test asserts all three.
  • A mainnet environment pins that asset and a payee other than the payer. An option to pay is exact on stellar:pubnet, in that asset, valid for 1 to 300 seconds; the token reports 7 decimals.
  • The RPC's latest ledger must be within 12 ledgers of public Horizon's, and the payer's USDC balance through the RPC must equal Horizon's. The Horizon reads take only a 200 JSON answer from a Horizon that says it serves mainnet, follow no redirect, read at most 1 MiB, and name Horizon by its origin only.

The run ledger (guard/run-ledger.ts, the ledger option of the x402 payment checks). Works on every network; no CLI option writes one yet.

  • preparePayment writes each signature's intent after the run is charged and before the key can be asked to sign. A ledger that cannot take it stops the payment with nothing signed.
  • Then the outcome: signed (with the expiry ledger decoded from what the key signed), not signed, or unknown; discarded unsent, or sent with the answer's status; X402-06's settlement after it is verified. A signature the ledger cannot record is never sent.
  • wasit-ledger-<UTC time>.json is created new, never overwritten, and rewritten whole through a flushed temporary file and a rename, so it stays valid JSON. Closing it records balances before and after and whether they moved as the ledger says.

Tests

Written before the code and seen failing first.

  • New: mainnet-plan.test.ts (26), horizon.test.ts (9, a loopback stand-in), run-ledger.test.ts (23), run-ledger-wiring.test.ts (9, the recording chain and a gated variant on a test network).
  • Mutation pass: each rule removed one at a time turns a test red (26 of 26 and 27 of 27 after four tests were added for the first pass's survivors).
  • Local, Node 26: build and typecheck clean; cli 277, core 1024, server 30, Action 196, 0 failures. Node 22 and 24: this PR's CI.
  • Not tested: a process killed in the middle of a ledger write (atomicity rests on the rename); a paid send that loses its connection, in the wiring (the record itself is unit-tested).

…orizon

The mainnet opt-in needs these rules in place before anything is signed,
and they are small enough to read on their own. Nothing calls them yet:
stellar:pubnet is still refused first, and the run budget still values no
mainnet asset.

MAINNET_USDC is Circle's USDC on Stellar mainnet as a Stellar Asset
Contract. It was checked three ways on 2026-11-21: the issuer Circle's page
lists, the contract the SDK derives from that issuer, and the x402 SDK's
own constant. A test asserts all three.

A mainnet environment must pin that asset and a payee other than the payer
(a transfer to oneself would hide what left the account). An option to pay
must be exact on stellar:pubnet, in that asset, valid for 1 to 300 seconds,
and the token must report 7 decimals. The RPC's latest ledger must be within
12 ledgers of public Horizon's, so an RPC cannot stretch how long a signature
stays valid, and the payer's USDC balance through the RPC must equal
Horizon's, so an RPC cannot under-report it past the $10 rule.

The Horizon reads accept only a 200 JSON answer from a Horizon that says it
serves mainnet, follow no redirect, read at most 1 MiB, and name Horizon by
its origin only.
…igns

A mainnet run must leave a record a person can compare with the chain, also
after a crash: no signature without an entry, and an entry before the
signature. This adds that record to core, working on every network, as the
x402 payment checks' `ledger` option. No CLI option writes one yet.

preparePayment writes the intent of a signature (check, endpoint, amount,
payee, asset) right after the run is charged and before the key can be
asked to sign; a ledger that cannot take it stops the payment with nothing
signed. Then it records what became of it: signed, with the ledger its
authorization expires at, decoded from the preimage the key signed; not
signed (the run stopped before the key was asked, or the signed-terms gate,
the only way to the key, refused); or unknown (a client without a gate
failed after it was asked). A signature the ledger cannot record is never
sent. A payload discarded unsent is recorded as such. Each paid send records
the answer's status, or that no answer came; X402-06 records the settlement
after verifying it. An entry with no outcome reads as signed and sent.

The file, wasit-ledger-<UTC time>.json, is created new and never
overwritten, and every change rewrites it whole through a flushed temporary
file and a rename, so it is valid JSON whenever the process stops. It names
the payer by its public address and the RPC by its origin; free text is
sanitised, 64-hex values in it are masked, and it is cut to one line.
Closing it records the payer's balances before and after, and whether its
USDC went down by exactly the verified settlements and its XLM did not move.
@vercel

vercel Bot commented Nov 21, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
wasit Ready Ready Preview Nov 21, 2026 1:19pm UTC

@dzakwannajmi
dzakwannajmi merged commit 8eedbf2 into main Nov 21, 2026
5 checks passed

This branch was successfully deployed

1 active deployment
Preview — 2b4756e9 Deployed Nov 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant