-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx402Server.test.ts
More file actions
384 lines (311 loc) · 15 KB
/
Copy pathx402Server.test.ts
File metadata and controls
384 lines (311 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* x402Server.test.ts
* ─────────────────────────────────────────────────────────────────────────────
* Tests for the x402 server middleware.
*
* WHAT THIS TESTS:
* - Returns 402 with correct x402 v2 body when no payment header
* - PAYMENT-REQUIRED header is base64-encoded
* - Decodes PAYMENT-SIGNATURE header (v2 canonical)
* - Decodes X-PAYMENT header (v1 fallback)
* - Returns 402 for invalid/malformed payment headers
* - Calls the handler after successful verification
* - Returns PAYMENT-RESPONSE header with tx hash
* - Off-chain nonce dedup (settleOnChain=false)
* - Rejects replayed payments (same nonce)
*/
import { describe, test, expect, vi, beforeEach } from 'vitest'
import { recoverTypedDataAddress } from 'viem'
import { withX402, _resetNonceStore, type X402ServerConfig } from '../frontend/src/lib/x402Server.example'
import * as eip3009Lib from '../frontend/src/lib/eip3009.example'
// ─── Mocks ──────────────────────────────────────────────────────────────────
// Mock viem — we can't connect to Arc in unit tests
vi.mock('viem', () => ({
createPublicClient: vi.fn(() => ({
waitForTransactionReceipt: vi.fn(() => Promise.resolve({ status: 'success' })),
})),
createWalletClient: vi.fn(() => ({
writeContract: vi.fn(() => Promise.resolve('0xtxhash123')),
})),
http: vi.fn(),
recoverTypedDataAddress: vi.fn(() => Promise.resolve('0xuser')),
}))
vi.mock('viem/accounts', () => ({
privateKeyToAccount: vi.fn(() => ({
address: '0xsettler',
signTypedData: vi.fn(),
})),
}))
vi.mock('../frontend/src/lib/eip3009.example', () => ({
decodePaymentHeader: vi.fn(),
decodeFullPaymentPayload: vi.fn(() => ({
x402Version: 2,
accepted: {
scheme: 'exact',
network: 'eip155:5042002',
amount: '1000',
asset: '0xusdc',
payTo: '0xtreasury',
maxTimeoutSeconds: 300,
},
payload: {
signature: '0xsignature',
authorization: {
from: '0xuser',
to: '0xtreasury',
value: '1000',
validAfter: '0',
validBefore: String(Math.floor(Date.now() / 1000) + 300),
nonce: '0xnonce123',
},
},
})),
buildTransferAuthorizationMessage: vi.fn(() => ({
domain: { name: 'USD Coin', version: '2', chainId: 5042002, verifyingContract: '0xusdc' },
types: { TransferWithAuthorization: [] },
primaryType: 'TransferWithAuthorization',
message: {},
})),
ARC_TESTNET_CAIP2: 'eip155:5042002',
}))
// ─── Test Config ────────────────────────────────────────────────────────────
const TEST_CONFIG: X402ServerConfig = {
resource: '/api/test',
priceUsdc: 0.001,
description: 'Test endpoint',
treasuryAddress: '0xtreasury',
rpcUrl: 'https://rpc.testnet.arc.network',
usdcAddress: '0xusdc',
settlerPrivateKey: '0xsettlerkey',
settleOnChain: false, // off-chain for tests
}
const VALID_SIGNED_AUTH = {
from: '0xuser',
to: '0xtreasury',
value: BigInt(1000), // 0.001 USDC in atomic units
validAfter: BigInt(0),
validBefore: BigInt(Math.floor(Date.now() / 1000) + 300),
nonce: '0xnonce123',
v: 27,
r: '0xr' as `0x${string}`,
s: '0xs' as `0x${string}`,
signature: '0xsignature' as `0x${string}`,
}
// ─── Helper ─────────────────────────────────────────────────────────────────
function makeRequest(headers: Record<string, string> = {}): Request {
return new Request('http://localhost/api/test', { headers })
}
// ─── Test Suite ─────────────────────────────────────────────────────────────
describe('withX402', () => {
const handler = vi.fn(async () => Response.json({ data: 'premium content' }))
beforeEach(() => {
// mockClear() preserves vi.mock() factory implementations while resetting call state
vi.mocked(handler).mockClear()
vi.mocked(recoverTypedDataAddress).mockClear()
vi.mocked(eip3009Lib.decodePaymentHeader).mockClear()
// Clear the in-memory nonce dedup store between tests
_resetNonceStore()
})
// ── 402 Response Format ────────────────────────────────────────────────
test('returns 402 when no payment header is present', async () => {
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest())
expect(response.status).toBe(402)
expect(handler).not.toHaveBeenCalled()
})
test('402 body contains x402Version 2 and accepts array', async () => {
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest())
const body = await response.json()
expect(body.x402Version).toBe(2)
expect(body.accepts).toBeDefined()
expect(body.accepts.length).toBe(1)
expect(body.accepts[0].scheme).toBe('exact')
expect(body.accepts[0].network).toBe('eip155:5042002')
// x402 v2 spec: extra.assetTransferMethod defaults to "eip3009"
expect(body.accepts[0].extra.assetTransferMethod).toBe('eip3009')
// x402 v2 spec: extensions field should be present
expect(body.extensions).toEqual({})
})
test('402 amount matches priceUsdc in atomic units', async () => {
const config = { ...TEST_CONFIG, priceUsdc: 0.05 } // 5 cents
const wrapped = withX402(config, handler)
const response = await wrapped(makeRequest())
const body = await response.json()
// 0.05 * 1e6 = 50000
expect(body.accepts[0].amount).toBe('50000')
})
test('PAYMENT-REQUIRED header is base64-encoded', async () => {
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest())
const header = response.headers.get('PAYMENT-REQUIRED')
expect(header).toBeDefined()
// Decode and verify it matches the body
const decoded = JSON.parse(Buffer.from(header!, 'base64').toString())
expect(decoded.x402Version).toBe(2)
expect(decoded.accepts[0].scheme).toBe('exact')
})
test('resource in 402 body matches config', async () => {
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest())
const body = await response.json()
expect(body.resource.url).toBe('/api/test')
expect(body.resource.description).toBe('Test endpoint')
})
// ── Header Decoding ────────────────────────────────────────────────────
test('decodes PAYMENT-SIGNATURE header (v2 canonical)', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'PAYMENT-SIGNATURE': 'base64encodedpayment',
}))
expect(eip3009Lib.decodePaymentHeader).toHaveBeenCalledWith('base64encodedpayment')
expect(response.status).toBe(200)
})
test('decodes X-PAYMENT header (v1 fallback)', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'X-PAYMENT': 'base64encodedpayment',
}))
expect(eip3009Lib.decodePaymentHeader).toHaveBeenCalledWith('base64encodedpayment')
expect(response.status).toBe(200)
})
test('returns 400 for malformed payment header', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockImplementationOnce(() => {
throw new Error('invalid base64')
})
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'PAYMENT-SIGNATURE': 'not-valid-base64',
}))
// x402 v2 spec: malformed payment → HTTP 400 (Invalid Payment)
expect(response.status).toBe(400)
const body = await response.json()
expect(body.code).toBe('INVALID_PAYLOAD')
})
// ── Payment Verification ───────────────────────────────────────────────
test('rejects expired payment authorization', async () => {
const expiredAuth = {
...VALID_SIGNED_AUTH,
validBefore: BigInt(Math.floor(Date.now() / 1000) - 100), // expired 100s ago
}
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(expiredAuth as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'PAYMENT-SIGNATURE': 'base64',
}))
// x402 v2 spec: invalid payment authorization → HTTP 400
expect(response.status).toBe(400)
const body = await response.json()
expect(body.code).toBe('PAYMENT_VERIFICATION_FAILED')
expect(body.detail).toContain('expired')
})
test('rejects insufficient payment amount', async () => {
const insufficientAuth = {
...VALID_SIGNED_AUTH,
value: BigInt(100), // 0.0001 USDC — less than required 0.001
}
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(insufficientAuth as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'PAYMENT-SIGNATURE': 'base64',
}))
// x402 v2 spec: invalid payment authorization → HTTP 400
expect(response.status).toBe(400)
const body = await response.json()
expect(body.detail).toContain('Insufficient')
})
test('rejects wrong recipient', async () => {
const wrongRecipientAuth = {
...VALID_SIGNED_AUTH,
to: '0xwrongaddress',
}
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(wrongRecipientAuth as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'PAYMENT-SIGNATURE': 'base64',
}))
// x402 v2 spec: invalid payment authorization → HTTP 400
expect(response.status).toBe(400)
const body = await response.json()
expect(body.detail).toContain('Wrong recipient')
})
test('rejects signature when recovered address does not match from', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
vi.mocked(recoverTypedDataAddress).mockResolvedValueOnce('0xWRONGADDRESS')
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({
'PAYMENT-SIGNATURE': 'base64',
}))
// x402 v2 spec: invalid payment authorization → HTTP 400
expect(response.status).toBe(400)
const body = await response.json()
expect(body.detail).toContain('Signer mismatch')
})
// ── Handler Execution ──────────────────────────────────────────────────
test('calls handler after successful verification', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
const wrapped = withX402(TEST_CONFIG, handler)
await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
expect(handler).toHaveBeenCalledOnce()
})
test('returns PAYMENT-RESPONSE header with settlement proof', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
// settleOnChain=false → txHash = 'verified-offchain'
// x402 v2 spec SettlementResponse: { success, transaction, network, payer }
const paymentResponseB64 = response.headers.get('PAYMENT-RESPONSE')
expect(paymentResponseB64).toBeTruthy()
const paymentResponse = JSON.parse(Buffer.from(paymentResponseB64!, 'base64').toString('utf-8'))
expect(paymentResponse.success).toBe(true)
expect(paymentResponse.transaction).toBe('verified-offchain')
expect(paymentResponse.network).toBe('eip155:5042002')
expect(paymentResponse.payer).toBe('0xuser')
// Legacy header for backwards compatibility
expect(response.headers.get('X-PAYMENT-TX-HASH')).toBe('verified-offchain')
})
test('returns handler response body', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
const wrapped = withX402(TEST_CONFIG, handler)
const response = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
const body = await response.json()
expect(body.data).toBe('premium content')
})
// ── Off-Chain Nonce Dedup ──────────────────────────────────────────────
test('rejects replayed payment (same nonce)', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader)
.mockReturnValueOnce(VALID_SIGNED_AUTH as any)
.mockReturnValueOnce({ ...VALID_SIGNED_AUTH } as any) // same nonce
const wrapped = withX402(TEST_CONFIG, handler)
const res1 = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
expect(res1.status).toBe(200) // first use succeeds
const res2 = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
expect(res2.status).toBe(402) // replay rejected
const body = await res2.json()
expect(body.code).toBe('PAYMENT_REPLAY_DETECTED')
})
test('accepts different nonces on consecutive requests', async () => {
const auth1 = { ...VALID_SIGNED_AUTH, nonce: '0xnonce1' as `0x${string}` }
const auth2 = { ...VALID_SIGNED_AUTH, nonce: '0xnonce2' as `0x${string}` }
vi.mocked(eip3009Lib.decodePaymentHeader)
.mockReturnValueOnce(auth1 as any)
.mockReturnValueOnce(auth2 as any)
const wrapped = withX402(TEST_CONFIG, handler)
const res1 = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
expect(res1.status).toBe(200)
const res2 = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
expect(res2.status).toBe(200)
})
// ── Config Variations ──────────────────────────────────────────────────
test('settleOnChain=true would call on-chain settlement (mocked)', async () => {
vi.mocked(eip3009Lib.decodePaymentHeader).mockReturnValueOnce(VALID_SIGNED_AUTH as any)
const configOnChain = { ...TEST_CONFIG, settleOnChain: true }
const wrapped = withX402(configOnChain, handler)
const response = await wrapped(makeRequest({ 'PAYMENT-SIGNATURE': 'base64' }))
// Handler should still be called (settlement is mocked)
expect(handler).toHaveBeenCalledOnce()
expect(response.status).toBe(200)
})
})