cred402/docs/p9.md
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

107 lines
4.9 KiB
Markdown

# p9 — Real x402, real data, real MCP
> **Make it real, phase 2.** The three machine-economy rails stop being
> self-contained: payments can settle through the **canonical Casper x402
> facilitator**, RWA evidence is computed from **live Open-Meteo data**, and the
> MCP server runs on the **official `@modelcontextprotocol/sdk`** so any real MCP
> client connects. Each is additive — the dependency-free defaults still run with
> no service and no key.
## 1. Real x402 facilitator (`make-software/casper-x402`)
Cred402's built-in flow (`lib/x402/x402.ts`) is already real: a genuine `402`,
an ed25519 signature over a domain-separated authorization, and on-chain receipt
commitment. p9 adds the **production settlement rail** — a typed client for the
canonical Casper x402 facilitator (Apache-2.0, from the Casper AI Toolkit), which
speaks the x402 V2 protocol and settles CEP-18 `transfer_with_authorization`
payments while paying gas itself.
`lib/x402/facilitator.ts` implements the exact wire contract from the facilitator's
`docs/api-reference.md`:
| Endpoint | Purpose |
| -------- | ------- |
| `GET /supported` | `(scheme, network)` pairs + fee-payer |
| `POST /verify` | validate an `ExactCasperPayload` vs `PaymentRequirements` (no chain write) |
| `POST /settle` | verify → build → sign → put deploy; returns the Casper deploy hash |
| `GET /health` | liveness |
Activate it by setting `CRED402_X402_FACILITATOR_URL` (else the built-in verifier
is used). Run the facilitator locally from the casper-x402 repo:
```bash
docker build -f infra/docker/build-facilitator.Dockerfile -t casper-x402 .
docker run -p 4022:4022 casper-x402 # GET /health -> {"status":"ok"}
```
The client's request shaping and response parsing are covered offline in
`test/x402_facilitator.test.ts` (injected fetch — real contract, no live service).
## 2. Real RWA data (Open-Meteo, live)
`api/rwa_data/solar_provider.ts` fetches **live** shortwave-radiation (GHI),
cloud-cover and precipitation from Open-Meteo (free, no key) for the asset's real
coordinates and runs a real photovoltaic model:
```
E = GHI(kWh/m²) · array_area(m²) · module_efficiency · performance_ratio
```
Verified live this phase: `source: open-meteo`, GHI ≈ 7.9 kWh/m²/day for Solar
Farm A17 (Izmir), receivable confidence 94. If the network is down it falls back
to a deterministic Allen/FAO clear-sky model — still real physics, never random
mock data. Optional EU power-market data (ENTSO-E) is gated behind
`ENTSOE_API_TOKEN`.
## 3. Real MCP server (official SDK)
The zero-dep stdio server (`mcp/server.ts`) stays for dependency-free use. p9 adds
`mcp/server_sdk.ts` — the same 32-tool registry served through the official
`@modelcontextprotocol/sdk` low-level `Server` over `StdioServerTransport`, with
SDK-handled capability negotiation, framing, and error semantics.
```bash
npm run mcp:sdk # official-SDK server over stdio
```
Claude Desktop / MCP Inspector config:
```json
{ "command": "npx", "args": ["tsx", "mcp/server_sdk.ts"], "cwd": "/path/to/cred402" }
```
`test/mcp_sdk.test.ts` proves it end to end: the **official MCP SDK client**
spawns the server as a subprocess and performs a real handshake
(initialize → `tools/list` → `tools/call`), asserting the full registry is served
and dispatched.
## Status
| Rail | Real integration | Verified |
| ---- | ---------------- | -------- |
| x402 settlement | `casper-x402` facilitator client (x402 V2) | offline contract tests (5) |
| RWA data | Open-Meteo live GHI + PV model | live fetch (source: open-meteo) |
| MCP | official `@modelcontextprotocol/sdk` server | real client↔server handshake (3) |
All env-gated: with nothing set, the built-in x402 verifier, the clear-sky
fallback, and the zero-dep MCP server keep the demo and the test suite green.
## Addendum — real EIP-712 + real Sidecar event feed
Two more canonical Casper rails are now wired:
- **`@casper-ecosystem/casper-eip-712`** — the x402 authorization is signed over a
**real EIP-712 typed-data digest** (`\x19\x01`-framed keccak256 with a
Casper-native `chain_name` domain), not a stable-JSON string. See
`lib/x402/eip712.ts`; the digest is now what `lib/x402/x402.ts` signs and
verifies, so the authorization is checkable by any EIP-712-aware contract or the
casper-x402 facilitator. Tests: `test/x402_eip712.test.ts` (deterministic,
field-sensitive, full ed25519 round-trip; tampering breaks the signature).
- **`casper-network/casper-sidecar`** — `lib/casper/sidecar.ts` consumes the
Sidecar's real SSE `/events` feed (faithful `data:`/`id:`/keep-alive framing),
decoding `ApiVersion`/`BlockAdded`/`TransactionProcessed`/`Shutdown` and
extracting contract-emitted `messages[]` — the production event source for the
WatchdogAgent/indexer. Run `npm run casper:sidecar` (set `CRED402_SIDECAR_URL`);
the in-process EventBus stays the default. Tests: `test/casper_sidecar.test.ts`
(uses the exact frame shapes from the Sidecar docs, incl. a streamed body).