|
1 | | -import { describe, it, expect } from "vitest"; |
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
2 | 2 | import { verifyAuthSignedData } from "../../src/utils/verifyAuthSignedData.js"; |
3 | | -import { |
4 | | - createTestClient, |
5 | | - http, |
6 | | - publicActions, |
7 | | - VerifyTypedDataParameters, |
8 | | - walletActions, |
9 | | -} from "viem"; |
10 | | -import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
11 | | -import { optimism, sepolia } from "viem/chains"; |
12 | 3 |
|
13 | | -describe("verifyAuthSignedData", async () => { |
14 | | - // Create first test wallet |
15 | | - const privateKey1 = generatePrivateKey(); |
16 | | - const testClient1 = createTestClient({ |
17 | | - account: privateKeyToAccount(privateKey1), |
18 | | - chain: sepolia, |
19 | | - mode: "anvil", |
20 | | - transport: http(), |
21 | | - }) |
22 | | - .extend(publicActions) |
23 | | - .extend(walletActions); |
24 | | - const address1 = testClient1.account.address; |
25 | | - |
26 | | - // Create second test wallet |
27 | | - const privateKey2 = generatePrivateKey(); |
28 | | - const testClient2 = createTestClient({ |
29 | | - account: privateKeyToAccount(privateKey2), |
30 | | - chain: optimism, |
31 | | - mode: "anvil", |
32 | | - transport: http(), |
33 | | - }) |
34 | | - .extend(publicActions) |
35 | | - .extend(walletActions); |
36 | | - const address2 = testClient2.account.address; |
37 | | - |
38 | | - const types = { |
39 | | - test: [{ name: "message", type: "string" }], |
40 | | - } as VerifyTypedDataParameters["types"]; |
41 | | - const domain = { |
42 | | - name: "Hypercerts", |
43 | | - version: "1", |
44 | | - chainId: sepolia.id, |
45 | | - } as const; |
46 | | - const message = { |
47 | | - message: "test", |
48 | | - } as VerifyTypedDataParameters["message"]; |
49 | | - |
50 | | - const signTypedData = async ( |
51 | | - client: typeof testClient1, |
52 | | - overrides?: { |
53 | | - domainOverride?: Partial<{ |
54 | | - name: string; |
55 | | - version: string; |
56 | | - chainId: number; |
57 | | - }>; |
58 | | - messageOverride?: Parameters< |
59 | | - typeof testClient1.signTypedData |
60 | | - >[0]["message"]; |
61 | | - }, |
62 | | - ) => { |
63 | | - return await client.signTypedData({ |
64 | | - domain: overrides?.domainOverride ?? domain, |
65 | | - message: overrides?.messageOverride ?? message, |
66 | | - types, |
67 | | - primaryType: "test", |
68 | | - }); |
69 | | - }; |
70 | | - it("Verifies signature correctly", async () => { |
71 | | - const signature = await signTypedData(testClient1); |
72 | | - |
73 | | - const result = await verifyAuthSignedData({ |
74 | | - address: address1, |
75 | | - message: { |
76 | | - message: "test", |
77 | | - }, |
78 | | - types, |
79 | | - signature, |
80 | | - primaryType: "test", |
81 | | - requiredChainId: sepolia.id, |
82 | | - }); |
83 | | - expect(result).toEqual(true); |
84 | | - }); |
85 | | - |
86 | | - it("Fails to verify signature with wrong address", async () => { |
87 | | - const signature = await signTypedData(testClient1); |
88 | | - |
89 | | - const result = await verifyAuthSignedData({ |
90 | | - address: address2, |
91 | | - message: { |
92 | | - message: "test", |
93 | | - }, |
94 | | - types, |
95 | | - signature, |
96 | | - primaryType: "test", |
97 | | - requiredChainId: sepolia.id, |
98 | | - }); |
99 | | - expect(result).toEqual(false); |
100 | | - }); |
101 | | - |
102 | | - it("Fails to verify signature with wrong message", async () => { |
103 | | - const signature = await signTypedData(testClient1); |
104 | | - |
105 | | - const result = await verifyAuthSignedData({ |
106 | | - address: address1, |
107 | | - message: { |
108 | | - message: "wrong", |
109 | | - }, |
110 | | - types, |
111 | | - signature, |
112 | | - primaryType: "test", |
113 | | - requiredChainId: sepolia.id, |
114 | | - }); |
115 | | - expect(result).toEqual(false); |
116 | | - }); |
117 | | - |
118 | | - it("Fails to verify signature with wrong chainId", async () => { |
119 | | - const signature = await signTypedData(testClient1); |
120 | | - |
121 | | - const result = await verifyAuthSignedData({ |
122 | | - address: address1, |
123 | | - message: { |
124 | | - message: "test", |
125 | | - }, |
126 | | - types, |
127 | | - signature, |
128 | | - primaryType: "test", |
129 | | - requiredChainId: optimism.id, |
130 | | - }); |
131 | | - expect(result).toEqual(false); |
| 4 | +const mockVerifyTypedData = vi.hoisted(() => vi.fn()); |
| 5 | + |
| 6 | +vi.mock("../../src/utils/getRpcUrl.js", () => ({ |
| 7 | + getEvmClient: vi.fn().mockReturnValue({ |
| 8 | + verifyTypedData: mockVerifyTypedData, |
| 9 | + account: undefined, |
| 10 | + batch: undefined, |
| 11 | + cacheTime: 0, |
| 12 | + chain: undefined, |
| 13 | + key: "mock", |
| 14 | + name: "Mock Client", |
| 15 | + pollingInterval: 0, |
| 16 | + request: vi.fn(), |
| 17 | + transport: { type: "mock" }, |
| 18 | + type: "publicClient", |
| 19 | + uid: "mock-client", |
| 20 | + }), |
| 21 | +})); |
| 22 | + |
| 23 | +describe("verifyAuthSignedData", () => { |
| 24 | + beforeEach(() => { |
| 25 | + mockVerifyTypedData.mockReset(); |
132 | 26 | }); |
133 | 27 |
|
134 | | - it("Fails to verify with wrong domain - missing chain id", async () => { |
135 | | - const signature = await signTypedData(testClient1, { |
136 | | - domainOverride: { name: "Hypercerts", version: "1" }, |
137 | | - }); |
| 28 | + it("verifies hypercerts domain added to message", async () => { |
| 29 | + mockVerifyTypedData.mockResolvedValue(true); |
138 | 30 |
|
139 | 31 | const result = await verifyAuthSignedData({ |
140 | | - address: address1, |
141 | | - message: { |
142 | | - message: "test", |
143 | | - }, |
144 | | - types, |
145 | | - signature, |
146 | | - primaryType: "test", |
147 | | - requiredChainId: sepolia.id, |
148 | | - }); |
149 | | - expect(result).toEqual(false); |
150 | | - }); |
151 | | - |
152 | | - it("Fails to verify with wrong domain - wrong chain id", async () => { |
153 | | - const signature = await signTypedData(testClient1, { |
154 | | - domainOverride: { |
| 32 | + address: "0x123", |
| 33 | + message: { test: "message" }, |
| 34 | + types: { Test: [{ name: "test", type: "string" }] }, |
| 35 | + signature: "0xsignature", |
| 36 | + primaryType: "Test", |
| 37 | + requiredChainId: 1, |
| 38 | + }); |
| 39 | + |
| 40 | + expect(result).toBe(true); |
| 41 | + expect(mockVerifyTypedData).toHaveBeenCalledWith({ |
| 42 | + address: "0x123", |
| 43 | + message: { test: "message" }, |
| 44 | + types: { Test: [{ name: "test", type: "string" }] }, |
| 45 | + signature: "0xsignature", |
| 46 | + primaryType: "Test", |
| 47 | + domain: { |
155 | 48 | name: "Hypercerts", |
156 | 49 | version: "1", |
157 | | - chainId: optimism.id, |
158 | | - }, |
159 | | - }); |
160 | | - |
161 | | - const result = await verifyAuthSignedData({ |
162 | | - address: address1, |
163 | | - message: { |
164 | | - message: "test", |
165 | | - }, |
166 | | - types, |
167 | | - signature, |
168 | | - primaryType: "test", |
169 | | - requiredChainId: sepolia.id, |
170 | | - }); |
171 | | - expect(result).toEqual(false); |
172 | | - }); |
173 | | - |
174 | | - it("Fails to verify with wrong domain - wrong name", async () => { |
175 | | - const signature = await signTypedData(testClient1, { |
176 | | - domainOverride: { |
177 | | - name: "Wrong", |
178 | | - version: "1", |
179 | | - chainId: sepolia.id, |
180 | | - }, |
181 | | - }); |
182 | | - |
183 | | - const result = await verifyAuthSignedData({ |
184 | | - address: address1, |
185 | | - message: { |
186 | | - message: "test", |
187 | | - }, |
188 | | - types, |
189 | | - signature, |
190 | | - primaryType: "test", |
191 | | - requiredChainId: sepolia.id, |
192 | | - }); |
193 | | - expect(result).toEqual(false); |
194 | | - }); |
195 | | - |
196 | | - it("Fails to verify with wrong domain - wrong version", async () => { |
197 | | - const signature = await signTypedData(testClient1, { |
198 | | - domainOverride: { |
199 | | - name: "Hypercerts", |
200 | | - version: "2", |
201 | | - chainId: sepolia.id, |
202 | | - }, |
203 | | - }); |
204 | | - |
205 | | - const result = await verifyAuthSignedData({ |
206 | | - address: address1, |
207 | | - message: { |
208 | | - message: "test", |
209 | | - }, |
210 | | - types, |
211 | | - signature, |
212 | | - primaryType: "test", |
213 | | - requiredChainId: sepolia.id, |
214 | | - }); |
215 | | - expect(result).toEqual(false); |
216 | | - }); |
217 | | - |
218 | | - it("fails to verify wrong domain - null fields", async () => { |
219 | | - const signature = await signTypedData(testClient1); |
220 | | - |
221 | | - const result = await verifyAuthSignedData({ |
222 | | - address: address1, |
223 | | - message: { |
224 | | - message: null, |
225 | | - }, |
226 | | - types, |
227 | | - signature, |
228 | | - primaryType: "test", |
229 | | - requiredChainId: sepolia.id, |
230 | | - }); |
231 | | - expect(result).toEqual(false); |
232 | | - }); |
233 | | - |
234 | | - it("fails to verify wrong message - different fields", async () => { |
235 | | - const signature = await signTypedData(testClient1, { |
236 | | - messageOverride: { message: "different", other: "fields" }, |
237 | | - }); |
238 | | - |
239 | | - const result = await verifyAuthSignedData({ |
240 | | - address: address1, |
241 | | - message: { |
242 | | - message: { message: "different", other: "fields" }, |
| 50 | + chainId: 1, |
243 | 51 | }, |
244 | | - types, |
245 | | - signature, |
246 | | - primaryType: "test", |
247 | | - requiredChainId: sepolia.id, |
248 | 52 | }); |
249 | | - expect(result).toEqual(false); |
250 | 53 | }); |
251 | 54 |
|
252 | | - it("fails to verify wrong message - verify primary type", async () => { |
253 | | - const signature = await signTypedData(testClient1); |
| 55 | + it("returns false on verification error", async () => { |
| 56 | + mockVerifyTypedData.mockRejectedValue(new Error("Verification failed")); |
254 | 57 |
|
255 | 58 | const result = await verifyAuthSignedData({ |
256 | | - address: address1, |
257 | | - message: { |
258 | | - message: { message: "test" }, |
259 | | - }, |
260 | | - types, |
261 | | - signature, |
262 | | - primaryType: "mock", |
263 | | - requiredChainId: sepolia.id, |
| 59 | + address: "0x123", |
| 60 | + message: { test: "message" }, |
| 61 | + types: { Test: [{ name: "test", type: "string" }] }, |
| 62 | + signature: "0xsignature", |
| 63 | + primaryType: "Test", |
| 64 | + requiredChainId: 1, |
264 | 65 | }); |
265 | | - expect(result).toEqual(false); |
266 | | - }); |
267 | | - |
268 | | - it("fails to verify wrong message - signed different primary type", async () => { |
269 | | - const signature = await signTypedData(testClient1); |
270 | | - |
271 | | - const result = await verifyAuthSignedData({ |
272 | | - address: address1, |
273 | | - message: { |
274 | | - message: { message: "test" }, |
275 | | - }, |
276 | | - types, |
277 | | - signature, |
278 | | - primaryType: "mock", |
279 | | - requiredChainId: sepolia.id, |
280 | | - }); |
281 | | - expect(result).toEqual(false); |
282 | | - }); |
283 | 66 |
|
284 | | - it("fails to verify with malformed signature", async () => { |
285 | | - const signature = "0xInvalidSignature"; |
286 | | - |
287 | | - const result = await verifyAuthSignedData({ |
288 | | - address: address1, |
289 | | - message: { |
290 | | - message: "test", |
291 | | - }, |
292 | | - types, |
293 | | - signature, |
294 | | - primaryType: "test", |
295 | | - requiredChainId: sepolia.id, |
296 | | - }); |
297 | | - expect(result).toEqual(false); |
298 | | - }); |
299 | | - |
300 | | - it("verifies message with special characters", async () => { |
301 | | - const specialMessage = { |
302 | | - message: "Test with special chars: éèàù@#$%^&*()_+", |
303 | | - }; |
304 | | - const signature = await signTypedData(testClient1, { |
305 | | - messageOverride: specialMessage, |
306 | | - }); |
307 | | - |
308 | | - const result = await verifyAuthSignedData({ |
309 | | - address: address1, |
310 | | - message: specialMessage, |
311 | | - types, |
312 | | - signature, |
313 | | - primaryType: "test", |
314 | | - requiredChainId: sepolia.id, |
315 | | - }); |
316 | | - expect(result).toEqual(true); |
| 67 | + expect(result).toBe(false); |
317 | 68 | }); |
318 | 69 | }); |
0 commit comments