1 | import { ethers } from "ethers";
|
2 |
|
3 | export class SignerWithAddress extends ethers.Signer {
|
4 | public static async create(signer: ethers.providers.JsonRpcSigner) {
|
5 | return new SignerWithAddress(await signer.getAddress(), signer);
|
6 | }
|
7 |
|
8 | private constructor(
|
9 | public readonly address: string,
|
10 | private readonly _signer: ethers.providers.JsonRpcSigner
|
11 | ) {
|
12 | super();
|
13 | (this as any).provider = _signer.provider;
|
14 | }
|
15 |
|
16 | public async getAddress(): Promise<string> {
|
17 | return this.address;
|
18 | }
|
19 |
|
20 | public signMessage(message: string | ethers.utils.Bytes): Promise<string> {
|
21 | return this._signer.signMessage(message);
|
22 | }
|
23 |
|
24 | public signTransaction(
|
25 | transaction: ethers.utils.Deferrable<ethers.providers.TransactionRequest>
|
26 | ): Promise<string> {
|
27 | return this._signer.signTransaction(transaction);
|
28 | }
|
29 |
|
30 | public sendTransaction(
|
31 | transaction: ethers.utils.Deferrable<ethers.providers.TransactionRequest>
|
32 | ): Promise<ethers.providers.TransactionResponse> {
|
33 | return this._signer.sendTransaction(transaction);
|
34 | }
|
35 |
|
36 | public connect(provider: ethers.providers.Provider): SignerWithAddress {
|
37 | return new SignerWithAddress(this.address, this._signer.connect(provider));
|
38 | }
|
39 |
|
40 | public _signTypedData(
|
41 | ...params: Parameters<ethers.providers.JsonRpcSigner["_signTypedData"]>
|
42 | ): Promise<string> {
|
43 | return this._signer._signTypedData(...params);
|
44 | }
|
45 |
|
46 | public toJSON() {
|
47 | return `<SignerWithAddress ${this.address}>`;
|
48 | }
|
49 | }
|