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(paywall): mobile wallet deep-linking for x402 payments
nirholasf80924a65c48
diff --git a/public/paywall.js b/public/paywall.jsindex d2905c962..92af0224f 100644--- a/public/paywall.js+++ b/public/paywall.js@@ -276,6 +276,10 @@paymentInFlight = false;revealUnlocked(outcome, accept);}).catch(function (err) {+ // On mobile with no injected wallet, pay() redirects into the wallet's+ // in-app browser — the page is unloading, so keep the "Opening…" status+ // instead of flashing a failure.+ if (err && err.code === 'mobile_redirect') return;paymentInFlight = false;setButtonsDisabled(false);setWalletError(statusEl, (err && err.message) || 'Payment failed. Please try again.');diff --git a/public/x402-pay-core.js b/public/x402-pay-core.jsindex 0063564eb..a24d51454 100644--- a/public/x402-pay-core.js+++ b/public/x402-pay-core.js@@ -287,6 +287,65 @@ function walletNotFound(walletName) {return err;}+// ──────────────────────────────────────────────────────────────── mobile ──────++// Mobile browsers have no injected wallet extension — the wallet app instead+// reopens the page inside its own in-app browser (where the provider IS+// injected) via a universal link. Detect the mobile case so we deep-link the+// user into their wallet instead of telling them to "install the extension",+// which is meaningless on a phone. `ua` is injectable so this is unit-testable.+export function isMobileBrowser(ua) {+ const s = ua != null ? ua : typeof navigator !== 'undefined' ? navigator.userAgent || '' : '';+ return /Android|iPhone|iPad|iPod|Mobile|Silk|Kindle|BlackBerry|Opera Mini|IEMobile/i.test(s);+}++// Universal/deep link that reopens `targetUrl` inside the wallet's in-app+// browser. The paywall passes its own page URL so the gated request (carried in+// `?req=`/`?return=`) is preserved and payment completes after the redirect.+export function walletDeeplink(walletName, targetUrl, ref) {+ if (!targetUrl) return null;+ const enc = encodeURIComponent(targetUrl);+ const refQ = ref ? `?ref=${encodeURIComponent(ref)}` : '';+ switch (walletName) {+ case 'phantom':+ return `https://phantom.app/ul/browse/${enc}${refQ}`;+ case 'solflare':+ return `https://solflare.com/ul/v1/browse/${enc}${refQ}`;+ case 'coinbase':+ return `https://go.cb-w.com/dapp?cb_url=${enc}`;+ case 'metamask':+ try {+ const u = new URL(targetUrl);+ return `https://metamask.app.link/dapp/${u.host}${u.pathname}${u.search}`;+ } catch {+ return `https://metamask.app.link/dapp/${enc}`;+ }+ default:+ return null; // WalletConnect already handles mobile via its own modal.+ }+}++// On mobile with no injected provider, navigate to the wallet's in-app browser.+// Returns true if a redirect was initiated (the caller should then stop, since+// the page is unloading). Side-effecting + window-guarded; the pure pieces above+// are what tests exercise.+function initiateMobileRedirect(walletName, label, onStatus) {+ if (typeof window === 'undefined' || !isMobileBrowser()) return false;+ const link = walletDeeplink(walletName, window.location.href, window.location.origin);+ if (!link) return false;+ onStatus?.('connecting', `Opening ${label}…`);+ window.location.href = link;+ return true;+}++// Thrown after a mobile redirect is initiated so the controller can distinguish+// "navigating to the wallet app" from a real failure and suppress the error UI.+function mobileRedirectError() {+ const err = new Error('Opening your wallet app…');+ err.code = 'mobile_redirect';+ return err;+}+// ─────────────────────────────────────────────────────── settle + retry ──────// Retry the gated resource with the X-PAYMENT proof and return the unlocked@@ -336,7 +395,10 @@ export async function paySolana({ accept, resourceUrl, walletName, onStatus, oriconst apiOrigin = origin || (typeof location !== 'undefined' ? location.origin : '');const label = walletName === 'solflare' ? 'Solflare' : 'Phantom';const provider = getSolanaProvider(walletName);- if (!provider || typeof provider.connect !== 'function') throw walletNotFound(walletName);+ if (!provider || typeof provider.connect !== 'function') {+ if (initiateMobileRedirect(walletName, label, onStatus)) throw mobileRedirectError();+ throw walletNotFound(walletName);+ }onStatus?.('connecting', `Opening ${label}…`);const conn = await provider.connect();@@ -395,7 +457,11 @@ async function resolveEvmProvider(walletName, onStatus) {return wc;}const provider = getEvmProvider(walletName);- if (!provider || typeof provider.request !== 'function') throw walletNotFound(walletName);+ if (!provider || typeof provider.request !== 'function') {+ const label = WALLET_LABELS[walletName] || 'wallet';+ if (initiateMobileRedirect(walletName, label, onStatus)) throw mobileRedirectError();+ throw walletNotFound(walletName);+ }return provider;}diff --git a/tests/public/x402-pay-core.test.js b/tests/public/x402-pay-core.test.jsindex ad312f2d0..00766e2c0 100644--- a/tests/public/x402-pay-core.test.js+++ b/tests/public/x402-pay-core.test.js@@ -22,6 +22,8 @@ import {uint8ArrayToBase64,explorerUrl,friendlyError,+ isMobileBrowser,+ walletDeeplink,} from '../../public/x402-pay-core.js';const THREE_MINT = 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump';@@ -208,3 +210,50 @@ describe('friendlyError', () => {expect(friendlyError(new Error('x'.repeat(500))).length).toBe(240);});});++describe('isMobileBrowser', () => {+ it('detects mobile user agents', () => {+ const iphone =+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15';+ const android =+ 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Mobile Safari/537.36';+ expect(isMobileBrowser(iphone)).toBe(true);+ expect(isMobileBrowser(android)).toBe(true);+ });++ it('treats desktop user agents as non-mobile', () => {+ const mac =+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/120 Safari/537.36';+ expect(isMobileBrowser(mac)).toBe(false);+ expect(isMobileBrowser('')).toBe(false);+ });+});++describe('walletDeeplink', () => {+ const page = 'https://three.ws/paywall.html?req=abc&return=%2Fapi%2Fpaid';++ it('builds Phantom/Solflare universal browse links carrying the page url', () => {+ const enc = encodeURIComponent(page);+ expect(walletDeeplink('phantom', page, 'https://three.ws')).toBe(+ `https://phantom.app/ul/browse/${enc}?ref=${encodeURIComponent('https://three.ws')}`,+ );+ expect(walletDeeplink('solflare', page)).toBe(`https://solflare.com/ul/v1/browse/${enc}`);+ });++ it('builds MetaMask dapp links from host + path (no scheme)', () => {+ expect(walletDeeplink('metamask', page)).toBe(+ 'https://metamask.app.link/dapp/three.ws/paywall.html?req=abc&return=%2Fapi%2Fpaid',+ );+ });++ it('builds a Coinbase Wallet dapp link', () => {+ expect(walletDeeplink('coinbase', page)).toBe(+ `https://go.cb-w.com/dapp?cb_url=${encodeURIComponent(page)}`,+ );+ });++ it('returns null for WalletConnect (its own modal handles mobile) and missing urls', () => {+ expect(walletDeeplink('walletconnect', page)).toBe(null);+ expect(walletDeeplink('phantom', '')).toBe(null);+ });+});