Skip to content

Commit

Permalink
Merge pull request #2050 from Web3Auth/feat/validateTypeData-viem
Browse files Browse the repository at this point in the history
feat: validate typedData via viem lib
  • Loading branch information
himanshuchawla009 authored Jan 10, 2025
2 parents e4876f9 + 085d851 commit 02b0d16
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 39 deletions.
71 changes: 46 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@web3auth/ethereum-provider": "^9.5.2",
"ethers": "^6.13.4",
"permissionless": "^0.2.23",
"viem": "^2.21.55"
"viem": "^2.22.5"
},
"peerDependencies": {
"@babel/runtime": "7.x"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function getProviderHandlers({
processTransaction: async (txParams: TransactionParams & { gas?: string }): Promise<string> => {
if (txParams.input && !txParams.data) txParams.data = addHexPrefix(txParams.input);
const { to, value, data } = txParams;
// @ts-expect-error viem types are too deep
const userOperationParams: SendUserOperationParameters = {
account: smartAccount,
calls: [
Expand All @@ -59,7 +60,7 @@ export function getProviderHandlers({
],
// should not use maxFeePerGas/maxPriorityFeePerGas from transaction params since that's fee for transaction not user operation and let bundler handle it instead
};
// @ts-expect-error viem types are too deep

const userOpHash = await bundlerClient.sendUserOperation(userOperationParams);

const txReceipt = await bundlerClient.waitForUserOperationReceipt({ hash: userOpHash });
Expand Down
3 changes: 2 additions & 1 deletion packages/providers/ethereum-mpc-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@web3auth/base": "^9.5.2",
"@web3auth/base-provider": "^9.5.2",
"@web3auth/ethereum-provider": "^9.5.2",
"ethers": "^6.13.4"
"ethers": "^6.13.4",
"viem": "^2.22.5"
},
"peerDependencies": {
"@babel/runtime": "7.x"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { log } from "@web3auth/base";
import {
IProviderHandlers,
MessageParams,
SignTypedDataMessageV4,
SignTypedDataVersion,
TransactionFormatter,
TransactionParams,
TypedMessageParams,
validateTypedSignMessageDataV4,
} from "@web3auth/ethereum-provider";
import { hashMessage, Signature, TypedDataEncoder } from "ethers";
import { hashMessage, Signature } from "ethers";
import { hashTypedData, hexToBytes, validateTypedData } from "viem";

async function signTx(
txParams: TransactionParams & { gas?: string },
Expand Down Expand Up @@ -89,9 +89,10 @@ async function signTypedData(
if (data === null || data === undefined) {
throw new Error("Missing data parameter");
}
const message: SignTypedDataMessageV4 = typeof data === "string" ? JSON.parse(data) : data;
const message = typeof data === "string" ? JSON.parse(data) : data;
validateTypedData(message);

const { v, r, s } = await sign(Buffer.from(TypedDataEncoder.hash(message.domain, message.types, message.message).slice(2), "hex"));
const { v, r, s } = await sign(Buffer.from(hexToBytes(hashTypedData(message))));

let modifiedV = v;
if (modifiedV <= 1) {
Expand Down Expand Up @@ -170,7 +171,6 @@ export function getProviderHandlers({
const chainId = await providerEngineProxy.request<unknown, string>({ method: "eth_chainId" });
await validateTypedSignMessageDataV4(msgParams, chainId);
const data = typeof msgParams.data === "string" ? JSON.parse(msgParams.data) : msgParams.data;
if (data.types.EIP712Domain) delete data.types.EIP712Domain;
const sig = signTypedData(sign, data, SignTypedDataVersion.V4);
return sig;
},
Expand Down
3 changes: 2 additions & 1 deletion packages/providers/ethereum-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -103,10 +104,10 @@ export function getProviderHandlers({
});
const chainId = await providerEngineProxy.request<never, string>({ 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;
},
};
Expand Down

0 comments on commit 02b0d16

Please sign in to comment.