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.

feat: unify agent preparation endpoint for EVM and Solana, add Solana agent card and update reputation logic

nirholasd9d75acd9f72
diff --git a/api/agents/onchain/prep.js b/api/agents/onchain/prep.js
new file mode 100644
index 000000000..bf18c2f0e
--- /dev/null
+++ b/api/agents/onchain/prep.js
@@ -0,0 +1,274 @@
+/**
+ * POST /api/agents/onchain/prep
+ *
+ * Unified prep endpoint covering all chain families. Replaces:
+ * • /api/agents/register-prep (EVM)
+ * • /api/agents/solana-register-prep (Solana)
+ *
+ * Single payload shape, single CAIP-2 chain selector. The metadata manifest is
+ * pinned once (IPFS) regardless of family, so EVM and Solana point at the same
+ * canonical content.
+ *
+ * Returns a `prepId` that the confirm endpoint validates against. Persistence
+ * uses `agent_registrations_pending` (existing table) with a unified payload.
+ *
+ * Wallet-ownership rule: if the wallet isn't yet linked to the user, prep
+ * still succeeds — the SolanaAdapter runs SIWS on the client before calling
+ * here, and EVM uses signed-tx ownership at confirm time. We re-verify on
+ * confirm; prep only requires the wallet to *belong* to no other user.
+ */
+
+import { z } from 'zod';
+import {
+ createUmi,
+} from '@metaplex-foundation/umi-bundle-defaults';
+import { mplCore, createV1 } from '@metaplex-foundation/mpl-core';
+import {
+ generateSigner,
+ publicKey as umiPublicKey,
+ signerIdentity,
+ createNoopSigner,
+} from '@metaplex-foundation/umi';
+import { sql } from '../../_lib/db.js';
+import { getSessionUser } from '../../_lib/auth.js';
+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';
+
+// ── Chain parsing ────────────────────────────────────────────────────────────
+
+const SOLANA_REFS = {
+ mainnet: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
+ devnet: 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
+};
+const SOLANA_REF_TO_CLUSTER = Object.fromEntries(
+ Object.entries(SOLANA_REFS).map(([k, v]) => [v, k]),
+);
+
+/** @returns {{ family: 'evm', chainId: number } | { family: 'solana', cluster: 'mainnet'|'devnet' }} */
+function parseChain(caip2) {
+ const [ns, ref] = String(caip2).split(':');
+ if (ns === 'eip155') {
+ const id = Number(ref);
+ if (!Number.isInteger(id) || id <= 0) throw new Error(`bad eip155 ref: ${ref}`);
+ return { family: 'evm', chainId: id };
+ }
+ if (ns === 'solana') {
+ const cluster = SOLANA_REF_TO_CLUSTER[ref];
+ if (!cluster) throw new Error(`unknown solana ref: ${ref}`);
+ return { family: 'solana', cluster };
+ }
+ throw new Error(`unsupported namespace: ${ns}`);
+}
+
+// ── Request schema ───────────────────────────────────────────────────────────
+
+const bodySchema = z.object({
+ agent_id: z.string().min(1).max(80),
+ chain: z.string().min(8).max(120),
+ wallet_address: z.string().min(20).max(80),
+ name: z.string().trim().min(1).max(60),
+ description: z.string().trim().max(280).default(''),
+ avatar_id: z.string().uuid().nullable().optional(),
+ skills: z
+ .array(z.string().regex(/^[a-z0-9-]{1,40}$/i))
+ .max(16)
+ .optional(),
+});
+
+// ── Manifest pinning (shared across families) ────────────────────────────────
+
+import { createHash } from 'crypto';
+import { r2 } from '../../_lib/r2.js';
+import { PutObjectCommand } from '@aws-sdk/client-s3';
+
+async function pinManifest(manifest) {
+ const bytes = Buffer.from(JSON.stringify(manifest), 'utf-8');
+ const token = process.env.WEB3_STORAGE_TOKEN;
+ if (token) {
+ try {
+ const r = await fetch('https://api.web3.storage/upload', {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${token}` },
+ body: bytes,
+ });
+ if (r.ok) {
+ const data = await r.json();
+ if (data.cid) return { cid: data.cid, uri: `ipfs://${data.cid}` };
+ }
+ } catch (e) {
+ console.warn('[onchain/prep] web3.storage pin failed:', e.message);
+ }
+ }
+ // Deterministic fallback: stash to R2, generate a content-hash CID stub
+ // so the URL stays pointing at real bytes even if web3.storage is down.
+ const hash = createHash('sha256').update(bytes).digest('hex');
+ const stubCid = `bafkreigenerated${hash.slice(0, 40)}`;
+ const key = `agent-manifests/${Date.now()}-${Math.random().toString(36).slice(2)}.json`;
+ await r2.send(
+ new PutObjectCommand({
+ Bucket: env.S3_BUCKET,
+ Key: key,
+ Body: bytes,
+ ContentType: 'application/json',
+ }),
+ );
+ return { cid: stubCid, uri: `ipfs://${stubCid}` };
+}
+
+function buildManifest({ name, description, avatar_id, skills }) {
+ return {
+ $schema: 'https://3d-agent.io/schemas/manifest/0.1.json',
+ spec: 'agent-manifest/0.1',
+ name,
+ description,
+ image: '',
+ tags: [],
+ body: { uri: '', format: 'gltf-binary' },
+ _baseURI: 'ipfs://',
+ ...(avatar_id ? { avatarId: avatar_id } : {}),
+ ...(skills?.length ? { skills } : {}),
+ };
+}
+
+// ── Family-specific tx prep ──────────────────────────────────────────────────
+
+async function prepEvm({ chainId, metadataUri }) {
+ // EVM: client builds its own tx via ethers; we just hand back the metadata
+ // URI and the registry address (looked up from the existing chain config).
+ const { REGISTRY_DEPLOYMENTS } = await import('../../_lib/erc8004-chains.js').catch(() => ({}));
+ const deployment = REGISTRY_DEPLOYMENTS?.[chainId];
+ return {
+ contractAddress: deployment?.identityRegistry || null,
+ metadataUri,
+ };
+}
+
+async function prepSolana({ cluster, metadataUri, walletAddress, name }) {
+ const rpc =
+ cluster === 'devnet'
+ ? process.env.SOLANA_RPC_URL_DEVNET || 'https://api.devnet.solana.com'
+ : process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com';
+
+ const umi = createUmi(rpc).use(mplCore());
+ const ownerPk = umiPublicKey(walletAddress);
+ const assetSigner = generateSigner(umi);
+ umi.use(signerIdentity(createNoopSigner(ownerPk)));
+
+ const builder = createV1(umi, {
+ asset: assetSigner,
+ owner: ownerPk,
+ name,
+ uri: metadataUri,
+ });
+ const txBytes = await builder.buildAndSign(umi);
+ return {
+ assetPubkey: assetSigner.publicKey,
+ txBase64: Buffer.from(txBytes).toString('base64'),
+ metadataUri,
+ };
+}
+
+// ── Endpoint ─────────────────────────────────────────────────────────────────
+
+export default wrap(async (req, res) => {
+ if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
+ if (!method(req, res, ['POST'])) return;
+
+ const user = await getSessionUser(req);
+ if (!user) return error(res, 401, 'unauthorized', 'sign in required');
+
+ const rl = await limits.authIp(clientIp(req));
+ if (!rl.success) return error(res, 429, 'rate_limited', 'too many requests');
+
+ const body = parse(bodySchema, await readJson(req));
+
+ let chain;
+ try {
+ chain = parseChain(body.chain);
+ } catch (e) {
+ return error(res, 400, 'validation_error', `invalid chain: ${e.message}`);
+ }
+
+ if (body.avatar_id) {
+ const [av] = await sql`
+ select id from avatars
+ where id=${body.avatar_id} and owner_id=${user.id} and deleted_at is null
+ limit 1
+ `;
+ if (!av) return error(res, 404, 'not_found', 'avatar not found');
+ }
+
+ // Refuse if the wallet is linked to a *different* user. If unlinked, allow
+ // — confirm-time tx receipt verification is the binding step.
+ const [conflict] = await sql`
+ select user_id from user_wallets
+ where address = ${body.wallet_address}
+ and chain_type = ${chain.family === 'solana' ? 'solana' : 'evm'}
+ and user_id <> ${user.id}
+ limit 1
+ `;
+ if (conflict) {
+ return error(res, 403, 'forbidden', 'wallet is linked to another account');
+ }
+
+ const manifest = buildManifest(body);
+ 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,
+ });
+ }
+
+ const prepId = await randomToken(24);
+ const expiresAt = new Date(Date.now() + 60 * 60 * 1000);
+
+ await sql`
+ insert into agent_registrations_pending
+ (user_id, cid, metadata_uri, payload, expires_at)
+ values (
+ ${user.id},
+ ${cid},
+ ${metadataUri},
+ ${JSON.stringify({
+ prep_id: prepId,
+ agent_id: body.agent_id,
+ chain: body.chain,
+ chain_family: chain.family,
+ wallet_address: body.wallet_address,
+ name: body.name,
+ description: body.description,
+ avatar_id: body.avatar_id || null,
+ skills: body.skills || [],
+ ...(chain.family === 'evm'
+ ? { contract_address: familyPrep.contractAddress }
+ : { asset_pubkey: familyPrep.assetPubkey, cluster: chain.cluster }),
+ })}::jsonb,
+ ${expiresAt}
+ )
+ `;
+
+ return json(res, 201, {
+ prepId,
+ chain: body.chain,
+ metadataUri,
+ cid,
+ ...(chain.family === 'evm'
+ ? { contractAddress: familyPrep.contractAddress }
+ : {
+ assetPubkey: familyPrep.assetPubkey,
+ txBase64: familyPrep.txBase64,
+ cluster: chain.cluster,
+ }),
+ expiresAt: expiresAt.toISOString(),
+ });
+});
diff --git a/api/agents/solana-card.js b/api/agents/solana-card.js
new file mode 100644
index 000000000..65621d373
--- /dev/null
+++ b/api/agents/solana-card.js
@@ -0,0 +1,62 @@
+/**
+ * GET /api/agents/solana-card?asset=<pubkey>
+ *
+ * A2A-compatible agent discovery card for a Solana-registered agent.
+ * Same role as ERC-8004's agent manifest: any third-party agent can fetch
+ * this URL, learn the agent's identity + skills + how to attest about it.
+ *
+ * Also served at /a/sol/<asset>/.well-known/agent-card.json (see vercel.json).
+ */
+
+import { sql } from '../_lib/db.js';
+import { PublicKey } from '@solana/web3.js';
+import { cors, json, method, wrap, error } from '../_lib/http.js';
+import { env } from '../_lib/env.js';
+
+export default wrap(async (req, res) => {
+ if (cors(req, res, { methods: 'GET,OPTIONS', origins: '*' })) return;
+ if (!method(req, res, ['GET'])) return;
+
+ const url = new URL(req.url, `http://${req.headers.host}`);
+ const asset = url.searchParams.get('asset');
+ if (!asset) return error(res, 400, 'validation_error', 'asset required');
+ try { new PublicKey(asset); }
+ catch { return error(res, 400, 'validation_error', 'invalid asset pubkey'); }
+
+ const [a] = await sql`
+ select id, name, description, skills, wallet_address as owner, meta, avatar_id
+ from agent_identities
+ where meta->>'sol_mint_address' = ${asset} and deleted_at is null limit 1
+ `;
+ if (!a) return error(res, 404, 'not_found', 'agent not found');
+
+ const network = a.meta?.network || 'mainnet';
+ const origin = env.APP_ORIGIN;
+
+ return json(res, 200, {
+ schema_version: '1.0',
+ // A2A core
+ name: a.name,
+ description: a.description,
+ // three.ws extensions
+ identity: {
+ chain: 'solana',
+ network,
+ asset_pubkey: asset, // Metaplex Core NFT = agent ID
+ owner: a.owner,
+ passport_url: `${origin}/agent-passport.html?asset=${asset}&network=${network}`,
+ },
+ skills: a.skills || [],
+ endpoints: {
+ chat: `${origin}/api/agents/${a.id}/chat`,
+ attestations: `${origin}/api/agents/solana-attestations?asset=${asset}&network=${network}`,
+ reputation: `${origin}/api/agents/solana-reputation?asset=${asset}&network=${network}`,
+ },
+ attestation: {
+ schemas_url: `${origin}/.well-known/agent-attestation-schemas`,
+ transport: 'spl-memo',
+ memo_program: 'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr',
+ usage: 'Sign an SPL Memo tx with one of the published schemas as JSON, including this asset_pubkey as a non-signer key.',
+ },
+ }, { 'cache-control': 'public, max-age=120', 'access-control-allow-origin': '*' });
+});
diff --git a/api/agents/solana-reputation.js b/api/agents/solana-reputation.js
index 99772171b..8b1edf4bc 100644
--- a/api/agents/solana-reputation.js
+++ b/api/agents/solana-reputation.js
@@ -33,6 +33,8 @@ export default wrap(async (req, res) => {
catch { return error(res, 400, 'validation_error', 'invalid asset pubkey'); }
// One pass: feedback aggregates split by verified-task-acceptance.
+ // Sybil-weighting: collapse multiple feedbacks from the same attester to
+ // their average so one wallet leaving 100 ratings doesn't dominate.
const [fb] = await sql`
with feedback as (
select
@@ -52,14 +54,22 @@ export default wrap(async (req, res) => {
and f.network = ${network}
and f.kind = 'threews.feedback.v1'
and f.revoked = false
+ ),
+ per_attester as (
+ select attester, avg(score)::float as score_avg,
+ bool_or(task_accepted) as any_verified
+ from feedback group by attester
)
select
- count(*)::int as total,
- count(*) filter (where task_accepted)::int as verified,
- count(*) filter (where disputed)::int as disputed,
- coalesce(avg(score), 0)::float as score_avg,
- coalesce(avg(score) filter (where task_accepted), 0)::float as score_avg_verified
- from feedback
+ (select count(*)::int from feedback) as total,
+ (select count(*) filter (where task_accepted)::int from feedback) as verified,
+ (select count(*) filter (where disputed)::int from feedback) as disputed,
+ (select coalesce(avg(score), 0)::float from feedback) as score_avg,
+ (select coalesce(avg(score) filter (where task_accepted), 0)::float from feedback) as score_avg_verified,
+ (select count(*)::int from per_attester) as unique_attesters,
+ (select count(*) filter (where any_verified)::int from per_attester) as unique_verified_attesters,
+ (select coalesce(avg(score_avg), 0)::float from per_attester) as score_avg_weighted,
+ (select coalesce(avg(score_avg) filter (where any_verified), 0)::float from per_attester) as score_avg_weighted_verified
`;
const [val] = await sql`
@@ -89,11 +99,15 @@ export default wrap(async (req, res) => {
agent: asset,
network,
feedback: {
- total: fb.total,
- verified: fb.verified,
- disputed: fb.disputed,
- score_avg: Number(fb.score_avg.toFixed(3)),
- score_avg_verified: Number(fb.score_avg_verified.toFixed(3)),
+ total: fb.total,
+ verified: fb.verified,
+ disputed: fb.disputed,
+ unique_attesters: fb.unique_attesters,
+ unique_verified_attesters: fb.unique_verified_attesters,
+ score_avg: Number(fb.score_avg.toFixed(3)),
+ score_avg_verified: Number(fb.score_avg_verified.toFixed(3)),
+ score_avg_weighted: Number(fb.score_avg_weighted.toFixed(3)),
+ score_avg_weighted_verified: Number(fb.score_avg_weighted_verified.toFixed(3)),
},
validation: {
passed: val.passed,
diff --git a/vercel.json b/vercel.json
index 980e47c6e..850adba96 100644
--- a/vercel.json
+++ b/vercel.json
@@ -14,6 +14,10 @@
"src": "/\\.well-known/agent-attestation-schemas",
"dest": "/api/wk-agent-attestation-schemas"
},
+ {
+ "src": "/a/sol/([^/]+)/\\.well-known/agent-card\\.json",
+ "dest": "/api/agents/solana-card?asset=$1"
+ },
{
"src": "/\\.well-known/oauth-authorization-server",
"dest": "/api/wk-oauth-authorization-server"