cred402/lib/casper/ledger_transport.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

88 lines
3.6 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
import { deployHash } from "../core/hash.js";
import type { DeploySpec } from "./deploy.js";
import type { RuntimeArg } from "./args.js";
import type { CasperTransport, DeployResult } from "./transport.js";
/**
* LedgerTransport — a fully working {@link CasperTransport} backed by the
* in-memory ledger.
*
* This is the local/dev twin of {@link CasperRpcTransport}: the SAME transport
* interface, executed against the simulated contract suite instead of a live
* node. It proves the seam is real — a `DeploySpec` is dispatched to the actual
* contract entry point, executes synchronously, and returns a real result. Point
* production at `CasperRpcTransport` + `Ed25519DeploySigner`; point dev/tests at
* this. No mocks: entry points run the genuine ledger logic.
*/
export class LedgerTransport implements CasperTransport {
constructor(private readonly ledger: Ledger) {}
async callEntryPoint(spec: DeploySpec): Promise<DeployResult> {
const a = byName(spec.args);
const hash = deployHash();
try {
switch (spec.entryPoint) {
case "register_agent":
this.ledger.agents.register_agent({
agent_id: req(a, "agent_id"),
owner_public_key: a.owner_public_key ?? spec.sender,
agent_public_key: a.agent_public_key ?? spec.sender,
service_type: req(a, "service_type") as never,
});
break;
case "set_credit_score":
this.ledger.agents.set_credit_score(req(a, "agent_id"), Number(req(a, "score")));
break;
case "update_reputation":
this.ledger.agents.update_reputation(req(a, "agent_id"), Number(a.delta ?? "0"), a.evidence_hash ?? "0x", a.reason_code);
break;
case "stake":
this.ledger.agents.stake(req(a, "agent_id"), BigInt(req(a, "amount")));
break;
case "deposit_liquidity":
this.ledger.pool.deposit_liquidity(BigInt(req(a, "amount")), spec.sender);
break;
case "finalize_receipt":
this.ledger.receipts.finalize_receipt(req(a, "receipt_id"));
break;
default:
return { deployHash: hash, status: "failure", errorMessage: `unsupported entry point: ${spec.entryPoint}` };
}
return { deployHash: hash, status: "success", cost: spec.paymentMotes.toString() };
} catch (err) {
return { deployHash: hash, status: "failure", errorMessage: (err as Error).message };
}
}
async queryContractKey(contractHash: string, path: string[]): Promise<unknown> {
// Resolve a few well-known reads: agents/<id>, pool/state.
const [collection, id] = path;
if (collection === "agents" && id) return this.ledger.agents.get(id) ?? null;
if (collection === "pool") return this.ledger.pool.poolState();
if (collection === "credit_lines" && id) return this.ledger.pool.get(id) ?? null;
return { contractHash, path, note: "unsupported read" };
}
async waitForDeploy(deployHashStr: string): Promise<DeployResult> {
// Ledger execution is synchronous, so any submitted deploy already succeeded.
return { deployHash: deployHashStr, status: "success" };
}
async health(): Promise<{ ok: boolean; chain?: string }> {
return { ok: true, chain: "casper-local-ledger" };
}
}
function byName(args: RuntimeArg[]): Record<string, string> {
const out: Record<string, string> = {};
for (const a of args) out[a.name] = a.value;
return out;
}
function req(a: Record<string, string>, name: string): string {
const v = a[name];
if (v === undefined) throw new Error(`missing arg: ${name}`);
return v;
}