From 085d85108b27ffca687c7ef5b82c33efbefd1f09 Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 10 Jan 2025 09:47:17 +0800 Subject: [PATCH] fix: validateTypeData for ethereum provider --- package-lock.json | 3 ++- packages/providers/ethereum-provider/package.json | 3 ++- .../privateKeyProviders/ethPrivatekeyUtils.ts | 11 ++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7e882835d..874039fe9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27265,7 +27265,8 @@ "bignumber.js": "^9.1.2", "bn.js": "^5.2.1", "ethers": "^6.13.4", - "jsonschema": "^1.4.1" + "jsonschema": "^1.4.1", + "viem": "^2.22.5" }, "devDependencies": { "@types/bn.js": "^5.1.6", diff --git a/packages/providers/ethereum-provider/package.json b/packages/providers/ethereum-provider/package.json index 12d053b73..4ec3c4cdd 100644 --- a/packages/providers/ethereum-provider/package.json +++ b/packages/providers/ethereum-provider/package.json @@ -31,7 +31,8 @@ "bignumber.js": "^9.1.2", "bn.js": "^5.2.1", "ethers": "^6.13.4", - "jsonschema": "^1.4.1" + "jsonschema": "^1.4.1", + "viem": "^2.22.5" }, "devDependencies": { "@types/bn.js": "^5.1.6", diff --git a/packages/providers/ethereum-provider/src/providers/privateKeyProviders/ethPrivatekeyUtils.ts b/packages/providers/ethereum-provider/src/providers/privateKeyProviders/ethPrivatekeyUtils.ts index 6d6f8dc15..8db65517f 100644 --- a/packages/providers/ethereum-provider/src/providers/privateKeyProviders/ethPrivatekeyUtils.ts +++ b/packages/providers/ethereum-provider/src/providers/privateKeyProviders/ethPrivatekeyUtils.ts @@ -3,9 +3,10 @@ import { signMessage } from "@toruslabs/base-controllers"; import { getPublicCompressed } from "@toruslabs/eccrypto"; import { JRPCRequest, providerErrors } from "@web3auth/auth"; import { log, SafeEventEmitterProvider } from "@web3auth/base"; -import { hashMessage, SigningKey, TypedDataEncoder } from "ethers"; +import { hashMessage, SigningKey } from "ethers"; +import { hashTypedData, validateTypedData } from "viem"; -import { IProviderHandlers, MessageParams, SignTypedDataMessageV4, TransactionParams, TypedMessageParams } from "../../rpc/interfaces"; +import { IProviderHandlers, MessageParams, TransactionParams, TypedMessageParams } from "../../rpc/interfaces"; import { TransactionFormatter } from "./TransactionFormatter/formatter"; import { validateTypedSignMessageDataV4 } from "./TransactionFormatter/utils"; @@ -103,10 +104,10 @@ export function getProviderHandlers({ }); const chainId = await providerEngineProxy.request({ method: "eth_chainId" }); await validateTypedSignMessageDataV4(msgParams, chainId); - const data: SignTypedDataMessageV4 = typeof msgParams.data === "string" ? JSON.parse(msgParams.data) : msgParams.data; + const data = typeof msgParams.data === "string" ? JSON.parse(msgParams.data) : msgParams.data; const ethersPrivateKey = new SigningKey(privKeyBuffer); - if (data.types.EIP712Domain) delete data.types.EIP712Domain; - const signature = ethersPrivateKey.sign(TypedDataEncoder.hash(data.domain, data.types, data.message)).serialized; + validateTypedData(data); + const signature = ethersPrivateKey.sign(hashTypedData(data)).serialized; return signature; }, };