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
// HTTP-level integration tests for the /play wallet sign-in endpoints.
//
// Exercises the full nonce → sign → verify pipeline against real module code:
// GET /api/play/nonce — gate config + fresh nonce
// POST /api/play/verify — ed25519 sig check, balance gate, pass issuance
import { describe, it, expect, beforeAll, vi } from 'vitest';
import { Readable } from 'node:stream';
import { ed25519 } from '@noble/curves/ed25519.js';
import bs58mod from 'bs58';
const bs58 = bs58mod.default || bs58mod;
// ── Helpers ──────────────────────────────────────────────────────────────────
function mockRes() {
const r = {
statusCode: 200, _headers: {}, _body: '', _ended: false,
setHeader(k, v) { this._headers[k.toLowerCase()] = v; },
getHeader(k) { return this._headers[k.toLowerCase()]; },
end(b) { this._body = b || ''; this._ended = true; },
get json() { try { return JSON.parse(this._body); } catch { return null; } },
};
return r;
}
function mockReq({ method = 'GET', url = '/', body = null, origin = 'http://localhost:3000' } = {}) {
const chunks = body != null ? [Buffer.from(JSON.stringify(body))] : [];
const r = Readable.from(chunks);
r.method = method;
r.url = url;
r.headers = { origin, 'content-type': 'application/json' };
return r;
}
// Generate a real ed25519 keypair + derive the Solana (base58) representation.
function newKeypair() {
const sk = ed25519.utils.randomSecretKey();
const pk = ed25519.getPublicKey(sk);
return { sk, pk, address: bs58.encode(pk) };
}
function buildMessage(address, nonce) {
return [
'three.ws wants you to sign in with your Solana account:',
address, '',
'Sign in to play three.ws. This proves you own this wallet and will not move any funds or tokens.',
'', `Nonce: ${nonce}`,
].join('\n');
}
function signMsg(sk, address, nonce) {
const msg = new TextEncoder().encode(buildMessage(address, nonce));
return bs58.encode(ed25519.sign(msg, sk));
}
// ── Module setup ─────────────────────────────────────────────────────────────
let nonceMod, verifyMod;
beforeAll(async () => {
process.env.NODE_ENV = 'development';
delete process.env.HOLDER_PASS_SECRET;
// Use SOL as the gate token — readable via public RPC so the balance check runs
// end-to-end. A freshly generated keypair holds 0 SOL, triggering balance_too_low.
process.env.PLAY_GATE_MINT = 'So11111111111111111111111111111111111111112';
process.env.PLAY_GATE_MIN = '1';
process.env.PLAY_GATE_SYMBOL = 'SOL';
nonceMod = await import('../api/play/nonce.js');
verifyMod = await import('../api/play/verify.js');
});
// ── Nonce endpoint ────────────────────────────────────────────────────────────
describe('GET /api/play/nonce', () => {
it('returns gate config and a fresh nonce when gate is active', async () => {
const res = mockRes();
await nonceMod.default(mockReq({ method: 'GET' }), res);
expect(res.statusCode).toBe(200);
const d = res.json.data;
expect(d.required).toBe(true);
expect(typeof d.nonce).toBe('string');
expect(d.nonce.length).toBeGreaterThan(30);
expect(d.mint).toBe(process.env.PLAY_GATE_MINT);
expect(d.minBalance).toBe(1);
expect(d.symbol).toBe('SOL');
});
it('rejects non-GET requests', async () => {
const res = mockRes();
await nonceMod.default(mockReq({ method: 'POST' }), res);
expect(res.statusCode).toBe(405);
});
});
// ── Verify endpoint ───────────────────────────────────────────────────────────
describe('POST /api/play/verify', () => {
it('rejects a missing / garbage nonce', async () => {
const { address, sk } = newKeypair();
// Use a string that passes schema validation (≥16 chars) but fails HMAC check.
const fakeNonce = 'a'.repeat(40) + '.' + 'b'.repeat(43);
const res = mockRes();
await verifyMod.default(mockReq({
method: 'POST',
body: { address, signature: signMsg(sk, address, fakeNonce), nonce: fakeNonce },
}), res);
expect(res.statusCode).toBe(400);
expect(res.json.error).toBe('nonce_invalid');
});
it('rejects a bad signature (zeroed bytes)', async () => {
const { address } = newKeypair();
const nr = mockRes();
await nonceMod.default(mockReq({ method: 'GET' }), nr);
const nonce = nr.json.data.nonce;
const res = mockRes();
await verifyMod.default(mockReq({
method: 'POST',
body: { address, signature: bs58.encode(new Uint8Array(64)), nonce },
}), res);
expect(res.statusCode).toBe(401);
expect(res.json.error).toBe('bad_signature');
});
it('rejects a valid signature for the wrong address', async () => {
const a = newKeypair();
const b = newKeypair();
const nr = mockRes();
await nonceMod.default(mockReq({ method: 'GET' }), nr);
const nonce = nr.json.data.nonce;
const res = mockRes();
// Sign with keypair A but claim address B.
await verifyMod.default(mockReq({
method: 'POST',
body: { address: b.address, signature: signMsg(a.sk, b.address, nonce), nonce },
}), res);
// The message is built from b.address, signed with a.sk — mismatch.
// ed25519.verify will fail because the public key (b.pk) doesn't match the signature.
expect(res.statusCode).toBe(401);
expect(res.json.error).toBe('bad_signature');
});
it('returns balance_too_low for a zero-balance wallet (real RPC)', async () => {
const { address, sk } = newKeypair();
const nr = mockRes();
await nonceMod.default(mockReq({ method: 'GET' }), nr);
const nonce = nr.json.data.nonce;
const res = mockRes();
await verifyMod.default(mockReq({
method: 'POST',
body: { address, signature: signMsg(sk, address, nonce), nonce },
}), res);
expect(res.statusCode).toBe(200);
const d = res.json.data;
expect(d.ok).toBe(false);
expect(d.reason).toBe('balance_too_low');
expect(d.wallet).toBe(address);
expect(d.balance).toBe(0);
expect(d.minBalance).toBe(1);
expect(typeof d.acquireUrl).toBe('string');
// For SOL the acquire URL should be an on-ramp, not a SOL-SOL DEX link.
expect(d.acquireUrl).not.toContain(`SOL-${process.env.PLAY_GATE_MINT}`);
expect(d.acquireUrl).toContain('jup.ag');
});
it('rejects a replayed nonce (single-use enforcement)', async () => {
const { address, sk } = newKeypair();
const nr = mockRes();
await nonceMod.default(mockReq({ method: 'GET' }), nr);
const nonce = nr.json.data.nonce;
const sig = signMsg(sk, address, nonce);
// First use — valid nonce. The nonce is burned regardless of the balance check
// outcome (RPC may be unavailable in CI, yielding a 502 — that's fine here).
const r1 = mockRes();
await verifyMod.default(mockReq({ method: 'POST', body: { address, signature: sig, nonce } }), r1);
expect([200, 502, 503]).toContain(r1.statusCode);
// Second use — the nonce must already be burned so replay is rejected,
// regardless of whether the first call got a RPC response.
const r2 = mockRes();
await verifyMod.default(mockReq({ method: 'POST', body: { address, signature: sig, nonce } }), r2);
expect(r2.statusCode).toBe(400);
expect(r2.json.error).toBe('nonce_invalid');
});
it('returns 400 gate_disabled when no mint is configured', async () => {
// PLAY_GATE_MINT is read once, at module load. Mutating the env var on the
// already-imported module would prove nothing, so drop the module graph and
// re-import both endpoints with the gate unpinned.
vi.resetModules();
const savedMint = process.env.PLAY_GATE_MINT;
const savedThree = process.env.THREE_MINT;
delete process.env.PLAY_GATE_MINT;
delete process.env.THREE_MINT;
try {
const gatelessVerify = (await import('../api/play/verify.js')).default;
const res = mockRes();
await gatelessVerify(mockReq({
method: 'POST',
body: { address: 'a'.repeat(40), signature: 'b'.repeat(80), nonce: 'c'.repeat(40) },
}), res);
expect(res.statusCode).toBe(400);
expect(res.json.error).toBe('gate_disabled');
// And the nonce endpoint tells the client to skip sign-in entirely rather
// than sending it into a verify that can only refuse it.
const gatelessNonce = (await import('../api/play/nonce.js')).default;
const nr = mockRes();
await gatelessNonce(mockReq({ method: 'GET' }), nr);
expect(nr.statusCode).toBe(200);
expect(nr.json.data.required).toBe(false);
expect(nr.json.data.mint).toBe('');
} finally {
if (savedMint === undefined) delete process.env.PLAY_GATE_MINT;
else process.env.PLAY_GATE_MINT = savedMint;
if (savedThree === undefined) delete process.env.THREE_MINT;
else process.env.THREE_MINT = savedThree;
vi.resetModules();
}
});
it('rejects missing required fields', async () => {
const res = mockRes();
await verifyMod.default(mockReq({ method: 'POST', body: {} }), res);
expect(res.statusCode).toBe(400);
});
});