Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Safe Typed Data #147

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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