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.

fix(_handlers.js): refactor RPC handling in handleRegisterPrep for improved error handling and fallback

nirholasc427291f2719
diff --git a/api/agents/onchain/[action].js b/api/agents/onchain/[action].js
index 226e719c2..413349f25 100644
--- a/api/agents/onchain/[action].js
+++ b/api/agents/onchain/[action].js
@@ -158,6 +158,11 @@ async function buildSolanaTx({ rpc, walletAddress, name, metadataUri }) {
return { assetSigner, txBytes };
}
+function isRpcAuthError(err) {
+ const msg = err?.message || '';
+ return msg.includes('401') || msg.includes('Unauthorized') || msg.includes('invalid api key');
+}
+
async function prepSolana({ cluster, metadataUri, walletAddress, name }) {
const configuredRpc =
cluster === 'devnet'
@@ -173,22 +178,44 @@ async function prepSolana({ cluster, metadataUri, walletAddress, name }) {
metadataUri,
}));
} catch (err) {
- // If the configured RPC rejects with an auth error, fall back to the public
- // endpoint so a bad/expired API key doesn't block all deploys.
- const isAuthErr =
- err?.message?.includes('401') ||
- err?.message?.includes('Unauthorized') ||
- err?.message?.includes('invalid api key');
+ // If the configured RPC rejects with an auth error AND it's not already the
+ // public endpoint, fall back to the public endpoint once.
const isUsingPublic = configuredRpc === SOLANA_PUBLIC_RPC[cluster];
- if (!isAuthErr || isUsingPublic) throw err;
+ if (!isRpcAuthError(err) || isUsingPublic) {
+ if (isRpcAuthError(err)) {
+ // Public RPC also returned auth error — Solana's public endpoints now
+ // require an API key. Surface a clear operator-facing message.
+ const e = new Error(
+ 'Solana RPC rejected the request with an auth error. ' +
+ 'Set SOLANA_RPC_URL (mainnet) or SOLANA_RPC_URL_DEVNET (devnet) ' +
+ 'to a valid RPC endpoint with an API key.',
+ );
+ e.code = 'rpc_auth_failed';
+ throw e;
+ }
+ throw err;
+ }
console.warn('[onchain/prep] configured Solana RPC auth failed, retrying with public RPC');
- ({ assetSigner, txBytes } = await buildSolanaTx({
- rpc: SOLANA_PUBLIC_RPC[cluster],
- walletAddress,
- name,
- metadataUri,
- }));
+ try {
+ ({ assetSigner, txBytes } = await buildSolanaTx({
+ rpc: SOLANA_PUBLIC_RPC[cluster],
+ walletAddress,
+ name,
+ metadataUri,
+ }));
+ } catch (fallbackErr) {
+ if (isRpcAuthError(fallbackErr)) {
+ const e = new Error(
+ 'Solana RPC rejected the request with an auth error. ' +
+ 'Set SOLANA_RPC_URL (mainnet) or SOLANA_RPC_URL_DEVNET (devnet) ' +
+ 'to a valid RPC endpoint with an API key.',
+ );
+ e.code = 'rpc_auth_failed';
+ throw e;
+ }
+ throw fallbackErr;
+ }
}
return {
@@ -243,15 +270,22 @@ async function handlePrep(req, res) {
const { cid, uri: metadataUri } = await pinManifest(manifest);
let familyPrep;
- if (chain.family === 'evm') {
- familyPrep = await prepEvm({ chainId: chain.chainId, metadataUri });
- } else {
- familyPrep = await prepSolana({
- cluster: chain.cluster,
- metadataUri,
- walletAddress: body.wallet_address,
- name: body.name,
- });
+ try {
+ if (chain.family === 'evm') {
+ familyPrep = await prepEvm({ chainId: chain.chainId, metadataUri });
+ } else {
+ familyPrep = await prepSolana({
+ cluster: chain.cluster,
+ metadataUri,
+ walletAddress: body.wallet_address,
+ name: body.name,
+ });
+ }
+ } catch (e) {
+ if (e.code === 'rpc_auth_failed') {
+ return error(res, 503, 'rpc_unavailable', e.message);
+ }
+ throw e;
}
const prepId = await randomToken(24);
diff --git a/api/agents/solana/_handlers.js b/api/agents/solana/_handlers.js
index 96aedf187..5c22d0d31 100644
--- a/api/agents/solana/_handlers.js
+++ b/api/agents/solana/_handlers.js
@@ -316,20 +316,38 @@ export const handleRegisterPrep = wrap(async (req, res) => {
}
}
- const SOLANA_RPC = process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com';
- const rpcEndpoint = network === 'devnet' ? (process.env.SOLANA_RPC_URL_DEVNET || 'https://api.devnet.solana.com') : SOLANA_RPC;
-
- const umi = createUmi(rpcEndpoint).use(mplCore());
- const ownerPubkey = umiPublicKey(wallet_address);
- const assetSigner = asset_pubkey ? createNoopSigner(umiPublicKey(asset_pubkey)) : generateSigner(umi);
- umi.use(signerIdentity(createNoopSigner(ownerPubkey)));
+ const PUBLIC_RPC = {
+ mainnet: 'https://api.mainnet-beta.solana.com',
+ devnet: 'https://api.devnet.solana.com',
+ };
+ const configuredRpc = network === 'devnet'
+ ? (process.env.SOLANA_RPC_URL_DEVNET || PUBLIC_RPC.devnet)
+ : (process.env.SOLANA_RPC_URL || PUBLIC_RPC.mainnet);
+ const publicRpc = PUBLIC_RPC[network] || PUBLIC_RPC.mainnet;
const appOrigin = env.APP_ORIGIN;
const metadataUri = body.metadata_uri || `${appOrigin}/api/agents/solana-metadata?name=${encodeURIComponent(name)}&desc=${encodeURIComponent(description)}`;
- const builder = createV1(umi, { asset: assetSigner, owner: ownerPubkey, name, uri: metadataUri });
- const tx = await builder.buildAndSign(umi);
- const txBytes = umi.transactions.serialize(tx);
+ const buildTx = async (rpc) => {
+ const umi = createUmi(rpc).use(mplCore());
+ const ownerPubkey = umiPublicKey(wallet_address);
+ const assetSigner = asset_pubkey ? createNoopSigner(umiPublicKey(asset_pubkey)) : generateSigner(umi);
+ umi.use(signerIdentity(createNoopSigner(ownerPubkey)));
+ const builder = createV1(umi, { asset: assetSigner, owner: ownerPubkey, name, uri: metadataUri });
+ const txBytes = await builder.buildAndSign(umi);
+ return { assetSigner, txBytes };
+ };
+
+ let assetSigner, txBytes;
+ try {
+ ({ assetSigner, txBytes } = await buildTx(configuredRpc));
+ } catch (err) {
+ const isAuthErr = err?.message?.includes('401') || err?.message?.includes('Unauthorized') || err?.message?.includes('invalid api key');
+ if (!isAuthErr || configuredRpc === publicRpc) throw err;
+ console.warn('[solana/register-prep] configured RPC auth failed, retrying with public RPC');
+ ({ assetSigner, txBytes } = await buildTx(publicRpc));
+ }
+
const txBase64 = Buffer.from(txBytes).toString('base64');
const prepId = await randomToken(24);