Skip to content

Commit

Permalink
removes metamask eth sig utils
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1995 committed Aug 29, 2024
1 parent e6b0576 commit 2b22069
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 67 deletions.
4 changes: 2 additions & 2 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
@@ -1,4 +1,3 @@
import type { MessageTypes, TypedDataV1, TypedMessage } from "@metamask/eth-sig-util";
import type { ISignClient, SessionTypes } from "@walletconnect/types";
import { getAccountsFromNamespaces, parseAccountId } from "@walletconnect/utils";
import { type JRPCRequest, providerErrors, rpcErrors } from "@web3auth/auth";
Expand Down Expand Up @@ -70,15 +69,7 @@ export function getProviderHandlers({ connector, chainId }: { connector: ISignCl
const methodRes = await sendJrpcRequest<string, string[]>(connector, chainId, "personal_sign", [msgParams.data, msgParams.from]);
return methodRes;
},
processTypedMessage: async (msgParams: MessageParams<TypedDataV1>, _: JRPCRequest<unknown>): Promise<string> => {
const methodRes = await sendJrpcRequest<string, unknown[]>(connector, chainId, "eth_signTypedData", [msgParams.data, msgParams.from]);
return methodRes;
},
processTypedMessageV3: async (msgParams: TypedMessageParams<TypedMessage<MessageTypes>>): Promise<string> => {
const methodRes = await sendJrpcRequest<string, unknown[]>(connector, chainId, "eth_signTypedData_v3", [msgParams.from, msgParams.data]);
return methodRes;
},
processTypedMessageV4: async (msgParams: TypedMessageParams<TypedMessage<MessageTypes>>): Promise<string> => {
processTypedMessageV4: async (msgParams: TypedMessageParams): Promise<string> => {
const methodRes = await sendJrpcRequest<string, unknown[]>(connector, chainId, "eth_signTypedData_v4", [msgParams.from, msgParams.data]);
return methodRes;
},
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/ethereum-mpc-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"@ethereumjs/common": "^4.3.0",
"@ethereumjs/tx": "^5.3.0",
"@ethereumjs/util": "^9.0.3",
"@metamask/eth-sig-util": "7.0.2",
"@toruslabs/base-controllers": "^6.0.2",
"@web3auth/auth": "^9.1.3",
"@web3auth/base": "^8.12.4",
"@web3auth/base-provider": "^8.12.4",
"@web3auth/ethereum-provider": "^8.12.4"
"@web3auth/ethereum-provider": "^8.12.4",
"ethers": "^6.13.2"
},
"peerDependencies": {
"@babel/runtime": "7.x"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { hashPersonalMessage, intToBytes, isHexString, publicToAddress, stripHexPrefix, toBytes } from "@ethereumjs/util";
import {
type MessageTypes,
TypedDataUtils,
type TypedDataV1,
type TypedDataV1Field,
type TypedMessage,
typedSignatureHash,
} from "@metamask/eth-sig-util";
import { concatSig } from "@toruslabs/base-controllers";
import { JRPCRequest, providerErrors, SafeEventEmitterProvider } from "@web3auth/auth";
import { isHexStrict, log } from "@web3auth/base";
import { log } from "@web3auth/base";
import {
IProviderHandlers,
MessageParams,
SignTypedDataMessageV4,
SignTypedDataVersion,
TransactionFormatter,
TransactionParams,
TypedMessageParams,
validateTypedMessageParams,
validateTypedSignMessageDataV4,
} from "@web3auth/ethereum-provider";
import { TypedDataEncoder } from "ethers";

async function signTx(
txParams: TransactionParams & { gas?: string },
Expand Down Expand Up @@ -120,18 +114,16 @@ function validateVersion(version: string, allowedVersions: string[]) {

async function signTypedData(
sign: (msgHash: Buffer, rawMsg?: Buffer) => Promise<{ v: number; r: Buffer; s: Buffer }>,
data: unknown,
data: TypedMessageParams,
version: SignTypedDataVersion
) {
validateVersion(version, undefined); // Note: this is intentional;
if (data === null || data === undefined) {
throw new Error("Missing data parameter");
}
const messageHash =
version === SignTypedDataVersion.V1
? Buffer.from(stripHexPrefix(typedSignatureHash(data as TypedDataV1Field[])), "hex")
: TypedDataUtils.eip712Hash(data as TypedMessage<MessageTypes>, version);
const { v, r, s } = await sign(Buffer.from(messageHash.buffer));
const message: SignTypedDataMessageV4 = typeof data === "string" ? JSON.parse(data) : data;

const { v, r, s } = await sign(Buffer.from(TypedDataEncoder.hash(message.domain, message.types, message.message)));

let modifiedV = v;
if (modifiedV <= 1) {
Expand Down Expand Up @@ -195,41 +187,7 @@ export function getProviderHandlers({
const sig = personalSign(sign, msgParams.data);
return sig;
},
processTypedMessage: async (msgParams: MessageParams<TypedDataV1>, _: JRPCRequest<unknown>): Promise<string> => {
log.debug("processTypedMessage", msgParams);
const providerEngineProxy = getProviderEngineProxy();
if (!providerEngineProxy)
throw providerErrors.custom({
message: "Provider is not initialized",
code: 4902,
});
const chainId = await providerEngineProxy.request<unknown, string>({ method: "eth_chainId" });
const finalChainId = Number.parseInt(chainId, isHexStrict(chainId) ? 16 : 10);
const params = {
...msgParams,
version: SignTypedDataVersion.V1,
};
await validateTypedMessageParams(params, finalChainId);
const data = typeof params.data === "string" ? JSON.parse(params.data) : params.data;
const sig = signTypedData(sign, data, SignTypedDataVersion.V1);
return sig;
},
processTypedMessageV3: async (msgParams: TypedMessageParams<TypedMessage<MessageTypes>>, _: JRPCRequest<unknown>): Promise<string> => {
log.debug("processTypedMessageV3", msgParams);
const providerEngineProxy = getProviderEngineProxy();
if (!providerEngineProxy)
throw providerErrors.custom({
message: "Provider is not initialized",
code: 4902,
});
const chainId = await providerEngineProxy.request<unknown, string>({ method: "eth_chainId" });
const finalChainId = Number.parseInt(chainId, isHexStrict(chainId) ? 16 : 10);
await validateTypedMessageParams(msgParams, finalChainId);
const data = typeof msgParams.data === "string" ? JSON.parse(msgParams.data) : msgParams.data;
const sig = signTypedData(sign, data, SignTypedDataVersion.V3);
return sig;
},
processTypedMessageV4: async (msgParams: TypedMessageParams<TypedMessage<MessageTypes>>, _: JRPCRequest<unknown>): Promise<string> => {
processTypedMessageV4: async (msgParams: TypedMessageParams, _: JRPCRequest<unknown>): Promise<string> => {
log.debug("processTypedMessageV4", msgParams);
const providerEngineProxy = getProviderEngineProxy();
if (!providerEngineProxy)
Expand All @@ -238,8 +196,7 @@ export function getProviderHandlers({
code: 4902,
});
const chainId = await providerEngineProxy.request<unknown, string>({ method: "eth_chainId" });
const finalChainId = Number.parseInt(chainId, isHexStrict(chainId) ? 16 : 10);
await validateTypedMessageParams(msgParams, finalChainId);
await validateTypedSignMessageDataV4(msgParams, chainId);
const data = typeof msgParams.data === "string" ? JSON.parse(msgParams.data) : msgParams.data;
const sig = signTypedData(sign, data, SignTypedDataVersion.V4);
return sig;
Expand Down

0 comments on commit 2b22069

Please sign in to comment.