-
Notifications
You must be signed in to change notification settings - Fork 240
/
LocalWallet.ts
44 lines (38 loc) · 1023 Bytes
/
LocalWallet.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
import {
LocalWallet as LocalWalletCore,
Connector,
} from "@thirdweb-dev/wallets";
import { ethers, utils } from "ethers";
import { LOCAL_WALLET_ICON } from "../../assets/svgs";
export class LocalWallet extends LocalWalletCore {
static meta = {
name: "Guest Wallet",
iconURL: LOCAL_WALLET_ICON,
};
getMeta() {
return LocalWallet.meta;
}
async generate() {
if (this.ethersWallet) {
throw new Error("wallet is already initialized");
}
const random = utils.randomBytes(32);
this.ethersWallet = new ethers.Wallet(random);
return this.ethersWallet.address;
}
protected async getConnector(): Promise<Connector> {
if (!this.ethersWallet) {
const data = await this.getSavedData();
if (!data) {
await this.generate();
await this.save({
strategy: "privateKey",
encryption: false,
});
} else {
this.ethersWallet = new ethers.Wallet(data.data);
}
}
return super.getConnector();
}
}