From 6e8f6c081a62fe71c4e05b8365d8fc70c2868915 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Wed, 13 Nov 2024 18:08:58 +0100 Subject: [PATCH] Support Safe Typed Data (#147) --- src/types/guards.ts | 7 +++++- tests/unit/types.guards.test.ts | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/types/guards.ts b/src/types/guards.ts index 1275ce0..9409586 100644 --- a/src/types/guards.ts +++ b/src/types/guards.ts @@ -1,6 +1,7 @@ import { Hex, isAddress, + isHex, parseTransaction, serializeTransaction, TransactionSerializable, @@ -30,7 +31,11 @@ const isTypedDataDomain = (domain: unknown): domain is TypedDataDomain => { return Object.entries(candidate).every(([key, value]) => { switch (key) { case "chainId": - return typeof value === "undefined" || typeof value === "number"; + return ( + typeof value === "undefined" || + typeof value === "number" || + isHex(value) + ); case "name": case "version": return typeof value === "undefined" || typeof value === "string"; diff --git a/tests/unit/types.guards.test.ts b/tests/unit/types.guards.test.ts index e123621..af73c31 100644 --- a/tests/unit/types.guards.test.ts +++ b/tests/unit/types.guards.test.ts @@ -89,6 +89,48 @@ describe("isEIP712TypedData", () => { expect(isEIP712TypedData(item)).toBe(false) ); }); + + it("recognizes safe EIP712 Typed data (with hex chainId)", async () => { + const safeTypedData = { + types: { + SafeTx: [ + { name: "to", type: "address" }, + { name: "value", type: "uint256" }, + { name: "data", type: "bytes" }, + { name: "operation", type: "uint8" }, + { name: "safeTxGas", type: "uint256" }, + { name: "baseGas", type: "uint256" }, + { name: "gasPrice", type: "uint256" }, + { name: "gasToken", type: "address" }, + { name: "refundReceiver", type: "address" }, + { name: "nonce", type: "uint256" }, + ], + EIP712Domain: [ + { name: "chainId", type: "uint256" }, + { name: "verifyingContract", type: "address" }, + ], + }, + domain: { + chainId: "0xaa36a7", + verifyingContract: "0x7fa8e8264985c7525fc50f98ac1a9b3765405489", + }, + primaryType: "SafeTx", + message: { + to: "0x102543f7e6b5786a444cc89ff73012825d13000d", + value: "100000000000000000", + data: "0x", + operation: "0", + safeTxGas: "0", + baseGas: "0", + gasPrice: "0", + gasToken: "0x0000000000000000000000000000000000000000", + refundReceiver: "0x0000000000000000000000000000000000000000", + nonce: "0", + }, + }; + + expect(isEIP712TypedData(safeTypedData)).toBe(true); + }); }); describe("isTransactionSerializable", () => {