|
| 1 | +import { expect, describe, it, vi, beforeEach } from "vitest"; |
| 2 | +import { AuthEngine } from "../src/controllers/engine"; |
| 3 | +import { AuthClientTypes } from "../src/types"; |
| 4 | + |
| 5 | +const WALLET_URL = "https://mywallet.app"; |
| 6 | +const DAPP_URL = "https://app.uniswap.org"; |
| 7 | + |
| 8 | +const walletMetadata: AuthClientTypes.Metadata = { |
| 9 | + name: "My Wallet", |
| 10 | + description: "", |
| 11 | + url: WALLET_URL, |
| 12 | + icons: [], |
| 13 | +}; |
| 14 | + |
| 15 | +const dappMetadata: AuthClientTypes.Metadata = { |
| 16 | + name: "Uniswap", |
| 17 | + description: "", |
| 18 | + url: DAPP_URL, |
| 19 | + icons: [], |
| 20 | +}; |
| 21 | + |
| 22 | +const authRequestPayload = { |
| 23 | + id: 1, |
| 24 | + jsonrpc: "2.0" as const, |
| 25 | + method: "wc_authRequest" as const, |
| 26 | + params: { |
| 27 | + requester: { |
| 28 | + publicKey: "abc123", |
| 29 | + metadata: dappMetadata, |
| 30 | + }, |
| 31 | + payloadParams: { |
| 32 | + type: "eip4361" as const, |
| 33 | + chainId: "eip155:1", |
| 34 | + domain: "app.uniswap.org", |
| 35 | + aud: "https://app.uniswap.org/login", |
| 36 | + version: "1", |
| 37 | + nonce: "deadbeef", |
| 38 | + iat: new Date().toISOString(), |
| 39 | + }, |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +function createMockClient(verifyResolveReturn: unknown) { |
| 44 | + return { |
| 45 | + metadata: walletMetadata, |
| 46 | + logger: { |
| 47 | + info: vi.fn(), |
| 48 | + error: vi.fn(), |
| 49 | + debug: vi.fn(), |
| 50 | + }, |
| 51 | + requests: { |
| 52 | + set: vi.fn().mockResolvedValue(undefined), |
| 53 | + }, |
| 54 | + core: { |
| 55 | + verify: { |
| 56 | + resolve: vi.fn().mockResolvedValue(verifyResolveReturn), |
| 57 | + }, |
| 58 | + pairing: { |
| 59 | + register: vi.fn(), |
| 60 | + }, |
| 61 | + }, |
| 62 | + emit: vi.fn(), |
| 63 | + } as any; |
| 64 | +} |
| 65 | + |
| 66 | +describe("AuthEngine.onAuthRequest verifyContext wiring", () => { |
| 67 | + it("produces VALID when attested origin matches requester (dApp) URL", async () => { |
| 68 | + const mockClient = createMockClient({ origin: DAPP_URL }); |
| 69 | + const engine = new AuthEngine(mockClient); |
| 70 | + |
| 71 | + await (engine as any).onAuthRequest("topic", authRequestPayload); |
| 72 | + |
| 73 | + const [eventName, eventArgs] = mockClient.emit.mock.calls[0]; |
| 74 | + expect(eventName).toBe("auth_request"); |
| 75 | + expect(eventArgs.verifyContext.verified.validation).toBe("VALID"); |
| 76 | + expect(eventArgs.verifyContext.verified.origin).toBe(DAPP_URL); |
| 77 | + }); |
| 78 | + |
| 79 | + it("produces INVALID when attested origin matches wallet URL but not requester URL (regression guard)", async () => { |
| 80 | + const mockClient = createMockClient({ origin: WALLET_URL }); |
| 81 | + const engine = new AuthEngine(mockClient); |
| 82 | + |
| 83 | + await (engine as any).onAuthRequest("topic", authRequestPayload); |
| 84 | + |
| 85 | + const [eventName, eventArgs] = mockClient.emit.mock.calls[0]; |
| 86 | + expect(eventName).toBe("auth_request"); |
| 87 | + expect(eventArgs.verifyContext.verified.validation).toBe("INVALID"); |
| 88 | + expect(eventArgs.verifyContext.verified.origin).toBe(WALLET_URL); |
| 89 | + }); |
| 90 | +}); |
0 commit comments