cred402/examples/paid-api-x402.ts
caelum0x afed64e4bd feat(p2): x402 Gateway middleware — any API becomes x402-payable + receipt-generating
Roadmap p2 shipped. The adoption wedge: drop one middleware in front of any route
and it issues 402 challenges, verifies EIP-712/ed25519 payments (replay-protected),
emits a receipt commitment to a sink, and proceeds — feeding the Cred402 credit moat.

- lib/x402/gateway.ts: framework-agnostic X402Gateway + Express and Web (Fetch)
  adapters, pluggable nonce store (replay guard), per-route analytics, receipt sink.
- Works for ANY p1 service category (inference/data/compute/…), not just RWA.
- Fix: eip712 uint256 now padded to 32 bytes (odd-length amounts no longer break signing).
- test/x402_gateway.test.ts (5 cases, real buyer→gateway round-trips), examples/paid-api-x402.ts,
  npm run demo:gateway, docs/roadmap (p2 ✅). 181 tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 17:20:46 +03:00

42 lines
1.7 KiB
TypeScript

/**
* Reference: turn ANY API into an x402-payable, receipt-generating endpoint with
* the Cred402 gateway (roadmap p2). Express shown; a Web/Fetch (Next.js/edge)
* adapter is also available via `gateway.web(handler)`.
*
* This is the adoption wedge: every wrapped route builds the seller agent's
* on-chain credit history — Cred402 works for any x402 service, not just RWA.
*/
import { X402Gateway } from "../lib/x402/gateway.js";
import { cspr } from "../lib/core/units.js";
// 1. Configure the gateway for your service + price + receiving agent.
const gateway = new X402Gateway({
serviceType: "inference.llm", // any p1 category: data.*, compute.*, api.*, rwa.*, …
priceMotes: cspr(0.05),
sellerAgent: "caid:casper:my-inference-agent",
// Anchor each receipt to Cred402 (POST to /v1/x402/receipts, write to Casper, …):
onReceipt: async (receipt) => {
// await fetch("https://cred402-1.onrender.com/v1/x402/receipts", {
// method: "POST", headers: { "content-type": "application/json" },
// body: JSON.stringify(receipt) });
console.log("anchor receipt", receipt.receipt_id, receipt.amount_motes);
},
});
// 2. Drop it in front of any route. Unpaid → 402; paid → your handler runs.
//
// import express from "express";
// const app = express();
// app.get("/v1/infer", gateway.express(), (req, res) => {
// // req.cred402.receipt + req.cred402.payer are available here
// res.json({ answer: runInference(req.query.q) });
// });
// app.listen(8080);
//
// Next.js / edge (Web Fetch):
//
// export const GET = gateway.web((req, { receipt, payer }) =>
// Response.json({ answer: runInference(new URL(req.url).searchParams.get("q")) }));
export { gateway };