cred402/lib/services/compliance_report.ts
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

63 lines
2.2 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
import { SanctionsScreener } from "../compliance/sanctions.js";
/**
* Per-jurisdiction compliance report — the view a compliance officer needs:
* operators grouped by jurisdiction, KYB coverage, and sanctions exposure. Built
* from the on-chain OperatorVerificationRegistry + sanctions lists.
*/
export interface JurisdictionRow {
jurisdiction: string;
operators: number;
verified: number;
sanctioned: boolean;
agents: string[];
}
export interface ComplianceReport {
generated_at: number;
total_operators: number;
verified_operators: number;
kyb_coverage: number; // 0..1
sanctioned_exposure: number; // operators in sanctioned jurisdictions
by_jurisdiction: JurisdictionRow[];
}
export function buildComplianceReport(ledger: Ledger): ComplianceReport {
const screener = new SanctionsScreener();
const operators = ledger.operators.list();
// Map operator_id -> agents that declare it (from passports).
const operatorAgents = new Map<string, string[]>();
for (const a of ledger.agents.list()) {
const op = ledger.buildPassport(a.agent_id)?.operator;
if (op) operatorAgents.set(op, [...(operatorAgents.get(op) ?? []), a.agent_id]);
}
const byJur = new Map<string, JurisdictionRow>();
let verified = 0;
let sanctionedExposure = 0;
for (const o of operators) {
const jur = (o.jurisdiction || "??").toUpperCase();
const row = byJur.get(jur) ?? { jurisdiction: jur, operators: 0, verified: 0, sanctioned: screener.screenJurisdiction(jur).matched, agents: [] };
row.operators++;
const isVerified = ledger.operators.is_verified(o.operator_id);
if (isVerified) {
row.verified++;
verified++;
}
if (row.sanctioned) sanctionedExposure++;
row.agents.push(...(operatorAgents.get(o.operator_id) ?? []));
byJur.set(jur, row);
}
return {
generated_at: ledger.clock.now(),
total_operators: operators.length,
verified_operators: verified,
kyb_coverage: operators.length ? Math.round((verified / operators.length) * 1000) / 1000 : 0,
sanctioned_exposure: sanctionedExposure,
by_jurisdiction: [...byJur.values()].sort((a, b) => b.operators - a.operators),
};
}