Skip to content

Commit

Permalink
feat: validate typedData via viem lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ieow committed Jan 9, 2025
1 parent e4876f9 commit 6ece2e4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
66 changes: 43 additions & 23 deletions package-lock.json

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

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

0 comments on commit 6ece2e4

Please sign in to comment.