cred402/api/near_receipt_anchor.ts
caelum0x c80dda9e91 feat(near): add NEAR Hackathon x402 credit-score integration
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).
2026-10-21 03:11:37 +03:00

181 lines
6.6 KiB
TypeScript

import { buildUniversalReceipt } from "../crosschain/standards/index.js";
import { hashObject } from "../lib/core/hash.js";
import type { Ledger } from "../lib/ledger/index.js";
import type { ExternalReceiptProofStore } from "../lib/x402/external_receipt_proof_store.js";
/**
* NEAR x402 receipt anchoring — mirrors `algorand_receipt_anchor.ts` /
* `qie_receipt_anchor.ts`: turns a settled NEAR credit-report payment into
* the same Casper-rooted Universal Receipt used by every other satellite
* chain, plus the durable proof persistence and public usage aggregation
* that back the paid endpoint.
*/
export interface NearAnchorContext {
/** The current live ledger (rebuilt on reset — always pass the fresh one). */
readonly ledger: Ledger;
/** Durable public receipt proofs (enabled with the production data dir). */
readonly externalReceiptProofs?: ExternalReceiptProofStore;
/** The Cred402 seller agent id that receives the anchored credit-score revenue. */
readonly sellerAgentId: string;
}
/** A settled NEAR credit-report payment awaiting a Casper-rooted anchor. */
export interface NearCreditScoreSettlement {
agentId: string;
network: string; // "near:testnet" | "near:mainnet"
networkName: string; // "testnet" | "mainnet"
amountYocto: string;
payer?: string; // NEAR account id
receiver: string; // NEAR account id
transaction?: string; // base58 tx hash
report: unknown;
}
/**
* Convert a successful NEAR x402 credit-report payment into the same
* Casper-rooted Universal Receipt used by the other satellite chains.
*
* Cred402 verifies the payer account and transaction itself (no
* facilitator), but it still does not assert a Cred402 agent id from the
* NEAR side. Preserve that boundary by using an account-derived external
* identity. Repeated delivery of the same settled transaction returns the
* existing receipt without crediting revenue twice.
*/
export function anchorNearCreditScoreSettlement(
ctx: NearAnchorContext,
input: NearCreditScoreSettlement,
): { receipt_id: string } | null {
const payer = input.payer?.trim();
const transaction = input.transaction?.trim();
if (!payer || !transaction || !/^\d+$/.test(input.amountYocto)) {
return null;
}
const existing = ctx.ledger.externalReceipts.list().find(
(receipt) =>
receipt.origin_chain === input.network &&
receipt.settlement_tx_hash === transaction,
);
if (existing) {
const sameSettlement =
existing.payer_agent_id === `near:${payer}` &&
existing.seller_agent_id === ctx.sellerAgentId &&
existing.envelope.seller_address === input.receiver &&
existing.asset === "near-native:NEAR" &&
existing.amount === input.amountYocto;
if (sameSettlement) {
persistExternalReceiptProof(ctx, existing.receipt_id);
return { receipt_id: existing.receipt_id };
}
return null;
}
const { envelope, receipt_id } = buildUniversalReceipt({
origin_chain: input.network,
settlement_network: input.networkName,
payer_agent_id: `near:${payer}`,
seller_agent_id: ctx.sellerAgentId,
payer_address: payer,
seller_address: input.receiver,
asset: "near-native:NEAR",
amount: input.amountYocto,
service_type: "credit-score",
request_hash: hashObject({
method: "GET",
route: "/v1/x402/near/credit-score/:agentId",
agent_id: input.agentId,
}),
result_hash: hashObject(input.report),
payment_proof_hash: hashObject({
network: input.network,
transaction,
payer,
receiver: input.receiver,
amount_yocto: input.amountYocto,
}),
settlement_tx_hash: transaction,
nonce: transaction,
created_at: ctx.ledger.clock.now(),
});
if (ctx.ledger.externalReceipts.get(receipt_id)) {
persistExternalReceiptProof(ctx, receipt_id);
return { receipt_id };
}
try {
// NEAR settlement is provisional until independently confirmed to a
// minimum finality depth against the configured NEAR RPC; only
// finalized receipts affect credit signals.
const anchored = ctx.ledger.anchorExternalReceipt(envelope, { finalize: false });
persistExternalReceiptProof(ctx, anchored.receipt_id);
return anchored;
} catch {
// Payment delivery must not fail if the local/indexing layer is
// temporarily unavailable. The on-chain transaction remains canonical.
return null;
}
}
export function finalizeNearExternalReceipt(ctx: NearAnchorContext, receiptId: string): void {
ctx.ledger.finalizeExternalReceipt(receiptId);
persistExternalReceiptProof(ctx, receiptId);
}
export function challengeNearExternalReceipt(ctx: NearAnchorContext, receiptId: string): void {
ctx.ledger.challengeExternalReceipt(receiptId);
persistExternalReceiptProof(ctx, receiptId);
}
/** Public, aggregate-only usage proof for the NEAR challenge endpoint. */
export function nearX402Usage(ctx: NearAnchorContext, network: string) {
const asset = "near-native:NEAR";
const byId = new Map(
(ctx.externalReceiptProofs?.list() ?? []).map((receipt) => [receipt.receipt_id, receipt]),
);
for (const receipt of ctx.ledger.externalReceipts.list()) byId.set(receipt.receipt_id, receipt);
const receipts = [...byId.values()]
.filter((receipt) =>
receipt.origin_chain === network &&
receipt.asset === asset &&
receipt.service_type === "credit-score" &&
receipt.status === "finalized",
)
.sort((a, b) => b.anchored_at - a.anchored_at);
const totalYocto = receipts.reduce(
(total, receipt) => total + BigInt(receipt.amount),
0n,
);
return {
schema_version: "cred402.near-x402-usage.v1",
network,
asset,
paid_requests: receipts.length,
unique_payers: new Set(receipts.map((receipt) => receipt.payer_agent_id)).size,
total_yocto: totalYocto.toString(),
latest_payment_at: receipts[0]
? new Date(receipts[0].anchored_at * 1000).toISOString()
: null,
latest_receipts: receipts.slice(0, 10).map((receipt) => ({
receipt_id: receipt.receipt_id,
transaction: receipt.settlement_tx_hash,
amount_yocto: receipt.amount,
anchored_at: new Date(receipt.anchored_at * 1000).toISOString(),
})),
};
}
export function persistExternalReceiptProof(ctx: NearAnchorContext, receiptId: string): void {
if (!ctx.externalReceiptProofs) return;
const receipt = ctx.ledger.externalReceipts.get(receiptId);
if (!receipt) return;
try {
ctx.externalReceiptProofs.put(receipt);
} catch {
// The paid response and live ledger anchor remain valid even if the
// durability volume is temporarily unavailable. The proof endpoint can
// still serve the live registry until storage recovers.
}
}