@reown/appkit-scaffold-ui
Version:
#### 🔗 [Website](https://reown.com/appkit)
96 lines • 4.82 kB
JavaScript
import { ApiController, ConnectorController, CoreHelperUtil, OptionsController, StorageUtil } from '@reown/appkit-controllers';
import { HelpersUtil } from '@reown/appkit-utils';
import { ConnectorUtil } from './ConnectorUtil.js';
import { ConstantsUtil } from './ConstantsUtil.js';
export const WalletUtil = {
filterOutDuplicatesByRDNS(wallets) {
const connectors = OptionsController.state.enableEIP6963
? ConnectorController.state.connectors
: [];
const recent = StorageUtil.getRecentWallets();
const connectorRDNSs = connectors
.map(connector => connector.info?.rdns)
.filter(Boolean);
const recentRDNSs = recent.map(wallet => wallet.rdns).filter(Boolean);
const allRDNSs = connectorRDNSs.concat(recentRDNSs);
if (allRDNSs.includes('io.metamask.mobile') && CoreHelperUtil.isMobile()) {
const index = allRDNSs.indexOf('io.metamask.mobile');
allRDNSs[index] = 'io.metamask';
}
const filtered = wallets.filter(wallet => !allRDNSs.includes(String(wallet?.rdns)));
return filtered;
},
filterOutDuplicatesByIds(wallets) {
const connectors = ConnectorController.state.connectors.filter(connector => connector.type === 'ANNOUNCED' || connector.type === 'INJECTED');
const recent = StorageUtil.getRecentWallets();
const connectorIds = connectors.map(connector => connector.explorerId);
const recentIds = recent.map(wallet => wallet.id);
const allIds = connectorIds.concat(recentIds);
const filtered = wallets.filter(wallet => !allIds.includes(wallet?.id));
return filtered;
},
filterOutDuplicateWallets(wallets) {
const uniqueByRDNS = this.filterOutDuplicatesByRDNS(wallets);
const uniqueWallets = this.filterOutDuplicatesByIds(uniqueByRDNS);
return uniqueWallets;
},
markWalletsAsInstalled(wallets) {
const { connectors } = ConnectorController.state;
const { featuredWalletIds } = OptionsController.state;
const installedWalletRdnsMap = connectors
.filter(connector => connector.type === 'ANNOUNCED')
.reduce((rdnsMap, connector) => {
if (!connector.info?.rdns) {
return rdnsMap;
}
rdnsMap[connector.info.rdns] = true;
return rdnsMap;
}, {});
const walletsWithInstallationStatus = wallets.map(wallet => ({
...wallet,
installed: Boolean(wallet.rdns) && Boolean(installedWalletRdnsMap[wallet.rdns ?? ''])
}));
const sortedWallets = walletsWithInstallationStatus.sort((walletA, walletB) => {
const installationComparison = Number(walletB.installed) - Number(walletA.installed);
if (installationComparison !== 0) {
return installationComparison;
}
if (featuredWalletIds?.length) {
const walletAFeaturedIndex = featuredWalletIds.indexOf(walletA.id);
const walletBFeaturedIndex = featuredWalletIds.indexOf(walletB.id);
if (walletAFeaturedIndex !== -1 && walletBFeaturedIndex !== -1) {
return walletAFeaturedIndex - walletBFeaturedIndex;
}
if (walletAFeaturedIndex !== -1) {
return -1;
}
if (walletBFeaturedIndex !== -1) {
return 1;
}
}
return 0;
});
return sortedWallets;
},
getConnectOrderMethod(_features, _connectors) {
const connectMethodOrder = _features?.connectMethodsOrder || OptionsController.state.features?.connectMethodsOrder;
const connectors = _connectors || ConnectorController.state.connectors;
if (connectMethodOrder) {
return connectMethodOrder;
}
const { injected, announced } = ConnectorUtil.getConnectorsByType(connectors, ApiController.state.recommended, ApiController.state.featured);
const shownInjected = injected.filter(ConnectorUtil.showConnector);
const shownAnnounced = announced.filter(ConnectorUtil.showConnector);
if (shownInjected.length || shownAnnounced.length) {
return ['wallet', 'email', 'social'];
}
return ConstantsUtil.DEFAULT_CONNECT_METHOD_ORDER;
},
isExcluded(wallet) {
const isRDNSExcluded = Boolean(wallet.rdns) && ApiController.state.excludedWallets.some(w => w.rdns === wallet.rdns);
const isNameExcluded = Boolean(wallet.name) &&
ApiController.state.excludedWallets.some(w => HelpersUtil.isLowerCaseMatch(w.name, wallet.name));
return isRDNSExcluded || isNameExcluded;
}
};
//# sourceMappingURL=WalletUtil.js.map