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
refactor(x402): give the Solana rail one definition of its CAIP-2 ids
nirholas7d78f83e359f
diff --git a/api/_lib/env.js b/api/_lib/env.jsindex 11779cd19..7475b73fd 100644--- a/api/_lib/env.js+++ b/api/_lib/env.js@@ -959,6 +959,14 @@ export const env = {get X402_ADVERTISE_BASE() {return opt('X402_ADVERTISE_BASE', 'false') === 'true';},+ // CAIP-2 id of a local `solana-test-validator` lane, e.g.+ // `solana:8p5TJ23ZcDXq5QA3vGbpf2sXZ25f`. Set only when running a proof or a+ // test against a validator on this machine; unset everywhere deployed, which+ // leaves the recognized Solana networks at mainnet + devnet. See+ // api/_lib/x402/solana-networks.js and specs/inference-receipts.md.+ get X402_SOLANA_LOCAL_NETWORK() {+ return opt('X402_SOLANA_LOCAL_NETWORK');+ },get X402_FACILITATOR_TOKEN_SOLANA() {return opt('X402_FACILITATOR_TOKEN_SOLANA', opt('X402_FACILITATOR_TOKEN'));},diff --git a/api/_lib/x402-solana-confirm.js b/api/_lib/x402-solana-confirm.jsindex 8f23e1549..4157750e3 100644--- a/api/_lib/x402-solana-confirm.js+++ b/api/_lib/x402-solana-confirm.js@@ -28,22 +28,14 @@ import {getAssociatedTokenAddressSync,} from '@solana/spl-token';-// CAIP-2 ids for Solana, inlined (rather than imported from x402-spec.js) to-// keep this module free of a circular import — x402-spec.js imports us.-const NETWORK_SOLANA_MAINNET = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';-const NETWORK_SOLANA_DEVNET = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1';-// SPL-Token instruction discriminators (first byte of instruction data).const IX_TRANSFER = 3;const IX_TRANSFER_CHECKED = 12;-export function isSolanaNetwork(network) {- return (- network === NETWORK_SOLANA_MAINNET ||- network === NETWORK_SOLANA_DEVNET ||- network === 'solana'- );-}+// The CAIP-2 ids and the rail predicate live in x402/solana-networks.js, a leaf+// module, so x402-spec.js (which imports us) can share one definition without a+// circular import. Re-exported here because callers already import it from us.+export { isSolanaNetwork } from './x402/solana-networks.js';// Normalize a VersionedTransaction's message into a flat list of// { programId: PublicKey, accountKeys: PublicKey[], data: Uint8Array } so we candiff --git a/api/_lib/x402-spec.js b/api/_lib/x402-spec.jsindex bde9d49d6..0ddf1882c 100644--- a/api/_lib/x402-spec.js+++ b/api/_lib/x402-spec.js@@ -44,7 +44,12 @@ import {import { env } from './env.js';import { X402Error } from './x402-errors.js';-import { confirmSolanaPayment, isSolanaNetwork } from './x402-solana-confirm.js';+import { confirmSolanaPayment } from './x402-solana-confirm.js';+import {+ NETWORK_SOLANA_MAINNET,+ NETWORK_SOLANA_DEVNET,+ isSolanaNetwork,+} from './x402/solana-networks.js';import {PAYMENT_EVENT_TOPIC as BSC_PAYMENT_EVENT_TOPIC,settleDirectPayment,@@ -101,8 +106,9 @@ export const NETWORK_BASE_SEPOLIA = 'eip155:84532';export const NETWORK_ARBITRUM_MAINNET = 'eip155:42161';export const NETWORK_BSC_MAINNET = 'eip155:56';export { NETWORK_XLAYER_MAINNET };-export const NETWORK_SOLANA_MAINNET = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';-export const NETWORK_SOLANA_DEVNET = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1';+// Solana ids come from x402/solana-networks.js, the one definition the whole+// rail shares (see isSolanaNetwork above).+export { NETWORK_SOLANA_MAINNET, NETWORK_SOLANA_DEVNET };// Networks the CDP facilitator settles when credentials are configured.// Confirmed against the live /supported probe, CDP advertises exact for Base@@ -264,11 +270,7 @@ function getCdpHeadersFactory() {// verifyPayment, settlePayment, probeFacilitators) all route through here, so// this is the single seam that decides who settles each network.export function facilitatorFor(network) {- if (- network === NETWORK_SOLANA_MAINNET ||- network === NETWORK_SOLANA_DEVNET ||- network === 'solana'- ) {+ if (isSolanaNetwork(network)) {// Solana resolution (see x402/ring-config.js): an explicit// X402_FACILITATOR_URL_SOLANA always wins; otherwise// X402_SELF_FACILITATOR_ENABLED=true defaults to our owndiff --git a/api/_lib/x402/a2a-client.js b/api/_lib/x402/a2a-client.jsindex 4ec033c5b..dd1d02852 100644--- a/api/_lib/x402/a2a-client.js+++ b/api/_lib/x402/a2a-client.js@@ -28,21 +28,20 @@ import {A2A_EXTENSIONS_HEADER,A2A_X402_EXTENSION_URI,} from './a2a-server.js';+import {+ NETWORK_SOLANA_MAINNET,+ NETWORK_SOLANA_DEVNET,+ isSolanaNetwork,+ solanaLocalTestNetwork,+} from './solana-networks.js';export { A2A_EXTENSIONS_HEADER, A2A_X402_EXTENSION_URI };// Solana is the primary A2A settlement rail. CAIP-2 ids must match what the-// server advertises (api/_lib/x402/a2a-server.js → NETWORK_SOLANA_*).-export const NETWORK_SOLANA_MAINNET = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';-export const NETWORK_SOLANA_DEVNET = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1';--export function isSolanaNetwork(network) {- return (- network === NETWORK_SOLANA_MAINNET ||- network === NETWORK_SOLANA_DEVNET ||- network === 'solana'- );-}+// server advertises (api/_lib/x402/a2a-server.js → NETWORK_SOLANA_*); both the+// ids and the rail predicate come from solana-networks.js so the client, the+// spec module and the payload decoder can never drift apart.+export { NETWORK_SOLANA_MAINNET, NETWORK_SOLANA_DEVNET, isSolanaNetwork };function isEvmExactNetwork(network) {return /^eip155:\d+$/.test(network);@@ -168,9 +167,27 @@ export async function buildSolanaExactPayload({ accept, signer, resource, rpcUrl'buildSolanaExactPayload: a @solana/kit signer is required (try createSolanaSigner)',);}+ // @x402/svm allowlists the three public Solana CAIP-2 ids, and it consults+ // the network for exactly one thing: which RPC client to construct. On the+ // local test lane we already hand it the RPC URL, so present the devnet+ // shape to that selector while the payload keeps the lane's real id. The+ // built transaction is unaffected: it is signed against the blockhash of+ // whatever RPC `rpcUrl` names, which is the local validator.+ const localLane = solanaLocalTestNetwork();+ let schemeAccept = accept;+ if (localLane && accept.network === localLane) {+ if (!rpcUrl) {+ throw new A2AClientError(+ 'missing_rpc_url',+ 'buildSolanaExactPayload: the local Solana test lane requires an explicit rpcUrl',+ { network: accept.network },+ );+ }+ schemeAccept = { ...accept, network: NETWORK_SOLANA_DEVNET };+ }const { ExactSvmScheme } = await import('@x402/svm');const scheme = new ExactSvmScheme(signer, rpcUrl ? { rpcUrl } : undefined);- const built = await scheme.createPaymentPayload(2, accept);+ const built = await scheme.createPaymentPayload(2, schemeAccept);return {x402Version: built.x402Version || 2,scheme: 'exact',diff --git a/api/_lib/x402/revenue-networks.js b/api/_lib/x402/revenue-networks.jsindex c3427af3e..9239b305e 100644--- a/api/_lib/x402/revenue-networks.js+++ b/api/_lib/x402/revenue-networks.js@@ -52,7 +52,15 @@ const FAMILIES = [{ family: 'arbitrum', label: 'Arbitrum One', chainId: 42161, ids: ['eip155:42161', 'arbitrum'] },{ family: 'optimism', label: 'Optimism', chainId: 10, ids: ['eip155:10', 'optimism'] },{ family: 'polygon', label: 'Polygon', chainId: 137, ids: ['eip155:137', 'polygon'] },- { family: 'xlayer', label: 'X Layer', chainId: 196, ids: ['eip155:196', 'xlayer', 'x-layer'] },+ {+ family: 'xlayer',+ label: 'X Layer',+ chainId: 196,+ // Not in the ERC-8004 chain table (no registry deployment there), so the+ // explorer is named here rather than left unresolvable.+ explorer: 'https://www.oklink.com/xlayer',+ ids: ['eip155:196', 'xlayer', 'x-layer'],+ },];const BY_ID = new Map();@@ -60,7 +68,13 @@ for (const f of FAMILIES) for (const id of f.ids) BY_ID.set(id.toLowerCase(), f)const BY_FAMILY = new Map(FAMILIES.map((f) => [f.family, f]));-const UNKNOWN = { family: 'unknown', label: 'Unknown network', cluster: null, chainId: null };+const UNKNOWN = {+ family: 'unknown',+ label: 'Unknown network',+ cluster: null,+ chainId: null,+ explorer: null,+};const EVM_TX_RE = /^0x[0-9a-f]{64}$/i;@@ -70,7 +84,7 @@ const EVM_TX_RE = /^0x[0-9a-f]{64}$/i;* the chain table, and any other `solana:<ref>` reads as Solana.** @param {string|null|undefined} raw- * @returns {{ id: string|null, family: string, label: string, cluster: string|null, chainId: number|null }}+ * @returns {{ id: string|null, family: string, label: string, cluster: string|null, chainId: number|null, explorer: string|null }}*/export function networkIdentity(raw) {const id = typeof raw === 'string' ? raw.trim() : '';@@ -84,6 +98,7 @@ export function networkIdentity(raw) {label: known.label,cluster: known.cluster || null,chainId: known.chainId || null,+ explorer: known.explorer || CHAIN_BY_ID[known.chainId]?.explorer || null,};}@@ -97,12 +112,13 @@ export function networkIdentity(raw) {label: chain?.name || `EVM chain ${chainId}`,cluster: null,chainId,+ explorer: chain?.explorer || null,};}// Any other Solana reference (a cluster we have not named) is still Solana.if (/^solana:/i.test(id)) {- return { id, family: 'solana', label: 'Solana', cluster: null, chainId: null };+ return { id, family: 'solana', label: 'Solana', cluster: null, chainId: null, explorer: null };}return { id, ...UNKNOWN };@@ -153,10 +169,7 @@ export function revenueTxUrl(hash, raw) {if (!sig) return null;const net = networkIdentity(raw);- if (net.chainId) {- const explorer = CHAIN_BY_ID[net.chainId]?.explorer;- return explorer ? `${explorer}/tx/${sig}` : null;- }+ if (net.chainId) return net.explorer ? `${net.explorer}/tx/${sig}` : null;if (net.family.startsWith('solana')) {return `https://solscan.io/tx/${sig}${net.cluster ? `?cluster=${net.cluster}` : ''}`;}diff --git a/api/_lib/x402/solana-networks.js b/api/_lib/x402/solana-networks.jsnew file mode 100644index 000000000..4f3e73d26--- /dev/null+++ b/api/_lib/x402/solana-networks.js@@ -0,0 +1,55 @@+// Solana CAIP-2 network ids for the x402 rail, in one place.+//+// These three constants used to be inlined in three modules (x402-spec.js,+// x402-solana-confirm.js, x402/a2a-client.js) because x402-spec.js imports+// x402-solana-confirm.js and an import back the other way would have been a+// cycle. This module is a leaf (env.js is itself import-free), so every caller+// can share one definition without reintroducing that cycle.+//+// A Solana CAIP-2 id is `solana:` followed by the first 32 base58 characters of+// the cluster's genesis hash, which is what makes the id chain-identifying: a+// receipt naming a network names the exact ledger the payment settled on.++import { env } from '../env.js';++export const NETWORK_SOLANA_MAINNET = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';+export const NETWORK_SOLANA_DEVNET = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1';++/** The CAIP-2 network id for a cluster, given its base58 genesis hash. */+export function caip2ForGenesisHash(genesisHash) {+ const hash = String(genesisHash || '').trim();+ if (!hash) throw new Error('caip2ForGenesisHash: genesis hash is required');+ return `solana:${hash.slice(0, 32)}`;+}++/**+ * The local test lane, or null when it is not configured.+ *+ * A `solana-test-validator` mints a fresh genesis on every reset, so its CAIP-2+ * id cannot be a constant: the operator passes the live id in+ * `X402_SOLANA_LOCAL_NETWORK` (derive it with `caip2ForGenesisHash()` from a+ * `getGenesisHash` call). Unset, as it is in production and in every deployed+ * environment, this returns null and `isSolanaNetwork()` behaves exactly as it+ * did before this module existed.+ *+ * Naming the lane honestly is the point. Settling a proof run on a local+ * validator while labelling the receipt `NETWORK_SOLANA_DEVNET` would put a+ * false chain id inside a signed attestation whose whole purpose is to be+ * true, so the local lane gets its own real id instead.+ */+export function solanaLocalTestNetwork() {+ const id = env.X402_SOLANA_LOCAL_NETWORK;+ if (!id) return null;+ const trimmed = String(id).trim();+ return /^solana:[1-9A-HJ-NP-Za-km-z]{1,32}$/.test(trimmed) ? trimmed : null;+}++/** True when this network settles over the Solana rail. */+export function isSolanaNetwork(network) {+ return (+ network === NETWORK_SOLANA_MAINNET ||+ network === NETWORK_SOLANA_DEVNET ||+ network === 'solana' ||+ (network != null && network === solanaLocalTestNetwork())+ );+}diff --git a/pages/x402-revenue.html b/pages/x402-revenue.htmlindex e130dcbfd..5e39236a0 100644--- a/pages/x402-revenue.html+++ b/pages/x402-revenue.html@@ -148,7 +148,7 @@<span id="xr-window-label" data-no-i18n>24h</span></span><span class="xr-big-val" id="xr-c-gross">—</span>- <span class="xr-big-sub" id="xr-c-gross-sub" data-i18n="x402_revenue.settled_usdc">settled USDC</span>+ <span class="xr-big-sub" id="xr-c-gross-sub" data-no-i18n>settled USDC</span></div><div class="xr-badges"><span class="xr-momentum" id="xr-momentum" data-dir="flat" hidden></span>@@ -192,7 +192,7 @@<div class="xr-kpi"><dt data-i18n="x402_revenue.success_rate">Success rate</dt><dd id="xr-c-success">—</dd>- <span class="xr-kpi-sub" id="xr-c-success-sub" data-i18n="x402_revenue.settled_vs_failed">settled vs failed</span>+ <span class="xr-kpi-sub" id="xr-c-success-sub" data-no-i18n>settled vs failed</span></div></dl>diff --git a/src/x402-revenue.js b/src/x402-revenue.jsindex ab2e188b5..a59202f07 100644--- a/src/x402-revenue.js+++ b/src/x402-revenue.js@@ -38,6 +38,7 @@ const state = {pollTimer: null,statsTimer: null,firstChart: true,+ feedState: 'loading', // mirrors the live-state chip, survives an i18n repainthasStats: false, // a successful stats render has landed at least oncehasFeed: false, // the feed has rendered rows or an honest empty state};@@ -539,6 +540,7 @@ function clearFilters() {// ── feed ────────────────────────────────────────────────────────────────────function setFeedState(s) {+ state.feedState = s;const el = $('xr-feed-state');if (!el) return;el.dataset.state = s;@@ -907,7 +909,7 @@ function wireSearch() {function wireKeys() {document.addEventListener('keydown', (e) => {- const typing = e.target.matches('input, textarea, [contenteditable]');+ const typing = !!e.target?.matches?.('input, textarea, [contenteditable]');if (typing) {if (e.key === 'Escape') e.target.blur();return;@@ -932,7 +934,9 @@ function onVisibility() {if (document.hidden) {if (!state.paused) setFeedState('paused');} else {- setFeedState(state.paused ? 'paused' : state.latestTs ? 'live' : 'empty');+ // Returning to a tab must not paint over a real failure with "no activity".+ if (state.feedState === 'error') setFeedState('error');+ else setFeedState(state.paused ? 'paused' : state.latestTs ? 'live' : 'empty');if (!state.paused) pollDelta();loadStats();}@@ -962,6 +966,19 @@ function init() {if (!document.hidden) loadStats();}, STATS_REFRESH_MS);document.addEventListener('visibilitychange', onVisibility);+ // The i18n runtime applies its catalog after first paint and again on every+ // locale switch, rewriting annotated copy. Anything this page keeps live is+ // re-asserted afterwards so a translation pass can never leave the feed+ // reading "connecting…" while rows are streaming in.+ window.addEventListener('i18n:change', reassertLiveText);+}++function reassertLiveText() {+ setFeedState(state.feedState || 'loading');+ for (const id of ['xr-window-label', 'xr-top-window', 'xr-net-window']) {+ const el = $(id);+ if (el) el.textContent = state.period;+ }}if (document.readyState === 'loading') {