@oceanalt/x402
v0.3.0
Published
Pre-payment checks for x402 clients. ./hook: an onBeforePaymentCreation hook for @x402/core that asks for allow, review or decline before the payment is signed and aborts when there is no answer (zero dependencies). Main entry: a fetch wrapper that screen
Maintainers
Readme
@oceanalt/x402
Hook for the x402 client (@oceanalt/x402/hook, new in 0.3.0)
One line in an existing @x402/core client. Before the payment is signed, the hook sends the selected requirement's payTo, network, asset, amount and the URL that asked for payment to POST https://oceanalt.com/api/decide (free, no key) and gets back allow, review or decline.
import { x402Client } from "@x402/fetch";
import { oceanaltBeforePaymentHook } from "@oceanalt/x402/hook";
const client = new x402Client()
.register("eip155:*", new ExactEvmScheme(signer))
.onBeforePaymentCreation(oceanaltBeforePaymentHook());allow: returns nothing; the client signs and pays.decline: returns{ abort: true, reason }; nothing is signed.onReviewcannot override it.review: aborts unlessonReview(result, context)returns exactlytrue(for example after a person approved).- No answer in time, network or HTTP error, malformed answer, or an answer about a different address: treated as
review. The hook fails closed. onDecision(result)receives the Ed25519-signed receipt; verify it offline withhttps://oceanalt.com/.well-known/oceanalt-keys.json.
Zero dependencies (global fetch; Node 18+, browsers, Deno, Bun). Tested against @x402/core 2.28.0 with a stub scheme: the scheme is never called on decline, review or timeout. Not affiliated with or endorsed by Coinbase or the x402 Foundation.
Fetch wrapper (main entry, needs @oceanalt/core)
Wrap fetch so that, at the moment a 402 asks for money, three things happen before your payer signs:
- the endpoint is screened (impersonated merchant endpoints hand agents freshly generated payee addresses — address lists never catch those);
- the seller's signed payment requirements are verified offline (
extensions.signedRequirements, Ed25519) — apayToswapped by a proxy, CDN or compromised SDK is rejected; - the payee gets a pre-settlement decision (
allow | review | decline).
Secrets never leave the agent (outgoing bodies are scanned). You keep your own x402 payer; OceanAlt holds no keys and moves no money.
import { OceanAltClient } from "@oceanalt/core";
import { wrapFetch } from "@oceanalt/x402";
const client = new OceanAltClient();
const fetchGuarded = wrapFetch(fetch, { client, requireSignature: false, pay: (pr) => myPayer.headersFor(pr) });
const res = await fetchGuarded("https://seller.example/api/thing");mode: "block" (default) throws on risky endpoints, tampered requirements, declined or under-review payees; mode: "advise" records and lets you decide. Every call leaves an intent → decision → outcome record via client.record().
