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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(wallet): move the wallet:write spend gate into spend-scope.js so every money route imports it directly
nirholas008d23bf26e7
diff --git a/api/_lib/auth.js b/api/_lib/auth.jsindex f43e64c54..43da369c1 100644--- a/api/_lib/auth.js+++ b/api/_lib/auth.js@@ -407,23 +407,6 @@ export function requestUserHasScope(user, required) {return hasScope(user.scope, required);}-// The scope a bearer principal must hold to move, commit, or redirect funds.-export const SPEND_SCOPE = 'wallet:write';--// Gate for a route that spends from a custodial wallet (withdraw, trade, pay,-// hire, launch, arm an autonomous spender) or changes where its money goes. The-// MCP tools behind these actions already demand `wallet:write`, but the REST-// routes accepted ANY bearer, so an `inference`-only key or an OAuth client the-// user approved for "Read your avatars" could withdraw an agent wallet to its-// own address. A cookie session never reaches here (pass the bearer only).-// Safe methods pass so a `wallet:read` key still reads balances and history.-export function assertBearerMaySpend(bearer, req) {- if (!bearer) return bearer;- const verb = String(req?.method || 'POST').toUpperCase();- if (verb === 'GET' || verb === 'HEAD' || verb === 'OPTIONS') return bearer;- if (hasScope(bearer.scope, SPEND_SCOPE)) return bearer;- throw Object.assign(- new Error(`this credential lacks the ${SPEND_SCOPE} scope required to move funds`),- { status: 403, code: 'insufficient_scope', expose: true },- );-}+// Re-exported for callers that import every auth helper from here; the gate+// itself lives in spend-scope.js so a mocked auth.js cannot remove it.+export { SPEND_SCOPE, assertBearerMaySpend } from './spend-scope.js';diff --git a/api/_lib/spend-scope.js b/api/_lib/spend-scope.jsnew file mode 100644index 000000000..afc10f039--- /dev/null+++ b/api/_lib/spend-scope.js@@ -0,0 +1,34 @@+// The bearer-scope gate for routes that move money.+//+// A cookie session is the account holder, present, and carries every scope. A+// bearer principal (API key or OAuth access token) holds only what it was+// granted. The MCP tools behind withdraw, trade, pay, hire and launch already+// demanded `wallet:write`, but their REST routes accepted ANY bearer, so an+// `inference`-only key or an OAuth client the user approved for "Read your+// avatars" could withdraw an agent wallet to its own address.+//+// Kept free of imports on purpose: route tests mock api/_lib/auth.js wholesale,+// and a gate that lived only there would vanish under those mocks.++// The scope a bearer principal must hold to move, commit, or redirect funds.+export const SPEND_SCOPE = 'wallet:write';++function grants(scope, required) {+ return String(scope || '').split(/\s+/).filter(Boolean).includes(required);+}++// Gate for a route that spends from a custodial wallet (withdraw, trade, pay,+// hire, launch, arm an autonomous spender) or changes where its money goes.+// Pass the bearer only; a session never reaches here. Safe methods pass so a+// `wallet:read` key still reads balances and history. Returns the bearer, or+// throws a 403 `insufficient_scope` that wrap() renders as-is.+export function assertBearerMaySpend(bearer, req) {+ if (!bearer) return bearer;+ const verb = String(req?.method || 'POST').toUpperCase();+ if (verb === 'GET' || verb === 'HEAD' || verb === 'OPTIONS') return bearer;+ if (grants(bearer.scope, SPEND_SCOPE)) return bearer;+ throw Object.assign(+ new Error(`this credential lacks the ${SPEND_SCOPE} scope required to move funds`),+ { status: 403, code: 'insufficient_scope', expose: true },+ );+}diff --git a/api/agents/a2a-call.js b/api/agents/a2a-call.jsindex 9cf41b124..ff72f03a4 100644--- a/api/agents/a2a-call.js+++ b/api/agents/a2a-call.js@@ -29,7 +29,8 @@// rail — USDC SPL TransferChecked, partially signed by the platform payer and// co-signed by the peer's facilitator fee payer — with EVM EIP-3009 as fallback.-import { authenticateBearer, extractBearer, getSessionUser, assertBearerMaySpend } from '../_lib/auth.js';+import { authenticateBearer, extractBearer, getSessionUser } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, error, json, method, rateLimited, readJson, respondError, serverError, wrap } from '../_lib/http.js';import { limits } from '../_lib/rate-limit.js';import { env } from '../_lib/env.js';diff --git a/api/agents/a2a-hire.js b/api/agents/a2a-hire.jsindex 2f1558bc1..25f7ae1ea 100644--- a/api/agents/a2a-hire.js+++ b/api/agents/a2a-hire.js@@ -23,7 +23,8 @@// 6. Finalize the hire + custody ledger so both wallets show the income/outlay and// the marketplace's completion stats update from real data only.-import { authenticateBearer, extractBearer, getSessionUser, assertBearerMaySpend } from '../_lib/auth.js';+import { authenticateBearer, extractBearer, getSessionUser } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, error, json, method, rateLimited, readJson, wrap } from '../_lib/http.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';diff --git a/api/agents/agent-strategy-objects.js b/api/agents/agent-strategy-objects.jsindex 8c326fc05..02cb296cb 100644--- a/api/agents/agent-strategy-objects.js+++ b/api/agents/agent-strategy-objects.js@@ -12,7 +12,8 @@// POST /api/agents/:id/strategies/sweep evaluate this agent's equips now (owner "Run now")// POST /api/agents/:id/strategies/close force-close ONE open position now { position_id } (owner "Sell now")-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { sql } from '../_lib/db.js';import { cors, json, method, error, readJson, rateLimited } from '../_lib/http.js';import { limits, clientIp } from '../_lib/rate-limit.js';diff --git a/api/agents/agent-trade.js b/api/agents/agent-trade.jsindex ecc2b1c8c..ee84f8854 100644--- a/api/agents/agent-trade.js+++ b/api/agents/agent-trade.js@@ -19,7 +19,8 @@// SNIPER_MODE=simulate or { simulate: true } runs the full real-quote path but// skips the broadcast (paper mode) — an ops/test toggle, never the default.-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';import { sql } from '../_lib/db.js';import { cors, json, method, error, readJson, rateLimited } from '../_lib/http.js';diff --git a/api/agents/pumpfun/[action].js b/api/agents/pumpfun/[action].jsindex 380a0035e..7a1a3149c 100644--- a/api/agents/pumpfun/[action].js+++ b/api/agents/pumpfun/[action].js@@ -12,7 +12,8 @@* One bundle so Vercel doesn't re-bundle @solana/web3.js + @pump-fun/* per file.*/-import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin, assertBearerMaySpend } from '../../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../../_lib/auth.js';+import { assertBearerMaySpend } from '../../_lib/spend-scope.js';import {cors,json,diff --git a/api/agents/recovery.js b/api/agents/recovery.jsindex 37a44689b..f380ba801 100644--- a/api/agents/recovery.js+++ b/api/agents/recovery.js@@ -14,7 +14,8 @@// POST /recovery/requests/:rid/complete finalize a ready recovery → transfer ownership// POST /recovery/inheritance/arm beneficiary/guardian: arm the dead-man once eligible-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { sql } from '../_lib/db.js';import { cors, json, method, error, readJson, rateLimited } from '../_lib/http.js';import { limits, clientIp } from '../_lib/rate-limit.js';diff --git a/api/agents/sns.js b/api/agents/sns.jsindex 505d1882a..cf82731ae 100644--- a/api/agents/sns.js+++ b/api/agents/sns.js@@ -24,7 +24,8 @@// Confirms on-chain owner = wallet_address, then sets meta.sns_domain// (the user owns the domain; we only store the alias against the agent).-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';import { sql } from '../_lib/db.js';diff --git a/api/agents/solana-guard.js b/api/agents/solana-guard.jsindex b685c7cfd..1093bb759 100644--- a/api/agents/solana-guard.js+++ b/api/agents/solana-guard.js@@ -11,7 +11,8 @@// PUT → update config (sensitivity | enabled | safe_address | clear_learned) [CSRF]// POST → adjudicate { event_id, action: approve|deny|mark_swept } | { action: unfreeze } [CSRF]-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { sql } from '../_lib/db.js';import { cors, json, method, error, readJson, rateLimited } from '../_lib/http.js';import { limits } from '../_lib/rate-limit.js';diff --git a/api/agents/solana-trade.js b/api/agents/solana-trade.jsindex f95d3e7f0..2bd2a61fb 100644--- a/api/agents/solana-trade.js+++ b/api/agents/solana-trade.js@@ -18,7 +18,8 @@// state. $THREE is the only coin three.ws promotes; this surface is coin-agnostic// plumbing that trades whatever mint the owner supplies at runtime.-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';import { sql } from '../_lib/db.js';import { cors, json, method, error, readJson, rateLimited } from '../_lib/http.js';diff --git a/api/agents/solana-wallet.js b/api/agents/solana-wallet.jsindex b7e7d9ab3..860ccb564 100644--- a/api/agents/solana-wallet.js+++ b/api/agents/solana-wallet.js@@ -1,7 +1,8 @@// /api/agents/:id/solana — wallet, activity, and airdrop handlers.// Dispatched from api/agents/[id].js with the `action` sub-path.-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';import { sql } from '../_lib/db.js';import { confirmOrThrow, pollConfirmation } from '../_lib/solana/confirm.js';diff --git a/api/agents/wallet-intents.js b/api/agents/wallet-intents.jsindex 54d58c18d..83bdae24e 100644--- a/api/agents/wallet-intents.js+++ b/api/agents/wallet-intents.js@@ -18,7 +18,8 @@*/import { cors, json, method, error, readJson, rateLimited, serverError } from '../_lib/http.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { limits } from '../_lib/rate-limit.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';diff --git a/api/autopilot/config.js b/api/autopilot/config.jsindex 4abd44a72..e9980ecae 100644--- a/api/autopilot/config.js+++ b/api/autopilot/config.js@@ -9,7 +9,8 @@// without a scope the owner granted here. $THREE is the only coin referenced.import { sql } from '../_lib/db.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, json, method, readJson, wrap, error } from '../_lib/http.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';diff --git a/api/autopilot/proposals.js b/api/autopilot/proposals.jsindex 1b13b9ca1..27aa3132c 100644--- a/api/autopilot/proposals.js+++ b/api/autopilot/proposals.js@@ -16,7 +16,8 @@// action = 'adjust' → { proposalId, params } edit a pending proposal's params.import { sql } from '../_lib/db.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, json, method, readJson, wrap, error, rateLimited } from '../_lib/http.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';diff --git a/api/launch/mm.js b/api/launch/mm.jsindex 8d5cd656c..0b617266f 100644--- a/api/launch/mm.js+++ b/api/launch/mm.js@@ -16,7 +16,8 @@// the owner to the agent wallet's audited withdraw flow for funds.import { cors, json, method, error, readJson, rateLimited, wrap } from '../_lib/http.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { limits, clientIp } from '../_lib/rate-limit.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';diff --git a/api/launcher/me.js b/api/launcher/me.jsindex 5c67f0460..2efe32eef 100644--- a/api/launcher/me.js+++ b/api/launcher/me.js@@ -28,7 +28,8 @@*/import { cors, error, json, method, rateLimited, readJson, wrap } from '../_lib/http.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { limits, clientIp } from '../_lib/rate-limit.js';import { sql } from '../_lib/db.js';import { rankNarratives } from '../_lib/launcher-trends.js';diff --git a/api/marketplace/purchase-as-agent.js b/api/marketplace/purchase-as-agent.jsindex 1ffdfd7af..41b837220 100644--- a/api/marketplace/purchase-as-agent.js+++ b/api/marketplace/purchase-as-agent.js@@ -30,7 +30,8 @@ import {} from '@solana/spl-token';import { sql } from '../_lib/db.js';-import { authenticateBearer, extractBearer, getSessionUser, assertBearerMaySpend } from '../_lib/auth.js';+import { authenticateBearer, extractBearer, getSessionUser } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, error, json, method, readJson, wrap, rateLimited, respondError } from '../_lib/http.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';diff --git a/api/monetization/wallet.js b/api/monetization/wallet.jsindex 52b16d1f6..f3b30b27e 100644--- a/api/monetization/wallet.js+++ b/api/monetization/wallet.js@@ -5,7 +5,8 @@import { z } from 'zod';import { sql } from '../_lib/db.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, json, method, wrap, error, readJson, rateLimited } from '../_lib/http.js';import { parse, isValidSolanaAddress, isValidEvmAddress, isUuid } from '../_lib/validate.js';import { limits, clientIp } from '../_lib/rate-limit.js';diff --git a/api/monetization/withdrawals.js b/api/monetization/withdrawals.jsindex a07b54bf3..6883e1cbd 100644--- a/api/monetization/withdrawals.js+++ b/api/monetization/withdrawals.js@@ -8,7 +8,8 @@import { z } from 'zod';import { sql } from '../_lib/db.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, json, method, wrap, error, readJson, rateLimited } from '../_lib/http.js';import { parse, isUuid } from '../_lib/validate.js';import { limits, clientIp } from '../_lib/rate-limit.js';diff --git a/api/pump/[action].js b/api/pump/[action].jsindex 70c71ff1e..e01d344d5 100644--- a/api/pump/[action].js+++ b/api/pump/[action].js@@ -56,7 +56,8 @@ import { Keypair, PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js';import { sql } from '../_lib/db.js';import { resolveSocialReward } from '../_lib/github-reward.js';import { submitProtected } from '../_lib/execution-engine.js';-import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { cors, json, method, readJson, wrap, error, rateLimited, respondError } from '../_lib/http.js';import { putObject, publicUrl as r2PublicUrl, thumbnailUrl } from '../_lib/r2.js';import { env } from '../_lib/env.js';diff --git a/api/sniper/close.js b/api/sniper/close.jsindex 4cf06cb4f..844dbf950 100644--- a/api/sniper/close.js+++ b/api/sniper/close.js@@ -23,7 +23,8 @@*/import { cors, json, method, error, readJson, rateLimited } from '../_lib/http.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { requireCsrf } from '../_lib/csrf.js';import { limits, clientIp } from '../_lib/rate-limit.js';import { sql } from '../_lib/db.js';diff --git a/api/sniper/strategy.js b/api/sniper/strategy.jsindex 640ad125c..9c6af5731 100644--- a/api/sniper/strategy.js+++ b/api/sniper/strategy.js@@ -15,7 +15,8 @@*/import { cors, json, method, readJson, wrap, error, rateLimited } from '../_lib/http.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { requireCsrf } from '../_lib/csrf.js';import { requireRealFundsAgreement } from '../_lib/real-funds-agreement.js';import { limits, clientIp } from '../_lib/rate-limit.js';diff --git a/api/x402-pay.js b/api/x402-pay.jsindex c1edc47cf..f9343f3cd 100644--- a/api/x402-pay.js+++ b/api/x402-pay.js@@ -47,7 +47,8 @@ import { getRedis as _getSharedRedis } from './_lib/redis.js';import { sql } from './_lib/db.js';import { thumbnailUrl } from './_lib/r2.js';import { logger } from './_lib/usage.js';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from './_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from './_lib/auth.js';+import { assertBearerMaySpend } from './_lib/spend-scope.js';import { recoverSolanaAgentKeypair } from './_lib/agent-wallet.js';import { requireRealFundsAgreement } from './_lib/real-funds-agreement.js';import { SpendLimitError, reserveSpendUsd, updateCustodyEvent, releaseSpendReservation } from './_lib/agent-trade-guards.js';diff --git a/api/x402/pay-by-name.js b/api/x402/pay-by-name.jsindex f029b3d38..b24efa96d 100644--- a/api/x402/pay-by-name.js+++ b/api/x402/pay-by-name.js@@ -35,7 +35,8 @@ import {createAssociatedTokenAccountIdempotentInstruction,} from '@solana/spl-token';-import { getSessionUser, authenticateBearer, extractBearer, assertBearerMaySpend } from '../_lib/auth.js';+import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';+import { assertBearerMaySpend } from '../_lib/spend-scope.js';import { submitProtected } from '../_lib/execution-engine.js';import { sql } from '../_lib/db.js';import { env } from '../_lib/env.js';diff --git a/tests/bearer-spend-scope.test.js b/tests/bearer-spend-scope.test.jsindex f2b70ce1c..d257e4812 100644--- a/tests/bearer-spend-scope.test.js+++ b/tests/bearer-spend-scope.test.js@@ -8,14 +8,12 @@// autopilot, or launch coins. assertBearerMaySpend is the single gate; this suite// pins its semantics and that every spending route still calls it.-import { describe, it, expect, vi } from 'vitest';+import { describe, it, expect } from 'vitest';import { readFileSync } from 'node:fs';import { fileURLToPath } from 'node:url';import path from 'node:path';-vi.mock('../api/_lib/db.js', () => ({ sql: vi.fn(async () => []) }));--const { assertBearerMaySpend, SPEND_SCOPE } = await import('../api/_lib/auth.js');+import { assertBearerMaySpend, SPEND_SCOPE } from '../api/_lib/spend-scope.js';const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');@@ -93,7 +91,7 @@ describe('every bearer-accepting spend route calls the gate', () => {for (const rel of ROUTES) {it(rel, () => {const src = readFileSync(path.join(ROOT, rel), 'utf8');- expect(src).toMatch(/import \{[^}]*\bassertBearerMaySpend\b[^}]*\} from '[./]*_lib\/auth\.js'/);+ expect(src).toMatch(/import \{ assertBearerMaySpend \} from '[./]*_lib\/spend-scope\.js'/);expect(src.match(/assertBearerMaySpend\(/g)?.length || 0).toBeGreaterThanOrEqual(1);});}