-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9533c7e
commit f2c7e18
Showing
1 changed file
with
39 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |