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(auth): consolidate authentication endpoints into a single handler with action-based routing

nirholas4c125b894e66
diff --git a/api/kol/[action].js b/api/kol/[action].js
new file mode 100644
index 000000000..6735f9579
--- /dev/null
+++ b/api/kol/[action].js
@@ -0,0 +1,80 @@
+// Consolidated KOL endpoints dispatcher.
+
+import { readFile, writeFile } from 'node:fs/promises';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { cors, json, method, readJson, wrap, error } from '../_lib/http.js';
+import { limits, clientIp } from '../_lib/rate-limit.js';
+
+const WALLETS_PATH = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../src/kol/wallets.json');
+
+async function loadWallets() {
+ try { return JSON.parse(await readFile(WALLETS_PATH, 'utf8')); } catch { return []; }
+}
+
+// ── import-gmgn ───────────────────────────────────────────────────────────────
+
+async function handleImportGmgn(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS', origins: '*' })) return;
+ if (!method(req, res, ['POST'])) return;
+ const rl = await limits.mcpIp(clientIp(req));
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many requests');
+ const body = await readJson(req);
+ if (!body || body.rawJson == null) return error(res, 400, 'validation_error', 'body.rawJson is required');
+ const { parseGmgnSmartWallets } = await import('../../src/kol/gmgn-parser.js');
+ let parsed;
+ try { parsed = parseGmgnSmartWallets(body.rawJson); }
+ catch (err) { return error(res, 400, 'validation_error', err.message); }
+ const existing = await loadWallets();
+ const byWallet = new Map(existing.map((w) => [w.wallet, w]));
+ for (const entry of parsed) byWallet.set(entry.wallet, entry);
+ const merged = [...byWallet.values()];
+ await writeFile(WALLETS_PATH, JSON.stringify(merged, null, '\t') + '\n', 'utf8');
+ return json(res, 200, { imported: parsed.length, wallets: merged });
+}
+
+// ── leaderboard ───────────────────────────────────────────────────────────────
+
+async function handleLeaderboard(req, res) {
+ if (cors(req, res, { methods: 'GET,OPTIONS', origins: '*' })) return;
+ if (!method(req, res, ['GET'])) return;
+ const rl = await limits.mcpIp(clientIp(req));
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many requests');
+ const url = new URL(req.url, `http://${req.headers.host}`);
+ const window = url.searchParams.get('window') || '7d';
+ const limitRaw = url.searchParams.get('limit');
+ const limit = limitRaw != null ? Number(limitRaw) : 25;
+ const { getLeaderboard } = await import('../../src/kol/leaderboard.js');
+ let items;
+ try { items = await getLeaderboard({ window, limit }); }
+ catch (err) { if (err.status === 400) return error(res, 400, err.code || 'validation_error', err.message); throw err; }
+ return json(res, 200, { items });
+}
+
+// ── trades ────────────────────────────────────────────────────────────────────
+
+async function handleTrades(req, res) {
+ if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
+ if (!method(req, res, ['GET'])) return;
+ const url = new URL(req.url, 'http://x');
+ const mint = url.searchParams.get('mint');
+ const limit = Math.min(100, Math.max(1, Number(url.searchParams.get('limit') || '20')));
+ if (!mint) return error(res, 400, 'validation_error', 'mint is required');
+ const { KOL_WALLETS } = await import('../../src/kol/wallets.js');
+ return json(res, 200, { mint, trades: [], wallets: KOL_WALLETS.length });
+}
+
+// ── dispatcher ────────────────────────────────────────────────────────────────
+
+const DISPATCH = {
+ 'import-gmgn': handleImportGmgn,
+ leaderboard: handleLeaderboard,
+ trades: handleTrades,
+};
+
+export default wrap(async (req, res) => {
+ const action = req.query?.action ?? new URL(req.url, 'http://x').pathname.split('/').pop();
+ const fn = DISPATCH[action];
+ if (!fn) return error(res, 404, 'not_found', `unknown kol action: ${action}`);
+ return fn(req, res);
+});
diff --git a/api/oauth/[action].js b/api/oauth/[action].js
new file mode 100644
index 000000000..76b7ec73e
--- /dev/null
+++ b/api/oauth/[action].js
@@ -0,0 +1,271 @@
+// Consolidated OAuth 2.1 endpoints dispatcher.
+// Routes: /oauth/authorize, /oauth/token, /oauth/register,
+// /oauth/revoke, /oauth/introspect
+
+import { sql } from '../_lib/db.js';
+import {
+ getSessionUser, csrfTokenFor, verifyCsrfToken, isSameSiteOrigin,
+ mintAccessToken, issueRefreshToken, rotateRefreshToken,
+ revokeRefreshToken, authenticateBearer, verifyAccessToken,
+} from '../_lib/auth.js';
+import { randomToken, sha256, sha256Base64Url, constantTimeEquals } from '../_lib/crypto.js';
+import { cors, method, wrap, error, redirect, readForm, readJson, json } from '../_lib/http.js';
+import { limits, clientIp } from '../_lib/rate-limit.js';
+import { env } from '../_lib/env.js';
+import { z } from 'zod';
+import { parse } from '../_lib/validate.js';
+
+// ── authorize ─────────────────────────────────────────────────────────────────
+
+function redirectUriMatches(requested, registered) {
+ let reqUrl;
+ try { reqUrl = new URL(requested); } catch { return false; }
+ if (reqUrl.hash || reqUrl.search) return false;
+ return registered.some((entry) => {
+ let regUrl;
+ try { regUrl = new URL(entry); } catch { return false; }
+ return reqUrl.protocol === regUrl.protocol && reqUrl.host === regUrl.host && reqUrl.port === regUrl.port && reqUrl.pathname === regUrl.pathname;
+ });
+}
+
+function parseQuery(url) {
+ return Object.fromEntries(new URL(url, 'http://x').searchParams);
+}
+
+function intersectScopes(requested, allowed) {
+ const a = new Set(allowed.split(/\s+/).filter(Boolean));
+ return requested.split(/\s+/).filter((s) => a.has(s)).join(' ') || allowed;
+}
+
+function scopeLabel(s) {
+ const labels = { 'avatars:read': 'Read your avatars', 'avatars:write': 'Create and update avatars', 'avatars:delete': 'Delete your avatars', profile: 'See your name and email', offline_access: 'Stay signed in across sessions' };
+ return labels[s] || esc(s);
+}
+
+function esc(s) {
+ return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);
+}
+
+function renderConsent(res, { client, user, params, csrf }) {
+ res.statusCode = 200;
+ res.setHeader('content-type', 'text/html; charset=utf-8');
+ res.setHeader('cache-control', 'no-store');
+ res.setHeader('content-security-policy', "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'");
+ res.setHeader('x-frame-options', 'DENY');
+ res.setHeader('x-content-type-options', 'nosniff');
+ res.setHeader('referrer-policy', 'strict-origin-when-cross-origin');
+ res.setHeader('strict-transport-security', 'max-age=63072000; includeSubDomains; preload');
+ const scope = params.scope || client.scope;
+ const scopeList = scope.split(/\s+/).filter(Boolean);
+ res.end(`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Authorize ${esc(client.name)} · three.ws</title><style>:root{color-scheme:light dark}body{font:16px/1.5 -apple-system,system-ui,Segoe UI,Roboto,sans-serif;background:#0b0b10;color:#eee;margin:0;min-height:100vh;display:grid;place-items:center;padding:24px}.card{background:#14141c;border:1px solid #2a2a36;border-radius:16px;padding:28px 28px 24px;max-width:440px;width:100%;box-shadow:0 10px 40px rgba(0,0,0,.4)}h1{font-size:20px;margin:0 0 8px}.sub{color:#aaa;margin:0 0 20px}.who{display:flex;align-items:center;gap:10px;margin-bottom:16px;padding:10px 12px;background:#1b1b25;border-radius:10px}.dot{width:32px;height:32px;border-radius:50%;background:linear-gradient(135deg,#6a5cff,#ff5ca8);display:grid;place-items:center;color:#fff;font-weight:600}ul{margin:12px 0 20px;padding-left:0;list-style:none}li{padding:8px 0;border-bottom:1px solid #22222e;display:flex;gap:10px}li:last-child{border:0}li::before{content:"✓";color:#6a5cff}.actions{display:flex;gap:10px}button{flex:1;padding:12px 16px;border-radius:10px;border:0;font-size:15px;font-weight:600;cursor:pointer}.allow{background:#6a5cff;color:#fff}.deny{background:transparent;color:#aaa;border:1px solid #2a2a36}.foot{margin-top:16px;font-size:12px;color:#777}a{color:#9a8cff}</style></head><body><form class="card" method="post" action="/api/oauth/authorize"><h1>Authorize <b>${esc(client.name)}</b></h1><p class="sub">Grant this application access to your three.ws account.</p><div class="who"><div class="dot">${esc((user.display_name || user.email)[0].toUpperCase())}</div><div><div>${esc(user.display_name || user.email)}</div><div style="color:#888;font-size:13px">${esc(user.email)}</div></div></div><p style="margin:0 0 4px"><b>${esc(client.name)}</b> will be able to:</p><ul>${scopeList.map((s) => `<li>${scopeLabel(s)}</li>`).join('')}</ul><input type="hidden" name="csrf" value="${esc(csrf || '')}"> ${Object.entries(params).filter(([k]) => k !== 'csrf' && k !== 'decision').map(([k, v]) => `<input type="hidden" name="${esc(k)}" value="${esc(v)}">`).join('')}<div class="actions"><button class="deny" type="submit" name="decision" value="deny">Cancel</button><button class="allow" type="submit" name="decision" value="allow">Authorize</button></div><p class="foot">You can revoke access any time from your <a href="/dashboard/connections">dashboard</a>.</p></form></body></html>`);
+}
+
+async function handleAuthorize(req, res) {
+ if (cors(req, res, { methods: 'GET,POST,OPTIONS', credentials: true })) return;
+ if (!method(req, res, ['GET', 'POST'])) return;
+ const params = req.method === 'POST' ? await readForm(req) : parseQuery(req.url);
+ const { response_type, client_id, redirect_uri, scope, state, code_challenge, code_challenge_method, resource } = params;
+ if (response_type !== 'code') return error(res, 400, 'unsupported_response_type', 'only response_type=code supported');
+ if (!client_id) return error(res, 400, 'invalid_request', 'client_id required');
+ if (!redirect_uri) return error(res, 400, 'invalid_request', 'redirect_uri required');
+ if (!code_challenge) return error(res, 400, 'invalid_request', 'code_challenge required (PKCE)');
+ if (!code_challenge_method) return error(res, 400, 'invalid_request', 'code_challenge_method required (must be S256)');
+ if (code_challenge_method !== 'S256') return error(res, 400, 'invalid_request', 'code_challenge_method must be S256');
+ const rows = await sql`select * from oauth_clients where client_id = ${client_id} limit 1`;
+ const client = rows[0];
+ if (!client) return error(res, 400, 'invalid_client', 'unknown client');
+ if (!redirectUriMatches(redirect_uri, client.redirect_uris)) return error(res, 400, 'invalid_redirect_uri', 'redirect_uri not registered');
+ const user = await getSessionUser(req);
+ if (!user) {
+ const next = encodeURIComponent(`/oauth/consent?${new URLSearchParams(params).toString()}`);
+ return redirect(res, `/login?next=${next}`);
+ }
+ if (req.method === 'GET') {
+ const csrf = await csrfTokenFor(req);
+ return renderConsent(res, { client, user, params, csrf });
+ }
+ if (!isSameSiteOrigin(req)) return error(res, 403, 'forbidden', 'cross-site request blocked');
+ if (!(await verifyCsrfToken(req, params.csrf))) return error(res, 403, 'forbidden', 'invalid csrf token');
+ if (params.decision !== 'allow') {
+ const denied = new URL(redirect_uri);
+ denied.searchParams.set('error', 'access_denied');
+ if (state) denied.searchParams.set('state', state);
+ return redirect(res, denied.toString());
+ }
+ const code = randomToken(24);
+ const grantedScope = intersectScopes(scope || client.scope, client.scope);
+ await sql`insert into oauth_auth_codes (code, client_id, user_id, redirect_uri, scope, resource, code_challenge, code_challenge_method, expires_at) values (${code}, ${client_id}, ${user.id}, ${redirect_uri}, ${grantedScope}, ${resource ?? env.MCP_RESOURCE}, ${code_challenge}, 'S256', now() + ${'60 seconds'}::interval)`;
+ const back = new URL(redirect_uri);
+ back.searchParams.set('code', code);
+ if (state) back.searchParams.set('state', state);
+ return redirect(res, back.toString());
+}
+
+// ── token ─────────────────────────────────────────────────────────────────────
+
+function basicAuthUser(req) {
+ const h = req.headers.authorization || '';
+ if (!h.toLowerCase().startsWith('basic ')) return null;
+ try { return Buffer.from(h.slice(6), 'base64').toString('utf8').split(':')[0]; } catch { return null; }
+}
+function basicAuthPass(req) {
+ const h = req.headers.authorization || '';
+ if (!h.toLowerCase().startsWith('basic ')) return null;
+ try { return Buffer.from(h.slice(6), 'base64').toString('utf8').split(':').slice(1).join(':'); } catch { return null; }
+}
+function isSubsetScope(requested, stored) {
+ const allowed = new Set(stored.split(/\s+/).filter(Boolean));
+ const asked = requested.split(/\s+/).filter(Boolean);
+ return asked.length > 0 && asked.every((s) => allowed.has(s));
+}
+
+async function handleToken(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
+ if (!method(req, res, ['POST'])) return;
+ const form = await readForm(req);
+ const clientId = form.client_id || basicAuthUser(req);
+ if (!clientId) return error(res, 400, 'invalid_client', 'client_id required');
+ const rl = await limits.oauthToken(clientId);
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many token requests');
+ const rows = await sql`select * from oauth_clients where client_id = ${clientId} limit 1`;
+ const client = rows[0];
+ if (!client) return error(res, 400, 'invalid_client', 'unknown client');
+ if (client.client_type === 'confidential') {
+ const secret = form.client_secret || basicAuthPass(req) || '';
+ const providedHash = await sha256(secret);
+ if (!client.client_secret_hash || !constantTimeEquals(providedHash, client.client_secret_hash)) return error(res, 401, 'invalid_client', 'bad client credentials');
+ }
+ const grantType = form.grant_type;
+ if (grantType === 'authorization_code') {
+ const { code, redirect_uri, code_verifier } = form;
+ if (!code || !redirect_uri || !code_verifier) return error(res, 400, 'invalid_request', 'code, redirect_uri, code_verifier required');
+ const r = await sql`select * from oauth_auth_codes where code = ${code} limit 1`;
+ const row = r[0];
+ if (!row) return error(res, 400, 'invalid_grant', 'unknown code');
+ if (row.consumed_at) {
+ await sql`update oauth_refresh_tokens set revoked_at = now() where user_id = ${row.user_id} and client_id = ${client.client_id} and revoked_at is null`;
+ return error(res, 400, 'invalid_grant', 'authorization code already used');
+ }
+ if (new Date(row.expires_at) < new Date()) return error(res, 400, 'invalid_grant', 'code expired');
+ if (row.client_id !== client.client_id) return error(res, 400, 'invalid_grant', 'client mismatch');
+ if (row.redirect_uri !== redirect_uri) return error(res, 400, 'invalid_grant', 'redirect_uri mismatch');
+ if (await sha256Base64Url(code_verifier) !== row.code_challenge) return error(res, 400, 'invalid_grant', 'PKCE verification failed');
+ await sql`update oauth_auth_codes set consumed_at = now() where code = ${code}`;
+ const accessToken = await mintAccessToken({ userId: row.user_id, clientId: client.client_id, scope: row.scope, resource: row.resource || env.MCP_RESOURCE });
+ const wantsRefresh = client.grant_types.includes('refresh_token');
+ const refresh = wantsRefresh ? await issueRefreshToken({ userId: row.user_id, clientId: client.client_id, scope: row.scope, resource: row.resource }) : null;
+ return json(res, 200, { access_token: accessToken, token_type: 'Bearer', expires_in: 3600, scope: row.scope, ...(refresh ? { refresh_token: refresh.token } : {}) });
+ }
+ if (grantType === 'refresh_token') {
+ const { refresh_token, scope } = form;
+ if (!refresh_token) return error(res, 400, 'invalid_request', 'refresh_token required');
+ let result;
+ try {
+ result = await rotateRefreshToken({ oldSecret: refresh_token, clientId: client.client_id, narrowScope: (stored) => { if (!scope) return stored; if (!isSubsetScope(scope, stored)) throw Object.assign(new Error('requested scope exceeds granted scope'), { status: 400, code: 'invalid_scope' }); return scope.split(/\s+/).filter(Boolean).join(' '); } });
+ } catch (err) { return error(res, err.status || 400, err.code || 'invalid_grant', err.message); }
+ const accessToken = await mintAccessToken({ userId: result.userId, clientId: client.client_id, scope: result.scope, resource: result.resource || env.MCP_RESOURCE });
+ return json(res, 200, { access_token: accessToken, token_type: 'Bearer', expires_in: 3600, scope: result.scope, refresh_token: result.next.token });
+ }
+ return error(res, 400, 'unsupported_grant_type', `grant_type=${grantType} not supported`);
+}
+
+// ── register (dynamic client registration) ────────────────────────────────────
+
+const registerSchema = z.object({
+ redirect_uris: z.array(z.string().url()).min(1).max(10),
+ client_name: z.string().trim().min(1).max(120).optional(),
+ client_uri: z.string().url().optional(),
+ logo_uri: z.string().url().optional(),
+ scope: z.string().max(500).optional(),
+ grant_types: z.array(z.string()).optional(),
+ response_types: z.array(z.string()).optional(),
+ token_endpoint_auth_method: z.enum(['none', 'client_secret_basic', 'client_secret_post']).optional(),
+ software_id: z.string().max(120).optional(),
+ software_version: z.string().max(60).optional(),
+});
+
+async function handleRegister(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
+ if (!method(req, res, ['POST'])) return;
+ const rl = await limits.oauthRegisterIp(clientIp(req));
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many registrations from this IP');
+ const body = parse(registerSchema, await readJson(req));
+ for (const uri of body.redirect_uris) {
+ const u = new URL(uri);
+ if (u.protocol === 'http:' && !/^localhost$|^127\.0\.0\.1$/.test(u.hostname)) return error(res, 400, 'invalid_redirect_uri', 'non-https redirect URIs only allowed for localhost');
+ }
+ const authMethod = body.token_endpoint_auth_method ?? 'none';
+ const clientType = authMethod === 'none' ? 'public' : 'confidential';
+ const clientId = `mcp_${randomToken(18)}`;
+ let clientSecret = null, secretHash = null;
+ if (clientType === 'confidential') { clientSecret = randomToken(32); secretHash = await sha256(clientSecret); }
+ const scope = body.scope ?? 'avatars:read';
+ const grantTypes = body.grant_types ?? ['authorization_code', 'refresh_token'];
+ const responseTypes = body.response_types ?? ['code'];
+ await sql`insert into oauth_clients (client_id, client_secret_hash, client_type, name, logo_uri, client_uri, redirect_uris, grant_types, response_types, token_endpoint_auth, scope, software_id, software_version, dynamically_registered) values (${clientId}, ${secretHash}, ${clientType}, ${body.client_name ?? 'MCP Client'}, ${body.logo_uri ?? null}, ${body.client_uri ?? null}, ${body.redirect_uris}, ${grantTypes}, ${responseTypes}, ${authMethod}, ${scope}, ${body.software_id ?? null}, ${body.software_version ?? null}, true)`;
+ return json(res, 201, { client_id: clientId, ...(clientSecret ? { client_secret: clientSecret } : {}), client_id_issued_at: Math.floor(Date.now() / 1000), token_endpoint_auth_method: authMethod, redirect_uris: body.redirect_uris, grant_types: grantTypes, response_types: responseTypes, scope, client_name: body.client_name ?? 'MCP Client' });
+}
+
+// ── revoke ────────────────────────────────────────────────────────────────────
+
+async function handleRevoke(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
+ if (!method(req, res, ['POST'])) return;
+ const form = await readForm(req);
+ const { token, token_type_hint, client_id, client_secret } = form;
+ if (!token || !client_id) return error(res, 400, 'invalid_request', 'token and client_id required');
+ const rows = await sql`select * from oauth_clients where client_id = ${client_id} limit 1`;
+ const client = rows[0];
+ if (!client) return json(res, 200, {});
+ if (client.client_type === 'confidential') {
+ const hash = await sha256(client_secret ?? '');
+ if (!client.client_secret_hash || !constantTimeEquals(hash, client.client_secret_hash)) return error(res, 401, 'invalid_client', 'bad client credentials');
+ }
+ if (token_type_hint !== 'access_token') await revokeRefreshToken(token, client_id);
+ return json(res, 200, {});
+}
+
+// ── introspect ────────────────────────────────────────────────────────────────
+
+async function handleIntrospect(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
+ if (!method(req, res, ['POST'])) return;
+ const form = await readForm(req);
+ const { token, client_id, client_secret } = form;
+ if (!token || !client_id) return error(res, 400, 'invalid_request', 'token and client_id required');
+ const rows = await sql`select * from oauth_clients where client_id = ${client_id} limit 1`;
+ const client = rows[0];
+ if (!client) return json(res, 200, { active: false });
+ if (client.client_type === 'confidential') {
+ const hash = await sha256(client_secret ?? '');
+ if (!client.client_secret_hash || !constantTimeEquals(hash, client.client_secret_hash)) return error(res, 401, 'invalid_client', 'bad client credentials');
+ }
+ try {
+ const payload = await verifyAccessToken(token);
+ if (payload.client_id && payload.client_id !== client_id) return json(res, 200, { active: false });
+ return json(res, 200, { active: true, scope: payload.scope, client_id: payload.client_id, sub: payload.sub, aud: payload.aud, iss: payload.iss, exp: payload.exp, iat: payload.iat, token_type: 'Bearer' });
+ } catch {
+ const h = await sha256(token);
+ const r = await sql`select user_id, scope, expires_at, revoked_at from oauth_refresh_tokens where token_hash = ${h} and client_id = ${client_id} limit 1`;
+ const row = r[0];
+ if (!row || row.revoked_at || new Date(row.expires_at) < new Date()) return json(res, 200, { active: false });
+ return json(res, 200, { active: true, scope: row.scope, client_id, sub: row.user_id, token_type: 'refresh_token' });
+ }
+}
+
+// ── dispatcher ────────────────────────────────────────────────────────────────
+
+const DISPATCH = {
+ authorize: handleAuthorize,
+ token: handleToken,
+ register: handleRegister,
+ revoke: handleRevoke,
+ introspect: handleIntrospect,
+};
+
+export default wrap(async (req, res) => {
+ const action = req.query?.action ?? new URL(req.url, 'http://x').pathname.split('/').pop();
+ const fn = DISPATCH[action];
+ if (!fn) return error(res, 404, 'not_found', `unknown oauth action: ${action}`);
+ return fn(req, res);
+});
diff --git a/api/payments/evm/[action].js b/api/payments/evm/[action].js
new file mode 100644
index 000000000..abe0ed24f
--- /dev/null
+++ b/api/payments/evm/[action].js
@@ -0,0 +1,103 @@
+// Consolidated EVM payment endpoints (checkout + confirm).
+
+import { z } from 'zod';
+import { sql } from '../../_lib/db.js';
+import { getSessionUser } from '../../_lib/auth.js';
+import { cors, json, method, readJson, wrap, error } from '../../_lib/http.js';
+import { limits, clientIp } from '../../_lib/rate-limit.js';
+import { parse } from '../../_lib/validate.js';
+import { randomToken } from '../../_lib/crypto.js';
+import { env } from '../../_lib/env.js';
+import { sendSubscriptionConfirmEmail } from '../../_lib/email.js';
+import { PLANS, EVM_USDC, getEvmRecipient, INTENT_TTL_MINUTES } from '../_config.js';
+
+// ── checkout ──────────────────────────────────────────────────────────────────
+
+const checkoutSchema = z.object({
+ plan: z.enum(['pro', 'team', 'enterprise']),
+ chain_id: z.number().int().positive(),
+});
+
+async function handleCheckout(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
+ if (!method(req, res, ['POST'])) return;
+ const user = await getSessionUser(req);
+ if (!user) return error(res, 401, 'unauthorized', 'sign in required');
+ const rl = await limits.authIp(clientIp(req));
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many requests');
+ const body = parse(checkoutSchema, await readJson(req));
+ const { plan, chain_id } = body;
+ if (!EVM_USDC[chain_id]) return error(res, 400, 'unsupported_chain', `chain ${chain_id} is not supported for payments`);
+ const recipient = getEvmRecipient(chain_id);
+ if (!recipient) return error(res, 503, 'not_configured', 'payment recipient not configured for this chain');
+ const planConfig = PLANS[plan];
+ const amountUsdc = planConfig.price_usd;
+ const nonce = await randomToken(16);
+ const expiresAt = new Date(Date.now() + INTENT_TTL_MINUTES * 60 * 1000);
+ const [intent] = await sql`insert into plan_payment_intents (user_id, plan, chain_type, chain_id, amount_usdc, recipient, nonce, expires_at) values (${user.id}, ${plan}, 'evm', ${chain_id}, ${amountUsdc}, ${recipient.toLowerCase()}, ${nonce}, ${expiresAt}) returning id, nonce, amount_usdc, recipient, expires_at`;
+ return json(res, 201, { intent_id: intent.id, plan, chain_id, usdc_address: EVM_USDC[chain_id], recipient, amount_usdc: amountUsdc, amount_atomics: String(BigInt(Math.round(amountUsdc * 1_000_000))), nonce, expires_at: intent.expires_at, instructions: `Transfer exactly ${amountUsdc} USDC to ${recipient} on chain ${chain_id}. Include nonce "${nonce}" in memo/calldata if possible, then call /api/payments/evm/confirm.` });
+}
+
+// ── confirm ───────────────────────────────────────────────────────────────────
+
+import { createPublicClient, http, parseAbi } from 'viem';
+import { mainnet, base, optimism, arbitrum, polygon, sepolia, baseSepolia } from 'viem/chains';
+
+const CHAINS = { 1: mainnet, 8453: base, 10: optimism, 42161: arbitrum, 137: polygon, 11155111: sepolia, 84532: baseSepolia };
+const ERC20_ABI = parseAbi(['event Transfer(address indexed from, address indexed to, uint256 value)']);
+
+const confirmSchema = z.object({
+ intent_id: z.string().uuid(),
+ tx_hash: z.string().regex(/^0x[a-fA-F0-9]{64}$/),
+});
+
+async function handleConfirm(req, res) {
+ if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
+ if (!method(req, res, ['POST'])) return;
+ const user = await getSessionUser(req);
+ if (!user) return error(res, 401, 'unauthorized', 'sign in required');
+ const rl = await limits.authIp(clientIp(req));
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many requests');
+ const body = parse(confirmSchema, await readJson(req));
+ const { intent_id, tx_hash } = body;
+ const [intent] = await sql`select * from plan_payment_intents where id = ${intent_id} and user_id = ${user.id} limit 1`;
+ if (!intent) return error(res, 404, 'not_found', 'intent not found');
+ if (intent.status === 'confirmed') return error(res, 409, 'already_confirmed', 'payment already confirmed');
+ if (intent.status === 'expired' || new Date(intent.expires_at) < new Date()) {
+ await sql`update plan_payment_intents set status='expired' where id=${intent_id}`;
+ return error(res, 410, 'intent_expired', 'payment session expired — start a new checkout');
+ }
+ const chainId = intent.chain_id;
+ const chain = CHAINS[chainId];
+ if (!chain) return error(res, 400, 'unsupported_chain', `chain ${chainId} not supported`);
+ const rpcUrl = env.getRpcUrl(chainId);
+ const client = createPublicClient({ chain, transport: http(rpcUrl || undefined) });
+ let receipt;
+ try { receipt = await client.getTransactionReceipt({ hash: tx_hash }); }
+ catch { return error(res, 422, 'tx_not_found', 'transaction not found on chain — may need more confirmations'); }
+ if (receipt.status !== 'success') return error(res, 422, 'tx_failed', 'transaction reverted');
+ const usdcAddress = EVM_USDC[chainId]?.toLowerCase();
+ const logs = await client.getLogs({ address: usdcAddress, event: ERC20_ABI[0], fromBlock: receipt.blockNumber, toBlock: receipt.blockNumber });
+ const expectedRecipient = intent.recipient.toLowerCase();
+ const expectedAtomics = BigInt(Math.round(Number(intent.amount_usdc) * 1_000_000));
+ const matchingLog = logs.find((log) => log.transactionHash?.toLowerCase() === tx_hash.toLowerCase() && log.args.to?.toLowerCase() === expectedRecipient && BigInt(log.args.value ?? 0) >= expectedAtomics);
+ if (!matchingLog) return error(res, 422, 'transfer_not_found', `No USDC transfer of ${intent.amount_usdc} USDC to ${expectedRecipient} found in tx`);
+ await sql`update plan_payment_intents set status='confirmed', tx_hash=${tx_hash}, confirmed_at=now() where id=${intent_id}`;
+ const planConfig = PLANS[intent.plan];
+ const activeUntil = new Date(Date.now() + planConfig.duration_days * 86400 * 1000);
+ await sql`insert into subscriptions (user_id, plan, chain_type, chain_id, token_address, tx_hash, amount_usd, status, active_until) values (${user.id}, ${intent.plan}, 'evm', ${chainId}, ${usdcAddress}, ${tx_hash}, ${intent.amount_usdc}, 'active', ${activeUntil}) on conflict (user_id) where status='active' do update set plan=excluded.plan, chain_type=excluded.chain_type, chain_id=excluded.chain_id, token_address=excluded.token_address, tx_hash=excluded.tx_hash, amount_usd=excluded.amount_usd, active_until=excluded.active_until, updated_at=now()`;
+ await sql`update users set plan=${intent.plan} where id=${user.id}`;
+ queueMicrotask(() => sendSubscriptionConfirmEmail({ to: user.email, plan: intent.plan, chain: `EVM chain ${chainId}`, txId: tx_hash }));
+ return json(res, 200, { ok: true, plan: intent.plan, active_until: activeUntil.toISOString(), tx_hash });
+}
+
+// ── dispatcher ────────────────────────────────────────────────────────────────
+
+const DISPATCH = { checkout: handleCheckout, confirm: handleConfirm };
+
+export default wrap(async (req, res) => {
+ const action = req.query?.action ?? new URL(req.url, 'http://x').pathname.split('/').pop();
+ const fn = DISPATCH[action];
+ if (!fn) return error(res, 404, 'not_found', `unknown evm payment action: ${action}`);
+ return fn(req, res);
+});
diff --git a/api/wk.js b/api/wk.js
new file mode 100644
index 000000000..cf79ff4a0
--- /dev/null
+++ b/api/wk.js
@@ -0,0 +1,94 @@
+// Consolidated /.well-known/* handler.
+// Dispatches on ?name= query param set by vercel.json rewrites.
+
+import { cors, json, method, wrap, error } from './_lib/http.js';
+import { env } from './_lib/env.js';
+
+// ── agent-attestation-schemas ─────────────────────────────────────────────────
+
+const COMMON = {
+ v: { type: 'integer', const: 1 },
+ kind: { type: 'string' },
+ agent: { type: 'string', description: 'Metaplex Core asset pubkey (base58)' },
+ ts: { type: 'integer', description: 'Unix seconds when attestation was created' },
+};
+
+const SCHEMAS = {
+ 'threews.feedback.v1': { description: 'Client → agent feedback.', required: ['v', 'kind', 'agent', 'score'], properties: { ...COMMON, score: { type: 'integer', minimum: 1, maximum: 5 }, task_id: { type: 'string' }, uri: { type: 'string', format: 'uri' } } },
+ 'threews.validation.v1': { description: 'Validator attestation about an agent task result.', required: ['v', 'kind', 'agent', 'task_hash', 'passed'], properties: { ...COMMON, task_hash: { type: 'string' }, passed: { type: 'boolean' }, uri: { type: 'string', format: 'uri' } } },
+ 'threews.task.v1': { description: 'Client posts a task offer to an agent.', required: ['v', 'kind', 'agent', 'task_id', 'scope_hash'], properties: { ...COMMON, task_id: { type: 'string' }, scope_hash: { type: 'string' }, uri: { type: 'string', format: 'uri' } } },
+ 'threews.accept.v1': { description: 'Agent accepts a task.', required: ['v', 'kind', 'agent', 'task_id'], properties: { ...COMMON, task_id: { type: 'string' } } },
+ 'threews.revoke.v1': { description: 'Revoke a previous attestation.', required: ['v', 'kind', 'agent', 'target_signature'], properties: { ...COMMON, target_signature: { type: 'string' }, reason: { type: 'string' } } },
+ 'threews.dispute.v1': { description: 'Agent owner disputes a feedback or validation.', required: ['v', 'kind', 'agent', 'target_signature'], properties: { ...COMMON, target_signature: { type: 'string' }, reason: { type: 'string' }, uri: { type: 'string', format: 'uri' } } },
+};
+
+function handleAttestationSchemas(req, res) {
+ return json(res, 200, {
+ version: 1,
+ transport: { type: 'spl-memo', program: 'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr', note: 'Each attestation is a signed memo tx with the agent asset pubkey as a non-signer key.' },
+ schemas: SCHEMAS,
+ discovery: { list_endpoint: '/api/agents/solana-attestations?asset=<pubkey>&kind=...', reputation_endpoint: '/api/agents/solana-reputation?asset=<pubkey>' },
+ }, { 'cache-control': 'public, max-age=300' });
+}
+
+// ── oauth-authorization-server ────────────────────────────────────────────────
+
+function handleOauthAuthServer(req, res) {
+ const base = env.APP_ORIGIN;
+ return json(res, 200, {
+ issuer: base,
+ authorization_endpoint: `${base}/oauth/authorize`,
+ token_endpoint: `${base}/api/oauth/token`,
+ registration_endpoint: `${base}/api/oauth/register`,
+ revocation_endpoint: `${base}/api/oauth/revoke`,
+ introspection_endpoint: `${base}/api/oauth/introspect`,
+ response_types_supported: ['code'],
+ grant_types_supported: ['authorization_code', 'refresh_token'],
+ code_challenge_methods_supported: ['S256'],
+ token_endpoint_auth_methods_supported: ['none', 'client_secret_basic', 'client_secret_post'],
+ scopes_supported: ['avatars:read', 'avatars:write', 'avatars:delete', 'profile', 'offline_access'],
+ service_documentation: `${base}/docs/mcp`,
+ ui_locales_supported: ['en'],
+ }, { 'cache-control': 'public, max-age=300' });
+}
+
+// ── oauth-protected-resource ──────────────────────────────────────────────────
+
+function handleOauthProtectedResource(req, res) {
+ return json(res, 200, {
+ resource: env.MCP_RESOURCE,
+ authorization_servers: [env.APP_ORIGIN],
+ bearer_methods_supported: ['header'],
+ resource_documentation: `${env.APP_ORIGIN}/docs/mcp`,
+ scopes_supported: ['avatars:read', 'avatars:write', 'avatars:delete', 'profile'],
+ }, { 'cache-control': 'public, max-age=300' });
+}
+
+// ── x402 ─────────────────────────────────────────────────────────────────────
+
+function handleX402(req, res) {
+ return json(res, 200, {
+ version: 1,
+ resources: ['POST /api/mcp'],
+ schemes: ['solana-pay', 'evm-erc20', 'pump-agent-payments'],
+ pump_agent_payments: { prep: '/api/pump/accept-payment-prep', confirm: '/api/pump/accept-payment-confirm', balances: '/api/pump/balances' },
+ }, { 'cache-control': 'public, max-age=300' });
+}
+
+// ── dispatcher ────────────────────────────────────────────────────────────────
+
+const DISPATCH = {
+ 'agent-attestation-schemas': handleAttestationSchemas,
+ 'oauth-authorization-server': handleOauthAuthServer,
+ 'oauth-protected-resource': handleOauthProtectedResource,
+ 'x402': handleX402,
+};
+
+export default wrap(async (req, res) => {
+ if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
+ if (!method(req, res, ['GET'])) return;
+ const name = req.query?.name ?? new URL(req.url, 'http://x').searchParams.get('name');
+ const fn = DISPATCH[name];
+ if (!fn) return error(res, 404, 'not_found', `unknown well-known resource: ${name}`);
+ return fn(req, res);
+});