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(premium): send wallet-ownership signature so gated status returns keys

fudjiab57a6fd2d594
diff --git a/src/dashboard-next/pages/data-api.js b/src/dashboard-next/pages/data-api.js
index 07ee551d0..6ff30e552 100644
--- a/src/dashboard-next/pages/data-api.js
+++ b/src/dashboard-next/pages/data-api.js
@@ -21,10 +21,17 @@ import {
const WALLET_LS = 'threews:premium:wallet';
+// A wallet-ownership signature is required to read the private half of
+// /api/premium/status (keys, usage, purchase history). The server accepts it
+// within a 5-minute window; we reuse a signature for 4 minutes so a single
+// approval covers a normal session of key management without re-prompting.
+const PROOF_TTL_MS = 4 * 60 * 1000;
+
const state = {
plans: null,
wallet: localStorage.getItem(WALLET_LS) || null,
status: null, // /api/premium/status for state.wallet
+ proof: null, // { wallet, signature, issuedAt, ts } ownership proof for keys/history
buying: null, // asset symbol while a purchase is in flight
freshKey: null, // plaintext shown exactly once after purchase/rotate
};
@@ -50,6 +57,35 @@ function solanaProvider() {
return window.phantom?.solana || window.solana || null;
}
+// Base64 of an ed25519 signature — the server (verifySiwsSignature) accepts base64.
+function bytesToB64(bytes) {
+ const arr = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
+ let bin = '';
+ for (const b of arr) bin += String.fromCharCode(b);
+ return btoa(bin);
+}
+
+function freshProof() {
+ const p = state.proof;
+ return p && p.wallet === state.wallet && Date.now() - p.ts < PROOF_TTL_MS ? p : null;
+}
+
+// Prove control of the connected wallet so the server returns keys/usage/history.
+// Reuses a still-fresh proof; otherwise asks the wallet to sign once. Returns the
+// proof, or null if the wallet can't sign (falls back to public pass-state only).
+async function ensureProof(provider) {
+ if (!state.wallet) return null;
+ const cached = freshProof();
+ if (cached) return cached;
+ if (!provider?.signMessage) return null;
+ const issuedAt = new Date().toISOString();
+ const message = `three.ws premium status\nWallet: ${state.wallet}\nIssued At: ${issuedAt}`;
+ const res = await provider.signMessage(new TextEncoder().encode(message), 'utf8');
+ const signature = bytesToB64(res?.signature ?? res);
+ state.proof = { wallet: state.wallet, signature, issuedAt, ts: Date.now() };
+ return state.proof;
+}
+
// ── Purchase flow ────────────────────────────────────────────────────────────
async function connectWallet() {
@@ -102,6 +138,7 @@ async function buy(asset) {
const out = await pollSubscribe(quote.id, signature);
state.freshKey = out.api_key || null;
setBuyNote('');
+ try { await ensureProof(provider); } catch { /* keys stay hidden until Verify */ }
await loadStatus();
} catch (err) {
if (!/user rejected|cancell?ed/i.test(err?.message || '')) {
@@ -147,7 +184,7 @@ function renderStatusCard() {
<button class="dn-btn primary" id="da-connect" type="button">Connect wallet</button>
</div>`;
$('da-connect')?.addEventListener('click', async () => {
- try { await connectWallet(); await loadStatus(); } catch (e) { setBuyNote(e.message, true); }
+ try { await verifyAndLoad(); } catch (e) { setBuyNote(e.message, true); }
});
return;
}
@@ -169,6 +206,10 @@ function renderStatusCard() {
</div>`;
} else {
const left = daysLeft(s.pass.expires_at);
+ // A fresh ownership signature unlocks the key-management panel below.
+ const verifyBtn = freshProof()
+ ? ''
+ : `<button class="dn-btn ghost" id="da-verify" type="button">Verify to manage keys</button>`;
el.innerHTML = `
<div class="da-hero da-hero-active">
<div>
@@ -176,15 +217,19 @@ function renderStatusCard() {
<div class="da-hero-title"><span class="da-dot"></span> Active — ${left} day${left === 1 ? '' : 's'} left</div>
<p class="da-hero-sub">Runs until ${esc(fmtDate(s.pass.expires_at))}. Renewing now appends 30 days to the end — no lost time. Browser searches: choose “sign with wallet” in the payment dialog on <a href="/markets/archive">/markets/archive</a>.</p>
</div>
- <button class="dn-btn ghost" id="da-switch" type="button">Switch wallet</button>
+ <div class="da-hero-actions">${verifyBtn}<button class="dn-btn ghost" id="da-switch" type="button">Switch wallet</button></div>
</div>`;
}
+ $('da-verify')?.addEventListener('click', async () => {
+ try { await verifyAndLoad(); } catch (e) { setBuyNote(e.message, true); }
+ });
$('da-switch')?.addEventListener('click', async () => {
localStorage.removeItem(WALLET_LS);
state.wallet = null;
state.status = null;
+ state.proof = null;
renderStatusCard();
- try { await connectWallet(); await loadStatus(); } catch { /* stays disconnected */ }
+ try { await verifyAndLoad(); } catch { /* stays disconnected */ }
});
}
@@ -255,11 +300,21 @@ function renderKeys() {
if (!el) return;
const keys = state.status?.keys || [];
if (!state.wallet || !keys.length) {
- el.innerHTML = `
- <div class="dn-panel">
- <div class="da-panel-head"><h2>API key</h2></div>
- <p class="da-note">No key yet — buying a pass mints one automatically (<code>x402_live_…</code>). It bypasses the per-call x402 charge on every premium endpoint via the <code>X-API-Key</code> header.</p>
- </div>`;
+ // An active pass with no visible key means we haven't proved wallet
+ // ownership yet — the key exists server-side but is gated behind a signature.
+ const locked = state.status?.active && !freshProof();
+ el.innerHTML = locked
+ ? `<div class="dn-panel">
+ <div class="da-panel-head"><h2>API key</h2></div>
+ <p class="da-note">Your key is protected. <button class="da-inline-link" id="da-keys-verify" type="button">Verify this wallet</button> to view its prefix, usage, and rotate/revoke controls.</p>
+ </div>`
+ : `<div class="dn-panel">
+ <div class="da-panel-head"><h2>API key</h2></div>
+ <p class="da-note">No key yet — buying a pass mints one automatically (<code>x402_live_…</code>). It bypasses the per-call x402 charge on every premium endpoint via the <code>X-API-Key</code> header.</p>
+ </div>`;
+ $('da-keys-verify')?.addEventListener('click', async () => {
+ try { await verifyAndLoad(); } catch (e) { setBuyNote(e.message, true); }
+ });
return;
}
const rows = keys.map((k) => `
@@ -373,13 +428,24 @@ function renderAll() {
async function loadStatus() {
if (!state.wallet) { renderAll(); return; }
try {
- state.status = await get(`/api/premium/status?wallet=${encodeURIComponent(state.wallet)}`);
+ const p = freshProof();
+ const auth = p
+ ? `&signature=${encodeURIComponent(p.signature)}&issuedAt=${encodeURIComponent(p.issuedAt)}`
+ : '';
+ state.status = await get(`/api/premium/status?wallet=${encodeURIComponent(state.wallet)}${auth}`);
} catch {
state.status = null;
}
renderAll();
}
+// Connect the wallet, prove ownership, then reload the (now full) status.
+async function verifyAndLoad() {
+ const { provider } = await connectWallet();
+ await ensureProof(provider);
+ await loadStatus();
+}
+
function injectStyles() {
if (document.getElementById('da-styles')) return;
ensureStateKitStyles();
@@ -393,6 +459,9 @@ function injectStyles() {
.da-hero-title { font-size: 1.35rem; font-weight: 680; color: var(--nxt-ink); margin: .2rem 0; display: flex; align-items: center; gap: .5rem; }
.da-hero-sub { margin: 0; font-size: .85rem; color: var(--nxt-ink-dim); line-height: 1.55; max-width: 60ch; }
.da-hero-sub a { color: var(--nxt-accent); }
+ .da-hero-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
+ .da-inline-link { background: none; border: none; padding: 0; font: inherit; color: var(--nxt-accent); cursor: pointer; text-decoration: underline; }
+ .da-inline-link:hover { color: var(--nxt-ink); }
.da-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--nxt-success); box-shadow: 0 0 8px var(--nxt-success); display: inline-block; }
.da-panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; flex-wrap: wrap; margin-bottom: .9rem; }
.da-panel-head h2 { margin: 0; font-size: .95rem; font-weight: 650; color: var(--nxt-ink); }
diff --git a/tests/premium-status-auth.test.js b/tests/premium-status-auth.test.js
new file mode 100644
index 000000000..f7de2e806
--- /dev/null
+++ b/tests/premium-status-auth.test.js
@@ -0,0 +1,125 @@
+/**
+ * GET /api/premium/status — authorization gate (security fix H1).
+ *
+ * The keys / resources / purchase-history payload is private, account-scoped
+ * data. Because wallet addresses are public, the handler must only return it to
+ * a caller who proves control of the wallet with a fresh ed25519 signature
+ * (SIWS-class), mirroring /api/x402/my-receipts. Unauthenticated callers get
+ * only the boolean pass state.
+ *
+ * `passStatus` and the rate limiter are mocked; the SIWS signature verification
+ * runs for real against a genuine ed25519 keypair.
+ */
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { ed25519 } from '@noble/curves/ed25519.js';
+import bs58 from 'bs58';
+
+const rl = { ok: true };
+vi.mock('../api/_lib/rate-limit.js', () => ({
+ limits: { premiumStatusIp: vi.fn(async () => ({ success: rl.ok, reset: Date.now() + 60_000 })) },
+ clientIp: () => '127.0.0.1',
+}));
+
+const FULL_STATUS = {
+ active: true,
+ pass: { id: 'pass-1', plan: 'premium', started_at: '2026-07-01', expires_at: '2026-08-01', asset: 'THREE' },
+ resources: ['news-archive'],
+ keys: [{ id: 'sub-1', name: 'default', key_prefix: 'x402_live_ABCDEF', rate_limit_per_minute: 120, expires_at: '2026-08-01', status: 'active', usage: { granted: 42, denied: 3, last_seen: null } }],
+ history: [{ id: 'pass-1', plan: 'premium', asset: 'THREE', amount_atomics: '3995000000', usd_price: 7.99, tx_signature: 'sig', started_at: '2026-07-01', expires_at: '2026-08-01', created_at: '2026-07-01' }],
+};
+vi.mock('../api/_lib/premium.js', () => ({
+ passStatus: vi.fn(async () => structuredClone(FULL_STATUS)),
+}));
+
+const { default: handler } = await import('../api/premium/status.js');
+
+// ── ed25519 wallet + signer ──────────────────────────────────────────────────
+const seed = new Uint8Array(32).fill(7);
+const pub = ed25519.getPublicKey(seed);
+const WALLET = bs58.encode(pub);
+
+function sign(message) {
+ const sig = ed25519.sign(new TextEncoder().encode(message), seed);
+ return Buffer.from(sig).toString('base64');
+}
+function ownershipMessage(wallet, issuedAt) {
+ return `three.ws premium status\nWallet: ${wallet}\nIssued At: ${issuedAt}`;
+}
+
+function makeReq(url) {
+ return { method: 'GET', url, headers: { origin: 'https://three.ws' }, socket: { remoteAddress: '127.0.0.1' } };
+}
+function makeRes() {
+ const r = { statusCode: 200, _h: {}, _b: null };
+ r.setHeader = (k, v) => { r._h[k] = v; };
+ r.getHeader = (k) => r._h[k];
+ r.end = (b) => { r._b = b; };
+ Object.defineProperty(r, '_s', { get() { return this.statusCode; } });
+ Object.defineProperty(r, 'json', { value: () => JSON.parse(r._b) });
+ return r;
+}
+async function call(qs = '') {
+ const req = makeReq(`/api/premium/status${qs}`);
+ const res = makeRes();
+ await handler(req, res);
+ return res;
+}
+
+beforeEach(() => {
+ rl.ok = true;
+ vi.clearAllMocks();
+});
+
+describe('GET /api/premium/status — public path (no ownership proof)', () => {
+ it('returns only pass state, never keys/resources/history', async () => {
+ const r = await call(`?wallet=${WALLET}`);
+ expect(r._s).toBe(200);
+ const body = r.json();
+ expect(body.active).toBe(true);
+ expect(body.pass).toMatchObject({ id: 'pass-1' });
+ expect(body.keys).toEqual([]);
+ expect(body.resources).toEqual([]);
+ expect(body.history).toEqual([]);
+ });
+
+ it('an attacker signing with a DIFFERENT key gets the stripped payload', async () => {
+ const otherSeed = new Uint8Array(32).fill(9);
+ const issuedAt = new Date().toISOString();
+ // Sign the victim wallet's message with the attacker's key — must not verify.
+ const forged = Buffer.from(ed25519.sign(new TextEncoder().encode(ownershipMessage(WALLET, issuedAt)), otherSeed)).toString('base64');
+ const r = await call(`?wallet=${WALLET}&issuedAt=${encodeURIComponent(issuedAt)}&signature=${encodeURIComponent(forged)}`);
+ expect(r._s).toBe(200);
+ expect(r.json().keys).toEqual([]);
+ });
+
+ it('a stale (expired) signature gets the stripped payload', async () => {
+ const issuedAt = new Date(Date.now() - 10 * 60 * 1000).toISOString(); // 10 min old
+ const signature = sign(ownershipMessage(WALLET, issuedAt));
+ const r = await call(`?wallet=${WALLET}&issuedAt=${encodeURIComponent(issuedAt)}&signature=${encodeURIComponent(signature)}`);
+ expect(r._s).toBe(200);
+ expect(r.json().keys).toEqual([]);
+ });
+});
+
+describe('GET /api/premium/status — authenticated path (valid ownership proof)', () => {
+ it('returns the full payload including keys and history', async () => {
+ const issuedAt = new Date().toISOString();
+ const signature = sign(ownershipMessage(WALLET, issuedAt));
+ const r = await call(`?wallet=${WALLET}&issuedAt=${encodeURIComponent(issuedAt)}&signature=${encodeURIComponent(signature)}`);
+ expect(r._s).toBe(200);
+ const body = r.json();
+ expect(body.keys).toHaveLength(1);
+ expect(body.keys[0].key_prefix).toBe('x402_live_ABCDEF');
+ expect(body.history).toHaveLength(1);
+ expect(body.resources).toEqual(['news-archive']);
+ });
+});
+
+describe('GET /api/premium/status — rate limiting', () => {
+ it('429 when the limiter declines', async () => {
+ rl.ok = false;
+ const r = await call(`?wallet=${WALLET}`);
+ expect(r._s).toBe(429);
+ });
+});