@interchain-kit/react 0.3.30 → 0.3.33
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/esm/hooks/useSigningClient.js +2 -2
- package/esm/modal/modal.js +4 -4
- package/esm/store/stateful-wallet.js +107 -60
- package/esm/store/store.js +16 -68
- package/esm/utils/index.js +1 -0
- package/esm/utils/restoreAccount.js +8 -0
- package/hooks/useSigningClient.js +2 -2
- package/modal/modal.js +4 -4
- package/modal/views/NotExist.d.ts +4 -3
- package/modal/views/Reject.d.ts +3 -3
- package/package.json +7 -7
- package/store/stateful-wallet.d.ts +5 -6
- package/store/stateful-wallet.js +106 -59
- package/store/store.d.ts +1 -1
- package/store/store.js +14 -66
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/restoreAccount.d.ts +2 -0
- package/utils/restoreAccount.js +12 -0
- package/utils/wallet.d.ts +0 -1
- package/esm/modal/provider.js +0 -38
- package/modal/provider.d.ts +0 -10
- package/modal/provider.js +0 -43
|
@@ -2,7 +2,7 @@ import { useWalletManager } from "./useWalletManager";
|
|
|
2
2
|
import { useAsync } from "./useAsync";
|
|
3
3
|
import { WalletState } from "@interchain-kit/core";
|
|
4
4
|
export const useSigningClient = (chainName, walletName) => {
|
|
5
|
-
const { getSigningClient, getChainWalletState, getRpcEndpoint } = useWalletManager();
|
|
5
|
+
const { getSigningClient, getChainWalletState, getRpcEndpoint, isReady } = useWalletManager();
|
|
6
6
|
const chainWalletState = getChainWalletState(walletName, chainName);
|
|
7
7
|
const { data, isLoading, error } = useAsync({
|
|
8
8
|
queryKey: `signing-client-${chainName}-${walletName}`,
|
|
@@ -13,7 +13,7 @@ export const useSigningClient = (chainName, walletName) => {
|
|
|
13
13
|
return getSigningClient(walletName, chainName);
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
-
enabled: chainWalletState?.walletState === WalletState.Connected,
|
|
16
|
+
enabled: chainWalletState?.walletState === WalletState.Connected && isReady,
|
|
17
17
|
disableCache: true,
|
|
18
18
|
});
|
|
19
19
|
return {
|
package/esm/modal/modal.js
CHANGED
|
@@ -8,18 +8,18 @@ import { transferToWalletUISchema } from "../utils";
|
|
|
8
8
|
export const InterchainWalletModal = () => {
|
|
9
9
|
const [shouldShowList, setShouldShowList] = useState(false);
|
|
10
10
|
const { modalIsOpen: isOpen, walletConnectQRCodeUri, wallets: statefulWallets, getChainWalletState, currentWalletName, currentChainName, openModal: open, closeModal: close, chains, setCurrentWalletName, getDownloadLink, getEnv, } = useWalletManager();
|
|
11
|
-
const [
|
|
11
|
+
const [walletToConnect, setWalletToConnect] = useState(null);
|
|
12
12
|
const walletsForUI = statefulWallets.map((w) => transferToWalletUISchema(w));
|
|
13
13
|
const chainNameToConnect = currentChainName || chains[0].chainName;
|
|
14
14
|
const chainToConnect = chains.find((chain) => chain.chainName === chainNameToConnect);
|
|
15
15
|
const currentWallet = statefulWallets.find((w) => w.info.name === currentWalletName);
|
|
16
|
-
const walletToShow =
|
|
17
|
-
const { account, errorMessage } = getChainWalletState(
|
|
16
|
+
const walletToShow = walletToConnect || currentWallet;
|
|
17
|
+
const { account, errorMessage } = getChainWalletState(walletToConnect?.info?.name || currentWalletName, currentChainName) || {};
|
|
18
18
|
const disconnect = () => {
|
|
19
19
|
walletToShow.disconnect(chainToConnect.chainId);
|
|
20
20
|
};
|
|
21
21
|
const onSelectWallet = (wallet) => {
|
|
22
|
-
|
|
22
|
+
setWalletToConnect(wallet);
|
|
23
23
|
setShouldShowList(false);
|
|
24
24
|
return wallet.connect(chainToConnect.chainId);
|
|
25
25
|
};
|
|
@@ -1,105 +1,129 @@
|
|
|
1
|
-
import { BaseWallet, WalletState, WCWallet } from "@interchain-kit/core";
|
|
1
|
+
import { BaseWallet, clientNotExistError, CosmosWallet, EthereumWallet, ExtensionWallet, WalletState, WCWallet } from "@interchain-kit/core";
|
|
2
2
|
export class StatefulWallet extends BaseWallet {
|
|
3
3
|
originalWallet;
|
|
4
4
|
walletName;
|
|
5
|
-
walletState;
|
|
6
|
-
walletSet;
|
|
7
|
-
walletGet;
|
|
8
|
-
set;
|
|
9
5
|
get;
|
|
10
|
-
constructor(wallet,
|
|
6
|
+
constructor(wallet, get) {
|
|
11
7
|
super(wallet.info);
|
|
12
8
|
this.originalWallet = wallet;
|
|
13
9
|
this.walletName = wallet.info.name;
|
|
14
|
-
this.walletState = WalletState.Disconnected;
|
|
15
|
-
this.errorMessage = "";
|
|
16
|
-
this.walletSet = walletSet;
|
|
17
|
-
this.walletGet = walletGet;
|
|
18
|
-
this.set = set;
|
|
19
10
|
this.get = get;
|
|
20
11
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
get store() {
|
|
13
|
+
return this.get();
|
|
14
|
+
}
|
|
15
|
+
get walletState() {
|
|
16
|
+
// 獲取此錢包在所有鏈上的狀態
|
|
17
|
+
const states = (this.store.chainWalletState || [])
|
|
18
|
+
.filter(cws => cws.walletName === this.walletName)
|
|
19
|
+
.map(cws => cws.walletState);
|
|
20
|
+
// If any chain is in the connected state, return connected
|
|
21
|
+
if (states.includes(WalletState.Connected)) {
|
|
22
|
+
return WalletState.Connected;
|
|
23
|
+
}
|
|
24
|
+
// 如果有任何一個鏈正在連接中,則返回連接中
|
|
25
|
+
if (states.includes(WalletState.Connecting)) {
|
|
26
|
+
return WalletState.Connecting;
|
|
27
|
+
}
|
|
28
|
+
// 如果所有鏈都是不存在狀態,則返回不存在
|
|
29
|
+
if (states.length > 0 && states.every(state => state === WalletState.NotExist)) {
|
|
30
|
+
return WalletState.NotExist;
|
|
31
|
+
}
|
|
32
|
+
// 如果有任何一個鏈是被拒絕狀態,則返回被拒絕
|
|
33
|
+
if (states.includes(WalletState.Rejected)) {
|
|
34
|
+
return WalletState.Rejected;
|
|
35
|
+
}
|
|
36
|
+
// 預設返回未連接
|
|
37
|
+
return WalletState.Disconnected;
|
|
38
|
+
}
|
|
39
|
+
get errorMessage() {
|
|
40
|
+
// 獲取此錢包在所有鏈上的錯誤訊息
|
|
41
|
+
const errors = (this.store.chainWalletState || [])
|
|
42
|
+
.filter(cws => cws.walletName === this.walletName)
|
|
43
|
+
.map(cws => cws.errorMessage)
|
|
44
|
+
.filter(error => error && error.trim() !== '');
|
|
45
|
+
// 返回第一個非空錯誤訊息,如果沒有則返回空字串
|
|
46
|
+
return errors.length > 0 ? errors[0] : '';
|
|
26
47
|
}
|
|
27
48
|
async init() {
|
|
28
|
-
|
|
49
|
+
try {
|
|
50
|
+
await this.originalWallet.init();
|
|
51
|
+
this.store.chains.forEach(chain => {
|
|
52
|
+
const lastChainWalletState = this.store.getChainWalletState(this.walletName, chain.chainName)?.walletState;
|
|
53
|
+
if (lastChainWalletState === WalletState.NotExist) {
|
|
54
|
+
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
55
|
+
walletState: WalletState.Disconnected,
|
|
56
|
+
errorMessage: ''
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error === clientNotExistError) {
|
|
63
|
+
this.store.chains.forEach(chain => {
|
|
64
|
+
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
65
|
+
walletState: WalletState.NotExist,
|
|
66
|
+
errorMessage: clientNotExistError.message
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
29
71
|
}
|
|
30
72
|
async connect(chainId) {
|
|
31
|
-
const {
|
|
32
|
-
const chainToConnect = this.
|
|
33
|
-
const state =
|
|
73
|
+
const { store, walletName, originalWallet } = this;
|
|
74
|
+
const chainToConnect = this.getChainById(chainId);
|
|
75
|
+
const state = store.getChainWalletState(walletName, chainToConnect.chainName)?.walletState;
|
|
34
76
|
if (state === WalletState.NotExist) {
|
|
35
77
|
return;
|
|
36
78
|
}
|
|
37
79
|
if (walletName === 'WalletConnect' && state === WalletState.Connected) {
|
|
38
80
|
return;
|
|
39
81
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
});
|
|
45
|
-
walletSet(draft => {
|
|
46
|
-
draft.walletState = WalletState.Connecting;
|
|
47
|
-
draft.errorMessage = '';
|
|
48
|
-
});
|
|
49
|
-
get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: WalletState.Connecting, errorMessage: '' });
|
|
82
|
+
store.setCurrentChainName(chainToConnect.chainName);
|
|
83
|
+
store.setCurrentWalletName(walletName);
|
|
84
|
+
store.setWalletConnectQRCodeUri('');
|
|
85
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: WalletState.Connecting, errorMessage: '' });
|
|
50
86
|
try {
|
|
51
87
|
if (originalWallet instanceof WCWallet) {
|
|
52
88
|
originalWallet.setOnPairingUriCreatedCallback((uri) => {
|
|
53
|
-
|
|
54
|
-
draft.walletConnectQRCodeUri = uri;
|
|
55
|
-
});
|
|
89
|
+
store.setWalletConnectQRCodeUri(uri);
|
|
56
90
|
});
|
|
57
91
|
}
|
|
58
92
|
await originalWallet.connect(chainToConnect.chainId);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
draft.walletState = WalletState.Connected;
|
|
62
|
-
});
|
|
63
|
-
await walletGet().getAccount(chainToConnect.chainId);
|
|
93
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: WalletState.Connected });
|
|
94
|
+
await this.getAccount(chainToConnect.chainId);
|
|
64
95
|
}
|
|
65
96
|
catch (error) {
|
|
66
|
-
if (error.message
|
|
67
|
-
|
|
68
|
-
walletSet(draft => {
|
|
69
|
-
draft.walletState = WalletState.Rejected;
|
|
70
|
-
draft.errorMessage = error.message;
|
|
71
|
-
});
|
|
97
|
+
if (error.message.includes('rejected')) {
|
|
98
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: WalletState.Rejected, errorMessage: error.message });
|
|
72
99
|
return;
|
|
73
100
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
draft.errorMessage = error.message;
|
|
78
|
-
});
|
|
101
|
+
else {
|
|
102
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: WalletState.Disconnected, errorMessage: error.message });
|
|
103
|
+
}
|
|
79
104
|
}
|
|
80
105
|
}
|
|
81
106
|
async disconnect(chainId) {
|
|
82
|
-
const {
|
|
83
|
-
const chainToConnect = this.
|
|
107
|
+
const { store, walletName, originalWallet } = this;
|
|
108
|
+
const chainToConnect = this.getChainById(chainId);
|
|
84
109
|
try {
|
|
85
|
-
if (this.
|
|
110
|
+
if (this.walletState === WalletState.Connected) {
|
|
86
111
|
await originalWallet.disconnect(chainToConnect.chainId);
|
|
87
112
|
}
|
|
88
|
-
|
|
89
|
-
walletSet(draft => {
|
|
90
|
-
draft.walletState = WalletState.Disconnected;
|
|
91
|
-
draft.errorMessage = "";
|
|
92
|
-
});
|
|
113
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: WalletState.Disconnected, account: null });
|
|
93
114
|
}
|
|
94
115
|
catch (error) {
|
|
95
116
|
}
|
|
96
117
|
}
|
|
97
118
|
async getAccount(chainId) {
|
|
98
|
-
const chainToConnect = this.
|
|
99
|
-
const {
|
|
119
|
+
const chainToConnect = this.getChainById(chainId);
|
|
120
|
+
const { store, walletName, originalWallet } = this;
|
|
100
121
|
try {
|
|
101
122
|
const account = await originalWallet.getAccount(chainToConnect.chainId);
|
|
102
|
-
|
|
123
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { account });
|
|
124
|
+
if (this.originalWallet instanceof WCWallet) {
|
|
125
|
+
this.originalWallet.setAccountToRestore(account);
|
|
126
|
+
}
|
|
103
127
|
return account;
|
|
104
128
|
}
|
|
105
129
|
catch (error) {
|
|
@@ -118,4 +142,27 @@ export class StatefulWallet extends BaseWallet {
|
|
|
118
142
|
getChainById(chainId) {
|
|
119
143
|
return this.originalWallet.getChainById(chainId);
|
|
120
144
|
}
|
|
145
|
+
executeSpecificWalletMethod(WalletClass, callback) {
|
|
146
|
+
if (this.originalWallet instanceof WalletClass) {
|
|
147
|
+
return callback(this.originalWallet);
|
|
148
|
+
}
|
|
149
|
+
if (this.originalWallet instanceof WCWallet) {
|
|
150
|
+
return callback(this.originalWallet);
|
|
151
|
+
}
|
|
152
|
+
if (this.originalWallet instanceof ExtensionWallet) {
|
|
153
|
+
if (WalletClass === CosmosWallet) {
|
|
154
|
+
const cosmosWallet = this.originalWallet.getWalletByChainType('cosmos');
|
|
155
|
+
if (cosmosWallet) {
|
|
156
|
+
return callback(cosmosWallet);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (WalletClass === EthereumWallet) {
|
|
160
|
+
const ethereumWallet = this.originalWallet.getWalletByChainType('eip155');
|
|
161
|
+
if (ethereumWallet) {
|
|
162
|
+
return callback(ethereumWallet);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
121
168
|
}
|
package/esm/store/store.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { WalletState } from "@interchain-kit/core";
|
|
2
2
|
import { createStore } from "zustand";
|
|
3
3
|
import { immer } from "zustand/middleware/immer";
|
|
4
4
|
import { persist, createJSONStorage } from 'zustand/middleware';
|
|
5
|
-
import { dedupeAsync } from '../utils';
|
|
5
|
+
import { dedupeAsync, restoreAccountFromLocalStorage } from '../utils';
|
|
6
6
|
import { StatefulWallet } from './stateful-wallet';
|
|
7
7
|
const immerSyncUp = (newWalletManager) => {
|
|
8
8
|
return (draft) => {
|
|
@@ -23,12 +23,7 @@ export const createInterchainStore = (walletManager) => {
|
|
|
23
23
|
currentChainName: '',
|
|
24
24
|
chains: [...walletManager.chains],
|
|
25
25
|
assetLists: [...walletManager.assetLists],
|
|
26
|
-
wallets: walletManager.wallets.map(wallet =>
|
|
27
|
-
const walletSet = (fn) => {
|
|
28
|
-
set((draft) => fn(draft.wallets.find(w => w.info.name === wallet.info.name)));
|
|
29
|
-
};
|
|
30
|
-
return new StatefulWallet(wallet, walletSet, () => get().wallets.find(w => w.info.name === wallet.info.name), set, get);
|
|
31
|
-
}),
|
|
26
|
+
wallets: walletManager.wallets.map(wallet => new StatefulWallet(wallet, get)),
|
|
32
27
|
signerOptions: walletManager.signerOptions,
|
|
33
28
|
endpointOptions: walletManager.endpointOptions,
|
|
34
29
|
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
@@ -54,28 +49,6 @@ export const createInterchainStore = (walletManager) => {
|
|
|
54
49
|
draft.chainWalletState[targetIndex] = { ...draft.chainWalletState[targetIndex], ...data };
|
|
55
50
|
});
|
|
56
51
|
},
|
|
57
|
-
createStatefulWallet: () => {
|
|
58
|
-
const wallets = walletManager.wallets.map(wallet => {
|
|
59
|
-
const walletSet = (fn) => {
|
|
60
|
-
set((draft) => fn(draft.wallets.find(w => w.info.name === wallet.info.name)));
|
|
61
|
-
};
|
|
62
|
-
return new StatefulWallet(wallet, walletSet, () => get().wallets.find(w => w.info.name === wallet.info.name), set, get);
|
|
63
|
-
});
|
|
64
|
-
set(draft => {
|
|
65
|
-
draft.wallets = wallets;
|
|
66
|
-
});
|
|
67
|
-
const defaultWalletStates = get().chainWalletState.reduce((acc, cws) => {
|
|
68
|
-
if (acc[cws.walletName] && cws.walletState === WalletState.Connected) {
|
|
69
|
-
return acc;
|
|
70
|
-
}
|
|
71
|
-
return { ...acc, [cws.walletName]: cws.walletState };
|
|
72
|
-
}, {});
|
|
73
|
-
set(draft => {
|
|
74
|
-
draft.wallets.forEach(wallet => {
|
|
75
|
-
wallet.walletState = defaultWalletStates[wallet.info.name];
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
},
|
|
79
52
|
init: async () => {
|
|
80
53
|
const oldChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
|
|
81
54
|
// get().createStatefulWallet()
|
|
@@ -102,42 +75,8 @@ export const createInterchainStore = (walletManager) => {
|
|
|
102
75
|
});
|
|
103
76
|
});
|
|
104
77
|
});
|
|
105
|
-
|
|
106
|
-
const ExistWallets = [];
|
|
107
|
-
await Promise.all(get().wallets.map(async (wallet) => {
|
|
108
|
-
try {
|
|
109
|
-
await wallet.init();
|
|
110
|
-
ExistWallets.push(wallet.info.name);
|
|
111
|
-
}
|
|
112
|
-
catch (error) {
|
|
113
|
-
if (error === clientNotExistError) {
|
|
114
|
-
NotExistWallets.push(wallet.info.name);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}));
|
|
78
|
+
await Promise.all(get().wallets.map(async (wallet) => wallet.init()));
|
|
118
79
|
set(draft => {
|
|
119
|
-
draft.chainWalletState = draft.chainWalletState.map(cws => {
|
|
120
|
-
if (NotExistWallets.includes(cws.walletName)) {
|
|
121
|
-
return { ...cws, walletState: WalletState.NotExist };
|
|
122
|
-
}
|
|
123
|
-
return cws;
|
|
124
|
-
});
|
|
125
|
-
draft.chainWalletState = draft.chainWalletState.map(cws => {
|
|
126
|
-
if (ExistWallets.includes(cws.walletName)) {
|
|
127
|
-
const newState = cws.walletState === WalletState.NotExist ? WalletState.Disconnected : cws.walletState;
|
|
128
|
-
return { ...cws, walletState: newState };
|
|
129
|
-
}
|
|
130
|
-
return cws;
|
|
131
|
-
});
|
|
132
|
-
draft.chainWalletState.forEach(cws => {
|
|
133
|
-
const lastExistWallet = draft.wallets.find(w => w.info.name === cws.walletName);
|
|
134
|
-
if (cws.walletState === WalletState.Connected && lastExistWallet.walletState !== WalletState.Connected) {
|
|
135
|
-
lastExistWallet.walletState = WalletState.Connected;
|
|
136
|
-
}
|
|
137
|
-
if (cws.walletState === WalletState.NotExist) {
|
|
138
|
-
lastExistWallet.walletState = WalletState.NotExist;
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
80
|
draft.isReady = true;
|
|
142
81
|
});
|
|
143
82
|
},
|
|
@@ -147,6 +86,9 @@ export const createInterchainStore = (walletManager) => {
|
|
|
147
86
|
setCurrentWalletName: (walletName) => {
|
|
148
87
|
set(draft => { draft.currentWalletName = walletName; });
|
|
149
88
|
},
|
|
89
|
+
setWalletConnectQRCodeUri: (uri) => {
|
|
90
|
+
set(draft => { draft.walletConnectQRCodeUri = uri; });
|
|
91
|
+
},
|
|
150
92
|
getDraftChainWalletState: (state, walletName, chainName) => {
|
|
151
93
|
const targetIndex = state.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
152
94
|
return state.chainWalletState[targetIndex];
|
|
@@ -260,17 +202,17 @@ export const createInterchainStore = (walletManager) => {
|
|
|
260
202
|
getDownloadLink(walletName) {
|
|
261
203
|
return walletManager.getDownloadLink(walletName);
|
|
262
204
|
},
|
|
263
|
-
getOfflineSigner(walletName, chainName) {
|
|
205
|
+
async getOfflineSigner(walletName, chainName) {
|
|
264
206
|
return walletManager.getOfflineSigner(walletName, chainName);
|
|
265
207
|
},
|
|
266
208
|
getPreferSignType(chainName) {
|
|
267
209
|
const result = walletManager.getPreferSignType(chainName);
|
|
268
|
-
set(immerSyncUp(walletManager))
|
|
210
|
+
// set(immerSyncUp(walletManager))
|
|
269
211
|
return result;
|
|
270
212
|
},
|
|
271
213
|
getSignerOptions(chainName) {
|
|
272
214
|
const result = walletManager.getSignerOptions(chainName);
|
|
273
|
-
set(immerSyncUp(walletManager))
|
|
215
|
+
// set(immerSyncUp(walletManager))
|
|
274
216
|
return result;
|
|
275
217
|
},
|
|
276
218
|
getWalletByName(walletName) {
|
|
@@ -307,6 +249,12 @@ export const createInterchainStore = (walletManager) => {
|
|
|
307
249
|
}
|
|
308
250
|
else {
|
|
309
251
|
// console.log('interchain-kit store hydration finished')
|
|
252
|
+
state.chainWalletState = state.chainWalletState.map(cws => {
|
|
253
|
+
return {
|
|
254
|
+
...cws,
|
|
255
|
+
account: cws.account ? restoreAccountFromLocalStorage(cws.account) : null
|
|
256
|
+
};
|
|
257
|
+
});
|
|
310
258
|
}
|
|
311
259
|
};
|
|
312
260
|
},
|
package/esm/utils/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const restoreAccountFromLocalStorage = (walletAccount) => {
|
|
2
|
+
const pubkey = walletAccount.pubkey;
|
|
3
|
+
if (typeof pubkey === 'object') {
|
|
4
|
+
// return from localstorage need to restructure to uinit8Array
|
|
5
|
+
return { ...walletAccount, pubkey: Uint8Array.from({ ...pubkey, length: Object.keys(pubkey).length }) };
|
|
6
|
+
}
|
|
7
|
+
return walletAccount;
|
|
8
|
+
};
|
|
@@ -5,7 +5,7 @@ const useWalletManager_1 = require("./useWalletManager");
|
|
|
5
5
|
const useAsync_1 = require("./useAsync");
|
|
6
6
|
const core_1 = require("@interchain-kit/core");
|
|
7
7
|
const useSigningClient = (chainName, walletName) => {
|
|
8
|
-
const { getSigningClient, getChainWalletState, getRpcEndpoint } = (0, useWalletManager_1.useWalletManager)();
|
|
8
|
+
const { getSigningClient, getChainWalletState, getRpcEndpoint, isReady } = (0, useWalletManager_1.useWalletManager)();
|
|
9
9
|
const chainWalletState = getChainWalletState(walletName, chainName);
|
|
10
10
|
const { data, isLoading, error } = (0, useAsync_1.useAsync)({
|
|
11
11
|
queryKey: `signing-client-${chainName}-${walletName}`,
|
|
@@ -16,7 +16,7 @@ const useSigningClient = (chainName, walletName) => {
|
|
|
16
16
|
return getSigningClient(walletName, chainName);
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
enabled: chainWalletState?.walletState === core_1.WalletState.Connected,
|
|
19
|
+
enabled: chainWalletState?.walletState === core_1.WalletState.Connected && isReady,
|
|
20
20
|
disableCache: true,
|
|
21
21
|
});
|
|
22
22
|
return {
|
package/modal/modal.js
CHANGED
|
@@ -11,18 +11,18 @@ const utils_1 = require("../utils");
|
|
|
11
11
|
const InterchainWalletModal = () => {
|
|
12
12
|
const [shouldShowList, setShouldShowList] = (0, react_1.useState)(false);
|
|
13
13
|
const { modalIsOpen: isOpen, walletConnectQRCodeUri, wallets: statefulWallets, getChainWalletState, currentWalletName, currentChainName, openModal: open, closeModal: close, chains, setCurrentWalletName, getDownloadLink, getEnv, } = (0, hooks_1.useWalletManager)();
|
|
14
|
-
const [
|
|
14
|
+
const [walletToConnect, setWalletToConnect] = (0, react_1.useState)(null);
|
|
15
15
|
const walletsForUI = statefulWallets.map((w) => (0, utils_1.transferToWalletUISchema)(w));
|
|
16
16
|
const chainNameToConnect = currentChainName || chains[0].chainName;
|
|
17
17
|
const chainToConnect = chains.find((chain) => chain.chainName === chainNameToConnect);
|
|
18
18
|
const currentWallet = statefulWallets.find((w) => w.info.name === currentWalletName);
|
|
19
|
-
const walletToShow =
|
|
20
|
-
const { account, errorMessage } = getChainWalletState(
|
|
19
|
+
const walletToShow = walletToConnect || currentWallet;
|
|
20
|
+
const { account, errorMessage } = getChainWalletState(walletToConnect?.info?.name || currentWalletName, currentChainName) || {};
|
|
21
21
|
const disconnect = () => {
|
|
22
22
|
walletToShow.disconnect(chainToConnect.chainId);
|
|
23
23
|
};
|
|
24
24
|
const onSelectWallet = (wallet) => {
|
|
25
|
-
|
|
25
|
+
setWalletToConnect(wallet);
|
|
26
26
|
setShouldShowList(false);
|
|
27
27
|
return wallet.connect(chainToConnect.chainId);
|
|
28
28
|
};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DownloadInfo } from "@interchain-kit/core";
|
|
2
|
+
import { StatefulWallet } from "../../store/stateful-wallet";
|
|
2
3
|
export declare const NotExistHeader: ({ wallet, close, onBack, }: {
|
|
3
|
-
wallet:
|
|
4
|
+
wallet: StatefulWallet;
|
|
4
5
|
close: () => void;
|
|
5
6
|
onBack: () => void;
|
|
6
7
|
}) => import("react/jsx-runtime").JSX.Element;
|
|
7
8
|
export declare const NotExistContent: ({ wallet, getDownloadLink, getEnv, }: {
|
|
8
|
-
wallet:
|
|
9
|
+
wallet: StatefulWallet;
|
|
9
10
|
getDownloadLink: (walletName: string) => DownloadInfo;
|
|
10
11
|
getEnv: () => {
|
|
11
12
|
browser?: string;
|
package/modal/views/Reject.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StatefulWallet } from "../../store/stateful-wallet";
|
|
2
2
|
export declare const RejectHeader: ({ wallet, close, onBack, }: {
|
|
3
|
-
wallet:
|
|
3
|
+
wallet: StatefulWallet;
|
|
4
4
|
close: () => void;
|
|
5
5
|
onBack: () => void;
|
|
6
6
|
}) => import("react/jsx-runtime").JSX.Element;
|
|
7
7
|
export declare const RejectContent: ({ wallet, onReconnect, }: {
|
|
8
|
-
wallet:
|
|
8
|
+
wallet: StatefulWallet;
|
|
9
9
|
onReconnect: () => void;
|
|
10
10
|
}) => import("react/jsx-runtime").JSX.Element;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interchain-kit/react",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.33",
|
|
4
4
|
"author": "Hyperweb <developers@hyperweb.io>",
|
|
5
5
|
"description": "interchain-kit wallet connector react package",
|
|
6
6
|
"main": "index.js",
|
|
@@ -34,14 +34,14 @@
|
|
|
34
34
|
"keywords": [],
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@chain-registry/v2-types": "^0.53.40",
|
|
37
|
-
"@interchain-kit/core": "
|
|
37
|
+
"@interchain-kit/core": "0.3.33",
|
|
38
38
|
"@interchain-ui/react": "1.26.1",
|
|
39
|
-
"@interchainjs/cosmos": "1.11.
|
|
40
|
-
"@interchainjs/cosmos-types": "1.11.
|
|
41
|
-
"@interchainjs/types": "1.11.
|
|
39
|
+
"@interchainjs/cosmos": "1.11.11",
|
|
40
|
+
"@interchainjs/cosmos-types": "1.11.11",
|
|
41
|
+
"@interchainjs/types": "1.11.11",
|
|
42
42
|
"@react-icons/all-files": "^4.1.0",
|
|
43
43
|
"@walletconnect/types": "^2.17.3",
|
|
44
|
-
"interchainjs": "1.11.
|
|
44
|
+
"interchainjs": "1.11.11",
|
|
45
45
|
"zustand": "^5.0.3"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"react": "^19.0.0",
|
|
65
65
|
"react-dom": "^19.0.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "5cab9e273389de0130ab5da00a6d369d0ccff8c5"
|
|
68
68
|
}
|
|
@@ -4,13 +4,11 @@ import { Chain } from "@chain-registry/v2-types";
|
|
|
4
4
|
export declare class StatefulWallet extends BaseWallet {
|
|
5
5
|
originalWallet: BaseWallet;
|
|
6
6
|
walletName: string;
|
|
7
|
-
walletState: WalletState;
|
|
8
|
-
walletSet: (arg: (draft: StatefulWallet) => void) => void;
|
|
9
|
-
walletGet: () => StatefulWallet;
|
|
10
|
-
set: (arg: (draft: InterchainStore) => void) => void;
|
|
11
7
|
get: () => InterchainStore;
|
|
12
|
-
constructor(wallet: BaseWallet,
|
|
13
|
-
|
|
8
|
+
constructor(wallet: BaseWallet, get: () => InterchainStore);
|
|
9
|
+
get store(): InterchainStore;
|
|
10
|
+
get walletState(): WalletState;
|
|
11
|
+
get errorMessage(): string;
|
|
14
12
|
init(): Promise<void>;
|
|
15
13
|
connect(chainId: Chain["chainId"]): Promise<void>;
|
|
16
14
|
disconnect(chainId: Chain["chainId"]): Promise<void>;
|
|
@@ -19,4 +17,5 @@ export declare class StatefulWallet extends BaseWallet {
|
|
|
19
17
|
addSuggestChain(chainId: Chain["chainId"]): Promise<void>;
|
|
20
18
|
getProvider(chainId?: Chain["chainId"]): Promise<any>;
|
|
21
19
|
getChainById(chainId: Chain["chainId"]): Chain;
|
|
20
|
+
executeSpecificWalletMethod<T, R>(WalletClass: new (...args: any[]) => T, callback: (wallet: T) => R): R | undefined;
|
|
22
21
|
}
|
package/store/stateful-wallet.js
CHANGED
|
@@ -5,104 +5,128 @@ const core_1 = require("@interchain-kit/core");
|
|
|
5
5
|
class StatefulWallet extends core_1.BaseWallet {
|
|
6
6
|
originalWallet;
|
|
7
7
|
walletName;
|
|
8
|
-
walletState;
|
|
9
|
-
walletSet;
|
|
10
|
-
walletGet;
|
|
11
|
-
set;
|
|
12
8
|
get;
|
|
13
|
-
constructor(wallet,
|
|
9
|
+
constructor(wallet, get) {
|
|
14
10
|
super(wallet.info);
|
|
15
11
|
this.originalWallet = wallet;
|
|
16
12
|
this.walletName = wallet.info.name;
|
|
17
|
-
this.walletState = core_1.WalletState.Disconnected;
|
|
18
|
-
this.errorMessage = "";
|
|
19
|
-
this.walletSet = walletSet;
|
|
20
|
-
this.walletGet = walletGet;
|
|
21
|
-
this.set = set;
|
|
22
13
|
this.get = get;
|
|
23
14
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
get store() {
|
|
16
|
+
return this.get();
|
|
17
|
+
}
|
|
18
|
+
get walletState() {
|
|
19
|
+
// 獲取此錢包在所有鏈上的狀態
|
|
20
|
+
const states = (this.store.chainWalletState || [])
|
|
21
|
+
.filter(cws => cws.walletName === this.walletName)
|
|
22
|
+
.map(cws => cws.walletState);
|
|
23
|
+
// If any chain is in the connected state, return connected
|
|
24
|
+
if (states.includes(core_1.WalletState.Connected)) {
|
|
25
|
+
return core_1.WalletState.Connected;
|
|
26
|
+
}
|
|
27
|
+
// 如果有任何一個鏈正在連接中,則返回連接中
|
|
28
|
+
if (states.includes(core_1.WalletState.Connecting)) {
|
|
29
|
+
return core_1.WalletState.Connecting;
|
|
30
|
+
}
|
|
31
|
+
// 如果所有鏈都是不存在狀態,則返回不存在
|
|
32
|
+
if (states.length > 0 && states.every(state => state === core_1.WalletState.NotExist)) {
|
|
33
|
+
return core_1.WalletState.NotExist;
|
|
34
|
+
}
|
|
35
|
+
// 如果有任何一個鏈是被拒絕狀態,則返回被拒絕
|
|
36
|
+
if (states.includes(core_1.WalletState.Rejected)) {
|
|
37
|
+
return core_1.WalletState.Rejected;
|
|
38
|
+
}
|
|
39
|
+
// 預設返回未連接
|
|
40
|
+
return core_1.WalletState.Disconnected;
|
|
41
|
+
}
|
|
42
|
+
get errorMessage() {
|
|
43
|
+
// 獲取此錢包在所有鏈上的錯誤訊息
|
|
44
|
+
const errors = (this.store.chainWalletState || [])
|
|
45
|
+
.filter(cws => cws.walletName === this.walletName)
|
|
46
|
+
.map(cws => cws.errorMessage)
|
|
47
|
+
.filter(error => error && error.trim() !== '');
|
|
48
|
+
// 返回第一個非空錯誤訊息,如果沒有則返回空字串
|
|
49
|
+
return errors.length > 0 ? errors[0] : '';
|
|
29
50
|
}
|
|
30
51
|
async init() {
|
|
31
|
-
|
|
52
|
+
try {
|
|
53
|
+
await this.originalWallet.init();
|
|
54
|
+
this.store.chains.forEach(chain => {
|
|
55
|
+
const lastChainWalletState = this.store.getChainWalletState(this.walletName, chain.chainName)?.walletState;
|
|
56
|
+
if (lastChainWalletState === core_1.WalletState.NotExist) {
|
|
57
|
+
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
58
|
+
walletState: core_1.WalletState.Disconnected,
|
|
59
|
+
errorMessage: ''
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (error === core_1.clientNotExistError) {
|
|
66
|
+
this.store.chains.forEach(chain => {
|
|
67
|
+
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
68
|
+
walletState: core_1.WalletState.NotExist,
|
|
69
|
+
errorMessage: core_1.clientNotExistError.message
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
32
74
|
}
|
|
33
75
|
async connect(chainId) {
|
|
34
|
-
const {
|
|
35
|
-
const chainToConnect = this.
|
|
36
|
-
const state =
|
|
76
|
+
const { store, walletName, originalWallet } = this;
|
|
77
|
+
const chainToConnect = this.getChainById(chainId);
|
|
78
|
+
const state = store.getChainWalletState(walletName, chainToConnect.chainName)?.walletState;
|
|
37
79
|
if (state === core_1.WalletState.NotExist) {
|
|
38
80
|
return;
|
|
39
81
|
}
|
|
40
82
|
if (walletName === 'WalletConnect' && state === core_1.WalletState.Connected) {
|
|
41
83
|
return;
|
|
42
84
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
});
|
|
48
|
-
walletSet(draft => {
|
|
49
|
-
draft.walletState = core_1.WalletState.Connecting;
|
|
50
|
-
draft.errorMessage = '';
|
|
51
|
-
});
|
|
52
|
-
get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connecting, errorMessage: '' });
|
|
85
|
+
store.setCurrentChainName(chainToConnect.chainName);
|
|
86
|
+
store.setCurrentWalletName(walletName);
|
|
87
|
+
store.setWalletConnectQRCodeUri('');
|
|
88
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connecting, errorMessage: '' });
|
|
53
89
|
try {
|
|
54
90
|
if (originalWallet instanceof core_1.WCWallet) {
|
|
55
91
|
originalWallet.setOnPairingUriCreatedCallback((uri) => {
|
|
56
|
-
|
|
57
|
-
draft.walletConnectQRCodeUri = uri;
|
|
58
|
-
});
|
|
92
|
+
store.setWalletConnectQRCodeUri(uri);
|
|
59
93
|
});
|
|
60
94
|
}
|
|
61
95
|
await originalWallet.connect(chainToConnect.chainId);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
draft.walletState = core_1.WalletState.Connected;
|
|
65
|
-
});
|
|
66
|
-
await walletGet().getAccount(chainToConnect.chainId);
|
|
96
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connected });
|
|
97
|
+
await this.getAccount(chainToConnect.chainId);
|
|
67
98
|
}
|
|
68
99
|
catch (error) {
|
|
69
|
-
if (error.message
|
|
70
|
-
|
|
71
|
-
walletSet(draft => {
|
|
72
|
-
draft.walletState = core_1.WalletState.Rejected;
|
|
73
|
-
draft.errorMessage = error.message;
|
|
74
|
-
});
|
|
100
|
+
if (error.message.includes('rejected')) {
|
|
101
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Rejected, errorMessage: error.message });
|
|
75
102
|
return;
|
|
76
103
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
draft.errorMessage = error.message;
|
|
81
|
-
});
|
|
104
|
+
else {
|
|
105
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Disconnected, errorMessage: error.message });
|
|
106
|
+
}
|
|
82
107
|
}
|
|
83
108
|
}
|
|
84
109
|
async disconnect(chainId) {
|
|
85
|
-
const {
|
|
86
|
-
const chainToConnect = this.
|
|
110
|
+
const { store, walletName, originalWallet } = this;
|
|
111
|
+
const chainToConnect = this.getChainById(chainId);
|
|
87
112
|
try {
|
|
88
|
-
if (this.
|
|
113
|
+
if (this.walletState === core_1.WalletState.Connected) {
|
|
89
114
|
await originalWallet.disconnect(chainToConnect.chainId);
|
|
90
115
|
}
|
|
91
|
-
|
|
92
|
-
walletSet(draft => {
|
|
93
|
-
draft.walletState = core_1.WalletState.Disconnected;
|
|
94
|
-
draft.errorMessage = "";
|
|
95
|
-
});
|
|
116
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Disconnected, account: null });
|
|
96
117
|
}
|
|
97
118
|
catch (error) {
|
|
98
119
|
}
|
|
99
120
|
}
|
|
100
121
|
async getAccount(chainId) {
|
|
101
|
-
const chainToConnect = this.
|
|
102
|
-
const {
|
|
122
|
+
const chainToConnect = this.getChainById(chainId);
|
|
123
|
+
const { store, walletName, originalWallet } = this;
|
|
103
124
|
try {
|
|
104
125
|
const account = await originalWallet.getAccount(chainToConnect.chainId);
|
|
105
|
-
|
|
126
|
+
store.updateChainWalletState(walletName, chainToConnect.chainName, { account });
|
|
127
|
+
if (this.originalWallet instanceof core_1.WCWallet) {
|
|
128
|
+
this.originalWallet.setAccountToRestore(account);
|
|
129
|
+
}
|
|
106
130
|
return account;
|
|
107
131
|
}
|
|
108
132
|
catch (error) {
|
|
@@ -121,5 +145,28 @@ class StatefulWallet extends core_1.BaseWallet {
|
|
|
121
145
|
getChainById(chainId) {
|
|
122
146
|
return this.originalWallet.getChainById(chainId);
|
|
123
147
|
}
|
|
148
|
+
executeSpecificWalletMethod(WalletClass, callback) {
|
|
149
|
+
if (this.originalWallet instanceof WalletClass) {
|
|
150
|
+
return callback(this.originalWallet);
|
|
151
|
+
}
|
|
152
|
+
if (this.originalWallet instanceof core_1.WCWallet) {
|
|
153
|
+
return callback(this.originalWallet);
|
|
154
|
+
}
|
|
155
|
+
if (this.originalWallet instanceof core_1.ExtensionWallet) {
|
|
156
|
+
if (WalletClass === core_1.CosmosWallet) {
|
|
157
|
+
const cosmosWallet = this.originalWallet.getWalletByChainType('cosmos');
|
|
158
|
+
if (cosmosWallet) {
|
|
159
|
+
return callback(cosmosWallet);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (WalletClass === core_1.EthereumWallet) {
|
|
163
|
+
const ethereumWallet = this.originalWallet.getWalletByChainType('eip155');
|
|
164
|
+
if (ethereumWallet) {
|
|
165
|
+
return callback(ethereumWallet);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
124
171
|
}
|
|
125
172
|
exports.StatefulWallet = StatefulWallet;
|
package/store/store.d.ts
CHANGED
|
@@ -25,8 +25,8 @@ export interface InterchainStore extends WalletManager {
|
|
|
25
25
|
getDraftChainWalletState: (state: InterchainStore, walletName: string, chainName: string) => ChainWalletState;
|
|
26
26
|
getChainWalletState: (walletName: string, chainName: string) => ChainWalletState | undefined;
|
|
27
27
|
updateChainWalletState: (walletName: string, chainName: string, data: Partial<ChainWalletState>) => void;
|
|
28
|
-
createStatefulWallet: () => void;
|
|
29
28
|
getStatefulWalletByName: (walletName: string) => StatefulWallet | undefined;
|
|
29
|
+
setWalletConnectQRCodeUri: (uri: string) => void;
|
|
30
30
|
}
|
|
31
31
|
export type InterchainStoreData = {
|
|
32
32
|
chains: Chain[];
|
package/store/store.js
CHANGED
|
@@ -26,12 +26,7 @@ const createInterchainStore = (walletManager) => {
|
|
|
26
26
|
currentChainName: '',
|
|
27
27
|
chains: [...walletManager.chains],
|
|
28
28
|
assetLists: [...walletManager.assetLists],
|
|
29
|
-
wallets: walletManager.wallets.map(wallet =>
|
|
30
|
-
const walletSet = (fn) => {
|
|
31
|
-
set((draft) => fn(draft.wallets.find(w => w.info.name === wallet.info.name)));
|
|
32
|
-
};
|
|
33
|
-
return new stateful_wallet_1.StatefulWallet(wallet, walletSet, () => get().wallets.find(w => w.info.name === wallet.info.name), set, get);
|
|
34
|
-
}),
|
|
29
|
+
wallets: walletManager.wallets.map(wallet => new stateful_wallet_1.StatefulWallet(wallet, get)),
|
|
35
30
|
signerOptions: walletManager.signerOptions,
|
|
36
31
|
endpointOptions: walletManager.endpointOptions,
|
|
37
32
|
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
@@ -57,28 +52,6 @@ const createInterchainStore = (walletManager) => {
|
|
|
57
52
|
draft.chainWalletState[targetIndex] = { ...draft.chainWalletState[targetIndex], ...data };
|
|
58
53
|
});
|
|
59
54
|
},
|
|
60
|
-
createStatefulWallet: () => {
|
|
61
|
-
const wallets = walletManager.wallets.map(wallet => {
|
|
62
|
-
const walletSet = (fn) => {
|
|
63
|
-
set((draft) => fn(draft.wallets.find(w => w.info.name === wallet.info.name)));
|
|
64
|
-
};
|
|
65
|
-
return new stateful_wallet_1.StatefulWallet(wallet, walletSet, () => get().wallets.find(w => w.info.name === wallet.info.name), set, get);
|
|
66
|
-
});
|
|
67
|
-
set(draft => {
|
|
68
|
-
draft.wallets = wallets;
|
|
69
|
-
});
|
|
70
|
-
const defaultWalletStates = get().chainWalletState.reduce((acc, cws) => {
|
|
71
|
-
if (acc[cws.walletName] && cws.walletState === core_1.WalletState.Connected) {
|
|
72
|
-
return acc;
|
|
73
|
-
}
|
|
74
|
-
return { ...acc, [cws.walletName]: cws.walletState };
|
|
75
|
-
}, {});
|
|
76
|
-
set(draft => {
|
|
77
|
-
draft.wallets.forEach(wallet => {
|
|
78
|
-
wallet.walletState = defaultWalletStates[wallet.info.name];
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
},
|
|
82
55
|
init: async () => {
|
|
83
56
|
const oldChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
|
|
84
57
|
// get().createStatefulWallet()
|
|
@@ -105,42 +78,8 @@ const createInterchainStore = (walletManager) => {
|
|
|
105
78
|
});
|
|
106
79
|
});
|
|
107
80
|
});
|
|
108
|
-
|
|
109
|
-
const ExistWallets = [];
|
|
110
|
-
await Promise.all(get().wallets.map(async (wallet) => {
|
|
111
|
-
try {
|
|
112
|
-
await wallet.init();
|
|
113
|
-
ExistWallets.push(wallet.info.name);
|
|
114
|
-
}
|
|
115
|
-
catch (error) {
|
|
116
|
-
if (error === core_1.clientNotExistError) {
|
|
117
|
-
NotExistWallets.push(wallet.info.name);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}));
|
|
81
|
+
await Promise.all(get().wallets.map(async (wallet) => wallet.init()));
|
|
121
82
|
set(draft => {
|
|
122
|
-
draft.chainWalletState = draft.chainWalletState.map(cws => {
|
|
123
|
-
if (NotExistWallets.includes(cws.walletName)) {
|
|
124
|
-
return { ...cws, walletState: core_1.WalletState.NotExist };
|
|
125
|
-
}
|
|
126
|
-
return cws;
|
|
127
|
-
});
|
|
128
|
-
draft.chainWalletState = draft.chainWalletState.map(cws => {
|
|
129
|
-
if (ExistWallets.includes(cws.walletName)) {
|
|
130
|
-
const newState = cws.walletState === core_1.WalletState.NotExist ? core_1.WalletState.Disconnected : cws.walletState;
|
|
131
|
-
return { ...cws, walletState: newState };
|
|
132
|
-
}
|
|
133
|
-
return cws;
|
|
134
|
-
});
|
|
135
|
-
draft.chainWalletState.forEach(cws => {
|
|
136
|
-
const lastExistWallet = draft.wallets.find(w => w.info.name === cws.walletName);
|
|
137
|
-
if (cws.walletState === core_1.WalletState.Connected && lastExistWallet.walletState !== core_1.WalletState.Connected) {
|
|
138
|
-
lastExistWallet.walletState = core_1.WalletState.Connected;
|
|
139
|
-
}
|
|
140
|
-
if (cws.walletState === core_1.WalletState.NotExist) {
|
|
141
|
-
lastExistWallet.walletState = core_1.WalletState.NotExist;
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
83
|
draft.isReady = true;
|
|
145
84
|
});
|
|
146
85
|
},
|
|
@@ -150,6 +89,9 @@ const createInterchainStore = (walletManager) => {
|
|
|
150
89
|
setCurrentWalletName: (walletName) => {
|
|
151
90
|
set(draft => { draft.currentWalletName = walletName; });
|
|
152
91
|
},
|
|
92
|
+
setWalletConnectQRCodeUri: (uri) => {
|
|
93
|
+
set(draft => { draft.walletConnectQRCodeUri = uri; });
|
|
94
|
+
},
|
|
153
95
|
getDraftChainWalletState: (state, walletName, chainName) => {
|
|
154
96
|
const targetIndex = state.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
155
97
|
return state.chainWalletState[targetIndex];
|
|
@@ -263,17 +205,17 @@ const createInterchainStore = (walletManager) => {
|
|
|
263
205
|
getDownloadLink(walletName) {
|
|
264
206
|
return walletManager.getDownloadLink(walletName);
|
|
265
207
|
},
|
|
266
|
-
getOfflineSigner(walletName, chainName) {
|
|
208
|
+
async getOfflineSigner(walletName, chainName) {
|
|
267
209
|
return walletManager.getOfflineSigner(walletName, chainName);
|
|
268
210
|
},
|
|
269
211
|
getPreferSignType(chainName) {
|
|
270
212
|
const result = walletManager.getPreferSignType(chainName);
|
|
271
|
-
set(immerSyncUp(walletManager))
|
|
213
|
+
// set(immerSyncUp(walletManager))
|
|
272
214
|
return result;
|
|
273
215
|
},
|
|
274
216
|
getSignerOptions(chainName) {
|
|
275
217
|
const result = walletManager.getSignerOptions(chainName);
|
|
276
|
-
set(immerSyncUp(walletManager))
|
|
218
|
+
// set(immerSyncUp(walletManager))
|
|
277
219
|
return result;
|
|
278
220
|
},
|
|
279
221
|
getWalletByName(walletName) {
|
|
@@ -310,6 +252,12 @@ const createInterchainStore = (walletManager) => {
|
|
|
310
252
|
}
|
|
311
253
|
else {
|
|
312
254
|
// console.log('interchain-kit store hydration finished')
|
|
255
|
+
state.chainWalletState = state.chainWalletState.map(cws => {
|
|
256
|
+
return {
|
|
257
|
+
...cws,
|
|
258
|
+
account: cws.account ? (0, utils_1.restoreAccountFromLocalStorage)(cws.account) : null
|
|
259
|
+
};
|
|
260
|
+
});
|
|
313
261
|
}
|
|
314
262
|
};
|
|
315
263
|
},
|
package/utils/index.d.ts
CHANGED
package/utils/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./wallet"), exports);
|
|
18
18
|
__exportStar(require("./dedupeAsync"), exports);
|
|
19
|
+
__exportStar(require("./restoreAccount"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.restoreAccountFromLocalStorage = void 0;
|
|
4
|
+
const restoreAccountFromLocalStorage = (walletAccount) => {
|
|
5
|
+
const pubkey = walletAccount.pubkey;
|
|
6
|
+
if (typeof pubkey === 'object') {
|
|
7
|
+
// return from localstorage need to restructure to uinit8Array
|
|
8
|
+
return { ...walletAccount, pubkey: Uint8Array.from({ ...pubkey, length: Object.keys(pubkey).length }) };
|
|
9
|
+
}
|
|
10
|
+
return walletAccount;
|
|
11
|
+
};
|
|
12
|
+
exports.restoreAccountFromLocalStorage = restoreAccountFromLocalStorage;
|
package/utils/wallet.d.ts
CHANGED
|
@@ -14,7 +14,6 @@ export declare const transferToWalletUISchema: (w: BaseWallet) => {
|
|
|
14
14
|
originalWallet: {
|
|
15
15
|
session: import("@walletconnect/types").SessionTypes.Struct;
|
|
16
16
|
info: import("@interchain-kit/core").Wallet;
|
|
17
|
-
errorMessage: string;
|
|
18
17
|
events: import("events")<import("@interchain-kit/core").WalletEvents>;
|
|
19
18
|
chainMap: Map<import("@chain-registry/v2-types").Chain["chainId"], import("@chain-registry/v2-types").Chain>;
|
|
20
19
|
assetLists: import("@chain-registry/v2-types").AssetList[];
|
package/esm/modal/provider.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useContext, useEffect, useRef, useState } from "react";
|
|
3
|
-
import { WalletState } from "@interchain-kit/core";
|
|
4
|
-
import { useChainWallet, useWalletManager } from "../hooks";
|
|
5
|
-
import { transferToWalletUISchema } from "../utils";
|
|
6
|
-
import { InterchainWalletModal } from "./modal";
|
|
7
|
-
const WalletModalContext = createContext(null);
|
|
8
|
-
export const WalletModalProvider = ({ children, }) => {
|
|
9
|
-
const [modalIsOpen, setModalIsOpen] = useState(false);
|
|
10
|
-
const open = () => setModalIsOpen(true);
|
|
11
|
-
const close = () => setModalIsOpen(false);
|
|
12
|
-
const { chains, wallets, setCurrentWalletName, currentChainName, currentWalletName, walletConnectQRCodeUri, getDownloadLink, getEnv, connect, getAccount, } = useWalletManager();
|
|
13
|
-
const { wallet, status, disconnect, username, address, message } = useChainWallet(currentChainName || chains[0].chainName, currentWalletName);
|
|
14
|
-
const [shouldShowList, setShouldShowList] = useState(!(currentChainName && currentWalletName));
|
|
15
|
-
const walletsForUI = wallets.map(transferToWalletUISchema);
|
|
16
|
-
const handleCloseModal = () => {
|
|
17
|
-
close();
|
|
18
|
-
setShouldShowList(false);
|
|
19
|
-
};
|
|
20
|
-
const currentChainNameRef = useRef("");
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
currentChainNameRef.current = currentChainName;
|
|
23
|
-
}, [currentChainName]);
|
|
24
|
-
const handleConnectWallet = async (walletName) => {
|
|
25
|
-
const chainToConnect = currentChainNameRef.current || chains[0].chainName;
|
|
26
|
-
setShouldShowList(false);
|
|
27
|
-
setCurrentWalletName(walletName);
|
|
28
|
-
await connect(walletName, chainToConnect);
|
|
29
|
-
};
|
|
30
|
-
return (_jsxs(WalletModalContext.Provider, { value: { modalIsOpen, open, close }, children: [children, _jsx(InterchainWalletModal, { shouldShowList: shouldShowList, username: username, address: address, disconnect: disconnect, isOpen: modalIsOpen, open: open, close: handleCloseModal, wallets: walletsForUI, walletConnectQRCodeUri: walletConnectQRCodeUri, currentWallet: wallet, isConnecting: status === WalletState.Connecting, isConnected: status === WalletState.Connected, isRejected: status === WalletState.Rejected, isDisconnected: status === WalletState.Disconnected, isNotExist: status === WalletState.NotExist, errorMessage: message, onSelectWallet: (w) => handleConnectWallet(w.info.name), onBack: () => setShouldShowList(true), onReconnect: () => handleConnectWallet(currentWalletName), getDownloadLink: () => getDownloadLink(wallet?.info.name), getEnv: getEnv })] }));
|
|
31
|
-
};
|
|
32
|
-
export const useWalletModal = () => {
|
|
33
|
-
const context = useContext(WalletModalContext);
|
|
34
|
-
if (!context) {
|
|
35
|
-
throw new Error("useWalletModal must be used within a WalletModalProvider");
|
|
36
|
-
}
|
|
37
|
-
return context;
|
|
38
|
-
};
|
package/modal/provider.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
type WalletModalContextType = {
|
|
2
|
-
modalIsOpen: boolean;
|
|
3
|
-
open: () => void;
|
|
4
|
-
close: () => void;
|
|
5
|
-
};
|
|
6
|
-
export declare const WalletModalProvider: ({ children, }: {
|
|
7
|
-
children: React.ReactNode;
|
|
8
|
-
}) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
-
export declare const useWalletModal: () => WalletModalContextType;
|
|
10
|
-
export {};
|
package/modal/provider.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useWalletModal = exports.WalletModalProvider = void 0;
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const core_1 = require("@interchain-kit/core");
|
|
7
|
-
const hooks_1 = require("../hooks");
|
|
8
|
-
const utils_1 = require("../utils");
|
|
9
|
-
const modal_1 = require("./modal");
|
|
10
|
-
const WalletModalContext = (0, react_1.createContext)(null);
|
|
11
|
-
const WalletModalProvider = ({ children, }) => {
|
|
12
|
-
const [modalIsOpen, setModalIsOpen] = (0, react_1.useState)(false);
|
|
13
|
-
const open = () => setModalIsOpen(true);
|
|
14
|
-
const close = () => setModalIsOpen(false);
|
|
15
|
-
const { chains, wallets, setCurrentWalletName, currentChainName, currentWalletName, walletConnectQRCodeUri, getDownloadLink, getEnv, connect, getAccount, } = (0, hooks_1.useWalletManager)();
|
|
16
|
-
const { wallet, status, disconnect, username, address, message } = (0, hooks_1.useChainWallet)(currentChainName || chains[0].chainName, currentWalletName);
|
|
17
|
-
const [shouldShowList, setShouldShowList] = (0, react_1.useState)(!(currentChainName && currentWalletName));
|
|
18
|
-
const walletsForUI = wallets.map(utils_1.transferToWalletUISchema);
|
|
19
|
-
const handleCloseModal = () => {
|
|
20
|
-
close();
|
|
21
|
-
setShouldShowList(false);
|
|
22
|
-
};
|
|
23
|
-
const currentChainNameRef = (0, react_1.useRef)("");
|
|
24
|
-
(0, react_1.useEffect)(() => {
|
|
25
|
-
currentChainNameRef.current = currentChainName;
|
|
26
|
-
}, [currentChainName]);
|
|
27
|
-
const handleConnectWallet = async (walletName) => {
|
|
28
|
-
const chainToConnect = currentChainNameRef.current || chains[0].chainName;
|
|
29
|
-
setShouldShowList(false);
|
|
30
|
-
setCurrentWalletName(walletName);
|
|
31
|
-
await connect(walletName, chainToConnect);
|
|
32
|
-
};
|
|
33
|
-
return ((0, jsx_runtime_1.jsxs)(WalletModalContext.Provider, { value: { modalIsOpen, open, close }, children: [children, (0, jsx_runtime_1.jsx)(modal_1.InterchainWalletModal, { shouldShowList: shouldShowList, username: username, address: address, disconnect: disconnect, isOpen: modalIsOpen, open: open, close: handleCloseModal, wallets: walletsForUI, walletConnectQRCodeUri: walletConnectQRCodeUri, currentWallet: wallet, isConnecting: status === core_1.WalletState.Connecting, isConnected: status === core_1.WalletState.Connected, isRejected: status === core_1.WalletState.Rejected, isDisconnected: status === core_1.WalletState.Disconnected, isNotExist: status === core_1.WalletState.NotExist, errorMessage: message, onSelectWallet: (w) => handleConnectWallet(w.info.name), onBack: () => setShouldShowList(true), onReconnect: () => handleConnectWallet(currentWalletName), getDownloadLink: () => getDownloadLink(wallet?.info.name), getEnv: getEnv })] }));
|
|
34
|
-
};
|
|
35
|
-
exports.WalletModalProvider = WalletModalProvider;
|
|
36
|
-
const useWalletModal = () => {
|
|
37
|
-
const context = (0, react_1.useContext)(WalletModalContext);
|
|
38
|
-
if (!context) {
|
|
39
|
-
throw new Error("useWalletModal must be used within a WalletModalProvider");
|
|
40
|
-
}
|
|
41
|
-
return context;
|
|
42
|
-
};
|
|
43
|
-
exports.useWalletModal = useWalletModal;
|