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.

test(x402): pin the receipt storage settlement-capture behaviour

nirholas8c7f1a69ec28
diff --git a/scripts/.receipts-e2e.tmp.mjs b/scripts/.receipts-e2e.tmp.mjs
deleted file mode 100644
index 6972180a9..000000000
--- a/scripts/.receipts-e2e.tmp.mjs
+++ /dev/null
@@ -1,136 +0,0 @@
-// Temporary E2E harness for /receipts (deleted after the run).
-import { chromium } from 'playwright';
-import nacl from 'tweetnacl';
-import bs58pkg from 'bs58';
-const bs58 = bs58pkg.default || bs58pkg;
-
-const OUT = '/tmp/claude-1000/-workspaces-three-ws/bdacc6ae-94a1-4fd1-9a02-d84da1c248a1/scratchpad';
-const kp = nacl.sign.keyPair();
-const address = bs58.encode(kp.publicKey);
-
-const browser = await chromium.launch();
-const page = await browser.newPage({ viewport: { width: 1280, height: 900 } });
-const errors = [];
-page.on('console', (m) => {
- if (m.type() === 'error') errors.push(m.text().slice(0, 200));
-});
-page.on('pageerror', (e) => errors.push('PAGEERROR ' + String(e).slice(0, 200)));
-page.on('response', (r) => {
- if (r.url().includes('my-receipts')) console.log(' API my-receipts →', r.status());
-});
-
-await page.exposeFunction('__sign', (bytes) =>
- Array.from(nacl.sign.detached(Uint8Array.from(bytes), kp.secretKey)),
-);
-await page.addInitScript((addr) => {
- window.solana = {
- isPhantom: true,
- publicKey: { toString: () => addr },
- connect: async () => ({ publicKey: { toString: () => addr } }),
- signMessage: async (encoded) => ({
- signature: Uint8Array.from(await window.__sign(Array.from(encoded))),
- }),
- };
-}, address);
-
-await page.goto('http://localhost:3000/receipts', { waitUntil: 'domcontentloaded' });
-await page.waitForSelector('#rc-signin', { state: 'visible' });
-console.log('title:', await page.title());
-console.log('signed-out card visible:', await page.locator('#rc-signin').isVisible());
-await page.screenshot({ path: `${OUT}/rc-1-signedout.png` });
-
-console.log('\n-- connect + sign + live fetch --');
-await page.click('#rc-connect-solana');
-await page.waitForSelector('#rc-vault:not([hidden])', { timeout: 30000 });
-await page.waitForFunction(() => !document.querySelector('.rc-skeleton'), { timeout: 30000 });
-console.log('wallet chip:', (await page.locator('#rc-wallet-addr').textContent()).trim(), '·', (await page.locator('#rc-wallet-net').textContent()).trim());
-console.log('KPIs:', await page.locator('#rc-k-total').textContent(), '/', await page.locator('#rc-k-endpoints').textContent(), '/', await page.locator('#rc-k-networks').textContent());
-console.log('empty state shown:', await page.locator('#rc-empty').isVisible(), '·', (await page.locator('#rc-empty-title').textContent()).trim());
-console.log('empty state has CTA link:', await page.locator('#rc-empty-body a').count());
-await page.screenshot({ path: `${OUT}/rc-2-empty.png` });
-
-console.log('\n-- populated render (injected rows through the real render path) --');
-await page.evaluate(() => {
- window.dispatchEvent(new Event('resize'));
-});
-// Drive the real renderer via the module's own list markup by seeding through
-// the API shape: reload with a stubbed fetch so renderVault() runs on real rows.
-await page.route('**/api/x402/my-receipts*', (route) =>
- route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify({
- network: 'solana',
- address,
- count: 3,
- receipts: [
- { id: 1, payer: address, network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', resourceUrl: 'https://three.ws/api/x402/d/token-snapshot', format: 'jws', amountAtomics: '10000', asset: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', transaction: '5xTr4nsAcT10nS1gNaTuReF0rTeSt1nGpUrP0SeS0nLy1234567890abcd', receipt: { format: 'jws', signature: 'abc' }, issuedAt: new Date(Date.now() - 3600e3).toISOString() },
- { id: 2, payer: address, network: 'eip155:8453', resourceUrl: 'https://three.ws/api/x402/d/whale-activity', format: 'jws', amountAtomics: '1000', asset: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', transaction: '0xdeadbeef1234567890abcdef', receipt: { format: 'jws', signature: 'def' }, issuedAt: new Date(Date.now() - 7200e3).toISOString() },
- { id: 3, payer: address, network: 'solana', resourceUrl: 'https://three.ws/api/x402/d/holder-concentration', format: 'jws', amountAtomics: null, asset: null, transaction: null, receipt: { format: 'jws', signature: 'ghi' }, issuedAt: new Date(Date.now() - 86400e3).toISOString() },
- ],
- }),
- }),
-);
-await page.click('#rc-refresh');
-await page.waitForFunction(() => document.querySelectorAll('.rc-row:not(.rc-skeleton)').length === 3, { timeout: 20000 });
-console.log('rows rendered:', await page.locator('.rc-row').count());
-console.log('KPIs:', await page.locator('#rc-k-total').textContent(), 'receipts /', await page.locator('#rc-k-endpoints').textContent(), 'endpoints /', await page.locator('#rc-k-networks').textContent());
-const links = await page.locator('a.rc-tx').evaluateAll((els) => els.map((e) => e.href));
-console.log('explorer links:', links);
-console.log('SPEND tile:', (await page.locator('#rc-k-spend').textContent()).trim(), '|', (await page.locator('#rc-k-spend-note').textContent()).trim());
-console.log('row amounts:', await page.locator('.rc-amount').allTextContents());
-console.log('private tx cell:', await page.locator('.rc-tx-none').count());
-await page.screenshot({ path: `${OUT}/rc-3-populated.png` });
-
-console.log('\n-- search filter --');
-await page.fill('#rc-search', 'whale');
-await page.waitForTimeout(300);
-console.log('rows after search "whale":', await page.locator('.rc-row').count());
-await page.fill('#rc-search', 'zzzz');
-await page.waitForTimeout(300);
-console.log('no-match empty state:', (await page.locator('#rc-empty-title').textContent()).trim());
-await page.screenshot({ path: `${OUT}/rc-4-nomatch.png` });
-await page.fill('#rc-search', '');
-await page.waitForTimeout(300);
-
-console.log('\n-- keyboard + actions --');
-await page.locator('body').click();
-await page.keyboard.press('/');
-console.log('"/" focuses search:', await page.evaluate(() => document.activeElement?.id));
-await page.keyboard.press('Escape');
-await page.locator('body').click();
-
-const dl = page.waitForEvent('download', { timeout: 10000 });
-await page.keyboard.press('e');
-const csv = await dl;
-console.log('CSV download:', csv.suggestedFilename());
-
-const dl2 = page.waitForEvent('download', { timeout: 10000 });
-await page.locator('[data-action="download"]').first().click();
-console.log('JSON download:', (await dl2).suggestedFilename());
-
-console.log('\n-- error state --');
-await page.unroute('**/api/x402/my-receipts*');
-await page.route('**/api/x402/my-receipts*', (route) =>
- route.fulfill({ status: 502, contentType: 'application/json', body: JSON.stringify({ error: 'receipt_query_failed', error_description: 'Receipt store unreachable.' }) }),
-);
-await page.click('#rc-refresh');
-await page.waitForSelector('#rc-load-error:not([hidden])', { timeout: 15000 });
-console.log('error state:', (await page.locator('#rc-load-error-msg').textContent()).trim());
-console.log('retry button present:', await page.locator('#rc-retry').isVisible());
-await page.screenshot({ path: `${OUT}/rc-5-error.png` });
-
-console.log('\n-- disconnect --');
-await page.click('#rc-disconnect');
-console.log('back to sign-in:', await page.locator('#rc-signin').isVisible());
-console.log('session cleared:', await page.evaluate(() => sessionStorage.getItem('twx_receipts_session')));
-
-console.log('\n-- responsive 375px --');
-await page.setViewportSize({ width: 375, height: 800 });
-await page.waitForTimeout(300);
-const hScroll = await page.evaluate(() => document.documentElement.scrollWidth > document.documentElement.clientWidth + 1);
-console.log('horizontal scroll at 375px:', hScroll);
-await page.screenshot({ path: `${OUT}/rc-6-mobile.png` });
-
-console.log('\nconsole errors:', errors.length ? errors : 'NONE');
-await browser.close();
diff --git a/tests/x402-receipt-storage.test.js b/tests/x402-receipt-storage.test.js
new file mode 100644
index 000000000..936ed51e0
--- /dev/null
+++ b/tests/x402-receipt-storage.test.js
@@ -0,0 +1,178 @@
+// Storage layer for the x402 offer-receipt log.
+//
+// Two behaviours here are load-bearing for the Receipt Vault (/receipts) and
+// both were previously wrong:
+//
+// 1. The signed artifact omits the settlement tx whenever the endpoint
+// declares includeTxHash=false (spec §5.2 privacy default). The storage
+// layer read the tx from the *payload* only, so our own audit trail lost
+// it for every privacy-preserving endpoint — which is all of them by
+// default. The settle response still carries it, and the buyer is
+// entitled to their own.
+// 2. The amount was never recorded at all, so a buyer could see WHAT they
+// bought but never what it cost.
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+const sqlState = { calls: [], queue: [] };
+
+vi.mock('../api/_lib/db.js', () => ({
+ sql: vi.fn(async (strings, ...values) => {
+ sqlState.calls.push({ query: strings.join('?'), values });
+ return sqlState.queue.length ? sqlState.queue.shift() : [];
+ }),
+ isDbUnavailableError: () => false,
+ isDbCapacityError: () => false,
+}));
+
+// The real extractor validates a full JWS/EIP-712 artifact; these tests are
+// about what the storage layer does with the extracted payload, so return the
+// artifact's own payload verbatim.
+vi.mock('@x402/extensions', () => ({
+ extractReceiptPayload: (signed) => signed.payload,
+}));
+
+const { recordReceipt, listReceiptsForPayer } = await import(
+ '../api/_lib/x402/receipt-storage.js'
+);
+
+const PAYER = 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump';
+const SETTLE_TX = '5xTr4nsAcT10nS1gNaTuReF0rTeSt1nGpUrP0SeS0nLy';
+
+function signedReceipt(payload = {}) {
+ return { format: 'jws', signature: 'sig', payload: { version: 1, ...payload } };
+}
+
+/** Column list → value, for the single insert the call under test performed. */
+function insertedValues() {
+ const call = sqlState.calls.at(-1);
+ const cols = call.query
+ .slice(call.query.indexOf('(') + 1, call.query.indexOf(')'))
+ .split(',')
+ .map((c) => c.trim());
+ return Object.fromEntries(cols.map((c, i) => [c, call.values[i]]));
+}
+
+beforeEach(() => {
+ sqlState.calls.length = 0;
+ sqlState.queue.length = 0;
+});
+
+describe('recordReceipt', () => {
+ it('falls back to the settle transaction when the payload omits it for privacy', async () => {
+ recordReceipt({
+ resourceUrl: 'https://three.ws/api/x402/token-intel',
+ // includeTxHash=false: the signed payload carries no transaction.
+ signedReceipt: signedReceipt({ network: 'solana', payer: PAYER }),
+ settled: { payer: PAYER, network: 'solana', transaction: SETTLE_TX },
+ });
+ expect(insertedValues().transaction).toBe(SETTLE_TX);
+ });
+
+ it('prefers the payload transaction when the receipt does carry one', async () => {
+ recordReceipt({
+ resourceUrl: 'https://three.ws/api/x402/token-intel',
+ signedReceipt: signedReceipt({ network: 'solana', transaction: 'from-payload' }),
+ settled: { payer: PAYER, network: 'solana', transaction: SETTLE_TX },
+ });
+ expect(insertedValues().transaction).toBe('from-payload');
+ });
+
+ it('records the settled amount and asset so the buyer can see what it cost', async () => {
+ recordReceipt({
+ resourceUrl: 'https://three.ws/api/x402/token-intel',
+ signedReceipt: signedReceipt({ network: 'solana' }),
+ settled: { payer: PAYER, network: 'solana', transaction: SETTLE_TX },
+ payment: { amountAtomics: 10000, asset: 'the-settlement-asset' },
+ });
+ const v = insertedValues();
+ // Stored as text: the atomic amount must survive without float rounding.
+ expect(v.amount_atomics).toBe('10000');
+ expect(v.asset).toBe('the-settlement-asset');
+ });
+
+ it('writes nulls rather than guessing when no payment context is supplied', async () => {
+ recordReceipt({
+ resourceUrl: 'https://three.ws/api/x402/token-intel',
+ signedReceipt: signedReceipt({ network: 'solana' }),
+ settled: { payer: PAYER, network: 'solana' },
+ });
+ const v = insertedValues();
+ expect(v.amount_atomics).toBe(null);
+ expect(v.asset).toBe(null);
+ expect(v.transaction).toBe(null);
+ });
+
+ it('lower-cases EVM payers and leaves Solana base58 verbatim', async () => {
+ recordReceipt({
+ resourceUrl: 'https://three.ws/x',
+ signedReceipt: signedReceipt({ network: 'eip155:8453' }),
+ settled: { payer: '0xAbCdEf0123456789aBcDeF0123456789ABCDEF01', network: 'base' },
+ });
+ expect(insertedValues().payer).toBe('0xabcdef0123456789abcdef0123456789abcdef01');
+
+ recordReceipt({
+ resourceUrl: 'https://three.ws/x',
+ signedReceipt: signedReceipt({ network: 'solana' }),
+ settled: { payer: PAYER, network: 'solana' },
+ });
+ expect(insertedValues().payer).toBe(PAYER);
+ });
+
+ it('drops the write when there is no payer to key it by', async () => {
+ recordReceipt({
+ resourceUrl: 'https://three.ws/x',
+ signedReceipt: signedReceipt({ network: 'solana' }),
+ settled: { network: 'solana' },
+ });
+ expect(sqlState.calls).toHaveLength(0);
+ });
+});
+
+describe('listReceiptsForPayer', () => {
+ it('maps the settlement columns into the buyer-facing shape', async () => {
+ sqlState.queue.push([
+ {
+ id: 'r1',
+ payer: PAYER,
+ network: 'solana',
+ resource_url: 'https://three.ws/api/x402/token-intel',
+ format: 'jws',
+ receipt: { format: 'jws' },
+ transaction: SETTLE_TX,
+ amount_atomics: '10000',
+ asset: 'the-settlement-asset',
+ issued_at: new Date('2026-07-28T00:00:00.000Z'),
+ },
+ ]);
+ const [row] = await listReceiptsForPayer({ payer: PAYER });
+ expect(row.amountAtomics).toBe('10000');
+ expect(row.asset).toBe('the-settlement-asset');
+ expect(row.transaction).toBe(SETTLE_TX);
+ expect(row.issuedAt).toBe('2026-07-28T00:00:00.000Z');
+ });
+
+ it('reports rows written before settlement capture as null, not zero', async () => {
+ sqlState.queue.push([
+ {
+ id: 'r0',
+ payer: PAYER,
+ network: 'solana',
+ resource_url: 'https://three.ws/x',
+ format: 'jws',
+ receipt: {},
+ transaction: null,
+ amount_atomics: null,
+ asset: null,
+ issued_at: '2026-07-01T00:00:00.000Z',
+ },
+ ]);
+ const [row] = await listReceiptsForPayer({ payer: PAYER });
+ expect(row.amountAtomics).toBe(null);
+ expect(row.asset).toBe(null);
+ });
+
+ it('returns nothing for a blank payer instead of querying', async () => {
+ expect(await listReceiptsForPayer({ payer: '' })).toEqual([]);
+ expect(sqlState.calls).toHaveLength(0);
+ });
+});