@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,111 @@
|
|
|
1
|
+
import { BaseWallet, CosmosWallet, getWalletByType, isInstanceOf, WalletState, WCWallet } from '@interchain-kit/core';
|
|
2
|
+
export class ChainWalletStore extends BaseWallet {
|
|
3
|
+
networkWalletMap;
|
|
4
|
+
wallet;
|
|
5
|
+
chain;
|
|
6
|
+
store;
|
|
7
|
+
walletManager;
|
|
8
|
+
constructor(wallet, chain, store, walletManager) {
|
|
9
|
+
super(wallet.info);
|
|
10
|
+
this.wallet = wallet;
|
|
11
|
+
this.chain = chain;
|
|
12
|
+
this.store = store;
|
|
13
|
+
this.walletManager = walletManager;
|
|
14
|
+
}
|
|
15
|
+
get walletState() {
|
|
16
|
+
return this.store.getChainWalletState(this.chain.chainName, this.wallet.info.name)?.walletState || WalletState.NotExist;
|
|
17
|
+
}
|
|
18
|
+
get errorMessage() {
|
|
19
|
+
return this.store.getChainWalletState(this.chain.chainName, this.wallet.info.name)?.errorMessage || '';
|
|
20
|
+
}
|
|
21
|
+
async init() {
|
|
22
|
+
this.wallet.events.on('accountChanged', () => {
|
|
23
|
+
this.refreshAccount();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async connect() {
|
|
27
|
+
const chainWalletState = this.store.getChainWalletState(this.wallet.info.name, this.chain.chainName);
|
|
28
|
+
if (chainWalletState && chainWalletState.walletState === WalletState.NotExist) {
|
|
29
|
+
return Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
if (isInstanceOf(this.wallet, WCWallet)) {
|
|
32
|
+
this.wallet.setOnPairingUriCreatedCallback((uri) => {
|
|
33
|
+
console.log('Callback called with uri:', uri);
|
|
34
|
+
this.store.setState({ walletConnectQRCodeUri: uri });
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Connecting });
|
|
39
|
+
await this.wallet.connect(this.chain.chainId);
|
|
40
|
+
const account = await this.getAccount();
|
|
41
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Connected, account });
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Disconnected, errorMessage: error.message });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async disconnect() {
|
|
48
|
+
if (isInstanceOf(this.wallet, WCWallet)) {
|
|
49
|
+
this.wallet.events.on('disconnect', () => {
|
|
50
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Disconnected, account: undefined });
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
await this.wallet.disconnect(this.chain.chainId);
|
|
55
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Disconnected, account: undefined, errorMessage: '' });
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
console.error(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async refreshAccount() {
|
|
62
|
+
try {
|
|
63
|
+
const account = await this.wallet.getAccount(this.chain.chainId);
|
|
64
|
+
console.log(this.wallet);
|
|
65
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { account });
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Disconnected, account: undefined, errorMessage: error.message });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async getAccount() {
|
|
72
|
+
const existed = this.store.getChainWalletState(this.wallet.info.name, this.chain.chainName)?.account;
|
|
73
|
+
if (existed) {
|
|
74
|
+
return existed;
|
|
75
|
+
}
|
|
76
|
+
const account = await this.wallet.getAccount(this.chain.chainId);
|
|
77
|
+
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { account });
|
|
78
|
+
return account;
|
|
79
|
+
}
|
|
80
|
+
addSuggestChain() {
|
|
81
|
+
return this.wallet.addSuggestChain(this.chain.chainId);
|
|
82
|
+
}
|
|
83
|
+
getProvider() {
|
|
84
|
+
return this.wallet.getProvider(this.chain.chainId);
|
|
85
|
+
}
|
|
86
|
+
async getOfflineSigner(preferSignType) {
|
|
87
|
+
const cosmosWallet = getWalletByType(this.wallet, CosmosWallet);
|
|
88
|
+
const preferredSignTypeFromSettings = this.walletManager.getPreferSignType(this.chain.chainName);
|
|
89
|
+
const account = await this.getAccount();
|
|
90
|
+
const aminoOfflineSigner = {
|
|
91
|
+
getAccounts: async () => [account],
|
|
92
|
+
signAmino: async (signer, signDoc) => {
|
|
93
|
+
return cosmosWallet.signAmino(this.chain.chainId, signer, signDoc, {});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const directOfflineSigner = {
|
|
97
|
+
getAccounts: async () => [account],
|
|
98
|
+
signDirect: async (signer, signDoc) => {
|
|
99
|
+
return cosmosWallet.signDirect(this.chain.chainId, signer, signDoc, {});
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const signType = preferSignType || preferredSignTypeFromSettings;
|
|
103
|
+
if (account.isNanoLedger || signType === 'amino') {
|
|
104
|
+
return aminoOfflineSigner;
|
|
105
|
+
}
|
|
106
|
+
return directOfflineSigner;
|
|
107
|
+
}
|
|
108
|
+
getWalletOfType(WalletClass) {
|
|
109
|
+
return getWalletByType(this.wallet, WalletClass);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { WalletManager, WalletState } from '@interchain-kit/core';
|
|
2
|
+
import { createCosmosQueryClient } from '@interchainjs/cosmos';
|
|
3
|
+
import { getSigner } from 'interchainjs';
|
|
4
|
+
import { createProxiedWallet } from '../proxied-wallets';
|
|
5
|
+
import { InterchainStore } from '../store';
|
|
6
|
+
import { LocalStorage } from '../utils/local-storage';
|
|
7
|
+
import { WalletStore } from './wallet-store';
|
|
8
|
+
export class WalletManagerStore {
|
|
9
|
+
wallets;
|
|
10
|
+
store;
|
|
11
|
+
walletManager;
|
|
12
|
+
chains;
|
|
13
|
+
assetLists;
|
|
14
|
+
localStorage;
|
|
15
|
+
constructor(chains, assetLists, wallets, signerOptions, endpointOptions) {
|
|
16
|
+
this.localStorage = new LocalStorage();
|
|
17
|
+
this.store = new InterchainStore();
|
|
18
|
+
const proxiedWallets = wallets.map(wallet => {
|
|
19
|
+
wallet.setChainMap(chains);
|
|
20
|
+
wallet.setAssetLists(assetLists);
|
|
21
|
+
return createProxiedWallet(wallet, this.store);
|
|
22
|
+
});
|
|
23
|
+
this.walletManager = new WalletManager(chains, assetLists, proxiedWallets, signerOptions, endpointOptions);
|
|
24
|
+
this.chains = chains;
|
|
25
|
+
this.assetLists = assetLists;
|
|
26
|
+
this.wallets = proxiedWallets.map(proxiedWallet => new WalletStore(proxiedWallet, chains, this.store, this.walletManager));
|
|
27
|
+
}
|
|
28
|
+
async init() {
|
|
29
|
+
try {
|
|
30
|
+
// 先恢复数据,再设置订阅
|
|
31
|
+
this.restore();
|
|
32
|
+
this.store.subscribe((state) => {
|
|
33
|
+
this.localStorage.save({
|
|
34
|
+
chainWalletStates: state.chainWalletStates,
|
|
35
|
+
currentWalletName: state.currentWalletName,
|
|
36
|
+
currentChainName: state.currentChainName,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
await Promise.all(this.wallets.map(wallet => {
|
|
40
|
+
return wallet.init();
|
|
41
|
+
}));
|
|
42
|
+
this.store.setState({ isReady: true });
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.log(error);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
restore() {
|
|
50
|
+
const oldState = this.localStorage.load();
|
|
51
|
+
// 直接设置state,避免触发emit
|
|
52
|
+
this.store.setState({
|
|
53
|
+
chainWalletStates: oldState.chainWalletStates || [],
|
|
54
|
+
currentWalletName: oldState.currentWalletName || '',
|
|
55
|
+
currentChainName: oldState.currentChainName || '',
|
|
56
|
+
});
|
|
57
|
+
// 重建索引映射
|
|
58
|
+
this.store.buildIndexMap();
|
|
59
|
+
// 获取当前有效的 wallet 和 chain 名称集合
|
|
60
|
+
const validWalletNames = new Set(this.wallets.map(wallet => wallet.info.name));
|
|
61
|
+
const validChainNames = new Set(this.chains.map(chain => chain.chainName));
|
|
62
|
+
// 1. 移除不需要的 chain wallet state(wallet 或 chain 不再存在)
|
|
63
|
+
const filteredChainWalletStates = this.store.getState().chainWalletStates.filter(state => {
|
|
64
|
+
return validWalletNames.has(state.walletName) && validChainNames.has(state.chainName);
|
|
65
|
+
});
|
|
66
|
+
// 2. 新增原本没有的 chain wallet state
|
|
67
|
+
const newChainWalletStates = [];
|
|
68
|
+
this.wallets.forEach(wallet => {
|
|
69
|
+
this.chains.forEach(chain => {
|
|
70
|
+
if (!this.store.isChainWalletStateExisted(wallet.info.name, chain.chainName)) {
|
|
71
|
+
newChainWalletStates.push({
|
|
72
|
+
walletName: wallet.info.name,
|
|
73
|
+
chainName: chain.chainName,
|
|
74
|
+
rpcEndpoint: '',
|
|
75
|
+
walletState: WalletState.Disconnected,
|
|
76
|
+
account: undefined,
|
|
77
|
+
errorMessage: ''
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
// 合并过滤后的状态和新增的状态
|
|
83
|
+
const finalChainWalletStates = [...filteredChainWalletStates, ...newChainWalletStates];
|
|
84
|
+
this.store.setState({ chainWalletStates: finalChainWalletStates });
|
|
85
|
+
this.store.buildIndexMap();
|
|
86
|
+
}
|
|
87
|
+
subscribe(callback) {
|
|
88
|
+
return this.store.subscribe(callback);
|
|
89
|
+
}
|
|
90
|
+
getState() {
|
|
91
|
+
return this.store.getState();
|
|
92
|
+
}
|
|
93
|
+
get walletConnectQRCodeUri() {
|
|
94
|
+
return this.store.getState().walletConnectQRCodeUri;
|
|
95
|
+
}
|
|
96
|
+
get isReady() {
|
|
97
|
+
return this.store.getState().isReady;
|
|
98
|
+
}
|
|
99
|
+
get currentWalletName() {
|
|
100
|
+
return this.store.getState().currentWalletName;
|
|
101
|
+
}
|
|
102
|
+
get currentChainName() {
|
|
103
|
+
return this.store.getState().currentChainName;
|
|
104
|
+
}
|
|
105
|
+
setCurrentWalletName(walletName) {
|
|
106
|
+
this.store.setCurrentWalletName(walletName);
|
|
107
|
+
}
|
|
108
|
+
setCurrentChainName(chainName) {
|
|
109
|
+
this.store.setCurrentChainName(chainName);
|
|
110
|
+
}
|
|
111
|
+
getChainById(chainId) {
|
|
112
|
+
return this.walletManager.getChainById(chainId);
|
|
113
|
+
}
|
|
114
|
+
getChainWalletState(walletName, chainName) {
|
|
115
|
+
return this.store.getChainWalletState(walletName, chainName);
|
|
116
|
+
}
|
|
117
|
+
updateChainWalletState(walletName, chainName, data) {
|
|
118
|
+
this.store.updateChainWalletState(walletName, chainName, data);
|
|
119
|
+
}
|
|
120
|
+
getChainWalletByName(walletName, chainName) {
|
|
121
|
+
const walletStore = this.wallets.find(w => w.info.name === walletName);
|
|
122
|
+
return walletStore?.getChainWalletStore(chainName);
|
|
123
|
+
}
|
|
124
|
+
get modalIsOpen() {
|
|
125
|
+
return this.store.getState().modalIsOpen;
|
|
126
|
+
}
|
|
127
|
+
openModal() {
|
|
128
|
+
this.store.setState({ modalIsOpen: true });
|
|
129
|
+
}
|
|
130
|
+
closeModal() {
|
|
131
|
+
this.store.setState({ modalIsOpen: false });
|
|
132
|
+
}
|
|
133
|
+
get signerOptions() {
|
|
134
|
+
return this.walletManager.signerOptions;
|
|
135
|
+
}
|
|
136
|
+
get endpointOptions() {
|
|
137
|
+
return this.walletManager.endpointOptions;
|
|
138
|
+
}
|
|
139
|
+
get preferredSignTypeMap() {
|
|
140
|
+
return this.walletManager.preferredSignTypeMap;
|
|
141
|
+
}
|
|
142
|
+
get signerOptionMap() {
|
|
143
|
+
return this.walletManager.signerOptionMap;
|
|
144
|
+
}
|
|
145
|
+
get endpointOptionsMap() {
|
|
146
|
+
return this.walletManager.endpointOptionsMap;
|
|
147
|
+
}
|
|
148
|
+
addChains(newChains, newAssetLists, signerOptions, newEndpointOptions) {
|
|
149
|
+
return this.walletManager.addChains(newChains, newAssetLists, signerOptions, newEndpointOptions);
|
|
150
|
+
}
|
|
151
|
+
getChainLogoUrl(chainName) {
|
|
152
|
+
return this.walletManager.getChainLogoUrl(chainName);
|
|
153
|
+
}
|
|
154
|
+
getWalletByName(walletName) {
|
|
155
|
+
return this.wallets.find(w => w.info.name === walletName);
|
|
156
|
+
}
|
|
157
|
+
getChainByName(chainName) {
|
|
158
|
+
return this.walletManager.getChainByName(chainName);
|
|
159
|
+
}
|
|
160
|
+
getAssetListByName(chainName) {
|
|
161
|
+
return this.walletManager.getAssetListByName(chainName);
|
|
162
|
+
}
|
|
163
|
+
connect(walletName, chainName) {
|
|
164
|
+
const chainWallet = this.getWalletByName(walletName).getChainWalletStore(chainName);
|
|
165
|
+
return chainWallet.connect();
|
|
166
|
+
}
|
|
167
|
+
disconnect(walletName, chainName) {
|
|
168
|
+
const chainWallet = this.getWalletByName(walletName).getChainWalletStore(chainName);
|
|
169
|
+
return chainWallet.disconnect();
|
|
170
|
+
}
|
|
171
|
+
getAccount(walletName, chainName) {
|
|
172
|
+
const chainWallet = this.getWalletByName(walletName).getChainWalletStore(chainName);
|
|
173
|
+
return chainWallet.getAccount();
|
|
174
|
+
}
|
|
175
|
+
async getRpcEndpoint(walletName, chainName) {
|
|
176
|
+
const existed = this.getChainWalletState(walletName, chainName)?.rpcEndpoint;
|
|
177
|
+
if (existed) {
|
|
178
|
+
return existed;
|
|
179
|
+
}
|
|
180
|
+
const rpcEndpoint = await this.walletManager.getRpcEndpoint(walletName, chainName);
|
|
181
|
+
this.updateChainWalletState(walletName, chainName, { rpcEndpoint });
|
|
182
|
+
return rpcEndpoint;
|
|
183
|
+
}
|
|
184
|
+
getPreferSignType(chainName) {
|
|
185
|
+
return this.walletManager.getPreferSignType(chainName);
|
|
186
|
+
}
|
|
187
|
+
getSignerOptions(chainName) {
|
|
188
|
+
return this.walletManager.getSignerOptions(chainName);
|
|
189
|
+
}
|
|
190
|
+
getOfflineSigner(walletName, chainName) {
|
|
191
|
+
return this.getWalletByName(walletName).getChainWalletStore(chainName).getOfflineSigner();
|
|
192
|
+
}
|
|
193
|
+
async getSigningClient(walletName, chainName) {
|
|
194
|
+
try {
|
|
195
|
+
const chain = this.getChainByName(chainName);
|
|
196
|
+
const rpcEndpoint = await this.getRpcEndpoint(walletName, chainName);
|
|
197
|
+
const preferredSignType = this.getPreferSignType(chainName);
|
|
198
|
+
const offlineSigner = (await this.getOfflineSigner(walletName, chainName));
|
|
199
|
+
const defaultSignerOptions = await this.getSignerOptions(chainName);
|
|
200
|
+
const signerOptions = {
|
|
201
|
+
queryClient: await createCosmosQueryClient(rpcEndpoint),
|
|
202
|
+
addressPrefix: chain.bech32Prefix,
|
|
203
|
+
chainId: chain.chainId,
|
|
204
|
+
...defaultSignerOptions,
|
|
205
|
+
};
|
|
206
|
+
if (preferredSignType === 'direct') {
|
|
207
|
+
return getSigner(offlineSigner, {
|
|
208
|
+
preferredSignType: 'direct',
|
|
209
|
+
signerOptions,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
return getSigner(offlineSigner, {
|
|
214
|
+
preferredSignType: 'amino',
|
|
215
|
+
signerOptions,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
console.log(error);
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
getEnv() {
|
|
225
|
+
return this.walletManager.getEnv();
|
|
226
|
+
}
|
|
227
|
+
getDownloadLink(walletName) {
|
|
228
|
+
return this.walletManager.getDownloadLink(walletName);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { BaseWallet, WalletState } from '@interchain-kit/core';
|
|
2
|
+
import { ChainWalletStore } from './chain-wallet-store';
|
|
3
|
+
export class WalletStore extends BaseWallet {
|
|
4
|
+
wallet;
|
|
5
|
+
chains;
|
|
6
|
+
chainWalletStoreMap = new Map();
|
|
7
|
+
store;
|
|
8
|
+
walletManager;
|
|
9
|
+
constructor(wallet, chains, store, walletManager) {
|
|
10
|
+
super(wallet.info);
|
|
11
|
+
this.wallet = wallet;
|
|
12
|
+
this.chains = chains;
|
|
13
|
+
chains.forEach(chain => {
|
|
14
|
+
this.chainWalletStoreMap.set(chain.chainName, new ChainWalletStore(this.wallet, chain, store, walletManager));
|
|
15
|
+
});
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.walletManager = walletManager;
|
|
18
|
+
}
|
|
19
|
+
get walletState() {
|
|
20
|
+
const chainWalletStates = [];
|
|
21
|
+
this.store.getState().chainWalletStates.forEach(cws => {
|
|
22
|
+
if (cws.walletName === this.info.name) {
|
|
23
|
+
chainWalletStates.push(cws.walletState);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
if (chainWalletStates.some(ws => ws === WalletState.NotExist)) {
|
|
27
|
+
return WalletState.NotExist;
|
|
28
|
+
}
|
|
29
|
+
if (chainWalletStates.some(ws => ws === WalletState.Connected)) {
|
|
30
|
+
return WalletState.Connected;
|
|
31
|
+
}
|
|
32
|
+
if (chainWalletStates.some(ws => ws === WalletState.Connecting)) {
|
|
33
|
+
return WalletState.Connecting;
|
|
34
|
+
}
|
|
35
|
+
return WalletState.Disconnected;
|
|
36
|
+
}
|
|
37
|
+
get errorMessage() {
|
|
38
|
+
let errorMessage = '';
|
|
39
|
+
for (const cws of this.store.getState().chainWalletStates) {
|
|
40
|
+
if (cws.walletName === this.info.name) {
|
|
41
|
+
errorMessage = cws.errorMessage;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return errorMessage;
|
|
46
|
+
}
|
|
47
|
+
async init() {
|
|
48
|
+
try {
|
|
49
|
+
const chainWalletStores = Array.from(this.chainWalletStoreMap.values());
|
|
50
|
+
await Promise.all(chainWalletStores.map(chainWalletStore => chainWalletStore.init()));
|
|
51
|
+
await this.wallet.init();
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
this.store.updateWalletState(this.wallet.info.name, { walletState: WalletState.NotExist, errorMessage: '' });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
getChainWalletStore(chainName) {
|
|
58
|
+
const chainWalletStore = this.chainWalletStoreMap.get(chainName);
|
|
59
|
+
if (!chainWalletStore) {
|
|
60
|
+
throw new Error(`Chain wallet store with chain name ${chainName} not found`);
|
|
61
|
+
}
|
|
62
|
+
return chainWalletStore;
|
|
63
|
+
}
|
|
64
|
+
async connect(chainId) {
|
|
65
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
66
|
+
if (!chain) {
|
|
67
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
68
|
+
}
|
|
69
|
+
return this.getChainWalletStore(chain.chainName).connect();
|
|
70
|
+
}
|
|
71
|
+
async disconnect(chainId) {
|
|
72
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
73
|
+
if (!chain) {
|
|
74
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
75
|
+
}
|
|
76
|
+
return this.getChainWalletStore(chain.chainName).disconnect();
|
|
77
|
+
}
|
|
78
|
+
async getAccount(chainId) {
|
|
79
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
80
|
+
if (!chain) {
|
|
81
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
82
|
+
}
|
|
83
|
+
return this.getChainWalletStore(chain.chainName).getAccount();
|
|
84
|
+
}
|
|
85
|
+
async addSuggestChain(chainId) {
|
|
86
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
87
|
+
if (!chain) {
|
|
88
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
89
|
+
}
|
|
90
|
+
return this.getChainWalletStore(chain.chainName).addSuggestChain();
|
|
91
|
+
}
|
|
92
|
+
async getProvider(chainId) {
|
|
93
|
+
const chain = this.walletManager.getChainById(chainId);
|
|
94
|
+
if (!chain) {
|
|
95
|
+
throw new Error(`Chain with ID ${chainId} not found`);
|
|
96
|
+
}
|
|
97
|
+
return this.getChainWalletStore(chain.chainName).getProvider();
|
|
98
|
+
}
|
|
99
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./wallet-manager"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
__exportStar(require("./store"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@interchain-kit/store",
|
|
3
|
+
"version": "0.3.47",
|
|
4
|
+
"author": "Hyperweb <developers@hyperweb.io>",
|
|
5
|
+
"description": "interchain-kit wallet connector store package",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/interchain-kit/store",
|
|
10
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/interchain-kit/store"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/interchain-kit/interchain-kit/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
24
|
+
"clean": "rimraf dist/**",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
|
|
27
|
+
"build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
|
|
28
|
+
"lint": "eslint . --fix",
|
|
29
|
+
"test": "jest",
|
|
30
|
+
"test:watch": "jest --watch",
|
|
31
|
+
"watch:dev": "tsc -w -p tsconfig.esm.json & tsc -w"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [],
|
|
34
|
+
"gitHead": "4583b572672abd85a0918f39f534a5bf9312c261"
|
|
35
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCosmosWallet = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const createCosmosWallet = (target, store) => {
|
|
6
|
+
return (0, utils_1.createAOPProxy)({
|
|
7
|
+
target,
|
|
8
|
+
advice: {
|
|
9
|
+
signAmino: {
|
|
10
|
+
before(methodName, target, chainId, signer, signDoc, signOptions) {
|
|
11
|
+
console.log('signAmino before - AOP proxy is working!', methodName, chainId);
|
|
12
|
+
},
|
|
13
|
+
onError(methodName, target, error, chainId, signer, signDoc, signOptions) {
|
|
14
|
+
console.log('signAmino onError - AOP caught error!', error, chainId);
|
|
15
|
+
const chain = target.getChainById(chainId);
|
|
16
|
+
store.updateChainWalletState(target.info.name, chain.chainName, { errorMessage: error.message });
|
|
17
|
+
throw error; // 重新抛出错误
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
signDirect: {
|
|
21
|
+
async around(methodName, target, originalMethod, chainId, signer, signDoc, signOptions) {
|
|
22
|
+
console.log('signDirect around - AOP is working!', methodName, chainId);
|
|
23
|
+
try {
|
|
24
|
+
const result = await originalMethod(chainId, signer, signDoc, signOptions);
|
|
25
|
+
console.log('signDirect success - AOP is working!', methodName, chainId);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.log('signDirect error - AOP caught the error!', error, chainId);
|
|
30
|
+
const chain = target.getChainById(chainId);
|
|
31
|
+
store.updateChainWalletState(target.info.name, chain.chainName, { errorMessage: error.message });
|
|
32
|
+
throw error; // 重新抛出错误
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
signArbitrary: {
|
|
37
|
+
async around(methodName, target, originalMethod, chainId, signer, data) {
|
|
38
|
+
console.log('signArbitrary around - AOP is working!', methodName, chainId);
|
|
39
|
+
try {
|
|
40
|
+
const result = await originalMethod(chainId, signer, data);
|
|
41
|
+
console.log('signArbitrary success - AOP is working!', methodName, chainId);
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.log('signArbitrary error - AOP caught the error!', error, chainId);
|
|
46
|
+
const chain = target.getChainById(chainId);
|
|
47
|
+
store.updateChainWalletState(target.info.name, chain.chainName, { errorMessage: error.message });
|
|
48
|
+
throw error; // 重新抛出错误
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
exports.createCosmosWallet = createCosmosWallet;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createProxiedWallet = void 0;
|
|
4
|
+
const core_1 = require("@interchain-kit/core");
|
|
5
|
+
const core_2 = require("@interchain-kit/core");
|
|
6
|
+
const cosmos_wallet_1 = require("./cosmos-wallet");
|
|
7
|
+
const ethereum_wallet_1 = require("./ethereum-wallet");
|
|
8
|
+
const multi_chain_wallet_1 = require("./multi-chain-wallet");
|
|
9
|
+
// import { createWCWallet } from './wc-wallet';
|
|
10
|
+
const createProxiedWallet = (wallet, store) => {
|
|
11
|
+
// WalletConnect wallets are now handled by the generic AOP system
|
|
12
|
+
// No need for special handling
|
|
13
|
+
if ((0, core_1.isInstanceOf)(wallet, core_2.CosmosWallet)) {
|
|
14
|
+
return (0, cosmos_wallet_1.createCosmosWallet)(wallet, store);
|
|
15
|
+
}
|
|
16
|
+
if ((0, core_1.isInstanceOf)(wallet, core_2.EthereumWallet)) {
|
|
17
|
+
return (0, ethereum_wallet_1.createEthereumWallet)(wallet, store);
|
|
18
|
+
}
|
|
19
|
+
if ((0, core_1.isInstanceOf)(wallet, core_2.MultiChainWallet)) {
|
|
20
|
+
Array.from(wallet.networkWalletMap.keys()).forEach(chainType => {
|
|
21
|
+
const chainWallet = wallet.networkWalletMap.get(chainType);
|
|
22
|
+
if ((0, core_1.isInstanceOf)(chainWallet, core_2.CosmosWallet)) {
|
|
23
|
+
wallet.setNetworkWallet(chainType, (0, cosmos_wallet_1.createCosmosWallet)(chainWallet, store));
|
|
24
|
+
}
|
|
25
|
+
if ((0, core_1.isInstanceOf)(chainWallet, core_2.EthereumWallet)) {
|
|
26
|
+
wallet.setNetworkWallet(chainType, (0, ethereum_wallet_1.createEthereumWallet)(chainWallet, store));
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return (0, multi_chain_wallet_1.createMultiChainWallet)(wallet, store);
|
|
30
|
+
}
|
|
31
|
+
// Return original wallet if no specific type matches
|
|
32
|
+
return wallet;
|
|
33
|
+
};
|
|
34
|
+
exports.createProxiedWallet = createProxiedWallet;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createEthereumWallet = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const createEthereumWallet = (target, store) => {
|
|
6
|
+
return (0, utils_1.createAOPProxy)({
|
|
7
|
+
target,
|
|
8
|
+
advice: {
|
|
9
|
+
sendTransaction: {
|
|
10
|
+
onError(methodName, target, error, transactionParameters) {
|
|
11
|
+
const chain = target.getChainById(transactionParameters.chainId);
|
|
12
|
+
store.updateChainWalletState(target.info.name, chain.chainName, { errorMessage: error.message });
|
|
13
|
+
throw error; // 重新抛出错误
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
signMessage: {
|
|
17
|
+
onError(methodName, target, error, message) {
|
|
18
|
+
target.getCurrentChainId().then(chainId => {
|
|
19
|
+
const chain = target.getChainById(chainId);
|
|
20
|
+
store.updateChainWalletState(target.info.name, chain.chainName, { errorMessage: error.message });
|
|
21
|
+
});
|
|
22
|
+
throw error; // 重新抛出错误
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
exports.createEthereumWallet = createEthereumWallet;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./cosmos-wallet"), exports);
|
|
18
|
+
__exportStar(require("./create-proxied-wallet"), exports);
|
|
19
|
+
__exportStar(require("./ethereum-wallet"), exports);
|
|
20
|
+
__exportStar(require("./multi-chain-wallet"), exports);
|
|
21
|
+
// export * from './wc-wallet'; // No longer needed - handled by generic AOP system
|