Skip to content

Commit

Permalink
Support Safe Typed Data (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Nov 13, 2024
1 parent 872c0eb commit 6e8f6c0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/types/guards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Hex,
isAddress,
isHex,
parseTransaction,
serializeTransaction,
TransactionSerializable,
Expand Down Expand Up @@ -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";
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/types.guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 6e8f6c0

Please sign in to comment.