cred402/test/x402_eip712.test.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

76 lines
3.0 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { generateAgentKeypair } from "../lib/x402/keys.js";
import { signPayment, verifyPayment, authorizationDigest, X402_DOMAIN, type PaymentChallenge } from "../lib/x402/x402.js";
import { paymentAuthorizationDigest } from "../lib/x402/eip712.js";
/**
* Wires `@casper-ecosystem/casper-eip-712` — the x402 authorization is now signed
* over a REAL EIP-712 typed-data digest (`\x19\x01`-framed keccak256), not a
* stable-JSON string. These tests prove the digest is standards-shaped
* (0x-prefixed 32 bytes), deterministic, field-sensitive, and that the full
* ed25519 sign → verify round-trip holds over it.
*/
function challenge(overrides: Partial<PaymentChallenge> = {}): PaymentChallenge {
return {
payment_id: "pay-abc",
amount_motes: "2000000",
network: "casper",
asset: "CSPR",
resource: "/verify/energy_output?rwa_id=SOLAR-A17",
service_type: "solar_output_verification",
seller_agent: "EvidenceSellerAgent",
nonce: "nonce-xyz",
expires_at: 9_999_999_999,
...overrides,
};
}
test("eip712: authorization digest is a 0x-prefixed 32-byte hash", () => {
const auth = {
domain: X402_DOMAIN,
payment_id: "pay-abc",
payer_agent: "RWARequestAgent",
seller_agent: "EvidenceSellerAgent",
service_type: "solar_output_verification" as const,
amount_motes: "2000000",
resource: "/verify/energy_output?rwa_id=SOLAR-A17",
nonce: "nonce-xyz",
};
const d = paymentAuthorizationDigest(auth);
assert.match(d, /^0x[0-9a-f]{64}$/);
assert.equal(authorizationDigest(auth), d, "x402 uses the EIP-712 digest");
// deterministic
assert.equal(paymentAuthorizationDigest(auth), d);
// field-sensitive: changing the amount changes the digest
assert.notEqual(paymentAuthorizationDigest({ ...auth, amount_motes: "2000001" }), d);
});
test("eip712: full ed25519 sign → verify round-trip over the typed-data digest", () => {
const buyer = generateAgentKeypair();
const { proof } = signPayment({
challenge: challenge(),
payer_agent: "RWARequestAgent",
payer_public_key: buyer.publicKeyHex,
payer_private_pem: buyer.privatePem,
});
const ok = verifyPayment({ challenge: challenge(), proof, now: 1_000 });
assert.equal(ok.ok, true, ok.reason);
});
test("eip712: a tampered amount fails verification (signature bound to the digest)", () => {
const buyer = generateAgentKeypair();
const { proof } = signPayment({
challenge: challenge(),
payer_agent: "RWARequestAgent",
payer_public_key: buyer.publicKeyHex,
payer_private_pem: buyer.privatePem,
});
// Forge the signed amount; the EIP-712 digest no longer matches the signature.
const tampered = { ...proof, authorization: { ...proof.authorization, amount_motes: "9999999" } };
const res = verifyPayment({ challenge: challenge({ amount_motes: "9999999" }), proof: tampered, now: 1_000 });
assert.equal(res.ok, false);
assert.equal(res.reason, "invalid signature");
});