Skip to content

Commit

Permalink
remove dynamic import of eth sig util
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Aug 12, 2024
1 parent d8d5064 commit d630b13
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { hashPersonalMessage, intToBytes, isHexString, publicToAddress, stripHexPrefix, toBytes } from "@ethereumjs/util";
import type { MessageTypes, TypedDataV1, TypedDataV1Field, TypedMessage } from "@metamask/eth-sig-util";
import {
type MessageTypes,
TypedDataUtils,
type TypedDataV1,
type TypedDataV1Field,
type TypedMessage,
typedSignatureHash,
} from "@metamask/eth-sig-util";
import { providerErrors } from "@metamask/rpc-errors";
import { concatSig } from "@toruslabs/base-controllers";
import { JRPCRequest, SafeEventEmitterProvider } from "@toruslabs/openlogin-jrpc";
Expand Down Expand Up @@ -121,7 +128,6 @@ async function signTypedData(
if (data === null || data === undefined) {
throw new Error("Missing data parameter");
}
const { TypedDataUtils, typedSignatureHash } = await import("@metamask/eth-sig-util");
const messageHash =
version === SignTypedDataVersion.V1
? Buffer.from(stripHexPrefix(typedSignatureHash(data as TypedDataV1Field[])), "hex")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isValidAddress } from "@ethereumjs/util";
import type { MessageTypeProperty, TypedDataV1Field, TypedMessage } from "@metamask/eth-sig-util";
import { type MessageTypeProperty, TYPED_MESSAGE_SCHEMA, type TypedDataV1Field, type TypedMessage, typedSignatureHash } from "@metamask/eth-sig-util";
import { rpcErrors } from "@metamask/rpc-errors";
import { get } from "@toruslabs/http-helpers";
import { isHexStrict } from "@web3auth/base";
Expand Down Expand Up @@ -73,7 +73,7 @@ export const validateTypedMessageParams = async (parameters: TypedMessageParams<
);
let data: unknown = null;
let chainId = null;
const { typedSignatureHash, TYPED_MESSAGE_SCHEMA } = await import("@metamask/eth-sig-util");

switch ((parameters as TypedMessageParams<unknown>).version) {
case SignTypedDataVersion.V1:
if (typeof parameters.data === "string") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { privateToAddress } from "@ethereumjs/util";
import type { MessageTypes, TypedDataV1, TypedMessage } from "@metamask/eth-sig-util";
import { type MessageTypes, personalSign, signTypedData, type TypedDataV1, type TypedMessage } from "@metamask/eth-sig-util";
import { providerErrors } from "@metamask/rpc-errors";
import { signMessage } from "@toruslabs/base-controllers";
import { JRPCRequest } from "@toruslabs/openlogin-jrpc";
Expand Down Expand Up @@ -65,7 +65,6 @@ export function getProviderHandlers({
},
processPersonalMessage: async (msgParams: MessageParams<string>, _: JRPCRequest<unknown>): Promise<string> => {
const privKeyBuffer = Buffer.from(privKey, "hex");
const { personalSign } = await import("@metamask/eth-sig-util");
const sig = personalSign({ privateKey: privKeyBuffer, data: msgParams.data });
return sig;
},
Expand All @@ -85,7 +84,6 @@ export function getProviderHandlers({
version: SignTypedDataVersion.V1,
};
await validateTypedMessageParams(params, finalChainId);
const { signTypedData } = await import("@metamask/eth-sig-util");
const data = typeof params.data === "string" ? JSON.parse(params.data) : params.data;
const sig = signTypedData({ privateKey: privKeyBuffer, data, version: SignTypedDataVersion.V1 });
return sig;
Expand All @@ -103,7 +101,6 @@ export function getProviderHandlers({
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 { signTypedData } = await import("@metamask/eth-sig-util");
const sig = signTypedData({ privateKey: privKeyBuffer, data, version: SignTypedDataVersion.V3 });
return sig;
},
Expand All @@ -120,7 +117,6 @@ export function getProviderHandlers({
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 { signTypedData } = await import("@metamask/eth-sig-util");
const sig = signTypedData({ privateKey: privKeyBuffer, data, version: SignTypedDataVersion.V4 });
return sig;
},
Expand Down

0 comments on commit d630b13

Please sign in to comment.