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
feat: EVM payments lib, pay page overhaul, x402 updates
nirholas37bb777c06e8
diff --git a/api/_lib/evm/rpc.js b/api/_lib/evm/rpc.jsnew file mode 100644index 000000000..fc5f7b8e9--- /dev/null+++ b/api/_lib/evm/rpc.js@@ -0,0 +1,96 @@+// Canonical EVM RPC endpoint resolution + failover transports/providers.+//+// EVM mirrors the Solana failover layer: a priority-ordered endpoint list per+// chain, plus ready-made viem and ethers constructs that try each endpoint in+// turn. Priority: an explicit RPC_URL_<chainId> override → Alchemy (where the+// chain is supported and ALCHEMY_API_KEY is set) → the chain's public rpcUrls+// (already curated in erc8004-chains.js). The keyless public endpoints are+// always last, never the sole dependency.+//+// Scope: GENERIC JSON-RPC (eth_call, eth_getBalance, eth_getTransactionReceipt,+// ENS, contract reads). It is NOT for Alchemy-proprietary methods like+// alchemy_getTokenBalances — those only exist on Alchemy, so a public fallback+// would 4xx. Keep those on a single Alchemy URL.++import { fallback, http } from 'viem';+import { CHAIN_BY_ID } from '../erc8004-chains.js';+import { env } from '../env.js';++// Alchemy network subdomains, keyed by chainId. Only chains Alchemy actually+// serves are listed; everything else relies on the public rpcUrls list.+const ALCHEMY_SUBDOMAIN = {+ 1: 'eth-mainnet',+ 8453: 'base-mainnet',+ 42161: 'arb-mainnet',+ 10: 'opt-mainnet',+ 137: 'polygon-mainnet',+ 56: 'bnb-mainnet',+ 43114: 'avax-mainnet',+ 59144: 'linea-mainnet',+ 534352: 'scroll-mainnet',+ 324: 'zksync-mainnet',+ // testnets+ 84532: 'base-sepolia',+ 421614: 'arb-sepolia',+ 11155111: 'eth-sepolia',+ 11155420: 'opt-sepolia',+ 80002: 'polygon-amoy',+ 43113: 'avax-fuji',+};++function alchemyUrl(chainId) {+ const sub = ALCHEMY_SUBDOMAIN[chainId];+ const key = process.env.ALCHEMY_API_KEY;+ return sub && key ? `https://${sub}.g.alchemy.com/v2/${key}` : null;+}++/**+ * Priority-ordered, de-duplicated RPC URL list for a chain.+ */+export function evmRpcEndpoints(chainId) {+ const chain = CHAIN_BY_ID[chainId];+ const urls = [+ env.getRpcUrl(chainId), // explicit RPC_URL_<id> / BASE_SEPOLIA_RPC_URL / SEPOLIA_RPC_URL+ alchemyUrl(chainId),+ ...((chain && chain.rpcUrls) || []),+ ];+ return urls.filter((u, i, a) => u && a.indexOf(u) === i);+}++/**+ * viem Transport with sequential failover across the chain's endpoints. Drop-in+ * for `http(url)`: `createPublicClient({ chain, transport: evmTransport(id) })`.+ * `rank: false` keeps strict priority order (override → Alchemy → public) rather+ * than latency-ranking, so we lead with the most reliable endpoint.+ */+export function evmTransport(chainId, { retryCount = 1 } = {}) {+ const urls = evmRpcEndpoints(chainId);+ if (urls.length === 0) return http(); // viem default for the chain+ if (urls.length === 1) return http(urls[0], { retryCount });+ return fallback(+ urls.map((u) => http(u, { retryCount })),+ { rank: false },+ );+}++/**+ * ethers v6 FallbackProvider with quorum 1 — the first endpoint to answer wins,+ * tried in priority order. Returns a single-provider JsonRpcProvider when only+ * one endpoint exists (FallbackProvider requires ≥2 and quorum ≤ count).+ */+export async function evmFallbackProvider(chainId) {+ const { JsonRpcProvider, FallbackProvider, Network } = await import('ethers');+ const urls = evmRpcEndpoints(chainId);+ if (urls.length === 0) throw new Error(`no RPC endpoint for chain ${chainId}`);+ const network = chainId ? Network.from(chainId) : undefined;+ // staticNetwork avoids a per-call eth_chainId round-trip on every provider.+ const mk = (u) => new JsonRpcProvider(u, network, { staticNetwork: network });+ if (urls.length === 1) return mk(urls[0]);+ const configs = urls.map((u, i) => ({+ provider: mk(u),+ priority: i + 1, // lower = tried first+ weight: 1,+ stallTimeout: 2000,+ }));+ return new FallbackProvider(configs, network, { quorum: 1 });+}diff --git a/api/payments/evm/[action].js b/api/payments/evm/[action].jsindex b7255de11..60dcdb214 100644--- a/api/payments/evm/[action].js+++ b/api/payments/evm/[action].js@@ -7,7 +7,6 @@ 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';@@ -40,7 +39,8 @@ async function handleCheckout(req, res) {// ── confirm ───────────────────────────────────────────────────────────────────-import { createPublicClient, http, parseAbi } from 'viem';+import { createPublicClient, parseAbi } from 'viem';+import { evmTransport } from '../../_lib/evm/rpc.js';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 };@@ -70,8 +70,7 @@ async function handleConfirm(req, res) {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) });+ const client = createPublicClient({ chain, transport: evmTransport(chainId) });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'); }diff --git a/public/pay/index.html b/public/pay/index.htmlindex 7fddf1c34..63cea451c 100644--- a/public/pay/index.html+++ b/public/pay/index.html@@ -707,13 +707,22 @@<button class="chain-tab" data-chain="bsc">⛓ BNB Chain <span class="chip">USDC</span></button></div><div class="agent-bar" id="agentBar">- <span class="label">agent wallet</span>+ <span class="label">pay with</span><div class="agent-list" id="agentList"><span class="login-cta" id="agentLoginCta" style="display:none"><a href="/login?redirect=/pay">Sign in</a> to use your own agent's wallet.<span class="demo-note">Using shared demo wallet for now.</span></span><span id="agentLoading" style="color:var(--muted);font-size:12px">loading agents…</span>+ <!-- Browser wallet cards — protected from renderAgentCards() by data-wallet -->+ <div class="agent-card" id="phantomCard" data-wallet="phantom" style="display:none">+ <div class="av" style="background:linear-gradient(135deg,#ab9ff2,#534bb1);color:#fff;font-size:11px">P</div>+ <div><div class="name">Phantom</div><span class="bal" id="phantomBalLabel">Solana USDC</span></div>+ </div>+ <div class="agent-card" id="evmCard" data-wallet="evm" style="display:none">+ <div class="av" style="background:linear-gradient(135deg,#f6851b,#e2761b);color:#fff;font-size:11px">M</div>+ <div><div class="name">EVM wallet</div><span class="bal" id="evmBalLabel">Base USDC</span></div>+ </div></div></div><div class="conv" id="conv">@@ -822,11 +831,13 @@ const genOverlay = $('genOverlay'), genCancel = $('genCancel'), genBtn = $('genBconst genSpinner = $('genSpinner');// ---- Agent state ---------------------------------------------------------let selectedAgent = null; // { id, name, solana_address, usdc, sol }+let selectedAgent = null; // { id, name, solana_address, usdc, sol }+let selectedBrowserWallet = null; // 'phantom' | 'evm' | nulllet pendingGenAgentId = null;function setSelectedAgent(agent) {selectedAgent = agent;+ selectedBrowserWallet = null;// update wallet pill to show selected agent's balanceif (agent) {walletText.textContent = `${agent.name} · ${@@ -837,13 +848,66 @@ function setSelectedAgent(agent) {}// re-render agent cards to highlight selectedrenderAgentCards(window._agentCache || []);+ updateBrowserWalletCards();+}++function setSelectedBrowserWallet(type) {+ selectedAgent = null;+ selectedBrowserWallet = type;+ walletText.textContent = type === 'phantom' ? 'Phantom wallet' : 'EVM wallet';+ // De-select any agent card without re-rendering the whole list+ for (const el of agentList.querySelectorAll('.agent-card[data-agent-id].selected')) {+ el.classList.remove('selected');+ }+ updateBrowserWalletCards();+}++function updateBrowserWalletCards() {+ const phantomCard = $('phantomCard');+ const evmCard = $('evmCard');+ if (phantomCard) {+ phantomCard.classList.toggle('selected', selectedBrowserWallet === 'phantom');+ }+ if (evmCard) {+ evmCard.classList.toggle('selected', selectedBrowserWallet === 'evm');+ }+}++// ---- Utility helpers ----------------------------------------------------+function b64decode(str) {+ if (!str) return null;+ try { return JSON.parse(atob(str)); } catch { return null; }+}+function base64ToUint8Array(b64) {+ const bin = atob(b64);+ const arr = new Uint8Array(bin.length);+ for (let i = 0; i < bin.length; i++) arr[i] = bin.charCodeAt(i);+ return arr;+}+function uint8ArrayToBase64(arr) {+ let bin = '';+ for (const b of arr) bin += String.fromCharCode(b);+ return btoa(bin);+}+function buildJsonRpc(call) {+ return JSON.stringify({+ jsonrpc: '2.0', id: 1,+ method: call.tool === 'tools/list' ? 'tools/list' : 'tools/call',+ params: call.tool === 'tools/list' ? undefined : { name: call.tool, arguments: call.args || {} },+ });+}+function isSolanaAccept(accept) {+ return /solana/i.test(accept?.network || '');+}+function isEip3009Accept(accept) {+ return /base|evm|ethereum/i.test(accept?.network || '') || accept?.scheme === 'eip3009';}function renderAgentCards(agents) {window._agentCache = agents;agentLoading.style.display = 'none';// remove existing cards- for (const el of agentList.querySelectorAll('.agent-card')) el.remove();+ for (const el of agentList.querySelectorAll('.agent-card[data-agent-id]')) el.remove();if (!agents.length) {agentLoginCta.style.display = 'block';agentLoginCta.innerHTML = `No agents yet. <a href="/create">Create your first agent →</a>`;@@ -930,6 +994,377 @@ genBtn.addEventListener('click', async () => {}});+// ---- Browser wallet cards -----------------------------------------------++function getSolanaProvider() {+ if (window.phantom?.solana?.isPhantom) return window.phantom.solana;+ if (window.solana?.isPhantom) return window.solana;+ if (window.backpack?.solana) return window.backpack.solana;+ if (window.solflare?.isSolflare) return window.solflare;+ return null;+}+function getEvmProvider() {+ return window.ethereum || null;+}++function initBrowserWalletCards() {+ const phantomCard = $('phantomCard');+ const evmCard = $('evmCard');+ if (phantomCard) {+ if (getSolanaProvider()) {+ phantomCard.style.display = '';+ phantomCard.addEventListener('click', () => setSelectedBrowserWallet('phantom'));+ }+ }+ if (evmCard) {+ if (getEvmProvider()) {+ evmCard.style.display = '';+ evmCard.addEventListener('click', () => setSelectedBrowserWallet('evm'));+ }+ }+}++// ---- Phantom (Solana) inline payment ------------------------------------++async function runPhantomPayment(call, displayLabel) {+ const provider = getSolanaProvider();+ if (!provider) throw new Error('Phantom wallet not found. Install Phantom to continue.');++ addMsg('user', escape(displayLabel || displayLabelFor(call)));+ resetSteps();+ const t0 = performance.now();+ sendBtn.disabled = true;+ for (const b of quickEl.querySelectorAll('button')) b.disabled = true;+ promptEl.disabled = true;++ try {+ // 1. Pull payment challenge+ setStep('challenge', 'active', 'probing MCP endpoint for 402…');+ const probeRes = await fetch('/api/mcp', {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: buildJsonRpc(call),+ });+ if (probeRes.status !== 402 && probeRes.status !== 401) {+ const t = await probeRes.text();+ throw new Error(`Endpoint returned ${probeRes.status}: ${t.slice(0, 120)}`);+ }+ // Parse challenge from body or payment-required header+ let challenge = null;+ try { challenge = await probeRes.clone().json(); } catch { /* fall through */ }+ if (!challenge?.accepts?.length) {+ const hdr = probeRes.headers.get('payment-required');+ challenge = b64decode(hdr);+ }+ if (!challenge?.accepts?.length) throw new Error('No accepts array in payment challenge');++ const solanaAccept = challenge.accepts.find(isSolanaAccept);+ if (!solanaAccept) throw new Error('Server does not accept Solana payments');+ setStep('challenge', 'done', `${(Number(solanaAccept.maxAmountRequired) / 1e6).toFixed(6)} USDC`);++ // 2. Connect Phantom + prepare unsigned tx+ setStep('built', 'active', 'connecting Phantom…');+ const connectResp = await provider.connect();+ const buyerAddress = connectResp.publicKey.toString();++ const prepRes = await fetch('/api/x402-checkout?action=prepare', {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: JSON.stringify({ accept: solanaAccept, buyer: buyerAddress }),+ });+ if (!prepRes.ok) {+ const e = await prepRes.json().catch(() => ({}));+ throw new Error(e.error || `prepare failed: ${prepRes.status}`);+ }+ const { tx_base64 } = await prepRes.json();++ // 3. Phantom signs+ setStep('built', 'active', 'sign the transaction in Phantom…');+ const { VersionedTransaction } = await import('https://esm.sh/@solana/web3.js@1');+ const vtxBytes = base64ToUint8Array(tx_base64);+ const vtx = VersionedTransaction.deserialize(vtxBytes);+ const signedVtx = await provider.signTransaction(vtx);+ const signedB64 = uint8ArrayToBase64(signedVtx.serialize());+ setStep('built', 'done', 'signed');++ // 4. Encode X-PAYMENT header+ setStep('verified', 'active', 'encoding payment payload…');+ const encRes = await fetch('/api/x402-checkout?action=encode', {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: JSON.stringify({ accept: solanaAccept, signed_tx_base64: signedB64, resource_url: '/api/mcp' }),+ });+ if (!encRes.ok) {+ const e = await encRes.json().catch(() => ({}));+ throw new Error(e.error || `encode failed: ${encRes.status}`);+ }+ const { x_payment } = await encRes.json();+ setStep('verified', 'done', 'payload encoded');++ // 5. Dispatch to MCP with X-PAYMENT+ setStep('dispatched', 'active', 'running MCP tool…');+ const dispatchRes = await fetch('/api/mcp', {+ method: 'POST',+ headers: {+ 'content-type': 'application/json',+ 'X-PAYMENT': x_payment,+ },+ body: buildJsonRpc(call),+ });+ const xPayResp = dispatchRes.headers.get('x-payment-response');+ let payResp = b64decode(xPayResp);+ const dispatchJson = await dispatchRes.json().catch(() => null);+ if (!dispatchRes.ok) {+ const errMsg = dispatchJson?.error || `MCP responded ${dispatchRes.status}`;+ throw new Error(errMsg);+ }+ setStep('dispatched', 'done', 'tool ran');++ // 6. Settled+ const totalMs = Math.round(performance.now() - t0);+ const tx = payResp?.transaction || '';+ setStep('settled', 'done', tx ? `${tx.slice(0,8)}…` : `${totalMs}ms`);++ // Build synthetic payment + durations for renderReceipt+ const payment = {+ tx,+ amount: solanaAccept.maxAmountRequired,+ network: 'solana-mainnet',+ payer: buyerAddress,+ payTo: solanaAccept.payTo || '',+ };+ const durations = { total_ms: totalMs };++ renderReceipt(call, payment, durations);+ addMsg('agent', renderResult(call, dispatchJson));+ bumpTicker();++ // Refresh Phantom balance label+ refreshPhantomBalance(buyerAddress);+ } catch (err) {+ const step = document.querySelector('.step.active')?.id?.replace('step-', '') || 'challenge';+ setStep(step, 'error', err.message);+ addMsg('agent', `<div class="err">Phantom payment failed: ${escape(err.message)}</div>`);+ } finally {+ const elapsed = Math.round(performance.now() - t0);+ document.title = `${elapsed}ms · three.ws · pay-per-call`;+ sendBtn.disabled = false;+ for (const b of quickEl.querySelectorAll('button')) b.disabled = false;+ promptEl.disabled = false;+ promptEl.focus();+ }+}++async function refreshPhantomBalance(address) {+ if (!address) return;+ const balLabel = $('phantomBalLabel');+ if (balLabel) balLabel.textContent = `${address.slice(0,4)}…${address.slice(-4)} · Solana`;+ try {+ // Fetch USDC balance directly from Solana RPC+ const USDC_MINT = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';+ const rpcRes = await fetch('https://api.mainnet-beta.solana.com', {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: JSON.stringify({+ jsonrpc: '2.0', id: 1,+ method: 'getTokenAccountsByOwner',+ params: [address, { mint: USDC_MINT }, { encoding: 'jsonParsed' }],+ }),+ });+ const rpcData = await rpcRes.json();+ const accounts = rpcData?.result?.value || [];+ let usdcAmount = 0;+ for (const acc of accounts) {+ const info = acc?.account?.data?.parsed?.info?.tokenAmount;+ if (info) usdcAmount += Number(info.uiAmount || 0);+ }+ if (balLabel) balLabel.textContent = `${usdcAmount.toFixed(3)} USDC · Solana`;+ } catch { /* non-critical — label already shows address */ }+}++// ---- MetaMask / Base (EIP-3009) inline payment --------------------------++const BASE_USDC_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'; // Base mainnet USDC+const BASE_CHAIN_ID = 8453;++async function runEvmPayment(call, displayLabel) {+ const ethereum = getEvmProvider();+ if (!ethereum) throw new Error('No EVM wallet detected. Install MetaMask to continue.');++ addMsg('user', escape(displayLabel || displayLabelFor(call)));+ resetSteps();+ const t0 = performance.now();+ sendBtn.disabled = true;+ for (const b of quickEl.querySelectorAll('button')) b.disabled = true;+ promptEl.disabled = true;++ try {+ // 1. Pull payment challenge+ setStep('challenge', 'active', 'probing MCP endpoint for 402…');+ const probeRes = await fetch('/api/mcp', {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: buildJsonRpc(call),+ });+ if (probeRes.status !== 402 && probeRes.status !== 401) {+ const t = await probeRes.text();+ throw new Error(`Endpoint returned ${probeRes.status}: ${t.slice(0, 120)}`);+ }+ let challenge = null;+ try { challenge = await probeRes.clone().json(); } catch { /* fall through */ }+ if (!challenge?.accepts?.length) {+ const hdr = probeRes.headers.get('payment-required');+ challenge = b64decode(hdr);+ }+ if (!challenge?.accepts?.length) throw new Error('No accepts array in payment challenge');++ const evmAccept = challenge.accepts.find(isEip3009Accept);+ if (!evmAccept) throw new Error('Server does not accept EVM/Base payments');+ setStep('challenge', 'done', `${(Number(evmAccept.maxAmountRequired) / 1e6).toFixed(6)} USDC`);++ // 2. Connect wallet + switch to Base+ setStep('built', 'active', 'connecting EVM wallet…');+ const accounts = await ethereum.request({ method: 'eth_requestAccounts' });+ const from = accounts[0];+ if (!from) throw new Error('No account returned from wallet');++ const chainHex = '0x' + BASE_CHAIN_ID.toString(16);+ try {+ await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: chainHex }] });+ } catch (switchErr) {+ if (switchErr.code === 4902) {+ await ethereum.request({+ method: 'wallet_addEthereumChain',+ params: [{+ chainId: chainHex,+ chainName: 'Base',+ nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },+ rpcUrls: ['https://mainnet.base.org'],+ blockExplorerUrls: ['https://basescan.org'],+ }],+ });+ } else throw switchErr;+ }++ // 3. Build EIP-3009 typed data + sign+ setStep('built', 'active', 'sign EIP-3009 transfer in wallet…');+ const amount = evmAccept.maxAmountRequired; // in USDC micro-units (6 decimals)+ const payTo = evmAccept.payTo;+ const validAfter = 0;+ const validBefore = Math.floor((Date.now() + 3600 * 1000) / 1000); // 1 hour+ // Random 32-byte nonce — use crypto.getRandomValues+ const nonceBytes = new Uint8Array(32);+ crypto.getRandomValues(nonceBytes);+ const nonce = '0x' + Array.from(nonceBytes).map(b => b.toString(16).padStart(2, '0')).join('');++ const typedData = {+ types: {+ EIP712Domain: [+ { name: 'name', type: 'string' },+ { name: 'version', type: 'string' },+ { name: 'chainId', type: 'uint256' },+ { name: 'verifyingContract', type: 'address' },+ ],+ TransferWithAuthorization: [+ { name: 'from', type: 'address' },+ { name: 'to', type: 'address' },+ { name: 'value', type: 'uint256' },+ { name: 'validAfter', type: 'uint256' },+ { name: 'validBefore',type: 'uint256' },+ { name: 'nonce', type: 'bytes32' },+ ],+ },+ primaryType: 'TransferWithAuthorization',+ domain: {+ name: 'USD Coin',+ version: '2',+ chainId: BASE_CHAIN_ID,+ verifyingContract: BASE_USDC_ADDRESS,+ },+ message: {+ from: from,+ to: payTo,+ value: String(amount),+ validAfter: String(validAfter),+ validBefore: String(validBefore),+ nonce,+ },+ };++ const sig = await ethereum.request({+ method: 'eth_signTypedData_v4',+ params: [from, JSON.stringify(typedData)],+ });+ setStep('built', 'done', 'EIP-3009 signed');++ // 4. Build x402 paymentPayload (EIP-3009 scheme)+ setStep('verified', 'active', 'building payment payload…');+ const payloadObj = {+ scheme: 'eip3009',+ network: 'base-mainnet',+ payload: {+ from,+ to: payTo,+ value: String(amount),+ validAfter: String(validAfter),+ validBefore: String(validBefore),+ nonce,+ signature: sig,+ },+ };+ const x_payment = btoa(JSON.stringify(payloadObj));+ setStep('verified', 'done', 'payload built');++ // 5. Dispatch to MCP+ setStep('dispatched', 'active', 'running MCP tool…');+ const dispatchRes = await fetch('/api/mcp', {+ method: 'POST',+ headers: {+ 'content-type': 'application/json',+ 'X-PAYMENT': x_payment,+ },+ body: buildJsonRpc(call),+ });+ const xPayResp = dispatchRes.headers.get('x-payment-response');+ let payResp = b64decode(xPayResp);+ const dispatchJson = await dispatchRes.json().catch(() => null);+ if (!dispatchRes.ok) {+ const errMsg = dispatchJson?.error || `MCP responded ${dispatchRes.status}`;+ throw new Error(errMsg);+ }+ setStep('dispatched', 'done', 'tool ran');++ // 6. Settlement+ const totalMs = Math.round(performance.now() - t0);+ const txHash = payResp?.transaction || '';+ setStep('settled', 'done', txHash ? `${txHash.slice(0,10)}…` : `${totalMs}ms`);++ const payment = {+ tx: txHash,+ amount,+ network: 'base-mainnet',+ payer: from,+ payTo,+ };+ const durations = { total_ms: totalMs };++ renderReceipt(call, payment, durations);+ addMsg('agent', renderResult(call, dispatchJson));+ bumpTicker();+ } catch (err) {+ const activeStep = document.querySelector('.step.active')?.id?.replace('step-', '') || 'challenge';+ setStep(activeStep, 'error', err.message);+ addMsg('agent', `<div class="err">EVM payment failed: ${escape(err.message)}</div>`);+ } finally {+ const elapsed = Math.round(performance.now() - t0);+ document.title = `${elapsed}ms · three.ws · pay-per-call`;+ sendBtn.disabled = false;+ for (const b of quickEl.querySelectorAll('button')) b.disabled = false;+ promptEl.disabled = false;+ promptEl.focus();+ }+}+const DEMO_MODEL = 'https://threejs.org/examples/models/gltf/DamagedHelmet/glTF-Binary/DamagedHelmet.glb';const TOOL_PRICE_USDC = 0.001;@@ -963,7 +1398,7 @@ function addMsg(role, html) {const STEP_DEFS = [{ id: 'challenge', label: 'Pull payment requirements (402)' },- { id: 'built', label: 'Build & sign Solana payment' },+ { id: 'built', label: 'Build & sign payment' },{ id: 'verified', label: 'Facilitator verify (off-chain)' },{ id: 'dispatched', label: 'Run MCP tool in-process' },{ id: 'settled', label: 'Facilitator settle (on-chain)' },@@ -1008,19 +1443,26 @@ function curlSnippet(call) {}function renderReceipt(call, payment, durations) {- const explorer = payment.tx ? `https://solscan.io/tx/${payment.tx}` : null;+ const isEvm = payment.network && /base|evm|ethereum/i.test(payment.network);+ const networkLabel = isEvm ? 'Base mainnet' : 'Solana mainnet';+ const explorer = payment.tx+ ? (isEvm ? `https://basescan.org/tx/${payment.tx}` : `https://solscan.io/tx/${payment.tx}`)+ : null;+ const explorerLabel = isEvm ? 'view on Basescan' : 'view on Solscan';const permalink = payment.tx ? `${location.origin}/pay/calls/${payment.tx}` : null;+ const decimals = isEvm ? 6 : 6;+ const amountDisplay = (Number(payment.amount) / 10 ** decimals).toFixed(6);receiptEl.innerHTML = `<div class="receipt"><h3>settlement receipt</h3>- <div class="row"><span class="k">amount</span><span class="v">${(Number(payment.amount) / 1e6).toFixed(6)} USDC</span></div>- <div class="row"><span class="k">network</span><span class="v">Solana mainnet</span></div>+ <div class="row"><span class="k">amount</span><span class="v">${amountDisplay} USDC</span></div>+ <div class="row"><span class="k">network</span><span class="v">${networkLabel}</span></div><div class="row"><span class="k">payer</span><span class="v">${payment.payer.slice(0,8)}…${payment.payer.slice(-4)}</span></div><div class="row"><span class="k">payTo</span><span class="v">${payment.payTo.slice(0,8)}…${payment.payTo.slice(-4)}</span></div><div class="row"><span class="k">total</span><span class="v">${durations.total_ms} ms</span></div>${explorer ? `<div class="row"><span class="k">tx</span><span class="v"><a href="${explorer}" target="_blank" rel="noopener">${payment.tx.slice(0,10)}…${payment.tx.slice(-6)} ↗</a></span></div>` : ''}<div class="actions">- ${explorer ? `<a href="${explorer}" target="_blank" rel="noopener">view on Solscan</a>` : ''}+ ${explorer ? `<a href="${explorer}" target="_blank" rel="noopener">${explorerLabel}</a>` : ''}${permalink ? `<button data-copy="${permalink}">copy permalink</button>` : ''}<button data-replay>↻ replay</button></div>@@ -1248,6 +1690,14 @@ async function* sseEvents(response) {async function run(call, displayLabel) {if (!call) return;+ // Dispatch to browser wallet flows which manage their own UI lifecycle+ if (selectedBrowserWallet === 'phantom') {+ return runPhantomPayment(call, displayLabel);+ }+ if (selectedBrowserWallet === 'evm') {+ return runEvmPayment(call, displayLabel);+ }+sendBtn.disabled = true;for (const b of quickEl.querySelectorAll('button')) b.disabled = true;promptEl.disabled = true;@@ -1331,6 +1781,8 @@ function bumpTicker() {// ---- Wallet balance + activity feed ------------------------------------async function refreshBalance() {+ // Browser wallet — don't poll the server; balance shown per-call+ if (selectedBrowserWallet) return;// If using an agent wallet, reload agents to get fresh balances.if (selectedAgent) {try {@@ -1422,6 +1874,9 @@ refreshFeed();setInterval(refreshBalance, 30_000);setInterval(refreshFeed, 20_000);+// Browser wallet cards — show Phantom/MetaMask if installed+initBrowserWalletCards();+// Agent picker — try to load if there's any sign the user is logged in.// The localStorage hint is set by the main site on login.const authHint = (() => { try { return JSON.parse(localStorage.getItem('3dagent:auth-hint') || 'null'); } catch { return null; } })();diff --git a/public/x402.js b/public/x402.jsindex 09fc17218..fd220b6b1 100644--- a/public/x402.js+++ b/public/x402.js@@ -1364,6 +1364,8 @@ async function postJson(url, body) {}// Probe the merchant endpoint with a benign request to extract the 402 challenge.+// Accepts HTTP 402 (standard x402) or HTTP 401 with a `payment-required` header+// (MCP 2025-06-18 spec, which uses 401 for resource-server authorization challenges).async function discoverChallenge(opts) {const headers = { ...(opts.headers || {}) };const init = {@@ -1373,20 +1375,28 @@ async function discoverChallenge(opts) {};if (init.body && !headers['content-type']) headers['content-type'] = 'application/json';const res = await fetch(opts.endpoint, init);- if (res.status !== 402) {++ // MCP 2025-06-18 endpoints return 401 with the full x402 challenge in the+ // `payment-required` header (base64-JSON). Accept that alongside standard 402.+ const prHeader = res.headers.get('payment-required');+ const is401WithChallenge = res.status === 401 && !!prHeader;++ if (res.status !== 402 && !is401WithChallenge) {// Endpoint isn't paid (200) or isn't an x402 endpoint at all. In either// case, surface a clear error — accidentally pointing the modal at a// free endpoint should not silently succeed.const txt = await res.text();throw new Error(`Endpoint did not return 402 (got ${res.status}). Body: ${txt.slice(0, 120)}`);}- let body = await res.json().catch(() => null);++ // For 401+header, decode directly — the full envelope is in the header.+ // For 402, read body first and fall back to header if body is minimal.+ let body = is401WithChallenge ? b64decode(prHeader) : await res.json().catch(() => null);if (!body || !Array.isArray(body.accepts) || !body.accepts.length) {// send402 (api/_lib/x402-spec.js) only emits `{error}` in the body and// puts the full v2 PaymentRequired envelope (accepts + extensions) in// the base64-JSON PAYMENT-REQUIRED header. b64decode returns already-// parsed JSON, so use its result directly.- const prHeader = res.headers.get('payment-required');const decoded = b64decode(prHeader);if (decoded && Array.isArray(decoded.accepts) && decoded.accepts.length) {body = decoded;