cred402/cli/lib/context.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

48 lines
1.7 KiB
TypeScript

/**
* Shared command context: the configured HTTP client, global flags, and small
* helpers for emitting either JSON (`--json`) or human output.
*/
import { Cred402Client } from "./http.js";
import { asJson } from "./render.js";
export interface CommandContext {
readonly client: Cred402Client;
readonly json: boolean;
readonly apiKey?: string;
/** Positional arguments after the command + subcommand. */
readonly args: readonly string[];
}
/** Print a result: raw JSON when `--json`, otherwise the human renderer output. */
export function emit(ctx: CommandContext, data: unknown, human: () => string): void {
if (ctx.json) {
process.stdout.write(asJson(data) + "\n");
} else {
process.stdout.write(human() + "\n");
}
}
/** A usage error for a command — message is shown without a stack trace. */
export class UsageError extends Error {
constructor(message: string) {
super(message);
this.name = "UsageError";
}
}
/** Require a positional argument or throw a usage error. */
export function requireArg(args: readonly string[], index: number, name: string): string {
const v = args[index];
if (v === undefined || v === "") throw new UsageError(`missing required argument: <${name}>`);
return v;
}
/** Parse a CSPR amount (decimal) into integer motes string. 1 CSPR = 1e9 motes. */
export function csprToMotes(input: string): string {
if (!/^\d+(\.\d+)?$/.test(input)) throw new UsageError(`invalid CSPR amount: "${input}"`);
const [whole, frac = ""] = input.split(".");
const fracPadded = (frac + "000000000").slice(0, 9);
const motes = BigInt(whole ?? "0") * 1_000_000_000n + BigInt(fracPadded || "0");
return motes.toString();
}