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.

Wallet identity: real-agent hydration gate, embedded-chip popover, fork/share fixes

vwkb6d58365c032a
diff --git a/src/avatar-gallery-picker.js b/src/avatar-gallery-picker.js
index 06adf1cc5..02c4a06a5 100644
--- a/src/avatar-gallery-picker.js
+++ b/src/avatar-gallery-picker.js
@@ -18,7 +18,7 @@
import './avatar-gallery-picker.css';
import { onchainBadgeHTML } from './shared/onchain-badge.js';
-import { walletChipHTML } from './shared/agent-wallet-chip.js';
+import { walletChipHTML, wireWalletChips } from './shared/agent-wallet-chip.js';
const PAGE_SIZE = 24;
@@ -459,6 +459,7 @@ export class AvatarGalleryPicker {
</div>
`;
+ wireWalletChips(card);
card.addEventListener('click', () => this._selectAvatar(a));
return card;
}
diff --git a/src/marketplace-detail.js b/src/marketplace-detail.js
index fb08ad64b..220a726e7 100644
--- a/src/marketplace-detail.js
+++ b/src/marketplace-detail.js
@@ -12,7 +12,7 @@
import { onchainBadgeHTML } from './shared/onchain-badge.js';
import { coinChipHTML } from './shared/agent-coin.js';
-import { walletChipHTML } from './shared/agent-wallet-chip.js';
+import { walletChipHTML, wireWalletChips } from './shared/agent-wallet-chip.js';
import { agentAvatarGlb, hasCustomAvatar, seeInWorldHref } from './shared/agent-3d.js';
import { log } from './shared/log.js';
import { resolveDevR2Url } from './shared/dev-r2-proxy.js';
@@ -502,6 +502,7 @@ function renderCreatorModal(payload, deps) {
<div class="meta">⊙ ${fmtNumber(a.views_count)} · ⑂ ${fmtNumber(a.forks_count)}</div>
</div>`;
}).join('');
+ wireWalletChips(agentsGrid);
agentsGrid.querySelectorAll('[data-agent-id]').forEach((card) => {
card.addEventListener('click', () => {
closeCreatorModal();
@@ -527,6 +528,7 @@ function renderCreatorModal(payload, deps) {
<div class="meta">${escapeHtml(formatDate(avt.created_at))}</div>
</div>`;
}).join('');
+ wireWalletChips(avatarsGrid);
avatarsGrid.querySelectorAll('[data-avatar-id]').forEach((card) => {
card.addEventListener('click', () => {
const avt = avatars.find((x) => x.id === card.dataset.avatarId);
diff --git a/src/shared/agent-wallet-chip.js b/src/shared/agent-wallet-chip.js
index 65448d381..ef7b29444 100644
--- a/src/shared/agent-wallet-chip.js
+++ b/src/shared/agent-wallet-chip.js
@@ -45,6 +45,7 @@ import { computeRarity } from '../solana/vanity/rarity.js';
const STYLE_ID = 'tws-agent-wallet-chip-styles';
const BASE58_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
const EVM_RE = /^0x[0-9a-fA-F]{40}$/;
+const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
function esc(s) {
return String(s == null ? '' : s).replace(
@@ -347,8 +348,10 @@ function tipAttrs(agent, status) {
const name = agent?.name || agent?.display_name || status.name || '';
const avatar = status.avatarUrl || '';
const accepted = (agent?.meta?.payments?.accepted_tokens || agent?.payments?.accepted_tokens || []).join(',');
+ const agentId = status.agentId || agent?.id || agent?.agent_id || '';
return (
`data-twc-tip="${esc(status.address)}"` +
+ (agentId ? ` data-twc-id="${esc(agentId)}"` : '') +
(name ? ` data-twc-name="${esc(name)}"` : '') +
(avatar ? ` data-twc-av="${esc(avatar)}"` : '') +
(accepted ? ` data-twc-pay="${esc(accepted)}"` : '')
@@ -385,6 +388,14 @@ export function walletChipHTML(agent, opts = {}) {
return `<span class="twc twc-pending" title="Wallet provisioning">${WALLET_SVG}<span>Wallet pending</span></span>`;
}
+ // Live balance + rich popover only make sense when the chip is backed by a real
+ // agent_identities row. Some surfaces (KOL/trader leaderboards) pass non-agent
+ // rows that still carry an address+vanity — those render the static chip but
+ // skip hydration so they never show a stuck skeleton or a dead popover action.
+ const isRealAgent = !!(status.agentId && UUID_RE.test(String(status.agentId)));
+ const wantBalance = balance && isRealAgent;
+ const wantPopover = popover && isRealAgent;
+
// A matched vanity address advertises its honest rarity tier (tinted); when the
// chip is interactive it links to the address's appraisal in the proof-of-grind
// gallery, otherwise (link:false, used inside card anchors) it stays a plain
@@ -406,10 +417,9 @@ export function walletChipHTML(agent, opts = {}) {
// Live balance slot — renders a skeleton, then hydrates to "$1.2K +2.3%" on
// viewport-enter via POST /api/agents/balances. Read-only display, safe inside
// card anchors (it's a <span>, no nested <a>).
- const balanceSlot =
- balance && status.agentId
- ? `<span class="twc-bal" data-twc-bal aria-label="Wallet value loading"><span class="twc-bal-sk"></span></span>`
- : '';
+ const balanceSlot = wantBalance
+ ? `<span class="twc-bal" data-twc-bal aria-label="Wallet value loading"><span class="twc-bal-sk"></span></span>`
+ : '';
const copyBtn = link
? `<button type="button" class="twc-act" data-twc-copy="${esc(status.address)}" title="Copy address" aria-label="Copy wallet address">${COPY_SVG}</button>`
@@ -431,7 +441,7 @@ export function walletChipHTML(agent, opts = {}) {
// Stash everything the popover needs as JSON so wiring can build it without
// re-normalizing. Kept off the visible chip; only public data.
- const popData = popover
+ const popData = wantPopover
? esc(JSON.stringify({
agentId: status.agentId, address: status.address, evm: status.evmAddress,
name: status.name, avatar: status.avatarUrl, isVanity: status.isVanity,
@@ -444,13 +454,15 @@ export function walletChipHTML(agent, opts = {}) {
}))
: '';
- const triggerAttrs = popover
- ? ` data-twc-trigger tabindex="0" role="button" aria-haspopup="dialog" aria-expanded="false" data-twc-pop="${popData}"`
+ // `link:false` means the chip lives inside a clickable card; mark it embedded
+ // so the popover stays a hover/focus enhancement there and never hijacks the
+ // card's own tap/navigation.
+ const triggerAttrs = wantPopover
+ ? ` data-twc-trigger tabindex="0" role="button" aria-haspopup="dialog" aria-expanded="false"${link ? '' : ' data-twc-embedded="1"'} data-twc-pop="${popData}"`
+ : '';
+ const hydrateAttrs = wantBalance
+ ? ` data-twc-aid="${esc(status.agentId)}" data-twc-net="${esc(network)}"${isOwner ? ' data-twc-isowner="1"' : ''}`
: '';
- const hydrateAttrs =
- balance && status.agentId
- ? ` data-twc-aid="${esc(status.agentId)}" data-twc-net="${esc(network)}"${isOwner ? ' data-twc-isowner="1"' : ''}`
- : '';
const title = `Agent wallet ${status.address}${status.isVanity ? ' (vanity)' : ''}`;
return (
@@ -510,6 +522,7 @@ function wireWalletChip(node) {
e.preventDefault();
e.stopPropagation();
const agent = {
+ id: tipBtn.getAttribute('data-twc-id') || undefined,
solana_address: tipBtn.getAttribute('data-twc-tip'),
name: tipBtn.getAttribute('data-twc-name') || 'this agent',
avatar_thumbnail_url: tipBtn.getAttribute('data-twc-av') || '',
@@ -749,12 +762,14 @@ function wirePopoverTrigger(node) {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); _openPopover?.trigger === node ? closePopover(node) : open(); }
else if (e.key === 'Escape') closePopover(node);
});
- // Touch: a chip NOT inside a card link toggles the popover; inside a card we
- // leave the tap to the card navigation and rely on the profile's own HUD.
- if (!node.closest('a')) {
+ // Touch: a standalone chip (not embedded in a clickable card, not inside an
+ // anchor) toggles the popover. Embedded/card chips leave the tap to the card's
+ // own navigation — the popover stays a hover/focus enhancement there.
+ if (!node.closest('a') && !node.hasAttribute('data-twc-embedded')) {
node.addEventListener('click', (e) => {
if (e.target.closest('[data-twc-stop],[data-twc-copy],[data-twc-tip]')) return;
e.preventDefault();
+ e.stopPropagation();
_openPopover?.trigger === node ? closePopover(node) : open();
});
}
@@ -942,18 +957,30 @@ function wirePopoverActions(el, meta) {
});
const fork = el.querySelector('[data-twc-fork]');
if (fork) fork.addEventListener('click', async () => {
+ const dest = `/agent/${meta.agentId}`;
const loggedIn = await probeLoggedIn();
- if (!loggedIn) { window.location.href = `/login?next=${encodeURIComponent(meta.hubUrl || '/')}`; return; }
- // Forking mints the caller their own wallet — route to the agent where the
- // existing fork action lives.
- window.location.href = meta.forkedFrom?.agent_id ? `/agent/${meta.agentId}?fork=1` : `/agent/${meta.agentId}?fork=1`;
+ // Forking mints the caller their own wallet (POST /api/avatars/fork). The
+ // fork-to-own CTA + its auth/CSRF handling already live on the agent's own
+ // page — route there rather than re-implementing the fork here. Anonymous
+ // visitors go through sign-in first and land back on that page.
+ window.location.href = loggedIn ? dest : `/login?next=${encodeURIComponent(dest)}`;
});
const share = el.querySelector('[data-twc-share]');
if (share) share.addEventListener('click', async () => {
const shareUrl = `${location.origin}/agent/${meta.agentId}`;
try {
const mod = await import('./share.js');
- if (mod.showSharePanel) { mod.showSharePanel({ url: shareUrl, title: meta.name || 'Agent wallet', image: `/api/agent-share?id=${encodeURIComponent(meta.agentId)}` }, share); return; }
+ if (mod.showSharePanel) {
+ // share.js entity contract: { kind, id, title, description, shareUrl }.
+ mod.showSharePanel({
+ kind: 'agent',
+ id: meta.agentId,
+ title: meta.name || 'Agent wallet',
+ description: `${meta.name || 'This agent'}'s wallet on three.ws`,
+ shareUrl,
+ }, share);
+ return;
+ }
} catch { /* fall through to clipboard */ }
copyToClipboard(shareUrl, share);
});
diff --git a/tests/agent-wallet-vanity.test.js b/tests/agent-wallet-vanity.test.js
index 7b2493936..f31790f0d 100644
--- a/tests/agent-wallet-vanity.test.js
+++ b/tests/agent-wallet-vanity.test.js
@@ -8,7 +8,7 @@
import { describe, it, expect } from 'vitest';
import { grindMintKeypair, estimateAttempts, isValidVanityPrefix, BASE58_ALPHABET } from '../api/_lib/pump-vanity.js';
-import { getWalletStatus, hasWallet, walletChipHTML } from '../src/shared/agent-wallet-chip.js';
+import { getWalletStatus, getWalletIdentity, hasWallet, walletChipHTML, formatWalletUsd } from '../src/shared/agent-wallet-chip.js';
describe('grindMintKeypair', () => {
it('finds an address with the requested prefix', async () => {
@@ -94,3 +94,58 @@ describe('getWalletStatus / wallet chip', () => {
expect(ro).not.toContain('<a');
});
});
+
+describe('wallet identity descriptor (multi-chain + ownership)', () => {
+ const PLAIN = '4Nd1mDQkSp9Xb6hT2pXc8QwJ5kR7yZ3aB9cD1eF2gH3';
+ const EVM = '0x1234567890abcdef1234567890ABCDEF12345678';
+ const UUID = '11111111-2222-4333-8444-555555555555';
+
+ it('exposes the EVM side of the identity, never confusing it with Solana', () => {
+ const s = getWalletStatus({ id: UUID, solana_address: PLAIN, wallet_address: EVM });
+ expect(s.address).toBe(PLAIN);
+ expect(s.evmAddress).toBe(EVM);
+ expect(s.evmExplorerUrl).toContain(EVM);
+ // A base58 in `wallet` must not leak into evmAddress and vice-versa.
+ expect(getWalletStatus({ id: UUID, wallet: PLAIN }).evmAddress).toBeNull();
+ expect(getWalletStatus({ id: UUID, solana_address: PLAIN, wallet: EVM }).evmAddress).toBe(EVM);
+ });
+
+ it('reads ownership attribution for the visitor "by @creator" view', () => {
+ const s = getWalletStatus({ id: UUID, solana_address: PLAIN, owner_name: 'satoshi', user_id: 'u1' });
+ expect(s.ownerName).toBe('satoshi');
+ expect(s.ownerId).toBe('u1');
+ const forked = getWalletStatus({ id: UUID, solana_address: PLAIN, meta: { forked_from: { owner_name: 'alice', agent_id: 'a9' } } });
+ expect(forked.forkedFrom.owner_name).toBe('alice');
+ });
+
+ it('getWalletIdentity is the same normalizer as getWalletStatus', () => {
+ expect(getWalletIdentity({ id: UUID, solana_address: PLAIN }).address).toBe(PLAIN);
+ });
+
+ it('only hydrates live balance for real (uuid) agents', () => {
+ // Real agent → balance slot + hydration attributes present.
+ const real = walletChipHTML({ id: UUID, solana_address: PLAIN }, { isOwner: false });
+ expect(real).toContain('twc-bal');
+ expect(real).toContain(`data-twc-aid="${UUID}"`);
+ expect(real).toContain('data-twc-trigger'); // rich popover enabled
+ // Non-agent row (KOL leaderboard) → static chip, no stuck skeleton / dead popover.
+ const kol = walletChipHTML({ id: 'kol-7', wallet: PLAIN }, { isOwner: false, link: false });
+ expect(kol).not.toContain('data-twc-aid');
+ expect(kol).not.toContain('twc-bal-sk');
+ });
+
+ it('marks the owner chip with a "Yours" badge', () => {
+ expect(walletChipHTML({ id: UUID, solana_address: PLAIN }, { isOwner: true })).toContain('twc-own');
+ expect(walletChipHTML({ id: UUID, solana_address: PLAIN }, { isOwner: false })).not.toContain('twc-own');
+ });
+
+ it('formats USD compactly', () => {
+ expect(formatWalletUsd(0)).toBe('$0');
+ expect(formatWalletUsd(0.004)).toBe('<$0.01');
+ expect(formatWalletUsd(9.4)).toBe('$9.40');
+ expect(formatWalletUsd(950)).toBe('$950');
+ expect(formatWalletUsd(1234)).toBe('$1.2K');
+ expect(formatWalletUsd(3_400_000)).toBe('$3.4M');
+ expect(formatWalletUsd(null)).toBeNull();
+ });
+});