caelum0x 2d43618725 feat: real integrations (Casper Testnet, x402/EIP-712, MCP, RealFi, cross-chain), wallet sign-in, branding
Replace the design-note docs (p1–p7) with the implemented "make it real" phase
docs (p8–p10) and wire production-grade, env-gated real integrations:

- p8 Casper Testnet: byte-exact deploys + WASM install via casper-js-sdk; live
  node reads (node.testnet.casper.network); CRED402_CHAIN=sim|testnet transport.
- p9 x402 + data + MCP: real EIP-712 typed-data digests (@casper-ecosystem/
  casper-eip-712); casper-x402 facilitator client (x402 V2); live Open-Meteo GHI;
  official @modelcontextprotocol/sdk server; casper-sidecar SSE event feed.
- p10 RealFi + cross-chain: real Stripe test-mode webhooks (HMAC-verified) and
  Plaid sandbox → privacy-preserving on-chain envelopes; Base Sepolia EVM deploy
  (Foundry); reachable via /api/realfi/* and /api/x402/facilitator routes.
- Sign-in with Casper Wallet: real extension provider + challenge/ed25519/session
  flow (/v1/auth/wallet/*), console connect + sign-in button.
- Branding: Cred402 logo mark, favicon, OG image; Vercel config for the console.

All env-gated with sim/free-API fallbacks; full suite green, all toolchains
(TS/Rust/Go/Solidity) build.

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

56 lines
2.1 KiB
TypeScript

import { createServer, type IncomingMessage } from "node:http";
import { verifyWebhookSignature } from "../../packages/ts-sdk/src/index.js";
/**
* Cred402 webhook receiver — a real, runnable endpoint that ingests protocol
* webhooks and verifies their HMAC signature with `@cred402/sdk`.
*
* 1. start it: CRED402_WEBHOOK_SECRET=whsec_... npx tsx examples/webhook-receiver/server.ts
* 2. subscribe it: curl -XPOST localhost:4021/v1/webhooks -d '{"url":"http://localhost:4055/","events":["*"]}'
* (the response's `secret` is your CRED402_WEBHOOK_SECRET)
* 3. trigger events: curl -XPOST localhost:4021/api/demo/run
*
* Every delivery is verified; tampered or stale signatures are rejected with 401.
*/
const PORT = Number(process.env.WEBHOOK_RECEIVER_PORT ?? 4055);
const SECRET = process.env.CRED402_WEBHOOK_SECRET ?? "";
async function readRaw(req: IncomingMessage): Promise<string> {
const chunks: Buffer[] = [];
for await (const c of req) chunks.push(c as Buffer);
return Buffer.concat(chunks).toString("utf8");
}
const server = createServer(async (req, res) => {
if (req.method !== "POST") {
res.writeHead(405).end();
return;
}
const raw = await readRaw(req);
const sig = (req.headers["x-cred402-signature"] as string) ?? "";
const event = (req.headers["x-cred402-event"] as string) ?? "?";
const verified = SECRET
? verifyWebhookSignature({ secret: SECRET, signatureHeader: sig, rawBody: raw })
: false;
if (SECRET && !verified) {
console.warn(`✗ REJECTED ${event} — bad/stale signature`);
res.writeHead(401, { "Content-Type": "application/json" }).end(JSON.stringify({ ok: false, error: "invalid signature" }));
return;
}
let payload: unknown = raw;
try {
payload = JSON.parse(raw);
} catch {
/* keep raw */
}
console.log(`✓ ${verified ? "VERIFIED" : "UNSIGNED"} ${event}`, JSON.stringify(payload));
res.writeHead(200, { "Content-Type": "application/json" }).end(JSON.stringify({ ok: true }));
});
server.listen(PORT, () => {
console.log(`Cred402 webhook receiver on http://localhost:${PORT}/ (${SECRET ? "verifying" : "no secret set — accepting unsigned"})`);
});