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

49 lines
1.8 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
/**
* Credit-score & reputation trend — a credit bureau is judged on its time series,
* not just a point score. This reconstructs an agent's credit-score and reputation
* trajectory from the canonical event log (CreditScoreSet / ReputationUpdated), so
* counterparties can see direction and volatility, not only the latest number.
*/
export interface TrendPoint {
seq: number;
timestamp: number;
value: number;
}
export interface ScoreTrend {
agent_id: string;
credit_score: { current: number; change: number; points: TrendPoint[] };
reputation: { current: number; change: number; points: TrendPoint[] };
}
function changeOf(points: TrendPoint[]): number {
if (points.length < 2) return 0;
return points[points.length - 1]!.value - points[0]!.value;
}
export function buildScoreTrend(ledger: Ledger, agentId: string): ScoreTrend | { error: string } {
const agent = ledger.agents.get(agentId);
if (!agent) return { error: `unknown agent: ${agentId}` };
const creditPoints: TrendPoint[] = [];
const repPoints: TrendPoint[] = [];
for (const e of ledger.bus.all()) {
if (e.name === "CreditScoreSet" && e.data.agent_id === agentId) {
const value = Number(e.data.credit_score);
if (Number.isFinite(value)) creditPoints.push({ seq: e.seq, timestamp: e.timestamp, value });
} else if (e.name === "ReputationUpdated" && e.data.agent_id === agentId) {
const value = Number(e.data.current ?? e.data.new_score);
if (Number.isFinite(value)) repPoints.push({ seq: e.seq, timestamp: e.timestamp, value });
}
}
return {
agent_id: agentId,
credit_score: { current: agent.credit_score, change: changeOf(creditPoints), points: creditPoints },
reputation: { current: agent.reputation_score, change: changeOf(repPoints), points: repPoints },
};
}