|
| 1 | +import { expect, describe, it, vi } from "vitest"; |
| 2 | +import { buildVerifyContext } from "../src/utils/verifyContext"; |
| 3 | + |
| 4 | +const noopLog = (_err: unknown) => { |
| 5 | + return; |
| 6 | +}; |
| 7 | + |
| 8 | +describe("buildVerifyContext", () => { |
| 9 | + it("returns UNKNOWN when resolver returns undefined", async () => { |
| 10 | + const ctx = await buildVerifyContext( |
| 11 | + { resolve: () => Promise.resolve(undefined), logError: noopLog }, |
| 12 | + "hash", |
| 13 | + { name: "x", description: "", url: "https://app.uniswap.org", icons: [] }, |
| 14 | + ); |
| 15 | + expect(ctx.verified.validation).toBe("UNKNOWN"); |
| 16 | + expect(ctx.verified.origin).toBe("https://app.uniswap.org"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("returns UNKNOWN when resolver throws", async () => { |
| 20 | + const ctx = await buildVerifyContext( |
| 21 | + { |
| 22 | + resolve: () => Promise.reject(new Error("network")), |
| 23 | + logError: noopLog, |
| 24 | + }, |
| 25 | + "hash", |
| 26 | + { name: "x", description: "", url: "https://app.uniswap.org", icons: [] }, |
| 27 | + ); |
| 28 | + expect(ctx.verified.validation).toBe("UNKNOWN"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns VALID when attested origin matches requester metadata.url", async () => { |
| 32 | + const ctx = await buildVerifyContext( |
| 33 | + { |
| 34 | + resolve: () => Promise.resolve({ origin: "https://app.uniswap.org" }), |
| 35 | + logError: noopLog, |
| 36 | + }, |
| 37 | + "hash", |
| 38 | + { name: "Uniswap", description: "", url: "https://app.uniswap.org", icons: [] }, |
| 39 | + ); |
| 40 | + expect(ctx.verified.validation).toBe("VALID"); |
| 41 | + expect(ctx.verified.origin).toBe("https://app.uniswap.org"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns INVALID when attested origin differs from claimed metadata.url", async () => { |
| 45 | + const ctx = await buildVerifyContext( |
| 46 | + { |
| 47 | + resolve: () => Promise.resolve({ origin: "https://attacker.com" }), |
| 48 | + logError: noopLog, |
| 49 | + }, |
| 50 | + "hash", |
| 51 | + { name: "Uniswap", description: "", url: "https://app.uniswap.org", icons: [] }, |
| 52 | + ); |
| 53 | + expect(ctx.verified.validation).toBe("INVALID"); |
| 54 | + expect(ctx.verified.origin).toBe("https://attacker.com"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("propagates isScam=true from attestation", async () => { |
| 58 | + const ctx = await buildVerifyContext( |
| 59 | + { |
| 60 | + resolve: () => Promise.resolve({ origin: "https://attacker.com", isScam: true }), |
| 61 | + logError: noopLog, |
| 62 | + }, |
| 63 | + "hash", |
| 64 | + { name: "Uniswap", description: "", url: "https://app.uniswap.org", icons: [] }, |
| 65 | + ); |
| 66 | + expect(ctx.verified.isScam).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("sets isScam for any truthy attestation value (matches upstream)", async () => { |
| 70 | + const ctx = await buildVerifyContext( |
| 71 | + { |
| 72 | + resolve: () => |
| 73 | + Promise.resolve({ origin: "https://x.com", isScam: 1 as unknown as boolean }), |
| 74 | + logError: noopLog, |
| 75 | + }, |
| 76 | + "hash", |
| 77 | + { name: "x", description: "", url: "https://x.com", icons: [] }, |
| 78 | + ); |
| 79 | + expect(ctx.verified.isScam).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it("supports string-shaped attestation response (legacy)", async () => { |
| 83 | + const ctx = await buildVerifyContext( |
| 84 | + { resolve: () => Promise.resolve("https://app.uniswap.org"), logError: noopLog }, |
| 85 | + "hash", |
| 86 | + { name: "Uniswap", description: "", url: "https://app.uniswap.org", icons: [] }, |
| 87 | + ); |
| 88 | + expect(ctx.verified.validation).toBe("VALID"); |
| 89 | + expect(ctx.verified.origin).toBe("https://app.uniswap.org"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("forwards verifyUrl from metadata to resolver", async () => { |
| 93 | + const spy = vi.fn().mockResolvedValue(undefined); |
| 94 | + await buildVerifyContext({ resolve: spy, logError: noopLog }, "h", { |
| 95 | + name: "x", |
| 96 | + description: "", |
| 97 | + url: "https://x.com", |
| 98 | + icons: [], |
| 99 | + verifyUrl: "https://verify.example.com", |
| 100 | + }); |
| 101 | + expect(spy).toHaveBeenCalledWith({ |
| 102 | + attestationId: "h", |
| 103 | + verifyUrl: "https://verify.example.com", |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("regression: legitimate dApp must produce VALID when caller passes dApp metadata", async () => { |
| 108 | + const dappMetadata = { |
| 109 | + name: "Uniswap", |
| 110 | + description: "", |
| 111 | + url: "https://app.uniswap.org", |
| 112 | + icons: [], |
| 113 | + }; |
| 114 | + const ctx = await buildVerifyContext( |
| 115 | + { |
| 116 | + resolve: () => Promise.resolve({ origin: "https://app.uniswap.org" }), |
| 117 | + logError: noopLog, |
| 118 | + }, |
| 119 | + "hash", |
| 120 | + dappMetadata, |
| 121 | + ); |
| 122 | + expect(ctx.verified.validation).toBe("VALID"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("returns UNKNOWN with empty origin when metadata is undefined", async () => { |
| 126 | + const ctx = await buildVerifyContext( |
| 127 | + { resolve: () => Promise.resolve(undefined), logError: noopLog }, |
| 128 | + "hash", |
| 129 | + undefined as any, |
| 130 | + ); |
| 131 | + expect(ctx.verified.validation).toBe("UNKNOWN"); |
| 132 | + expect(ctx.verified.origin).toBe(""); |
| 133 | + expect(ctx.verified.verifyUrl).toBe(""); |
| 134 | + }); |
| 135 | + |
| 136 | + it("returns UNKNOWN with empty origin when metadata is null", async () => { |
| 137 | + const ctx = await buildVerifyContext( |
| 138 | + { resolve: () => Promise.resolve(undefined), logError: noopLog }, |
| 139 | + "hash", |
| 140 | + null as any, |
| 141 | + ); |
| 142 | + expect(ctx.verified.validation).toBe("UNKNOWN"); |
| 143 | + expect(ctx.verified.origin).toBe(""); |
| 144 | + }); |
| 145 | + |
| 146 | + it("returns UNKNOWN with empty origin when metadata.url is empty string", async () => { |
| 147 | + const ctx = await buildVerifyContext( |
| 148 | + { resolve: () => Promise.resolve(undefined), logError: noopLog }, |
| 149 | + "hash", |
| 150 | + { name: "x", description: "", url: "", icons: [] }, |
| 151 | + ); |
| 152 | + expect(ctx.verified.validation).toBe("UNKNOWN"); |
| 153 | + expect(ctx.verified.origin).toBe(""); |
| 154 | + }); |
| 155 | + |
| 156 | + it("propagates isScam even when metadata is undefined", async () => { |
| 157 | + const ctx = await buildVerifyContext( |
| 158 | + { |
| 159 | + resolve: () => Promise.resolve({ origin: "https://attacker.com", isScam: true }), |
| 160 | + logError: noopLog, |
| 161 | + }, |
| 162 | + "hash", |
| 163 | + undefined as any, |
| 164 | + ); |
| 165 | + expect(ctx.verified.isScam).toBe(true); |
| 166 | + expect(ctx.verified.validation).toBe("UNKNOWN"); |
| 167 | + }); |
| 168 | +}); |
0 commit comments