-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPolkadotWallet.ts
86 lines (77 loc) · 2.49 KB
/
PolkadotWallet.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
75
76
77
78
79
80
81
82
83
84
85
86
import { InjectedAccountWithMeta } from "@polkadot/extension-inject/types";
import { Polkadot } from "@unique-nft/utils/extension";
import { Address } from "@unique-nft/utils";
import {
BaseWalletEntity,
BaseWalletType,
SignerTypeEnum,
} from "./types";
/**
* Represents the names of the supported Polkadot-based wallets.
*/
export type PolkadotWalletName =
| "polkadot-js"
| "subwallet-js"
| "talisman"
| "enkrypt"
| "novawallet";
/**
* Class representing a Polkadot wallet integration.
*
* @implements {BaseWalletEntity<InjectedAccountWithMeta>}
*/
export class PolkadotWallet
implements BaseWalletEntity<InjectedAccountWithMeta>
{
/**
* A map that holds the accounts associated with this wallet.
*
* @private
*/
_accounts = new Map<string, BaseWalletType<InjectedAccountWithMeta>>();
/**
* The name of the wallet being used.
*
* @type {PolkadotWalletName}
*/
wallet: PolkadotWalletName;
constructor(defaultWallet: PolkadotWalletName = "polkadot-js") {
this.wallet = defaultWallet;
}
/**
* Loads and returns the accounts associated with the connected wallet.
*
* @throws Will throw an error if the account processing fails.
*/
async getAccounts() {
const wallets = await Polkadot.loadWalletByName(this.wallet);
const accountEntries = wallets.accounts
.filter(({ address }) => Address.is.substrateAddress(address))
.map((account) => {
if (!account.address) return null;
try {
const normalizedAddress = Address.normalize.substrateAddress(account.address);
const address = Address.normalize.substrateAddress(account.address, 7391);
return [
account.address,
{
name: account.meta.name || "",
normalizedAddress,
address,
walletType: this.wallet,
walletMetaInformation: account,
signerType: SignerTypeEnum.Polkadot,
sign: account.signer.sign,
signer: { ...account.signer, address },
} as BaseWalletType<InjectedAccountWithMeta>,
] as [string, BaseWalletType<InjectedAccountWithMeta>];
} catch (error) {
console.error(`Failed to process account ${account.address}:`, error);
return null;
}
})
.filter((entry): entry is [string, BaseWalletType<InjectedAccountWithMeta>] => entry !== null); // Ensure no null values
this._accounts = new Map(accountEntries);
return this._accounts;
}
}