Adds a third chain-priced rail (alongside the existing Algorand and QIE integrations, both untouched) for the NEAR Hackathon (Nov 1-14, 2026, Agent Economy track): GET /v1/x402/near/credit-score/:agentId, settled in native NEAR on NEAR Testnet. No @x402/near scheme package exists, so lib/x402/near_gateway.ts is a from-scratch x402 v2 "exact" scheme built on @x402/core's chain-agnostic header codecs and types. It needs no third-party facilitator: Cred402 verifies settlement itself directly against the configured NEAR RPC (lib/x402/near_finality.ts), the same role the Algorand Indexer check and QIE RPC check play for their chains. - lib/x402/near_networks.ts, near_rpc.ts, near_finality.ts, near_payment_attempt_store.ts, near_gateway.ts: resource server, self-verifying settlement, durable exact-once replay protection. - api/near_x402_routes.ts, api/near_receipt_anchor.ts: HTTP routes (status, usage, payment attempts, paid credit score) and Casper-rooted receipt anchoring, wired into api/server.ts and api/state.ts. - examples/near-paid-score/typescript.ts (+ README): a real buyer that signs and submits a NEAR Testnet transaction via near-api-js. - test/near_x402.test.ts: 14 new tests (TDD), full suite 450/450 green, typecheck clean. - docs/near_x402.md, HACKATHON.md, SUBMISSION-CHECKLIST.md: protocol writeup (incl. Chain Signatures / NEAR Intents extension points), submission guide, and an explicit owner-only checklist (team info, payout account, demo video/post, form submission).
125 lines
4.2 KiB
TypeScript
125 lines
4.2 KiB
TypeScript
/**
|
|
* Minimal NEAR JSON-RPC client. Deliberately dependency-free (plain
|
|
* `fetch`), matching `qie_rpc.ts` / the rest of Cred402's "real-behind-env"
|
|
* integrations. Only the handful of NEAR JSON-RPC methods the x402 NEAR
|
|
* settlement path needs are implemented:
|
|
*
|
|
* - `EXPERIMENTAL_tx_status` — look up a transaction by hash + signer and
|
|
* read its outcome (`SuccessValue` / `Failure`) and the actions it
|
|
* executed.
|
|
* - `block` — read a block's height, either by hash (to locate where a
|
|
* transaction landed) or by `finality: "final"` (to read the current
|
|
* finalized height for a confirmation-depth check).
|
|
*
|
|
* Reference: https://docs.near.org/api/rpc/transactions and
|
|
* https://docs.near.org/api/rpc/block-chunk
|
|
*/
|
|
|
|
export interface NearRpcOptions {
|
|
rpcUrl: string;
|
|
timeoutMs?: number;
|
|
fetchFn?: typeof fetch;
|
|
}
|
|
|
|
export class NearRpcError extends Error {
|
|
constructor(
|
|
message: string,
|
|
readonly cause?: unknown,
|
|
) {
|
|
super(message);
|
|
this.name = "NearRpcError";
|
|
}
|
|
}
|
|
|
|
export interface NearTransferAction {
|
|
Transfer: { deposit: string };
|
|
}
|
|
|
|
export interface NearFunctionCallAction {
|
|
FunctionCall: { method_name: string; args: string; gas: number; deposit: string };
|
|
}
|
|
|
|
export type NearTransactionAction = NearTransferAction | NearFunctionCallAction | Record<string, unknown>;
|
|
|
|
export interface NearTxStatusResult {
|
|
status: { SuccessValue?: string; SuccessReceiptId?: string; Failure?: unknown };
|
|
transaction: {
|
|
signer_id: string;
|
|
receiver_id: string;
|
|
hash: string;
|
|
actions: NearTransactionAction[];
|
|
};
|
|
transaction_outcome: {
|
|
block_hash: string;
|
|
};
|
|
}
|
|
|
|
export interface NearBlockResult {
|
|
header: { height: number; hash: string };
|
|
}
|
|
|
|
/** Thin JSON-RPC 2.0 client for a single NEAR endpoint (testnet or mainnet). */
|
|
export class NearRpcClient {
|
|
private readonly rpcUrl: string;
|
|
private readonly timeoutMs: number;
|
|
private readonly fetchFn: typeof fetch;
|
|
|
|
constructor(opts: NearRpcOptions) {
|
|
this.rpcUrl = opts.rpcUrl;
|
|
this.timeoutMs = opts.timeoutMs ?? 10_000;
|
|
this.fetchFn = opts.fetchFn ?? fetch;
|
|
}
|
|
|
|
/** Look up a transaction's final outcome. Returns `null` when NEAR has not
|
|
* yet indexed the transaction (not an error — the payment may still be in flight). */
|
|
async txStatus(txHash: string, senderAccountId: string): Promise<NearTxStatusResult | null> {
|
|
try {
|
|
return await this.call<NearTxStatusResult>("EXPERIMENTAL_tx_status", {
|
|
tx_hash: txHash,
|
|
sender_account_id: senderAccountId,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof NearRpcError && /UNKNOWN_TRANSACTION|does not exist/i.test(error.message)) {
|
|
return null;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async blockByHash(blockHash: string): Promise<NearBlockResult> {
|
|
return this.call<NearBlockResult>("block", { block_id: blockHash });
|
|
}
|
|
|
|
async finalBlock(): Promise<NearBlockResult> {
|
|
return this.call<NearBlockResult>("block", { finality: "final" });
|
|
}
|
|
|
|
private async call<T>(method: string, params: unknown): Promise<T> {
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
try {
|
|
const res = await this.fetchFn(this.rpcUrl, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ jsonrpc: "2.0", id: "cred402", method, params }),
|
|
signal: controller.signal,
|
|
});
|
|
if (!res.ok) throw new NearRpcError(`NEAR RPC ${method} -> HTTP ${res.status}`);
|
|
const body = (await res.json()) as { result?: T; error?: { name?: string; cause?: { name?: string }; message?: string } };
|
|
if (body.error) {
|
|
const name = body.error.cause?.name ?? body.error.name ?? "unknown RPC error";
|
|
throw new NearRpcError(`NEAR RPC ${method} -> ${name}: ${body.error.message ?? ""}`.trim());
|
|
}
|
|
return body.result as T;
|
|
} catch (error) {
|
|
if (error instanceof NearRpcError) throw error;
|
|
if ((error as Error).name === "AbortError") {
|
|
throw new NearRpcError(`NEAR RPC ${method} timed out after ${this.timeoutMs}ms`);
|
|
}
|
|
throw new NearRpcError(`NEAR RPC ${method} failed: ${error instanceof Error ? error.message : String(error)}`, error);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
}
|
|
}
|
|
}
|