From f2c7e1809e37532c8e093c7bf4b5aff5ffe2a7e9 Mon Sep 17 00:00:00 2001 From: Harman-singh-waraich Date: Thu, 11 Apr 2024 17:28:08 +0700 Subject: [PATCH] fix: export with try catch --- packages/siwe/lib/ethersCompat.ts | 72 +++++++++++++++++-------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/packages/siwe/lib/ethersCompat.ts b/packages/siwe/lib/ethersCompat.ts index 64d8e5ff..74fc5eb9 100644 --- a/packages/siwe/lib/ethersCompat.ts +++ b/packages/siwe/lib/ethersCompat.ts @@ -1,5 +1,3 @@ -import ethers from 'ethers'; - type Ethers6BigNumberish = string | number | bigint; // NB: This compatibility type omits the `Signature` class defined in ethers v6; @@ -7,41 +5,49 @@ type Ethers6BigNumberish = string | number | bigint; type Ethers6SignatureLike = | string | { - r: string; - s: string; - v: Ethers6BigNumberish; - yParity?: 0 | 1; - yParityAndS?: string; - } + r: string; + s: string; + v: Ethers6BigNumberish; + yParity?: 0 | 1; + yParityAndS?: string; + } | { - r: string; - yParityAndS: string; - yParity?: 0 | 1; - s?: string; - v?: number; - } + r: string; + yParityAndS: string; + yParity?: 0 | 1; + s?: string; + v?: number; + } | { - r: string; - s: string; - yParity: 0 | 1; - v?: Ethers6BigNumberish; - yParityAndS?: string; - }; + r: string; + s: string; + yParity: 0 | 1; + v?: Ethers6BigNumberish; + yParityAndS?: string; + }; + +let ethersVerifyMessage = null; +let ethersHashMessage = null; +let ethersGetAddress = null; -export const verifyMessage = - // @ts-expect-error -- ethers v6 compatibility hack - ethers?.utils.verifyMessage ?? - (ethers?.verifyMessage as ( +try { + const { utils } = require('ethers'); + ethersVerifyMessage = utils.verifyMessage; + ethersHashMessage = utils.hashMessage; + ethersGetAddress = utils.getAddress; +} catch (error) { + const { verifyMessage, getAddress, hashMessage } = require('ethers'); + + ethersVerifyMessage = verifyMessage as ( message: Uint8Array | string, sig: Ethers6SignatureLike - ) => string); + ) => string; + + ethersHashMessage = hashMessage as (message: Uint8Array | string) => string; -export const hashMessage = - // @ts-expect-error -- ethers v6 compatibility hack - ethers.utils?.hashMessage ?? - (ethers?.hashMessage as (message: Uint8Array | string) => string); + ethersGetAddress = getAddress as (address: string) => string; +} -export const getAddress = - // @ts-expect-error -- ethers v6 compatibility hack - ethers.utils?.getAddress ?? - (ethers?.getAddress as (address: string) => string); +export const verifyMessage = ethersVerifyMessage; +export const hashMessage = ethersHashMessage; +export const getAddress = ethersGetAddress;