From 3aad7ae6fedae4ebe6484e9444de04869b026361 Mon Sep 17 00:00:00 2001 From: TurbanCoder <51452848+Harman-singh-waraich@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:01:07 +0700 Subject: [PATCH] Fix ethers v5 and v6 handling (#198) Close #187 --- packages/siwe/lib/client.ts | 6 ++--- packages/siwe/lib/ethersCompat.ts | 45 ++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/siwe/lib/client.ts b/packages/siwe/lib/client.ts index d46ac14e..bf3b9fac 100644 --- a/packages/siwe/lib/client.ts +++ b/packages/siwe/lib/client.ts @@ -4,11 +4,9 @@ import { ParsedMessage, parseIntegerNumber, } from '@spruceid/siwe-parser'; -// @ts-expect-error -- ethers v6 compatibility hack -import { providers } from 'ethers'; import * as uri from 'valid-url'; -import { getAddress, verifyMessage } from './ethersCompat'; +import { getAddress, Provider, verifyMessage } from './ethersCompat'; import { SiweError, SiweErrorType, @@ -194,7 +192,7 @@ export class SiweMessage { * @param signature Signature to match the address in the message. * @param provider Ethers provider to be used for EIP-1271 validation */ - async validate(signature: string, provider?: providers.Provider) { + async validate(signature: string, provider?: Provider) { console.warn( 'validate() has been deprecated, please update your code to use verify(). validate() may be removed in future versions.' ); diff --git a/packages/siwe/lib/ethersCompat.ts b/packages/siwe/lib/ethersCompat.ts index 1dd64645..5c66c150 100644 --- a/packages/siwe/lib/ethersCompat.ts +++ b/packages/siwe/lib/ethersCompat.ts @@ -1,10 +1,4 @@ -import { - // @ts-expect-error -- ethers v6 compatibility hack - utils, - verifyMessage as ethersVerifyMessage, - hashMessage as ethersHashMessage, - getAddress as ethersGetAddress, -} from 'ethers'; +import { ethers } from 'ethers'; type Ethers6BigNumberish = string | number | bigint; @@ -34,16 +28,35 @@ type Ethers6SignatureLike = yParityAndS?: string; }; -export const verifyMessage = - utils?.verifyMessage ?? - (ethersVerifyMessage as ( +let ethersVerifyMessage = null; +let ethersHashMessage = null; +let ethersGetAddress = null; + +try { + // @ts-expect-error -- v6 compatibility hack + ethersVerifyMessage = ethers.utils.verifyMessage; + // @ts-expect-error -- v6 compatibility hack + ethersHashMessage = ethers.utils.hashMessage; + // @ts-expect-error -- v6 compatibility hack + ethersGetAddress = ethers.utils.getAddress; +} catch { + ethersVerifyMessage = ethers.verifyMessage as ( message: Uint8Array | string, sig: Ethers6SignatureLike - ) => string); + ) => string; + + ethersHashMessage = ethers.hashMessage as ( + message: Uint8Array | string + ) => string; + + ethersGetAddress = ethers.getAddress as (address: string) => string; +} -export const hashMessage = - utils?.hashMessage ?? - (ethersHashMessage as (message: Uint8Array | string) => string); +// @ts-expect-error -- v6 compatibility hack +type ProviderV5 = ethers.providers.Provider +type ProviderV6 = ethers.Provider -export const getAddress = - utils?.getAddress ?? (ethersGetAddress as (address: string) => string); +export type Provider = ProviderV6 extends undefined ? ProviderV5 : ProviderV6 +export const verifyMessage = ethersVerifyMessage; +export const hashMessage = ethersHashMessage; +export const getAddress = ethersGetAddress;