Skip to content

Commit

Permalink
fix: export with try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Harman-singh-waraich committed Apr 11, 2024
1 parent 9533c7e commit f2c7e18
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions packages/siwe/lib/ethersCompat.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
import ethers from 'ethers';

type Ethers6BigNumberish = string | number | bigint;

// NB: This compatibility type omits the `Signature` class defined in ethers v6;
// however, a `Signature` instance is compatible with the object type defined.
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;

0 comments on commit f2c7e18

Please sign in to comment.