@interchain-kit/store 0.3.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/esm/index.js +3 -0
- package/esm/proxied-wallets/cosmos-wallet.js +51 -0
- package/esm/proxied-wallets/create-proxied-wallet.js +30 -0
- package/esm/proxied-wallets/ethereum-wallet.js +24 -0
- package/esm/proxied-wallets/index.js +5 -0
- package/esm/proxied-wallets/multi-chain-wallet.js +27 -0
- package/esm/store/index.js +125 -0
- package/esm/types/index.js +1 -0
- package/esm/types/store.js +1 -0
- package/esm/utils/aop.js +172 -0
- package/esm/utils/index.js +1 -0
- package/esm/utils/local-storage.js +9 -0
- package/esm/wallet-manager/chain-wallet-store.js +111 -0
- package/esm/wallet-manager/index.js +3 -0
- package/esm/wallet-manager/wallet-manager-store.js +230 -0
- package/esm/wallet-manager/wallet-store.js +99 -0
- package/index.d.ts +3 -0
- package/index.js +19 -0
- package/package.json +35 -0
- package/proxied-wallets/cosmos-wallet.d.ts +3 -0
- package/proxied-wallets/cosmos-wallet.js +55 -0
- package/proxied-wallets/create-proxied-wallet.d.ts +2 -0
- package/proxied-wallets/create-proxied-wallet.js +34 -0
- package/proxied-wallets/ethereum-wallet.d.ts +3 -0
- package/proxied-wallets/ethereum-wallet.js +28 -0
- package/proxied-wallets/index.d.ts +4 -0
- package/proxied-wallets/index.js +21 -0
- package/proxied-wallets/multi-chain-wallet.d.ts +3 -0
- package/proxied-wallets/multi-chain-wallet.js +31 -0
- package/store/index.d.ts +21 -0
- package/store/index.js +129 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +17 -0
- package/types/store.d.ts +17 -0
- package/types/store.js +2 -0
- package/utils/aop.d.ts +58 -0
- package/utils/aop.js +178 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +17 -0
- package/utils/local-storage.d.ts +5 -0
- package/utils/local-storage.js +13 -0
- package/wallet-manager/chain-wallet-store.d.ts +22 -0
- package/wallet-manager/chain-wallet-store.js +115 -0
- package/wallet-manager/index.d.ts +3 -0
- package/wallet-manager/index.js +19 -0
- package/wallet-manager/wallet-manager-store.d.ts +58 -0
- package/wallet-manager/wallet-manager-store.js +234 -0
- package/wallet-manager/wallet-store.d.ts +21 -0
- package/wallet-manager/wallet-store.js +103 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { AssetList, Chain } from '@chain-registry/types';
|
|
2
|
+
import { BaseWallet, ChainName, CosmosSigningOptions, DeviceType, DownloadInfo, EndpointOptions, Endpoints, OS, SignerOptions, SignType, WalletAccount, WalletManager } from '@interchain-kit/core';
|
|
3
|
+
import { ISigningClient } from '@interchainjs/cosmos';
|
|
4
|
+
import { HttpEndpoint } from '@interchainjs/types';
|
|
5
|
+
import { InterchainStore } from '../store';
|
|
6
|
+
import { ChainWalletState, InterchainStoreType } from '../types';
|
|
7
|
+
import { LocalStorage } from '../utils/local-storage';
|
|
8
|
+
import { WalletStore } from './wallet-store';
|
|
9
|
+
export declare class WalletManagerStore implements WalletManager {
|
|
10
|
+
wallets: WalletStore[];
|
|
11
|
+
store: InterchainStore;
|
|
12
|
+
walletManager: WalletManager;
|
|
13
|
+
chains: Chain[];
|
|
14
|
+
assetLists: AssetList[];
|
|
15
|
+
localStorage: LocalStorage;
|
|
16
|
+
constructor(chains: Chain[], assetLists: AssetList[], wallets: BaseWallet[], signerOptions?: SignerOptions, endpointOptions?: EndpointOptions);
|
|
17
|
+
init(): Promise<void>;
|
|
18
|
+
restore(): void;
|
|
19
|
+
subscribe(callback: (state: InterchainStoreType) => void): () => void;
|
|
20
|
+
getState(): InterchainStoreType;
|
|
21
|
+
get walletConnectQRCodeUri(): string;
|
|
22
|
+
get isReady(): boolean;
|
|
23
|
+
get currentWalletName(): string;
|
|
24
|
+
get currentChainName(): string;
|
|
25
|
+
setCurrentWalletName(walletName: string): void;
|
|
26
|
+
setCurrentChainName(chainName: string): void;
|
|
27
|
+
getChainById(chainId: string): Chain | undefined;
|
|
28
|
+
getChainWalletState(walletName: string, chainName: string): ChainWalletState | undefined;
|
|
29
|
+
updateChainWalletState(walletName: string, chainName: string, data: Partial<ChainWalletState>): void;
|
|
30
|
+
getChainWalletByName(walletName: string, chainName: Chain['chainName']): import("./chain-wallet-store").ChainWalletStore;
|
|
31
|
+
get modalIsOpen(): boolean;
|
|
32
|
+
openModal(): void;
|
|
33
|
+
closeModal(): void;
|
|
34
|
+
get signerOptions(): SignerOptions;
|
|
35
|
+
get endpointOptions(): EndpointOptions;
|
|
36
|
+
get preferredSignTypeMap(): Record<string, SignType>;
|
|
37
|
+
get signerOptionMap(): Record<string, CosmosSigningOptions>;
|
|
38
|
+
get endpointOptionsMap(): Record<string, Endpoints>;
|
|
39
|
+
addChains(newChains: Chain[], newAssetLists: AssetList[], signerOptions?: SignerOptions, newEndpointOptions?: EndpointOptions): Promise<void>;
|
|
40
|
+
getChainLogoUrl(chainName: ChainName): string;
|
|
41
|
+
getWalletByName(walletName: string): WalletStore | undefined;
|
|
42
|
+
getChainByName(chainName: string): Chain | undefined;
|
|
43
|
+
getAssetListByName(chainName: string): AssetList;
|
|
44
|
+
connect(walletName: string, chainName: string): Promise<void>;
|
|
45
|
+
disconnect(walletName: string, chainName: string): Promise<void>;
|
|
46
|
+
getAccount(walletName: string, chainName: string): Promise<WalletAccount>;
|
|
47
|
+
getRpcEndpoint(walletName: string, chainName: string): Promise<string | HttpEndpoint>;
|
|
48
|
+
getPreferSignType(chainName: string): SignType;
|
|
49
|
+
getSignerOptions(chainName: string): CosmosSigningOptions;
|
|
50
|
+
getOfflineSigner(walletName: string, chainName: string): Promise<any>;
|
|
51
|
+
getSigningClient(walletName: string, chainName: ChainName): Promise<ISigningClient>;
|
|
52
|
+
getEnv(): {
|
|
53
|
+
browser: string;
|
|
54
|
+
device: DeviceType;
|
|
55
|
+
os: OS;
|
|
56
|
+
};
|
|
57
|
+
getDownloadLink(walletName: string): DownloadInfo;
|
|
58
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WalletManagerStore = void 0;
|
|
4
|
+
const core_1 = require("@interchain-kit/core");
|
|
5
|
+
const cosmos_1 = require("@interchainjs/cosmos");
|
|
6
|
+
const interchainjs_1 = require("interchainjs");
|
|
7
|
+
const proxied_wallets_1 = require("../proxied-wallets");
|
|
8
|
+
const store_1 = require("../store");
|
|
9
|
+
const local_storage_1 = require("../utils/local-storage");
|
|
10
|
+
const wallet_store_1 = require("./wallet-store");
|
|
11
|
+
class WalletManagerStore {
|
|
12
|
+
wallets;
|
|
13
|
+
store;
|
|
14
|
+
walletManager;
|
|
15
|
+
chains;
|
|
16
|
+
assetLists;
|
|
17
|
+
localStorage;
|
|
18
|
+
constructor(chains, assetLists, wallets, signerOptions, endpointOptions) {
|
|
19
|
+
this.localStorage = new local_storage_1.LocalStorage();
|
|
20
|
+
this.store = new store_1.InterchainStore();
|
|
21
|
+
const proxiedWallets = wallets.map(wallet => {
|
|
22
|
+
wallet.setChainMap(chains);
|
|
23
|
+
wallet.setAssetLists(assetLists);
|
|
24
|
+
return (0, proxied_wallets_1.createProxiedWallet)(wallet, this.store);
|
|
25
|
+
});
|
|
26
|
+
this.walletManager = new core_1.WalletManager(chains, assetLists, proxiedWallets, signerOptions, endpointOptions);
|
|
27
|
+
this.chains = chains;
|
|
28
|
+
this.assetLists = assetLists;
|
|
29
|
+
this.wallets = proxiedWallets.map(proxiedWallet => new wallet_store_1.WalletStore(proxiedWallet, chains, this.store, this.walletManager));
|
|
30
|
+
}
|
|
31
|
+
async init() {
|
|
32
|
+
try {
|
|
33
|
+
// 先恢复数据,再设置订阅
|
|
34
|
+
this.restore();
|
|
35
|
+
this.store.subscribe((state) => {
|
|
36
|
+
this.localStorage.save({
|
|
37
|
+
chainWalletStates: state.chainWalletStates,
|
|
38
|
+
currentWalletName: state.currentWalletName,
|
|
39
|
+
currentChainName: state.currentChainName,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
await Promise.all(this.wallets.map(wallet => {
|
|
43
|
+
return wallet.init();
|
|
44
|
+
}));
|
|
45
|
+
this.store.setState({ isReady: true });
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.log(error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
restore() {
|
|
53
|
+
const oldState = this.localStorage.load();
|
|
54
|
+
// 直接设置state,避免触发emit
|
|
55
|
+
this.store.setState({
|
|
56
|
+
chainWalletStates: oldState.chainWalletStates || [],
|
|
57
|
+
currentWalletName: oldState.currentWalletName || '',
|
|
58
|
+
currentChainName: oldState.currentChainName || '',
|
|
59
|
+
});
|
|
60
|
+
// 重建索引映射
|
|
61
|
+
this.store.buildIndexMap();
|
|
62
|
+
// 获取当前有效的 wallet 和 chain 名称集合
|
|
63
|
+
const validWalletNames = new Set(this.wallets.map(wallet => wallet.info.name));
|
|
64
|
+
const validChainNames = new Set(this.chains.map(chain => chain.chainName));
|
|
65
|
+
// 1. 移除不需要的 chain wallet state(wallet 或 chain 不再存在)
|
|
66
|
+
const filteredChainWalletStates = this.store.getState().chainWalletStates.filter(state => {
|
|
67
|
+
return validWalletNames.has(state.walletName) && validChainNames.has(state.chainName);
|
|
68
|
+
});
|
|
69
|
+
// 2. 新增原本没有的 chain wallet state
|
|
70
|
+
const newChainWalletStates = [];
|
|
71
|
+
this.wallets.forEach(wallet => {
|
|
72
|
+
this.chains.forEach(chain => {
|
|
73
|
+
if (!this.store.isChainWalletStateExisted(wallet.info.name, chain.chainName)) {
|
|
74
|
+
newChainWalletStates.push({
|
|
75
|
+
walletName: wallet.info.name,
|
|
76
|
+
chainName: chain.chainName,
|
|
77
|
+
rpcEndpoint: '',
|
|
78
|
+
walletState: core_1.WalletState.Disconnected,
|
|
79
|
+
account: undefined,
|
|
80
|
+
errorMessage: ''
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
// 合并过滤后的状态和新增的状态
|
|
86
|
+
const finalChainWalletStates = [...filteredChainWalletStates, ...newChainWalletStates];
|
|
87
|
+
this.store.setState({ chainWalletStates: finalChainWalletStates });
|
|
88
|
+
this.store.buildIndexMap();
|
|
89
|
+
}
|
|
90
|
+
subscribe(callback) {
|
|
91
|
+
return this.store.subscribe(callback);
|
|
92
|
+
}
|
|
93
|
+
getState() {
|
|
94
|
+
return this.store.getState();
|
|
95
|
+
}
|
|
96
|
+
get walletConnectQRCodeUri() {
|
|
97
|
+
return this.store.getState().walletConnectQRCodeUri;
|
|
98
|
+
}
|
|
99
|
+
get isReady() {
|
|
100
|
+
return this.store.getState().isReady;
|
|
101
|
+
}
|
|
102
|
+
get currentWalletName() {
|
|
103
|
+
return this.store.getState().currentWalletName;
|
|
104
|
+
}
|
|
105
|
+
get currentChainName() {
|
|
106
|
+
return this.store.getState().currentChainName;
|
|
107
|
+
}
|
|
108
|
+
setCurrentWalletName(walletName) {
|
|
109
|
+
this.store.setCurrentWalletName(walletName);
|
|
110
|
+
}
|
|
111
|
+
setCurrentChainName(chainName) {
|
|
112
|
+
this.store.setCurrentChainName(chainName);
|
|
113
|
+
}
|
|
114
|
+
getChainById(chainId) {
|
|
115
|
+
return this.walletManager.getChainById(chainId);
|
|
116
|
+
}
|
|
117
|
+
getChainWalletState(walletName, chainName) {
|
|
118
|
+
return this.store.getChainWalletState(walletName, chainName);
|
|
119
|
+
}
|
|
120
|
+
updateChainWalletState(walletName, chainName, data) {
|
|
121
|
+
this.store.updateChainWalletState(walletName, chainName, data);
|
|
122
|
+
}
|
|
123
|
+
getChainWalletByName(walletName, chainName) {
|
|
124
|
+
const walletStore = this.wallets.find(w => w.info.name === walletName);
|
|
125
|
+
return walletStore?.getChainWalletStore(chainName);
|
|
126
|
+
}
|
|
127
|
+
get modalIsOpen() {
|
|
128
|
+
return this.store.getState().modalIsOpen;
|
|
129
|
+
}
|
|
130
|
+
openModal() {
|
|
131
|
+
this.store.setState({ modalIsOpen: true });
|
|
132
|
+
}
|
|
133
|
+
closeModal() {
|
|
134
|
+
this.store.setState({ modalIsOpen: false });
|
|
135
|
+
}
|
|
136
|
+
get signerOptions() {
|
|
137
|
+
return this.walletManager.signerOptions;
|
|
138
|
+
}
|
|
139
|
+
get endpointOptions() {
|
|
140
|
+
return this.walletManager.endpointOptions;
|
|
141
|
+
}
|
|
142
|
+
get preferredSignTypeMap() {
|
|
143
|
+
return this.walletManager.preferredSignTypeMap;
|
|
144
|
+
}
|
|
145
|
+
get signerOptionMap() {
|
|
146
|
+
return this.walletManager.signerOptionMap;
|
|
147
|
+
}
|
|
148
|
+
get endpointOptionsMap() {
|
|
149
|
+
return this.walletManager.endpointOptionsMap;
|
|
150
|
+
}
|
|
151
|
+
addChains(newChains, newAssetLists, signerOptions, newEndpointOptions) {
|
|
152
|
+
return this.walletManager.addChains(newChains, newAssetLists, signerOptions, newEndpointOptions);
|
|
153
|
+
}
|
|
154
|
+
getChainLogoUrl(chainName) {
|
|
155
|
+
return this.walletManager.getChainLogoUrl(chainName);
|
|
156
|
+
}
|
|
157
|
+
getWalletByName(walletName) {
|
|
158
|
+
return this.wallets.find(w => w.info.name === walletName);
|
|
159
|
+
}
|
|
160
|
+
getChainByName(chainName) {
|
|
161
|
+
return this.walletManager.getChainByName(chainName);
|
|
162
|
+
}
|
|
163
|
+
getAssetListByName(chainName) {
|
|
164
|
+
return this.walletManager.getAssetListByName(chainName);
|
|
165
|
+
}
|
|
166
|
+
connect(walletName, chainName) {
|
|
167
|
+
const chainWallet = this.getWalletByName(walletName).getChainWalletStore(chainName);
|
|
168
|
+
return chainWallet.connect();
|
|
169
|
+
}
|
|
170
|
+
disconnect(walletName, chainName) {
|
|
171
|
+
const chainWallet = this.getWalletByName(walletName).getChainWalletStore(chainName);
|
|
172
|
+
return chainWallet.disconnect();
|
|
173
|
+
}
|
|
174
|
+
getAccount(walletName, chainName) {
|
|
175
|
+
const chainWallet = this.getWalletByName(walletName).getChainWalletStore(chainName);
|
|
176
|
+
return chainWallet.getAccount();
|
|
177
|
+
}
|
|
178
|
+
async getRpcEndpoint(walletName, chainName) {
|
|
179
|
+
const existed = this.getChainWalletState(walletName, chainName)?.rpcEndpoint;
|
|
180
|
+
if (existed) {
|
|
181
|
+
return existed;
|
|
182
|
+
}
|
|
183
|
+
const rpcEndpoint = await this.walletManager.getRpcEndpoint(walletName, chainName);
|
|
184
|
+
this.updateChainWalletState(walletName, chainName, { rpcEndpoint });
|
|
185
|
+
return rpcEndpoint;
|
|
186
|
+
}
|
|
187
|
+
getPreferSignType(chainName) {
|
|
188
|
+
return this.walletManager.getPreferSignType(chainName);
|
|
189
|
+
}
|
|
190
|
+
getSignerOptions(chainName) {
|
|
191
|
+
return this.walletManager.getSignerOptions(chainName);
|
|
192
|
+
}
|
|
193
|
+
getOfflineSigner(walletName, chainName) {
|
|
194
|
+
return this.getWalletByName(walletName).getChainWalletStore(chainName).getOfflineSigner();
|
|
195
|
+
}
|
|
196
|
+
async getSigningClient(walletName, chainName) {
|
|
197
|
+
try {
|
|
198
|
+
const chain = this.getChainByName(chainName);
|
|
199
|
+
const rpcEndpoint = await this.getRpcEndpoint(walletName, chainName);
|
|
200
|
+
const preferredSignType = this.getPreferSignType(chainName);
|
|
201
|
+
const offlineSigner = (await this.getOfflineSigner(walletName, chainName));
|
|
202
|
+
const defaultSignerOptions = await this.getSignerOptions(chainName);
|
|
203
|
+
const signerOptions = {
|
|
204
|
+
queryClient: await (0, cosmos_1.createCosmosQueryClient)(rpcEndpoint),
|
|
205
|
+
addressPrefix: chain.bech32Prefix,
|
|
206
|
+
chainId: chain.chainId,
|
|
207
|
+
...defaultSignerOptions,
|
|
208
|
+
};
|
|
209
|
+
if (preferredSignType === 'direct') {
|
|
210
|
+
return (0, interchainjs_1.getSigner)(offlineSigner, {
|
|
211
|
+
preferredSignType: 'direct',
|
|
212
|
+
signerOptions,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
return (0, interchainjs_1.getSigner)(offlineSigner, {
|
|
217
|
+
preferredSignType: 'amino',
|
|
218
|
+
signerOptions,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
console.log(error);
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
getEnv() {
|
|
228
|
+
return this.walletManager.getEnv();
|
|
229
|
+
}
|
|
230
|
+
getDownloadLink(walletName) {
|
|
231
|
+
return this.walletManager.getDownloadLink(walletName);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.WalletManagerStore = WalletManagerStore;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Chain } from '@chain-registry/types';
|
|
2
|
+
import { BaseWallet, WalletAccount, WalletManager, WalletState } from '@interchain-kit/core';
|
|
3
|
+
import { InterchainStore } from '../store';
|
|
4
|
+
import { ChainWalletStore } from './chain-wallet-store';
|
|
5
|
+
export declare class WalletStore extends BaseWallet {
|
|
6
|
+
wallet: BaseWallet;
|
|
7
|
+
chains: Chain[];
|
|
8
|
+
chainWalletStoreMap: Map<Chain['chainName'], ChainWalletStore>;
|
|
9
|
+
store: InterchainStore;
|
|
10
|
+
walletManager: WalletManager;
|
|
11
|
+
constructor(wallet: BaseWallet, chains: Chain[], store: InterchainStore, walletManager: WalletManager);
|
|
12
|
+
get walletState(): WalletState;
|
|
13
|
+
get errorMessage(): string;
|
|
14
|
+
init(): Promise<void>;
|
|
15
|
+
getChainWalletStore(chainName: Chain['chainName']): ChainWalletStore;
|
|
16
|
+
connect(chainId: Chain['chainId']): Promise<void>;
|
|
17
|
+
disconnect(chainId: Chain['chainId']): Promise<void>;
|
|
18
|
+
getAccount(chainId: Chain['chainId']): Promise<WalletAccount>;
|
|
19
|
+
addSuggestChain(chainId: Chain['chainId']): Promise<void>;
|
|
20
|
+
getProvider(chainId: Chain['chainId']): Promise<any>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WalletStore = void 0;
|
|
4
|
+
const core_1 = require("@interchain-kit/core");
|
|
5
|
+
const chain_wallet_store_1 = require("./chain-wallet-store");
|
|
6
|
+
class WalletStore extends core_1.BaseWallet {
|
|
7
|
+
wallet;
|
|
8
|
+
chains;
|
|
9
|
+
chainWalletStoreMap = new Map();
|
|
10
|
+
store;
|
|
11
|
+
walletManager;
|
|
12
|
+
constructor(wallet, chains, store, walletManager) {
|
|
13
|
+
super(wallet.info);
|
|
14
|
+
this.wallet = wallet;
|
|
15
|
+
this.chains = chains;
|
|
16
|
+
chains.forEach(chain => {
|
|
17
|
+
this.chainWalletStoreMap.set(chain.chainName, new chain_wallet_store_1.ChainWalletStore(this.wallet, chain, store, walletManager));
|
|
18
|
+
});
|
|
19
|
+
this.store = store;
|
|
20
|
+
this.walletManager = walletManager;
|
|
21
|
+
}
|
|
22
|
+
get walletState() {
|
|
23
|
+
const chainWalletStates = [];
|
|
24
|
+
this.store.getState().chainWalletStates.forEach(cws => {
|
|
25
|
+
if (cws.walletName === this.info.name) {
|
|
26
|
+
chainWalletStates.push(cws.walletState);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (chainWalletStates.some(ws => ws === core_1.WalletState.NotExist)) {
|
|
30
|
+
return core_1.WalletState.NotExist;
|
|
31
|
+
}
|
|
32
|
+
if (chainWalletStates.some(ws => ws === core_1.WalletState.Connected)) {
|
|
33
|
+
return core_1.WalletState.Connected;
|
|
34
|
+
}
|
|
35
|
+
if (chainWalletStates.some(ws => ws === core_1.WalletState.Connecting)) {
|
|
36
|
+
return core_1.WalletState.Connecting;
|
|
37
|
+
}
|
|
38
|
+
return core_1.WalletState.Disconnected;
|
|
39
|
+
}
|
|
40
|
+
get errorMessage() {
|
|
41
|
+
let errorMessage = '';
|
|
42
|
+
for (const cws of this.store.getState().chainWalletStates) {
|
|
43
|
+
if (cws.walletName === this.info.name) {
|
|
44
|
+
errorMessage = cws.errorMessage;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return errorMessage;
|
|
49
|
+
}
|
|
50
|
+
async init() {
|
|
51
|
+
try {
|
|
52
|
+
const chainWalletStores = Array.from(this.chainWalletStoreMap.values());
|
|
53
|
+
await Promise.all(chainWalletStores.map(chainWalletStore => chainWalletStore.init()));
|
|
54
|
+
await this.wallet.init();
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
this.store.updateWalletState(this.wallet.info.name, { walletState: core_1.WalletState.NotExist, errorMessage: '' });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
getChainWalletStore(chainName) {
|
|
61
|
+
const chainWalletStore = this.chainWalletStoreMap.get(chainName);
|
|
62
|
+
if (!chainWalletStore) {
|
|
63
|
+
throw new Error(`Chain wallet store with chain name ${chainName} not found`);
|
|
64
|
+
}
|
|
65
|
+
return chainWalletStore;
|
|
66
|
+
}
|
|
67
|
+
async connect(chainId) {
|
|
68
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
69
|
+
if (!chain) {
|
|
70
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
71
|
+
}
|
|
72
|
+
return this.getChainWalletStore(chain.chainName).connect();
|
|
73
|
+
}
|
|
74
|
+
async disconnect(chainId) {
|
|
75
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
76
|
+
if (!chain) {
|
|
77
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
78
|
+
}
|
|
79
|
+
return this.getChainWalletStore(chain.chainName).disconnect();
|
|
80
|
+
}
|
|
81
|
+
async getAccount(chainId) {
|
|
82
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
83
|
+
if (!chain) {
|
|
84
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
85
|
+
}
|
|
86
|
+
return this.getChainWalletStore(chain.chainName).getAccount();
|
|
87
|
+
}
|
|
88
|
+
async addSuggestChain(chainId) {
|
|
89
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
90
|
+
if (!chain) {
|
|
91
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
92
|
+
}
|
|
93
|
+
return this.getChainWalletStore(chain.chainName).addSuggestChain();
|
|
94
|
+
}
|
|
95
|
+
async getProvider(chainId) {
|
|
96
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
97
|
+
if (!chain) {
|
|
98
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
99
|
+
}
|
|
100
|
+
return this.getChainWalletStore(chain.chainName).getProvider();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.WalletStore = WalletStore;
|