|
| 1 | +/** |
| 2 | + * Tests for Permit2 (permit2-exact) x402 payment signing. |
| 3 | + * |
| 4 | + * The digest test vector was computed with an independent EIP-712 |
| 5 | + * implementation (Python eth_account) over the same typed data, so it |
| 6 | + * cross-validates the nested-struct hashing end to end. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { |
| 11 | + createEvmPaymentPayload, |
| 12 | + createPermit2ExactPayload, |
| 13 | + hashPermit2WitnessTransfer, |
| 14 | + PERMIT2_ADDRESS, |
| 15 | +} from '../x402-evm.js'; |
| 16 | +import { generateEvmWallet } from '../wallet.js'; |
| 17 | + |
| 18 | +const REQUIREMENTS_PERMIT2 = { |
| 19 | + scheme: 'exact', |
| 20 | + network: 'eip155:56', |
| 21 | + asset: '0x1000000000000000000000000000000000000001', |
| 22 | + amount: '10000000000000000', |
| 23 | + payTo: '0x3000000000000000000000000000000000000003', |
| 24 | + maxTimeoutSeconds: 300, |
| 25 | + extra: { |
| 26 | + name: 'Test Token', |
| 27 | + version: '1', |
| 28 | + assetTransferMethod: 'permit2-exact', |
| 29 | + signerAddress: '0x4000000000000000000000000000000000000004', |
| 30 | + spenderAddress: '0x2000000000000000000000000000000000000002', |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('hashPermit2WitnessTransfer', () => { |
| 35 | + it('matches an independently computed EIP-712 digest', () => { |
| 36 | + // Reference digest computed with Python eth_account over identical input. |
| 37 | + const digest = hashPermit2WitnessTransfer(56, { |
| 38 | + permitted: { |
| 39 | + token: '0x1000000000000000000000000000000000000001', |
| 40 | + amount: 10000000000000000n, |
| 41 | + }, |
| 42 | + spender: '0x2000000000000000000000000000000000000002', |
| 43 | + nonce: 123456789n, |
| 44 | + deadline: 1750000000n, |
| 45 | + witness: { |
| 46 | + to: '0x3000000000000000000000000000000000000003', |
| 47 | + validAfter: 1749999000n, |
| 48 | + }, |
| 49 | + }); |
| 50 | + expect('0x' + digest.toString('hex')).toBe( |
| 51 | + '0x84f21d844cc4f15e21f03e16da32687d28e4a1ce8bca3de73a5b8625f02c390b', |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('uses the canonical Permit2 contract address', () => { |
| 56 | + expect(PERMIT2_ADDRESS).toBe('0x000000000022D473030F116dDEE9F6B43aC78BA3'); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('createPermit2ExactPayload', () => { |
| 61 | + it('builds a permit2Authorization payload with decimal-string fields', () => { |
| 62 | + const wallet = generateEvmWallet(); |
| 63 | + const b64 = createPermit2ExactPayload( |
| 64 | + REQUIREMENTS_PERMIT2, wallet.privateKey, wallet.address, 'https://api.example.com/x', |
| 65 | + ); |
| 66 | + const payload = JSON.parse(Buffer.from(b64, 'base64').toString()); |
| 67 | + |
| 68 | + expect(payload.x402Version).toBe(2); |
| 69 | + expect(payload.accepted).toEqual(REQUIREMENTS_PERMIT2); |
| 70 | + expect(payload.resource).toEqual({ url: 'https://api.example.com/x' }); |
| 71 | + |
| 72 | + const auth = payload.payload.permit2Authorization; |
| 73 | + expect(auth.from).toBe(wallet.address); |
| 74 | + expect(auth.spender).toBe('0x2000000000000000000000000000000000000002'); |
| 75 | + expect(auth.permitted).toEqual({ |
| 76 | + token: REQUIREMENTS_PERMIT2.asset, |
| 77 | + amount: REQUIREMENTS_PERMIT2.amount, |
| 78 | + }); |
| 79 | + expect(auth.witness.to).toBe(REQUIREMENTS_PERMIT2.payTo); |
| 80 | + // Wire-format numerics are decimal strings. |
| 81 | + expect(auth.nonce).toMatch(/^\d+$/); |
| 82 | + expect(auth.deadline).toMatch(/^\d+$/); |
| 83 | + expect(auth.witness.validAfter).toMatch(/^\d+$/); |
| 84 | + // EIP-3009 fields must be absent. |
| 85 | + expect(payload.payload.authorization).toBeUndefined(); |
| 86 | + expect(payload.payload.signature).toMatch(/^0x[0-9a-f]{130}$/); |
| 87 | + }); |
| 88 | + |
| 89 | + it('throws when spenderAddress is missing', () => { |
| 90 | + const wallet = generateEvmWallet(); |
| 91 | + const req = { |
| 92 | + ...REQUIREMENTS_PERMIT2, |
| 93 | + extra: { ...REQUIREMENTS_PERMIT2.extra, spenderAddress: undefined }, |
| 94 | + }; |
| 95 | + expect(() => |
| 96 | + createPermit2ExactPayload(req, wallet.privateKey, wallet.address, 'u'), |
| 97 | + ).toThrow(/spenderAddress/); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('createEvmPaymentPayload method routing', () => { |
| 102 | + it('routes permit2-exact to the Permit2 payload', () => { |
| 103 | + const wallet = generateEvmWallet(); |
| 104 | + const b64 = createEvmPaymentPayload( |
| 105 | + REQUIREMENTS_PERMIT2, wallet.privateKey, wallet.address, 'u', |
| 106 | + ); |
| 107 | + const payload = JSON.parse(Buffer.from(b64, 'base64').toString()); |
| 108 | + expect(payload.payload.permit2Authorization).toBeDefined(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('keeps the EIP-3009 path when assetTransferMethod is eip3009 or absent', () => { |
| 112 | + const wallet = generateEvmWallet(); |
| 113 | + for (const extra of [ |
| 114 | + { name: 'Test Token', version: '1' }, |
| 115 | + { name: 'Test Token', version: '1', assetTransferMethod: 'eip3009' }, |
| 116 | + ]) { |
| 117 | + const b64 = createEvmPaymentPayload( |
| 118 | + { ...REQUIREMENTS_PERMIT2, extra }, wallet.privateKey, wallet.address, 'u', |
| 119 | + ); |
| 120 | + const payload = JSON.parse(Buffer.from(b64, 'base64').toString()); |
| 121 | + expect(payload.payload.authorization).toBeDefined(); |
| 122 | + expect(payload.payload.permit2Authorization).toBeUndefined(); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + it('throws on unsupported transfer methods so fallback can continue', () => { |
| 127 | + const wallet = generateEvmWallet(); |
| 128 | + const req = { |
| 129 | + ...REQUIREMENTS_PERMIT2, |
| 130 | + extra: { ...REQUIREMENTS_PERMIT2.extra, assetTransferMethod: 'permit2-upto' }, |
| 131 | + }; |
| 132 | + expect(() => |
| 133 | + createEvmPaymentPayload(req, wallet.privateKey, wallet.address, 'u'), |
| 134 | + ).toThrow(/assetTransferMethod/); |
| 135 | + }); |
| 136 | +}); |
0 commit comments