Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: wallet checks
on:
push:
branches: [main, feat/wallet-ask-20261018]
pull_request:
permissions:
contents: read
jobs:
check:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
- run: npm ci --ignore-scripts --no-audit --no-fund
- run: npm run check
- name: Offline ask integration (three runs)
run: for run in 1 2 3; do npm run check:ask || exit 1; done
- name: Negative controls and restored pass
run: npm run check:ask:regression
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ flowchart LR

## Unreleased — weak-model round 3 (2026-10-17)

- Add wallet ask: bounded router AI calls using the existing durable pay transport, with fast/smart quality, answer, USDC cost and receipt.
- Weak-model round 3 (2026-10-17; Emin; Hermes): wallet titled as the AGENT's own USDC wallet, not the person's bank; bridge is never a currency swap.

## Unreleased — model clarity (2026-10-17)
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# paymentrequired wallet service

The wallet service of paymentrequired.com, served at `wallet.paymentrequired.com`. It runs the
`wallet` MCP tool (`src/mcp-wallet-tool.mjs:116-123`): balance, fund, pay, send, history, bridge,
`wallet` MCP tool (`src/mcp-wallet-tool.mjs`): balance, fund, pay, ask, send, history, bridge,
discover. The agent Safe is the payer. It holds no Wirex client and no Wirex credentials. Card,
IBAN, account and bank-root payments belong to the banking service.

Expand Down Expand Up @@ -30,6 +30,32 @@ Boot refuses when:
- an agent custody value is missing (`src/server-boot-check.mjs:6-15`)
- `SERVICE_RPC_SECRET` is shorter than 32 characters (`src/service-rpc.mjs:22`)

## Ask AI from the agent wallet

One `wallet` tool call (`src/mcp-wallet-tool.mjs`, `ask`):

```json
{"action":"ask","wallet_key":"<your wallet_key>","prompt":"Explain x402 in one sentence.","max_amount":"0.05","quality":"fast","request_id":"explain-x402-1"}
```

Returns `answer`, `cost` (USDC), `model`, `receipt` and `request_id`, plus `ok`.
`quality` defaults to `fast`; `smart` selects the other model in `ASK_MODELS`.
The fixed router URL receives a non-streaming POST. It generates an answer, returns its
exact x402 invoice, then releases the answer after the existing wallet pay transport settles it.
No preflight generation and no new signing implementation.

Supply `request_id` before spending. Retry with the same ID and unchanged fields, including
the cap; the durable payment journal prevents a second payment, including after a restart.
An over-cap invoice signs and pays nothing: **send again with a NEW request_id and a higher
max_amount**. The rejected operation remains final; changing its cap conflicts with its binding.
If the paid response body was lost, same-ID replay reads the router's retained answer without
another payment signature (`src/agent-kernel-runtime.mjs`).

Offline checks (Node 22, no wallet credentials or network): `npm run check:ask` and
`npm run check:ask:regression`. They exercise the real x402 parsing/signing and durable journal
against a fake router, RPC, signer, Kernel account and wallet-credential adapter. CI runs these
on GitHub-hosted runners; no live payment or deployment is part of the checks.

## Surface

| Route | Source |
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"start": "node src/wallet-server.mjs",
"build:privy": "esbuild public/privy-bridge.jsx --bundle --minify --format=iife --platform=browser --target=es2022 --outfile=public/privy-bridge.js",
"precheck": "for f in src/*.mjs public/short-link.mjs; do node --check \"$f\" || exit 1; done",
"check": "npm run precheck && node scripts/check-import-closure.mjs"
"check": "npm run precheck && node scripts/check-import-closure.mjs",
"check:ask": "node --experimental-test-module-mocks scripts/check-wallet-ask.mjs",
"check:ask:regression": "node scripts/check-wallet-ask-regression.mjs"
},
"engines": {
"node": ">=22"
Expand Down
35 changes: 35 additions & 0 deletions scripts/check-wallet-ask-regression.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Prove that the offline check rejects the original retry advice and a missing cap.
// Task-owned mutations only; restore the exact bytes even on assertion failure.
import assert from 'node:assert/strict';
import { readFileSync, writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
const run = () => spawnSync(process.execPath, ['--experimental-test-module-mocks', 'scripts/check-wallet-ask.mjs'], { encoding: 'utf8', timeout: 60000 });
const cases = [
['over-cap retry advice', 'src/mcp-wallet-tool.mjs',
'Nothing was paid; send again with a NEW request_id and a higher max_amount.',
'Nothing was paid. Raise max_amount only if you want to spend more.'],
['ceiling before signing', 'src/agent-kernel-runtime.mjs',
"if (maxAmountUnits !== undefined) check(BigInt(offer.amount) <= BigInt(maxAmountUnits), 'payment_exceeds_max_amount');",
'// Synthetic negative control: cap removed.'],
['paid answer recovery', 'src/agent-kernel-runtime.mjs',
"url === ROUTER_CHAT_URL && op.state === 'paid' && !op.data.result?.resource",
"false && op.state === 'paid' && !op.data.result?.resource"],
];
for (const [name, path, old, replacement] of cases) {
const original = readFileSync(path, 'utf8');
assert.ok(original.includes(old), `Missing mutation anchor: ${name}`);
try {
writeFileSync(path, original.replaceAll(old, replacement));
const broken = run();
assert.equal(broken.error, undefined, `${name}: test process must actually execute`);
assert.notEqual(broken.status, 0, `${name}: check did not catch the sabotage`);
assert.match(broken.stderr, /AssertionError/, `${name}: must fail an assertion, not startup`);
console.log(`negative control caught: ${name} (exit ${broken.status})`);
console.log(broken.stderr.split('\n').filter(line => /AssertionError|assert\.|NEW request_id|recovered.ok|refused.ok/.test(line)).join('\n'));
} finally { writeFileSync(path, original); }
}
const restored = run();
process.stdout.write(restored.stdout);
process.stderr.write(restored.stderr);
assert.equal(restored.status, 0, 'Restored candidate must pass');
console.log('wallet ask regression controls ok: three mutations fail; restored candidate passes');
201 changes: 201 additions & 0 deletions scripts/check-wallet-ask.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// Offline integration: real wallet ask + durable kernel ledger + x402 parser/signer.
// Fake wallet adapter, RPC, Kernel account and Privy signer; no usable keys or network.
// Run: node --experimental-test-module-mocks scripts/check-wallet-ask.mjs
import assert from 'node:assert/strict';
import { mock } from 'node:test';
import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import * as viem from 'viem';
import * as kernel from '../src/agent-kernel-wallet.mjs';
import * as privy from '../src/privy-agent-app-signer.mjs';
import * as relay from '../src/safe-pay-relay.mjs';

const SAFE = '0x1111111111111111111111111111111111111111';
const SELLER = '0x2222222222222222222222222222222222222222';
const USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
const KEY = 'A'.repeat(44); // Synthetic, never usable as a real wallet credential.
assert.equal(KEY.length, 44);
const state = { signatures: 0, payments: 0, calls: [], invoices: new Map(), nonces: new Set(), receipts: new Map(), loseBody: false, badAnswer: false, recovery402: false };
globalThis.fetch = async () => { throw new Error('Unexpected network access in offline ask check'); };
mock.module('viem', { namedExports: { ...viem, createPublicClient: () => ({
getChainId: async () => 8453,
getBlockNumber: async () => 100n,
getCode: async () => '0x01',
readContract: async ({ functionName, args }) => {
if (functionName === 'balanceOf') return 1000000n;
if (functionName === 'permissionConfig') return { signer: SAFE };
if (functionName === 'authorizationState') return state.nonces.has(args[1]);
throw new Error(`Unexpected fake RPC: ${functionName}`);
},
waitForTransactionReceipt: async ({ hash }) => {
assert.ok(state.receipts.has(hash), 'Only known synthetic settlements may confirm');
return state.receipts.get(hash);
},
}) } });
mock.module('../src/agent-kernel-wallet.mjs', { namedExports: { ...kernel,
createAgentKernelWallet: async () => ({ agentAccount: { signTypedData: async () => {
state.signatures++;
return `0x${'11'.repeat(64)}1b`; // Deliberately synthetic, never sent to a network.
} } }),
} });
mock.module('../src/privy-agent-app-signer.mjs', { namedExports: { ...privy, connectedSafeAgentSigner: async () => ({ address: SAFE }) } });
mock.module('../src/safe-pay-relay.mjs', { namedExports: { ...relay, safeSignatureAccepted: async () => true } });
const { createKernelRuntime } = await import('../src/agent-kernel-runtime.mjs');
const { createMcpWalletTool, ASK_URL, ASK_MODELS } = await import('../src/mcp-wallet-tool.mjs');
const { decimalToUnits } = await import('../src/wallet-units.mjs');
const { instructions } = await import('../src/mcp-instructions.mjs');

async function fakeRouter(url, headers, onHeaders, shape) {
assert.equal(url, ASK_URL);
assert.equal(shape.method, 'POST');
const body = JSON.parse(shape.bytes.toString());
assert.equal(body.stream, false);
assert.deepEqual(Object.keys(body).sort(), ['messages', 'model', 'stream']);
assert.equal(body.messages[0].role, 'user');
const id = headers['Idempotency-Key'];
assert.match(id, /^[a-z0-9-]{36}$/i);
state.calls.push({ id, headers, body });
let invoice = state.invoices.get(id);
if (!invoice) {
invoice = { body: JSON.stringify(body), paid: false, answer: state.badAnswer ? {} : { choices: [{ message: { content: 'Synthetic router answer.' } }], model: body.model } };
state.invoices.set(id, invoice);
}
assert.equal(invoice.body, JSON.stringify(body), 'Same invoice must bind the same prompt and model');
const signature = headers['PAYMENT-SIGNATURE'] ?? headers['payment-signature'];
if (signature) {
assert.equal(invoice.paid, false, 'The fake router must never be paid twice');
const payment = JSON.parse(Buffer.from(signature, 'base64').toString());
assert.equal(payment.accepted.amount, '2070');
assert.equal(payment.resource.url, `${ASK_URL}?invoice=${id}`);
state.nonces.add(payment.payload.authorization.nonce);
invoice.paid = true;
state.payments++;
const tx = `0x${state.payments.toString(16).padStart(64, '0')}`;
invoice.receipt = { success: true, network: 'eip155:8453', transaction: tx, payer: SAFE };
state.receipts.set(tx, { transactionHash: tx, status: 'success', blockNumber: 101n, logs: [{
address: USDC,
topics: viem.encodeEventTopics({ abi: viem.parseAbi(['event Transfer(address indexed from,address indexed to,uint256 value)']), eventName: 'Transfer', args: { from: SAFE, to: SELLER } }),
data: viem.encodeAbiParameters([{ type: 'uint256' }], [2070n]),
}] });
}
if (!invoice.paid || (!signature && state.recovery402)) {
const required = { x402Version: 2, resource: { url: `${ASK_URL}?invoice=${id}`, mimeType: 'application/json' }, accepts: [{
scheme: 'exact', network: 'eip155:8453', asset: USDC, amount: '2070', payTo: SELLER, maxTimeoutSeconds: 300,
extra: { name: 'USD Coin', version: '2', assetTransferMethod: 'eip3009' },
}] };
return { status: 402, headers: { 'payment-required': Buffer.from(JSON.stringify(required)).toString('base64') }, bytes: Buffer.from('{}') };
}
const responseHeaders = { 'content-type': 'application/json', 'payment-response': Buffer.from(JSON.stringify(invoice.receipt)).toString('base64') };
onHeaders?.(200, responseHeaders);
if (signature && state.loseBody) { state.loseBody = false; throw new Error('Synthetic disconnect after settlement headers'); }
return { status: 200, headers: responseHeaders, bytes: Buffer.from(JSON.stringify(invoice.answer)) };
}
const dir = mkdtempSync(join(tmpdir(), 'wallet-ask-'));
const path = join(dir, 'ledger.sqlite');
const row = { id: 'synthetic-wallet', address: SAFE, claimed: true, record: { chainId: 8453, maxTransferUnits: kernel.NO_LIMIT, permit2Enabled: true, policyVersion: '0.0.5', initialOwnerAddress: SAFE, agentAddress: SAFE } };
let runtime;
function openTool() {
runtime = createKernelRuntime({ path, env: { AGENT_KERNEL_ALPHA: 'true' }, store: { getKernelWallet: id => id === row.id ? row : null }, merchantFetch: fakeRouter });
// Only credential resolution and the inner MCP field projection are synthetic.
// All challenge parsing, ceiling enforcement, signing and journal replay are real.
return createMcpWalletTool({ path, chainId: 8453, accountFlow: { authorized: async () => null }, agentWalletFactory: () => ({
newest: async () => null,
close: () => runtime.close(),
call: async args => {
assert.equal(args.wallet_key, KEY);
if (args.action === 'create') return { ok: true, wallet: { wallet_id: row.id, address: SAFE, claimed: true } };
assert.equal(args.action, 'pay');
assert.notEqual(args.dry_run, true, 'Ask must not generate an extra preflight invoice');
const op = await runtime.pay({ walletId: row.id, requestId: args.request_id, url: args.url, method: args.method, body: args.body, maxAmountUnits: decimalToUnits(args.max_amount, 6) });
return { ok: true, operation: { status: op.status, reason: op.reason, amount_units: op.amountUnits, to: op.to,
transaction_hash: op.transaction, resource: op.resource, content_delivered: op.contentDelivered, http_status: op.httpStatus } };
},
}) });
}
let tool = openTool();
const ask = { action: 'ask', wallet_key: KEY, prompt: 'Explain this in one sentence.', max_amount: '0.05', request_id: 'ask-once' };
try {
const invalid = await tool.call({ action: 'ask', quality: 'unknown' });
assert.equal(invalid.ok, false);
for (const field of ['wallet_key', 'prompt', 'max_amount', 'quality', 'request_id']) assert.ok(invalid.error.includes(field));
assert.equal(state.calls.length, 0);
for (const max_amount of ['0', '-1', '0.0000001', 0.05]) {
assert.equal((await tool.call({ ...ask, max_amount })).ok, false);
}
assert.equal((await tool.call({ ...ask, url: 'https://other.example/' })).ok, true, 'Unrelated schema fields cannot override the fixed router URL');
const result = await tool.call(ask);
assert.equal(result.ok, true, JSON.stringify(result));
assert.equal(result.answer, 'Synthetic router answer.');
assert.equal(result.cost, '0.00207');
assert.equal(result.model, ASK_MODELS.fast);
assert.equal(result.receipt.to, SELLER);
assert.match(result.receipt.tx, /^0x[0-9a-f]{64}$/);
assert.equal(state.payments, 1);
assert.equal(state.signatures, 1);
assert.equal(state.calls.length, 2, 'One 402 and one signed retry');
const replay = await Promise.all([tool.call(ask), tool.call(ask)]);
for (const value of replay) assert.deepEqual(value, result);
tool.close(); tool = openTool();
assert.deepEqual(await tool.call(ask), result, 'Replay survives a runtime restart');
assert.equal(state.calls.length, 2);
assert.equal(state.payments, 1);
const conflict = await tool.call({ ...ask, prompt: 'Different question' });
assert.equal(conflict.ok, false);
await assert.rejects(runtime.pay({ walletId: row.id, requestId: state.calls[0].id, url: ASK_URL, method: 'POST',
body: { ...state.calls[0].body, messages: [{ role: 'user', content: 'Different question' }] }, maxAmountUnits: '50000' }), /request_id_conflict/);
assert.equal(state.payments, 1, 'A conflicting prompt cannot pay again');

const capped = { ...ask, request_id: 'too-small', max_amount: '0.001' };
const refused = await tool.call(capped);
assert.equal(refused.ok, false);
assert.match(refused.error, /send again with a NEW request_id and a higher max_amount/);
assert.ok(instructions(['wallet']).includes('NEW request_id and a higher max_amount'));
assert.equal(state.payments, 1);
assert.equal(state.signatures, 1, 'Over-cap invoice must be rejected before signing');
const failed = runtime.history(row.id).find(op => op.reason === 'payment_exceeds_max_amount');
assert.equal(failed.status, 'failed', 'Over-cap operation is durable and final');
assert.deepEqual(await tool.call(capped), refused);
assert.equal((await tool.call({ ...capped, max_amount: '0.05' })).ok, false, 'Changing an immutable binding cannot silently spend');
const higher = await tool.call({ ...capped, request_id: 'higher-cap-new-id', max_amount: '0.05' });
assert.equal(higher.ok, true, JSON.stringify(higher));
assert.equal(state.payments, 2);

const smart = await tool.call({ ...ask, request_id: 'smart-answer', quality: 'smart' });
assert.equal(smart.ok, true, JSON.stringify(smart));
assert.equal(smart.model, ASK_MODELS.smart);
assert.equal(state.calls.at(-1).body.model, ASK_MODELS.smart);

state.loseBody = true;
const lostArgs = { ...ask, request_id: 'lost-body' };
const lost = await tool.call(lostArgs);
assert.equal(lost.ok, false);
assert.ok(lost.receipt, 'Original paid receipt survives response loss');
const paidCount = state.payments, signedCount = state.signatures;
state.recovery402 = true;
assert.equal((await tool.call(lostArgs)).ok, false, 'A recovery 402 must never trigger payment');
state.recovery402 = false;
tool.close(); tool = openTool();
const recovered = await tool.call(lostArgs);
assert.equal(recovered.ok, true, JSON.stringify(recovered));
assert.equal(recovered.answer, result.answer);
assert.equal(recovered.cost, result.cost);
assert.deepEqual(recovered.receipt, lost.receipt);
assert.equal(state.payments, paidCount);
assert.equal(state.signatures, signedCount);
assert.deepEqual(Object.keys(state.calls.at(-1).headers), ['Idempotency-Key'], 'Recovery can only read, never authorize payment');

state.badAnswer = true;
const emptyArgs = { ...ask, request_id: 'empty-answer' };
const empty = await tool.call(emptyArgs);
assert.equal(empty.ok, false);
assert.match(empty.error, /no answer/);
assert.ok(empty.receipt);
assert.deepEqual(await tool.call(emptyArgs), empty);
console.log(`wallet ask ok: 402 -> signed 200; ${state.payments} synthetic payments; cap rejects before signing; new-id higher-cap retry; durable replay; fast/smart; lost-body recovery without payment; validation and malformed answer`);
const runtimeSource = readFileSync(new URL('../src/agent-kernel-runtime.mjs', import.meta.url), 'utf8');
assert.ok(runtimeSource.includes('url === ROUTER_CHAT_URL ? 190000 : 20000'), 'Router receives a generation-sized bounded timeout');
} finally {
tool.close();
rmSync(dir, { recursive: true, force: true });
}
Loading
Loading