Skip to content

Commit

Permalink
Fix ethers v5 and v6 handling (#198)
Browse files Browse the repository at this point in the history
Close #187
  • Loading branch information
Harman-singh-waraich authored Apr 15, 2024
1 parent 681993c commit 3aad7ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
6 changes: 2 additions & 4 deletions packages/siwe/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.'
);
Expand Down
45 changes: 29 additions & 16 deletions packages/siwe/lib/ethersCompat.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;

0 comments on commit 3aad7ae

Please sign in to comment.