@interchain-kit/react 0.0.1-beta.9 → 0.2.0
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 +1 -1
- package/README.md +173 -24
- package/esm/hooks/index.js +0 -4
- package/esm/hooks/useChain.js +28 -30
- package/esm/hooks/useChainWallet.js +25 -12
- package/esm/hooks/useWalletManager.js +4 -3
- package/esm/index.js +1 -0
- package/esm/modal/modal.js +96 -24
- package/esm/modal/provider.js +2 -2
- package/esm/modal/views/Connected.js +16 -17
- package/esm/modal/views/Connecting.js +14 -23
- package/esm/modal/views/NotExist.js +44 -0
- package/esm/modal/views/QRCode.js +19 -0
- package/esm/modal/views/Reject.js +4 -9
- package/esm/modal/views/WalletList.js +27 -19
- package/esm/modal/views/index.js +2 -0
- package/esm/provider.js +13 -13
- package/esm/store/chain-wallet.js +59 -0
- package/esm/store/index.js +191 -0
- package/esm/types/index.js +1 -1
- package/esm/utils/index.js +1 -8
- package/esm/utils/wallet.js +8 -1
- package/hooks/index.d.ts +0 -4
- package/hooks/index.js +0 -4
- package/hooks/useChain.d.ts +2 -2
- package/hooks/useChain.js +28 -30
- package/hooks/useChainWallet.d.ts +2 -2
- package/hooks/useChainWallet.js +25 -12
- package/hooks/useWalletManager.d.ts +1 -2
- package/hooks/useWalletManager.js +3 -2
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/modal/modal.js +95 -23
- package/modal/provider.d.ts +1 -1
- package/modal/provider.js +2 -2
- package/modal/views/Astronaut.js +1 -2
- package/modal/views/Connected.d.ts +6 -2
- package/modal/views/Connected.js +15 -16
- package/modal/views/Connecting.d.ts +6 -2
- package/modal/views/Connecting.js +12 -21
- package/modal/views/NotExist.d.ts +8 -0
- package/modal/views/NotExist.js +49 -0
- package/modal/views/QRCode.d.ts +4 -0
- package/modal/views/QRCode.js +24 -0
- package/modal/views/Reject.d.ts +7 -2
- package/modal/views/Reject.js +4 -9
- package/modal/views/WalletList.d.ts +4 -1
- package/modal/views/WalletList.js +27 -19
- package/modal/views/index.d.ts +2 -0
- package/modal/views/index.js +2 -0
- package/package.json +15 -9
- package/provider.d.ts +11 -11
- package/provider.js +13 -13
- package/store/chain-wallet.d.ts +26 -0
- package/store/chain-wallet.js +63 -0
- package/store/index.d.ts +45 -0
- package/store/index.js +195 -0
- package/types/chain.d.ts +12 -13
- package/types/index.d.ts +1 -1
- package/types/index.js +1 -1
- package/types/sign-client.d.ts +2 -0
- package/utils/index.d.ts +1 -7
- package/utils/index.js +15 -10
- package/utils/wallet.d.ts +7 -1
- package/utils/wallet.js +10 -15
- package/esm/hooks/useAccount.js +0 -28
- package/esm/hooks/useActiveWallet.js +0 -13
- package/esm/hooks/useConfig.js +0 -10
- package/esm/hooks/useConnect.js +0 -11
- package/esm/hooks/useInterchainClient.js +0 -52
- package/esm/hooks/useOfflineSigner.js +0 -15
- package/hooks/useAccount.d.ts +0 -2
- package/hooks/useAccount.js +0 -32
- package/hooks/useActiveWallet.d.ts +0 -1
- package/hooks/useActiveWallet.js +0 -17
- package/hooks/useConfig.d.ts +0 -8
- package/hooks/useConfig.js +0 -14
- package/hooks/useConnect.d.ts +0 -1
- package/hooks/useConnect.js +0 -15
- package/hooks/useInterchainClient.d.ts +0 -14
- package/hooks/useInterchainClient.js +0 -56
- package/hooks/useOfflineSigner.d.ts +0 -4
- package/hooks/useOfflineSigner.js +0 -19
- package/types/wallet.d.ts +0 -11
- /package/esm/types/{wallet.js → sign-client.js} +0 -0
- /package/types/{wallet.js → sign-client.js} +0 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { ConnectModalHead, ConnectModalStatus } from "@interchain-ui/react";
|
|
3
|
+
import { useWalletManager } from "../../hooks"; // replace with the actual path
|
|
4
|
+
import { useWalletModal } from "../provider";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { FaAndroid } from "@react-icons/all-files/fa/FaAndroid";
|
|
7
|
+
import { GoDesktopDownload } from "@react-icons/all-files/go/GoDesktopDownload";
|
|
8
|
+
import { GrFirefox } from "@react-icons/all-files/gr/GrFirefox";
|
|
9
|
+
import { RiAppStoreFill } from "@react-icons/all-files/ri/RiAppStoreFill";
|
|
10
|
+
import { RiChromeFill } from "@react-icons/all-files/ri/RiChromeFill";
|
|
11
|
+
export const NotExistHeader = ({ wallet, onBack, }) => {
|
|
12
|
+
const { close } = useWalletModal();
|
|
13
|
+
return (_jsx(ConnectModalHead, { title: wallet?.info?.prettyName || "", hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: { onClick: close } }));
|
|
14
|
+
};
|
|
15
|
+
export const NotExistContent = ({ wallet }) => {
|
|
16
|
+
const walletManager = useWalletManager();
|
|
17
|
+
const downloadLink = useMemo(() => {
|
|
18
|
+
return walletManager.getDownloadLink(wallet.info.name);
|
|
19
|
+
}, [wallet?.info?.name]);
|
|
20
|
+
const onInstall = () => {
|
|
21
|
+
if (downloadLink) {
|
|
22
|
+
window.open(downloadLink.link, "_blank");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const IconComp = getIcon(walletManager.getEnv());
|
|
26
|
+
return (_jsx(ConnectModalStatus, { status: "NotExist", wallet: {
|
|
27
|
+
name: wallet?.info?.prettyName,
|
|
28
|
+
logo: typeof wallet?.info?.logo === "string" ? wallet?.info?.logo : "",
|
|
29
|
+
mobileDisabled: true,
|
|
30
|
+
}, contentHeader: `${wallet?.info?.prettyName} Not Installed`, contentDesc: true
|
|
31
|
+
? `If ${wallet?.info?.prettyName.toLowerCase()} is installed on your device, please refresh this page or follow the browser instruction.`
|
|
32
|
+
: `Download link not provided. Try searching it or consulting the developer team.`, onInstall: onInstall, installIcon: _jsx(IconComp, {}), disableInstall: downloadLink === null }));
|
|
33
|
+
};
|
|
34
|
+
function getIcon(env) {
|
|
35
|
+
if (env?.browser === "chrome")
|
|
36
|
+
return RiChromeFill;
|
|
37
|
+
if (env?.browser === "firefox")
|
|
38
|
+
return GrFirefox;
|
|
39
|
+
if (env?.os === "android")
|
|
40
|
+
return FaAndroid;
|
|
41
|
+
if (env?.os === "ios")
|
|
42
|
+
return RiAppStoreFill;
|
|
43
|
+
return GoDesktopDownload;
|
|
44
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { ConnectModalHead, ConnectModalQRCode } from "@interchain-ui/react";
|
|
3
|
+
import { useWalletManager } from "../../hooks";
|
|
4
|
+
import { useWalletModal } from "../provider";
|
|
5
|
+
export const QRCodeHeader = ({ onBack }) => {
|
|
6
|
+
const { currentChainName, currentWalletName, getWalletByName, disconnect } = useWalletManager();
|
|
7
|
+
const { close } = useWalletModal();
|
|
8
|
+
const currentWallet = getWalletByName(currentWalletName);
|
|
9
|
+
return (_jsx(ConnectModalHead, { title: currentWallet?.info?.prettyName || "", hasBackButton: true, onClose: () => void 0, onBack: async () => {
|
|
10
|
+
await disconnect(currentWallet?.info?.name || "", currentChainName);
|
|
11
|
+
onBack();
|
|
12
|
+
}, closeButtonProps: { onClick: close } }));
|
|
13
|
+
};
|
|
14
|
+
export const QRCodeContent = () => {
|
|
15
|
+
const { currentChainName, currentWalletName, getWalletByName, connect } = useWalletManager();
|
|
16
|
+
const currentWallet = getWalletByName(currentWalletName);
|
|
17
|
+
const data = currentWallet.pairingUri;
|
|
18
|
+
return (_jsx(ConnectModalQRCode, { status: data ? "Done" : "Pending", link: data, description: "Open App to connect", errorTitle: "errorTitle", errorDesc: currentWallet.errorMessage || "", onRefresh: () => connect(currentWallet?.info?.name || "", currentChainName) }));
|
|
19
|
+
};
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { ConnectModalHead, ConnectModalStatus } from "@interchain-ui/react";
|
|
3
3
|
import { useWalletModal } from "../provider";
|
|
4
|
-
import { useActiveWallet, useWalletManager } from "../../hooks";
|
|
5
4
|
import { getWalletInfo } from "../../utils";
|
|
6
|
-
export const RejectHeader = ({ onBack }) => {
|
|
5
|
+
export const RejectHeader = ({ wallet, onBack, }) => {
|
|
7
6
|
const { close } = useWalletModal();
|
|
8
|
-
|
|
9
|
-
return (_jsx(ConnectModalHead, { title: activeWallet.option.prettyName, hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: { onClick: close } }));
|
|
7
|
+
return (_jsx(ConnectModalHead, { title: wallet?.info?.prettyName, hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: { onClick: close } }));
|
|
10
8
|
};
|
|
11
|
-
export const RejectContent = () => {
|
|
12
|
-
|
|
13
|
-
const walletManager = useWalletManager();
|
|
14
|
-
const { close } = useWalletModal();
|
|
15
|
-
return (_jsx(ConnectModalStatus, { status: "Rejected", wallet: getWalletInfo(activeWallet), contentHeader: 'Request Rejected', contentDesc: activeWallet.errorMessage || 'Connection permission is denied.', onConnect: () => walletManager.connect(activeWallet.option.name).then(close) }));
|
|
9
|
+
export const RejectContent = ({ wallet, onReconnect, }) => {
|
|
10
|
+
return (_jsx(ConnectModalStatus, { status: "Rejected", wallet: getWalletInfo(wallet), contentHeader: "Request Rejected", contentDesc: wallet.errorMessage || "Connection permission is denied.", onConnect: onReconnect }));
|
|
16
11
|
};
|
|
@@ -6,27 +6,35 @@ export const WalletListHeader = () => {
|
|
|
6
6
|
const { close } = useWalletModal();
|
|
7
7
|
return (_jsx(ConnectModalHead, { title: "Select your wallet", hasBackButton: false, onClose: close, closeButtonProps: { onClick: close } }));
|
|
8
8
|
};
|
|
9
|
-
export const WalletListContent = () => {
|
|
9
|
+
export const WalletListContent = ({ onSelectWallet, }) => {
|
|
10
10
|
const walletManager = useWalletManager();
|
|
11
|
-
const { close } = useWalletModal();
|
|
12
11
|
const wallets = walletManager.wallets.map((w) => {
|
|
13
|
-
return
|
|
14
|
-
name: w.
|
|
15
|
-
prettyName: w.
|
|
16
|
-
logo: w.
|
|
12
|
+
return {
|
|
13
|
+
name: w.info.name,
|
|
14
|
+
prettyName: w.info.prettyName,
|
|
15
|
+
logo: w.info.logo,
|
|
17
16
|
mobileDisabled: true,
|
|
18
|
-
shape:
|
|
19
|
-
originalWallet:
|
|
20
|
-
|
|
17
|
+
shape: "list",
|
|
18
|
+
originalWallet: {
|
|
19
|
+
...w,
|
|
20
|
+
pairing: null,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
21
23
|
});
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
const wcWallet = walletManager.wallets.find((w) => w.info.mode === "wallet-connect");
|
|
25
|
+
if (wcWallet) {
|
|
26
|
+
const activePairings = wcWallet.getActivePairing();
|
|
27
|
+
activePairings.forEach((pairing) => {
|
|
28
|
+
wallets.push({
|
|
29
|
+
name: pairing?.peerMetadata?.name,
|
|
30
|
+
prettyName: pairing?.peerMetadata?.name,
|
|
31
|
+
logo: pairing?.peerMetadata?.icons?.[0],
|
|
32
|
+
mobileDisabled: true,
|
|
33
|
+
shape: "list",
|
|
34
|
+
originalWallet: { ...wcWallet, pairing },
|
|
35
|
+
subLogo: wcWallet.info.logo,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return (_jsx(ConnectModalWalletList, { wallets: wallets, onWalletItemClick: onSelectWallet }));
|
|
32
40
|
};
|
package/esm/modal/views/index.js
CHANGED
package/esm/provider.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect,
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
3
|
import { createContext, useContext } from "react";
|
|
4
|
-
import { WalletManager } from
|
|
5
|
-
import { WalletModalProvider } from
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
import { WalletManager, } from "@interchain-kit/core";
|
|
5
|
+
import { WalletModalProvider } from "./modal";
|
|
6
|
+
import { createInterchainStore } from "./store";
|
|
7
|
+
const InterchainWalletContext = createContext(null);
|
|
8
|
+
export const ChainProvider = ({ chains, assetLists, wallets, signerOptions, endpointOptions, children, }) => {
|
|
9
|
+
// const [_, forceRender] = useState({});
|
|
10
|
+
const walletManager = new WalletManager(chains, assetLists, wallets, signerOptions, endpointOptions);
|
|
11
|
+
const store = useRef(createInterchainStore(walletManager));
|
|
12
12
|
useEffect(() => {
|
|
13
13
|
walletManager.init();
|
|
14
14
|
}, []);
|
|
15
|
-
return (_jsx(
|
|
15
|
+
return (_jsx(InterchainWalletContext.Provider, { value: store.current, children: _jsx(WalletModalProvider, { children: children }) }));
|
|
16
16
|
};
|
|
17
|
-
export const
|
|
18
|
-
const context = useContext(
|
|
17
|
+
export const useInterchainWalletContext = () => {
|
|
18
|
+
const context = useContext(InterchainWalletContext);
|
|
19
19
|
if (!context) {
|
|
20
|
-
throw new Error(
|
|
20
|
+
throw new Error("useInterChainWalletContext must be used within a InterChainProvider");
|
|
21
21
|
}
|
|
22
22
|
return context;
|
|
23
23
|
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { BaseWallet } from "@interchain-kit/core";
|
|
2
|
+
export class ChainWallet extends BaseWallet {
|
|
3
|
+
originalWallet;
|
|
4
|
+
connectWithState;
|
|
5
|
+
disconnectWithState;
|
|
6
|
+
getAccountWithState;
|
|
7
|
+
constructor(originalWallet, connectWithState, disconnectWithState, getAccountWithState) {
|
|
8
|
+
super(originalWallet?.info);
|
|
9
|
+
this.originalWallet = originalWallet;
|
|
10
|
+
this.connectWithState = connectWithState;
|
|
11
|
+
this.disconnectWithState = disconnectWithState;
|
|
12
|
+
this.getAccountWithState = getAccountWithState;
|
|
13
|
+
}
|
|
14
|
+
async init(meta) {
|
|
15
|
+
return this.originalWallet.init(meta);
|
|
16
|
+
}
|
|
17
|
+
async connect(chainId) {
|
|
18
|
+
return this.connectWithState(chainId);
|
|
19
|
+
}
|
|
20
|
+
async disconnect(chainId) {
|
|
21
|
+
return this.disconnectWithState(chainId);
|
|
22
|
+
}
|
|
23
|
+
async getAccount(chainId) {
|
|
24
|
+
return this.getAccountWithState(chainId);
|
|
25
|
+
}
|
|
26
|
+
async getAccounts(chainIds) {
|
|
27
|
+
return this.originalWallet.getAccounts(chainIds);
|
|
28
|
+
}
|
|
29
|
+
async getSimpleAccount(chainId) {
|
|
30
|
+
return this.originalWallet.getSimpleAccount(chainId);
|
|
31
|
+
}
|
|
32
|
+
getOfflineSigner(chainId) {
|
|
33
|
+
return this.originalWallet.getOfflineSigner(chainId);
|
|
34
|
+
}
|
|
35
|
+
getOfflineSignerAmino(chainId) {
|
|
36
|
+
return this.originalWallet.getOfflineSignerAmino(chainId);
|
|
37
|
+
}
|
|
38
|
+
getOfflineSignerDirect(chainId) {
|
|
39
|
+
return this.originalWallet.getOfflineSignerDirect(chainId);
|
|
40
|
+
}
|
|
41
|
+
async signAmino(chainId, signer, signDoc, signOptions) {
|
|
42
|
+
return this.originalWallet.signAmino(chainId, signer, signDoc, signOptions);
|
|
43
|
+
}
|
|
44
|
+
async signArbitrary(chainId, signer, data) {
|
|
45
|
+
return this.originalWallet.signArbitrary(chainId, signer, data);
|
|
46
|
+
}
|
|
47
|
+
async verifyArbitrary(chainId, signer, data) {
|
|
48
|
+
return this.originalWallet.verifyArbitrary(chainId, signer, data);
|
|
49
|
+
}
|
|
50
|
+
async signDirect(chainId, signer, signDoc, signOptions) {
|
|
51
|
+
return this.originalWallet.signDirect(chainId, signer, signDoc, signOptions);
|
|
52
|
+
}
|
|
53
|
+
async sendTx(chainId, tx, mode) {
|
|
54
|
+
return this.originalWallet.sendTx(chainId, tx, mode);
|
|
55
|
+
}
|
|
56
|
+
async addSuggestChain(chain, assetLists) {
|
|
57
|
+
return this.originalWallet.addSuggestChain(chain, assetLists);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { WalletState } from "@interchain-kit/core";
|
|
2
|
+
import { createStore } from "zustand";
|
|
3
|
+
import { immer } from "zustand/middleware/immer";
|
|
4
|
+
import { persist, createJSONStorage } from 'zustand/middleware';
|
|
5
|
+
const immerSyncUp = (newWalletManager) => {
|
|
6
|
+
return (draft) => {
|
|
7
|
+
draft.chains = newWalletManager.chains;
|
|
8
|
+
draft.assetLists = newWalletManager.assetLists;
|
|
9
|
+
draft.wallets = newWalletManager.wallets;
|
|
10
|
+
draft.signerOptions = newWalletManager.signerOptions;
|
|
11
|
+
draft.endpointOptions = newWalletManager.endpointOptions;
|
|
12
|
+
draft.signerOptionMap = newWalletManager.signerOptionMap;
|
|
13
|
+
draft.endpointOptionsMap = newWalletManager.endpointOptionsMap;
|
|
14
|
+
draft.preferredSignTypeMap = newWalletManager.preferredSignTypeMap;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export const createInterchainStore = (walletManager) => {
|
|
18
|
+
const { chains, assetLists, wallets, signerOptions, endpointOptions } = walletManager;
|
|
19
|
+
// const walletManager = new WalletManager(chains, assetLists, wallets, signerOptions, endpointOptions)
|
|
20
|
+
const chainWalletState = [];
|
|
21
|
+
wallets.forEach(wallet => {
|
|
22
|
+
chains.forEach(chain => {
|
|
23
|
+
chainWalletState.push({
|
|
24
|
+
chainName: chain.chainName,
|
|
25
|
+
walletName: wallet.info.name,
|
|
26
|
+
walletState: WalletState.Disconnected,
|
|
27
|
+
rpcEndpoint: "",
|
|
28
|
+
errorMessage: "",
|
|
29
|
+
signerOption: undefined,
|
|
30
|
+
account: undefined
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return createStore(persist(immer((set, get) => ({
|
|
35
|
+
chainWalletState,
|
|
36
|
+
currentWalletName: '',
|
|
37
|
+
currentChainName: '',
|
|
38
|
+
chains: [...walletManager.chains],
|
|
39
|
+
assetLists: [...walletManager.assetLists],
|
|
40
|
+
wallets: walletManager.wallets,
|
|
41
|
+
signerOptions: walletManager.signerOptions,
|
|
42
|
+
endpointOptions: walletManager.endpointOptions,
|
|
43
|
+
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
44
|
+
signerOptionMap: { ...walletManager.signerOptionMap },
|
|
45
|
+
endpointOptionsMap: { ...walletManager.endpointOptionsMap },
|
|
46
|
+
updateChainWalletState: (walletName, chainName, data) => {
|
|
47
|
+
set(draft => {
|
|
48
|
+
let targetIndex = draft.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
49
|
+
draft.chainWalletState[targetIndex] = { ...draft.chainWalletState[targetIndex], ...data };
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
init: () => walletManager.init(),
|
|
53
|
+
setCurrentChainName: (chainName) => {
|
|
54
|
+
set(draft => { draft.currentChainName = chainName; });
|
|
55
|
+
},
|
|
56
|
+
setCurrentWalletName: (walletName) => {
|
|
57
|
+
set(draft => { draft.currentWalletName = walletName; });
|
|
58
|
+
},
|
|
59
|
+
getDraftChainWalletState: (state, walletName, chainName) => {
|
|
60
|
+
const targetIndex = state.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
61
|
+
return state.chainWalletState[targetIndex];
|
|
62
|
+
},
|
|
63
|
+
getChainWalletState: (walletName, chainName) => {
|
|
64
|
+
return get().chainWalletState.find(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
65
|
+
},
|
|
66
|
+
addChains: (chains, assetLists, signerOptions, endpointOptions) => {
|
|
67
|
+
walletManager.addChains(chains, assetLists, signerOptions, endpointOptions);
|
|
68
|
+
// console.log(walletManager.chains, walletManager.assetLists)
|
|
69
|
+
// set(immerSyncUp(walletManager))
|
|
70
|
+
// set(draft => {
|
|
71
|
+
// draft.chains = walletManager.chains
|
|
72
|
+
// })
|
|
73
|
+
set(draft => {
|
|
74
|
+
chains.forEach(newChain => {
|
|
75
|
+
const existChain = draft.chains.find(c => c.chainId === newChain.chainId);
|
|
76
|
+
if (!existChain) {
|
|
77
|
+
draft.chains.push(newChain);
|
|
78
|
+
const assetList = assetLists.find(a => a.chainName === newChain.chainName);
|
|
79
|
+
draft.assetLists.push(assetList);
|
|
80
|
+
draft.wallets.forEach(w => {
|
|
81
|
+
draft.chainWalletState.push({
|
|
82
|
+
chainName: newChain.chainName,
|
|
83
|
+
walletName: w.info.name,
|
|
84
|
+
walletState: WalletState.Disconnected,
|
|
85
|
+
rpcEndpoint: "",
|
|
86
|
+
errorMessage: "",
|
|
87
|
+
signerOption: signerOptions?.signing?.(newChain.chainName),
|
|
88
|
+
account: undefined
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
draft.signerOptionMap[newChain.chainName] = signerOptions?.signing?.(newChain.chainName);
|
|
93
|
+
draft.endpointOptionsMap[newChain.chainName] = endpointOptions?.endpoints?.[newChain.chainName];
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
connect: async (walletName, chainName) => {
|
|
98
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connecting });
|
|
99
|
+
try {
|
|
100
|
+
await walletManager.connect(walletName, chainName);
|
|
101
|
+
set(draft => {
|
|
102
|
+
draft.currentChainName = chainName;
|
|
103
|
+
draft.currentWalletName = walletName;
|
|
104
|
+
});
|
|
105
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connected });
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (error.message === 'Request rejected') {
|
|
109
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Rejected, errorMessage: error.message });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Disconnected, errorMessage: error.message });
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
disconnect: async (walletName, chainName) => {
|
|
116
|
+
try {
|
|
117
|
+
await walletManager.disconnect(walletName, chainName);
|
|
118
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Disconnected, account: null });
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
getAccount: async (walletName, chainName) => {
|
|
124
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
125
|
+
get().updateChainWalletState(walletName, chainName, { account });
|
|
126
|
+
return account;
|
|
127
|
+
},
|
|
128
|
+
getRpcEndpoint: async (walletName, chainName) => {
|
|
129
|
+
const exist = get().getChainWalletState(walletName, chainName).rpcEndpoint;
|
|
130
|
+
if (exist)
|
|
131
|
+
return exist;
|
|
132
|
+
const rpcEndpoint = await walletManager.getRpcEndpoint(walletName, chainName);
|
|
133
|
+
get().updateChainWalletState(walletName, chainName, { rpcEndpoint });
|
|
134
|
+
return rpcEndpoint;
|
|
135
|
+
},
|
|
136
|
+
getChainLogoUrl(chainName) {
|
|
137
|
+
return walletManager.getChainLogoUrl(chainName);
|
|
138
|
+
},
|
|
139
|
+
getChainByName(chainName) {
|
|
140
|
+
return walletManager.getChainByName(chainName);
|
|
141
|
+
},
|
|
142
|
+
getAssetListByName(chainName) {
|
|
143
|
+
return walletManager.getAssetListByName(chainName);
|
|
144
|
+
},
|
|
145
|
+
getDownloadLink(walletName) {
|
|
146
|
+
return walletManager.getDownloadLink(walletName);
|
|
147
|
+
},
|
|
148
|
+
getOfflineSigner(walletName, chainName) {
|
|
149
|
+
return walletManager.getOfflineSigner(walletName, chainName);
|
|
150
|
+
},
|
|
151
|
+
getPreferSignType(chainName) {
|
|
152
|
+
const result = walletManager.getPreferSignType(chainName);
|
|
153
|
+
set(immerSyncUp(walletManager));
|
|
154
|
+
return result;
|
|
155
|
+
},
|
|
156
|
+
getSignerOptions(chainName) {
|
|
157
|
+
const result = walletManager.getSignerOptions(chainName);
|
|
158
|
+
set(immerSyncUp(walletManager));
|
|
159
|
+
return result;
|
|
160
|
+
},
|
|
161
|
+
getWalletByName(walletName) {
|
|
162
|
+
return walletManager.getWalletByName(walletName);
|
|
163
|
+
},
|
|
164
|
+
getSigningClient(walletName, chainName) {
|
|
165
|
+
return walletManager.getSigningClient(walletName, chainName);
|
|
166
|
+
},
|
|
167
|
+
getEnv() {
|
|
168
|
+
return walletManager.getEnv();
|
|
169
|
+
},
|
|
170
|
+
})), {
|
|
171
|
+
name: 'interchain-kit-store',
|
|
172
|
+
storage: createJSONStorage(() => localStorage),
|
|
173
|
+
partialize: state => ({
|
|
174
|
+
chainWalletState: state.chainWalletState,
|
|
175
|
+
currentWalletName: state.currentWalletName,
|
|
176
|
+
currentChainName: state.currentChainName
|
|
177
|
+
}),
|
|
178
|
+
onRehydrateStorage: (state) => {
|
|
179
|
+
console.log('interchain-kit store hydration starts');
|
|
180
|
+
// optional
|
|
181
|
+
return (state, error) => {
|
|
182
|
+
if (error) {
|
|
183
|
+
console.log('an error happened during hydration', error);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
console.log('interchain-kit store hydration finished');
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
}));
|
|
191
|
+
};
|
package/esm/types/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './sign-client';
|
package/esm/utils/index.js
CHANGED
package/esm/utils/wallet.js
CHANGED
package/hooks/index.d.ts
CHANGED
package/hooks/index.js
CHANGED
|
@@ -15,9 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./useWalletManager"), exports);
|
|
18
|
-
__exportStar(require("./useActiveWallet"), exports);
|
|
19
|
-
__exportStar(require("./useAccount"), exports);
|
|
20
18
|
__exportStar(require("./useChain"), exports);
|
|
21
|
-
__exportStar(require("./useConnect"), exports);
|
|
22
19
|
__exportStar(require("./useChainWallet"), exports);
|
|
23
|
-
__exportStar(require("./useConfig"), exports);
|
package/hooks/useChain.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const useChain: (chainName: string) => UseChainReturnType
|
|
1
|
+
import { UseChainReturnType } from '../types/chain';
|
|
2
|
+
export declare const useChain: (chainName: string) => UseChainReturnType;
|
package/hooks/useChain.js
CHANGED
|
@@ -1,46 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useChain = void 0;
|
|
4
|
-
const useWalletManager_1 = require("./useWalletManager");
|
|
5
|
-
const useAccount_1 = require("./useAccount");
|
|
6
|
-
const useActiveWallet_1 = require("./useActiveWallet");
|
|
7
|
-
const useInterchainClient_1 = require("./useInterchainClient");
|
|
8
4
|
const modal_1 = require("../modal");
|
|
9
|
-
const
|
|
5
|
+
const useWalletManager_1 = require("./useWalletManager");
|
|
6
|
+
const core_1 = require("@interchain-kit/core");
|
|
7
|
+
const chain_wallet_1 = require("../store/chain-wallet");
|
|
10
8
|
const useChain = (chainName) => {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const activeWallet = (0, useActiveWallet_1.useActiveWallet)();
|
|
15
|
-
const account = (0, useAccount_1.useAccount)(chainName, activeWallet?.option?.name);
|
|
16
|
-
const interchainClient = (0, useInterchainClient_1.useInterchainClient)(chainName, activeWallet?.option?.name);
|
|
17
|
-
if (!chainToShow) {
|
|
9
|
+
const { assetLists, currentWalletName, disconnect, setCurrentChainName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = (0, useWalletManager_1.useWalletManager)();
|
|
10
|
+
const chain = getChainByName(chainName);
|
|
11
|
+
if (!chain) {
|
|
18
12
|
throw new core_1.ChainNameNotExist(chainName);
|
|
19
13
|
}
|
|
14
|
+
const assetList = assetLists.find(a => a.chainName === chainName);
|
|
15
|
+
const wallet = getWalletByName(currentWalletName);
|
|
16
|
+
const chainWalletStateToShow = getChainWalletState(currentWalletName, chainName);
|
|
20
17
|
const { open, close } = (0, modal_1.useWalletModal)();
|
|
21
|
-
|
|
18
|
+
return {
|
|
19
|
+
//for migration cosmos kit
|
|
22
20
|
connect: () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
setCurrentChainName(chainName);
|
|
22
|
+
open();
|
|
23
|
+
},
|
|
24
|
+
disconnect: async () => disconnect(currentWalletName, chainName),
|
|
25
|
+
openView: () => {
|
|
26
|
+
setCurrentChainName(chainName);
|
|
26
27
|
open();
|
|
27
28
|
},
|
|
28
|
-
openView: open,
|
|
29
29
|
closeView: close,
|
|
30
|
-
getRpcEndpoint: () =>
|
|
31
|
-
status:
|
|
32
|
-
username: account?.username,
|
|
33
|
-
message:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
chain: chainToShow,
|
|
30
|
+
getRpcEndpoint: () => getRpcEndpoint(currentWalletName, chainName),
|
|
31
|
+
status: chainWalletStateToShow?.walletState,
|
|
32
|
+
username: chainWalletStateToShow?.account?.username,
|
|
33
|
+
message: chainWalletStateToShow?.errorMessage,
|
|
34
|
+
// new api
|
|
35
|
+
logoUrl: getChainLogoUrl(chainName),
|
|
36
|
+
chain,
|
|
39
37
|
assetList,
|
|
40
|
-
address: account?.address,
|
|
41
|
-
wallet:
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
address: chainWalletStateToShow?.account?.address,
|
|
39
|
+
wallet: new chain_wallet_1.ChainWallet(wallet, () => connect(currentWalletName, chainName), () => disconnect(currentWalletName, chainName), () => getAccount(currentWalletName, chainName)),
|
|
40
|
+
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
41
|
+
getSigningClient: () => getSigningClient(currentWalletName, chainName),
|
|
44
42
|
};
|
|
45
43
|
};
|
|
46
44
|
exports.useChain = useChain;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const useChainWallet: (chainName: string, walletName: string) =>
|
|
1
|
+
import { UseChainWalletReturnType } from "../types/chain";
|
|
2
|
+
export declare const useChainWallet: (chainName: string, walletName: string) => UseChainWalletReturnType;
|
package/hooks/useChainWallet.js
CHANGED
|
@@ -2,21 +2,34 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useChainWallet = void 0;
|
|
4
4
|
const useWalletManager_1 = require("./useWalletManager");
|
|
5
|
-
const
|
|
6
|
-
const useInterchainClient_1 = require("./useInterchainClient");
|
|
5
|
+
const chain_wallet_1 = require("../store/chain-wallet");
|
|
7
6
|
const useChainWallet = (chainName, walletName) => {
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const interchainClient = (0, useInterchainClient_1.useInterchainClient)(chainName, walletName);
|
|
7
|
+
const { assetLists, disconnect, setCurrentChainName, setCurrentWalletName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = (0, useWalletManager_1.useWalletManager)();
|
|
8
|
+
const chain = getChainByName(chainName);
|
|
9
|
+
const wallet = getWalletByName(walletName);
|
|
10
|
+
const assetList = assetLists.find(a => a.chainName === chainName);
|
|
11
|
+
const chainWalletStateToShow = getChainWalletState(walletName, chainName);
|
|
14
12
|
return {
|
|
15
|
-
|
|
13
|
+
//for migration cosmos kit
|
|
14
|
+
connect: async () => {
|
|
15
|
+
await connect(walletName, chainName);
|
|
16
|
+
setCurrentWalletName(walletName);
|
|
17
|
+
setCurrentChainName(chainName);
|
|
18
|
+
await getAccount(walletName, chainName);
|
|
19
|
+
},
|
|
20
|
+
disconnect: () => disconnect(walletName, chainName),
|
|
21
|
+
getRpcEndpoint: () => getRpcEndpoint(walletName, chainName),
|
|
22
|
+
status: chainWalletStateToShow?.walletState,
|
|
23
|
+
username: chainWalletStateToShow?.account?.username,
|
|
24
|
+
message: chainWalletStateToShow?.errorMessage,
|
|
25
|
+
// new api
|
|
26
|
+
logoUrl: getChainLogoUrl(chainName),
|
|
27
|
+
chain,
|
|
16
28
|
assetList,
|
|
17
|
-
address: account?.address,
|
|
18
|
-
wallet,
|
|
19
|
-
|
|
29
|
+
address: chainWalletStateToShow?.account?.address,
|
|
30
|
+
wallet: new chain_wallet_1.ChainWallet(wallet, () => connect(walletName, chainName), () => disconnect(walletName, chainName), () => getAccount(walletName, chainName)),
|
|
31
|
+
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
32
|
+
getSigningClient: () => getSigningClient(walletName, chainName),
|
|
20
33
|
};
|
|
21
34
|
};
|
|
22
35
|
exports.useChainWallet = useChainWallet;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const useWalletManager: () => WalletManager;
|
|
1
|
+
export declare const useWalletManager: () => import("..").InterchainStore;
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useWalletManager = void 0;
|
|
4
4
|
const provider_1 = require("../provider");
|
|
5
|
+
const zustand_1 = require("zustand");
|
|
5
6
|
const useWalletManager = () => {
|
|
6
|
-
const
|
|
7
|
-
return
|
|
7
|
+
const store = (0, provider_1.useInterchainWalletContext)();
|
|
8
|
+
return (0, zustand_1.useStore)(store);
|
|
8
9
|
};
|
|
9
10
|
exports.useWalletManager = useWalletManager;
|
package/index.d.ts
CHANGED