From 18a1c44bd675373ccc856b45e2305e8d9744a8da Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Wed, 15 Jan 2025 10:46:23 +0100 Subject: [PATCH] Improve TypedDataDomain guard (#158) Uniswap WC requests had chainId as a string in the domain structure. This PR improves the type guard to capture this. --- src/types/guards.ts | 7 +++++-- tests/unit/types.guards.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/types/guards.ts b/src/types/guards.ts index 9409586..1e2f8cf 100644 --- a/src/types/guards.ts +++ b/src/types/guards.ts @@ -22,7 +22,9 @@ export function isSignMethod(method: unknown): method is SignMethod { ); } -const isTypedDataDomain = (domain: unknown): domain is TypedDataDomain => { +export const isTypedDataDomain = ( + domain: unknown +): domain is TypedDataDomain => { if (typeof domain !== "object" || domain === null) return false; const candidate = domain as Record; @@ -34,7 +36,8 @@ const isTypedDataDomain = (domain: unknown): domain is TypedDataDomain => { return ( typeof value === "undefined" || typeof value === "number" || - isHex(value) + isHex(value) || + (typeof value === "string" && typeof parseInt(value) === "number") ); case "name": case "version": diff --git a/tests/unit/types.guards.test.ts b/tests/unit/types.guards.test.ts index af73c31..784e7d8 100644 --- a/tests/unit/types.guards.test.ts +++ b/tests/unit/types.guards.test.ts @@ -4,6 +4,7 @@ import { isRlpHex, isSignMethod, isTransactionSerializable, + isTypedDataDomain, } from "../../src/"; const validEIP1559Transaction: TransactionSerializable = { @@ -145,6 +146,31 @@ describe("isTransactionSerializable", () => { }); }); +describe("isTypedDataDomain", () => { + it("returns true for various valid domains", async () => { + const permit2Domain = { + name: "Permit2", + chainId: "43114", + verifyingContract: "0x000000000022d473030f116ddee9f6b43ac78ba3", + }; + expect(isTypedDataDomain(permit2Domain)).toBe(true); + expect( + isTypedDataDomain({ + name: "Ether Mail", + version: "1", + chainId: 1, + verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", + }) + ).toBe(true); + expect( + isTypedDataDomain({ + chainId: "0xaa36a7", + verifyingContract: "0x7fa8e8264985c7525fc50f98ac1a9b3765405489", + }) + ).toBe(true); + }); +}); + describe("isRlpHex", () => { it("should return true for valid RLP-encoded transaction hex", () => { // This is an example of a valid RLP-encoded transaction hex: