UNPKG

@web3-wallet/core

Version:
114 lines (113 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCurrentWallet = exports.isWallet = void 0; const zustand_1 = require("zustand"); const middleware_1 = require("zustand/middleware"); const types_1 = require("./types"); const isWallet = (connectorOrWallet) => typeof connectorOrWallet.getConnector === 'function'; exports.isWallet = isWallet; const createCurrentWallet = (connectorsOrWallets, options) => { const { defaultCurrentWallet, persistKey = '@web3-wallet/current-wallet' } = options ?? {}; const connectors = connectorsOrWallets.map((v) => (0, exports.isWallet)(v) ? v.getConnector() : v); const getConnector = (walletName) => { return connectors.find((v) => v.walletName === walletName); }; const currentWalletName = defaultCurrentWallet || connectors[0].walletName; const DEFAULT_STATE = { connectionStatus: types_1.WalletConnectionStatus.Untouched, walletName: currentWalletName, ...getConnector(currentWalletName).store.getState(), }; const store = (0, zustand_1.create)()((0, middleware_1.persist)(() => DEFAULT_STATE, { name: persistKey, version: 0, partialize: ({ walletName, connectionStatus }) => ({ isConnecting: false, chainId: undefined, accounts: [], walletName, connectionStatus, }), })); const getCurrentConnector = () => { const walletName = store.getState().walletName; const connector = getConnector(walletName); if (connector) return connector; console.debug(`Wallet ${walletName}, don't exist, reset current wallet to ${connectors[0].walletName}`); store.setState({ walletName: connectors[0].walletName, connectionStatus: types_1.WalletConnectionStatus.Untouched, }); return connectors[0]; }; let unsubscribe; const switchCurrentWallet = (walletName) => { if (getConnector(walletName)) { store.setState({ walletName, ...getConnector(walletName).store.getState(), }); unsubscribe?.(); unsubscribe = getConnector(walletName).store.subscribe((state) => { // copy the wallet store state to current wallet store store.setState({ ...state, }); }); } else { console.debug(`Wallet '${walletName}' don't exists`); } }; // update current wallet store switchCurrentWallet(store.getState().walletName); const getConnect = (walletName) => async (...args) => { const connector = walletName ? getConnector(walletName) : getCurrentConnector(); const result = await connector.connect(...args); store.setState({ walletName: connector.walletName, connectionStatus: types_1.WalletConnectionStatus.Connected, }); return result; }; const autoConnect = async (...args) => { const connector = getCurrentConnector(); if (store.getState().connectionStatus === types_1.WalletConnectionStatus.Disconnected) { return false; } const result = await connector.autoConnect(...args); store.setState({ walletName: connector.walletName, connectionStatus: types_1.WalletConnectionStatus.Connected, }); return result; }; const disconnect = async (...args) => { const connector = getCurrentConnector(); const result = await connector.disconnect(...args); store.setState({ connectionStatus: types_1.WalletConnectionStatus.Disconnected, }); return result; }; return { getWalletName: () => getCurrentConnector().walletName, getStore: () => store, getConnector: getCurrentConnector, connect: getConnect(), autoConnect, disconnect, watchAsset: (...args) => getCurrentConnector().watchAsset(...args), detectProvider: () => getCurrentConnector().detectProvider(), // current wallet only apis switchCurrentWallet, connectAsCurrentWallet: (walletName, ...args) => { switchCurrentWallet(walletName); return getConnect(walletName)(...args); }, }; }; exports.createCurrentWallet = createCurrentWallet;