Skip to content

Commit

Permalink
Improve TypedDataDomain guard (#158)
Browse files Browse the repository at this point in the history
Uniswap WC requests had chainId as a string in the domain structure.
This PR improves the type guard to capture this.
  • Loading branch information
bh2smith authored Jan 15, 2025
1 parent 8d1f001 commit 18a1c44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/types/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
Expand All @@ -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":
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/types.guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isRlpHex,
isSignMethod,
isTransactionSerializable,
isTypedDataDomain,
} from "../../src/";

const validEIP1559Transaction: TransactionSerializable = {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 18a1c44

Please sign in to comment.