Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(x402): checkout record replay-guard wiring + a2a payments test coverage

nirholas8f6ae2a3965e
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.js
index 764e24ea6..8d9804178 100644
--- a/api/_lib/rate-limit.js
+++ b/api/_lib/rate-limit.js
@@ -225,6 +225,10 @@ export const limits = {
// without Redis in prod rather than allowing unbounded paid inference.
bountyJudge: (userId) =>
getLimiter('bounty:judge:user', { limit: 30, window: '1 h', critical: true }).limit(userId),
+ // Agent action-log append (api/agent-actions POST). Append-only, never-deleted
+ // table, so cap per user to prevent unbounded storage growth from a script.
+ agentActionAppend: (userId) =>
+ getLimiter('agent:action:append', { limit: 120, window: '1 m' }).limit(userId),
// Bounty creation + submission. Both are authenticated writes to public,
// everyone-reads tables, so cap per user to stop one account scripting spam
// that pollutes the feed and bloats storage.
diff --git a/api/x402-checkout-record.js b/api/x402-checkout-record.js
index 2bc5679c9..33b40c9d3 100644
--- a/api/x402-checkout-record.js
+++ b/api/x402-checkout-record.js
@@ -11,8 +11,9 @@
import { z } from 'zod';
import { sql } from './_lib/db.js';
-import { cors, json, readJson, wrap, error } from './_lib/http.js';
+import { cors, json, readJson, wrap, error, rateLimited } from './_lib/http.js';
import { parse } from './_lib/validate.js';
+import { limits, clientIp } from './_lib/rate-limit.js';
const recordSchema = z.object({
sku_id: z.string().uuid(),
@@ -29,6 +30,11 @@ export default wrap(async (req, res) => {
if (cors(req, res, { origins: '*', methods: 'POST,OPTIONS' })) return;
if (req.method !== 'POST') return error(res, 405, 'method_not_allowed', 'use POST');
+ // Public + write endpoint — bound per IP so it can't be scripted into a flood
+ // of fabricated revenue rows.
+ const rl = await limits.x402RecordIp(clientIp(req));
+ if (!rl.success) return rateLimited(res, rl, 'too many checkout records, slow down');
+
const body = parse(recordSchema, await readJson(req));
// Confirm the SKU exists (and is active) before inserting — keeps the table
@@ -36,11 +42,26 @@ export default wrap(async (req, res) => {
const [sku] = await sql`select id from x402_skus where id = ${body.sku_id} and archived_at is null limit 1`;
if (!sku) return error(res, 404, 'sku_not_found', 'no active SKU with that id');
+ // Replay guard: a given on-chain tx may only be recorded once per SKU.
+ // Without this, replaying one real payment header inflates the merchant's
+ // revenue/conversion analytics arbitrarily. Idempotently return the existing
+ // row instead of erroring so a legitimate double-submit is a no-op. The
+ // (sku_id, tx_signature) unique index makes this race-safe under concurrency.
+ if (body.tx_signature) {
+ const [existing] = await sql`
+ select id, paid_at from x402_checkout_calls
+ where sku_id = ${body.sku_id} and tx_signature = ${body.tx_signature} limit 1
+ `;
+ if (existing) return json(res, 200, { ok: true, id: existing.id, paid_at: existing.paid_at, deduped: true });
+ }
+
const ip = (req.headers['x-forwarded-for'] || req.socket?.remoteAddress || '').toString().split(',')[0].trim();
const ipHash = ip ? simpleHash(ip) : null;
const ua = (req.headers['user-agent'] || '').toString().slice(0, 240);
- const [row] = await sql`
+ let row;
+ try {
+ [row] = await sql`
insert into x402_checkout_calls (
sku_id, network, tx_signature, payer_address,
amount_atomics, asset, response_status, error_code,
@@ -52,8 +73,20 @@ export default wrap(async (req, res) => {
${body.response_status}, ${body.error_code ?? null},
${ipHash}, ${ua}
)
- returning id, paid_at
- `;
+ returning id, paid_at
+ `;
+ } catch (err) {
+ // Lost the race to a concurrent record of the same tx — the unique index
+ // rejected the duplicate. Return the row that won, idempotently.
+ if (body.tx_signature && /duplicate key|unique/i.test(err?.message || '')) {
+ const [existing] = await sql`
+ select id, paid_at from x402_checkout_calls
+ where sku_id = ${body.sku_id} and tx_signature = ${body.tx_signature} limit 1
+ `;
+ if (existing) return json(res, 200, { ok: true, id: existing.id, paid_at: existing.paid_at, deduped: true });
+ }
+ throw err;
+ }
return json(res, 201, { ok: true, id: row.id, paid_at: row.paid_at });
});
diff --git a/tests/agent-a2a-payments.test.js b/tests/agent-a2a-payments.test.js
index 7b5761934..92186671c 100644
--- a/tests/agent-a2a-payments.test.js
+++ b/tests/agent-a2a-payments.test.js
@@ -252,6 +252,14 @@ vi.mock('../api/_lib/x402/a2a-client.js', () => ({
submitPayment: vi.fn(async () => clientState.submit),
buildEvmExactPayload: vi.fn(async () => ({ scheme: 'exact', payload: {} })),
createPrivateKeySigner: vi.fn(async () => ({ address: '0xPayer', signAuthorization: vi.fn() })),
+ buildSolanaExactPayload: vi.fn(async () => ({ scheme: 'exact', payload: { transaction: 'base64tx' } })),
+ createSolanaSigner: vi.fn(async () => ({ address: 'SoLPayer1111111111111111111111111111111111' })),
+ NETWORK_SOLANA_MAINNET: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
+ NETWORK_SOLANA_DEVNET: 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
+ isSolanaNetwork: (network) =>
+ network === 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' ||
+ network === 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1' ||
+ network === 'solana',
}));
const { invoke } = await import('./_helpers/monetization.js');