cred402/lib/x402/x402.ts
caelum0x 24d073ee83 feat: KeeperHub execution layer + Flare interoperable/confidential credit
Two hackathon integrations spanning the whole credit stack, sim-first
(green with no keys) with real integrations behind env flags.

KeeperHub — the on-chain execution & reliability layer (lib/keeperhub/):
- OnchainExecutor: pay-per-execution (x402/MPP) -> simulate -> smart gas
  with exponential backoff -> MEV-protected private routing -> audit trail;
  real MCP client to app.keeperhub.com; deterministic sim fallback.
- Autonomous Credit Keeper: FTSO position health -> check->execute deleverage.
- Credit Automations: declarative price/health/schedule rules as workflows.
- Autonomous Scheduler: KeeperHub-cron loop running keeper + automations.

Flare — interoperable + confidential primitives (packages/.../flare, lib/flare):
- FXRP credit satellite: CAN-gated, FTSO-priced, FDC-attested; Coston2
  Solidity vault (contracts/flare/).
- Confidential Compute credit scoring (TEE-attested, private inputs).
- FTSO-priced multi-asset collateral; FAssets mint->collateralize lifecycle.

x402 Credit-Service Marketplace: sells cred402's intelligence pay-per-call
over x402 (402->pay->200); paid receipts feed the revenue->reputation loop.

Wiring: 78 MCP tools, /v1 + /api routes, console Flare + x402 tabs, 11 demo
CLIs, 8 feature docs + docs/SUBMISSIONS.md, recorded demo video (media/).
346 tests pass; 7 adversarial verification workflows caught + fixed ~37
correctness/security issues.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-11 23:34:23 +03:00

132 lines
4.8 KiB
TypeScript

import type { ServiceType } from "../core/types.js";
import { blake2b256 } from "../core/hash.js";
import { sign, verifyCasperHex } from "./keys.js";
import { paymentAuthorizationDigest } from "./eip712.js";
/**
* x402 — HTTP-native machine-to-machine payments.
*
* Flow (matches Casper's AI Toolkit x402 story):
* 1. Buyer hits a paid endpoint.
* 2. Server replies `402 Payment Required` with a signed PaymentChallenge.
* 3. Buyer signs a domain-separated PaymentAuthorization (casper-eip-712 style)
* and retries with an `X-Payment` header.
* 4. Server verifies the proof, delivers the report, and records a receipt.
*/
export const X402_DOMAIN = {
name: "Cred402",
version: "1",
network: "casper-testnet",
} as const;
export interface PaymentChallenge {
payment_id: string;
amount_motes: string;
network: "casper";
asset: "CSPR";
resource: string;
service_type: ServiceType;
seller_agent: string;
nonce: string;
expires_at: number;
}
/** The structured, domain-separated message an agent signs to authorize payment. */
export interface PaymentAuthorization {
domain: typeof X402_DOMAIN;
payment_id: string;
payer_agent: string;
seller_agent: string;
service_type: ServiceType;
amount_motes: string;
resource: string;
nonce: string;
}
export interface PaymentProof {
authorization: PaymentAuthorization;
payer_public_key: string;
signature: string;
}
/**
* The 32-byte EIP-712 typed-data digest that gets signed — a standards-compliant
* `\x19\x01` typed-data hash (via `@casper-ecosystem/casper-eip-712`), verifiable
* by any EIP-712-aware contract or the casper-x402 facilitator, not just by us.
*/
export function authorizationDigest(auth: PaymentAuthorization): string {
return paymentAuthorizationDigest(auth);
}
/** Buyer side: sign a PaymentAuthorization with the agent's ed25519 key. */
export function signPayment(args: {
challenge: PaymentChallenge;
payer_agent: string;
payer_public_key: string;
payer_private_pem: string;
}): { header: string; proof: PaymentProof } {
const authorization: PaymentAuthorization = {
domain: X402_DOMAIN,
payment_id: args.challenge.payment_id,
payer_agent: args.payer_agent,
seller_agent: args.challenge.seller_agent,
service_type: args.challenge.service_type,
amount_motes: args.challenge.amount_motes,
resource: args.challenge.resource,
nonce: args.challenge.nonce,
};
const signature = sign(args.payer_private_pem, authorizationDigest(authorization));
const proof: PaymentProof = {
authorization,
payer_public_key: args.payer_public_key,
signature,
};
// The X-Payment header carries the base64-encoded proof, per x402 convention.
const header = Buffer.from(JSON.stringify(proof), "utf8").toString("base64");
return { header, proof };
}
export function decodePaymentHeader(header: string): PaymentProof {
return JSON.parse(Buffer.from(header, "base64").toString("utf8")) as PaymentProof;
}
/** Seller side: verify a payment proof matches the challenge and signature. */
export function verifyPayment(args: {
challenge: PaymentChallenge;
proof: PaymentProof;
now?: number;
}): { ok: boolean; reason?: string } {
const { challenge, proof } = args;
const a = proof.authorization;
if (a.payment_id !== challenge.payment_id) return { ok: false, reason: "payment_id mismatch" };
if (a.amount_motes !== challenge.amount_motes) return { ok: false, reason: "amount mismatch" };
if (a.nonce !== challenge.nonce) return { ok: false, reason: "nonce mismatch" };
if (a.seller_agent !== challenge.seller_agent) return { ok: false, reason: "seller mismatch" };
if (a.resource !== challenge.resource) return { ok: false, reason: "resource mismatch" };
if (a.domain.network !== X402_DOMAIN.network) return { ok: false, reason: "wrong network" };
// Reject expired challenges (replay window protection, p2 §14 threat 2).
if (args.now !== undefined && args.now > challenge.expires_at) return { ok: false, reason: "challenge expired" };
const validSig = verifyCasperHex(proof.payer_public_key, authorizationDigest(a), proof.signature);
if (!validSig) return { ok: false, reason: "invalid signature" };
return { ok: true };
}
/** The on-chain commitment hash stored in the X402ReceiptRegistry. */
export function paymentProofHash(proof: PaymentProof): string {
return blake2b256(JSON.stringify(proof));
}
/** Render the 402 response headers an agent endpoint returns. */
export function challengeHeaders(challenge: PaymentChallenge): Record<string, string> {
return {
"X-Payment-Amount": (Number(challenge.amount_motes) / 1e9).toString(),
"X-Payment-Amount-Motes": challenge.amount_motes,
"X-Payment-Network": challenge.network,
"X-Payment-Asset": challenge.asset,
"X-Payment-Id": challenge.payment_id,
"X-Payment-Nonce": challenge.nonce,
"X-Payment-Resource": challenge.resource,
};
}