-
Notifications
You must be signed in to change notification settings - Fork 236
/
signer.ts
74 lines (64 loc) · 2.01 KB
/
signer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { AsyncStorage, createAsyncLocalStorage } from "../../core";
import { Connector } from "../interfaces/connector";
import { walletIds } from "../constants/walletIds";
import { AbstractClientWallet, WalletOptions } from "./base";
import {
Chain,
defaultChains,
Ethereum,
updateChainRPCs,
} from "@thirdweb-dev/chains";
import { Signer } from "ethers";
export type SignerWalletAdditionalOptions = {
chain?: Chain;
storage?: AsyncStorage;
secretKey?: string;
signer: Signer;
};
// eslint-disable-next-line @typescript-eslint/ban-types
export type SignerWalletConnectionArgs = {};
/**
* @internal
*/
export class SignerWallet extends AbstractClientWallet<
SignerWalletAdditionalOptions,
SignerWalletConnectionArgs
> {
connector?: Connector;
options: WalletOptions<SignerWalletAdditionalOptions>;
signer?: Signer;
private _storage: AsyncStorage;
constructor(options: WalletOptions<SignerWalletAdditionalOptions>) {
super("signerWallet", options);
if (options.clientId && options.chain) {
options.chain = updateChainRPCs(options.chain, options.clientId);
}
this.options = options;
this.signer = options.signer;
this._storage =
options?.storage || createAsyncLocalStorage(walletIds.localWallet);
}
protected async getConnector(): Promise<Connector> {
if (!this.connector) {
const { SignerConnector } = await import("../connectors/signer/index");
if (!this.signer) {
this.signer = this.options.signer;
}
const defaults = (
this.options.chain
? [...defaultChains, this.options.chain]
: defaultChains
).map((c) => updateChainRPCs(c, this.options.clientId));
this.connector = new SignerConnector({
chain:
this.options.chain ||
updateChainRPCs(Ethereum, this.options.clientId),
signer: this.signer,
chains: this.chains || defaults,
clientId: this.options.clientId,
secretKey: this.options.secretKey,
});
}
return this.connector;
}
}