import { WalletModule , WalletInitOptions , AllWalletInitOptions } from '../../interfaces' ; import { isWalletInit } from '../../validation' ; import { getProviderName } from '../../utilities' ; const desktopDefaultWalletNames = [ 'tally' , 'metamask' , 'binance' , 'mathwallet' , 'frame' , 'torus' , 'opera' , 'liquality' , 'blankwallet' ] ; const mobileDefaultWalletNames = [ 'metamask' , 'coinbase' , 'trust' , 'torus' , 'opera' , 'operaTouch' , 'status' , 'hyperpay' , 'tokenpocket' , 'mathwallet' , 'dcent' , 'atoken' , 'liquality' , 'alphawallet' , 'ownbit' , 'bitpie' , 'authereum' , 'tp' ] ; const providerNameToWalletName = ( providerName ) => providerName === 'imToken' ? providerName : providerName === 'WalletConnect' ? 'walletConnect' : providerName . toLocaleLowerCase ( ) ; function select ( wallets , networkId , isMobile ) { let detectedProviderName ; let detectedWalletName ; if ( window ?. ethereum ) { detectedProviderName = getProviderName ( window . ethereum ) ; if ( detectedProviderName ) { detectedWalletName = providerNameToWalletName ( detectedProviderName ) ; } } if ( wallets ) { const hideWallet = ( wallet ) => wallet ?. display && wallet ?. display [ isMobile ? 'mobile' : 'desktop' ] === false ; const walletNotIncluded = wallets . every ( wallet => isWalletInit ( wallet ) && wallet . walletName !== detectedWalletName ) ; if ( detectedWalletName && walletNotIncluded ) { wallets . unshift ( { walletName : detectedWalletName } ) ; } else if ( ! detectedWalletName && detectedProviderName ) { wallets . unshift ( { walletName : 'detectedwallet' } ) ; } const setOfWallets = new Set < string > ( ) ; return Promise . all ( wallets . map ( wallet => { if ( isWalletInit ( wallet ) && ! hideWallet ( wallet ) ) { const { walletName , ... initParams } = wallet ; if ( ! setOfWallets . has ( walletName ) ) { try { const module = getModule ( walletName ) . then ( ( m ) => m . default ( { ... initParams , networkId , isMobile } ) ) ; setOfWallets . add ( walletName ) ; return module ; } catch ( error ) { const { type , message } = error as { type : string ; message : string ; } ; if ( type === 'DeprecatedWalletError' ) { console . warn ( message ) ; } else { throw error ; } } } } return Promise . resolve ( wallet ) ; } ) ) ; } const defaultWalletNames = isMobile ? mobileDefaultWalletNames : desktopDefaultWalletNames ; if ( detectedWalletName && ! defaultWalletNames . includes ( detectedWalletName ) ) { defaultWalletNames . unshift ( detectedWalletName ) ; } else if ( ! detectedWalletName && detectedProviderName ) { defaultWalletNames . unshift ( 'detectedwallet' ) ; } return Promise . all ( defaultWalletNames . map ( walletName => getModule ( walletName ) . then ( ( m ) => m . default ( { networkId } ) ) ) ) ; } function getModule ( name ) { switch ( name ) { case 'dapper' : case 'squarelink' : case 'unilogin' : throw { type : 'DeprecatedWalletError' , message : `${ name } wallet has been deprecated` } ; case 'meetone' : return import ( './wallets/meetone' ) ; case 'tally' : return import ( './wallets/tally' ) ; case 'metamask' : return import ( './wallets/metamask' ) ; case 'portis' : return import ( './wallets/portis' ) ; case 'fortmatic' : return import ( './wallets/fortmatic' ) ; case 'authereum' : return import ( './wallets/authereum' ) ; case 'trust' : return import ( './wallets/trust' ) ; case 'coinbase' : return import ( './wallets/coinbase' ) ; case 'walletConnect' : return import ( './wallets/wallet-connect' ) ; case 'opera' : return import ( './wallets/opera' ) ; case 'operaTouch' : return import ( './wallets/opera-touch' ) ; case 'torus' : return import ( './wallets/torus' ) ; case 'status' : return import ( './wallets/status' ) ; case 'trezor' : return import ( './wallets/trezor' ) ; case 'lattice' : return import ( './wallets/lattice' ) ; case 'keystone' : return import ( './wallets/keystone' ) ; case 'cobovault' : return import ( './wallets/cobovault' ) ; case 'ledger' : return import ( './wallets/ledger' ) ; case 'walletLink' : return import ( './wallets/wallet-link' ) ; case 'imToken' : return import ( './wallets/imtoken' ) ; case 'mykey' : return import ( './wallets/mykey' ) ; case 'huobiwallet' : return import ( './wallets/huobiwallet' ) ; case 'wallet.io' : return import ( './wallets/wallet-io' ) ; case 'hyperpay' : return import ( './wallets/hyperpay' ) ; case 'tokenpocket' : return import ( './wallets/tokenpocket' ) ; case 'dcent' : return import ( './wallets/dcent' ) ; case 'atoken' : return import ( './wallets/atoken' ) ; case 'liquality' : return import ( './wallets/liquality' ) ; case 'frame' : return import ( './wallets/frame' ) ; case 'xdefi' : return import ( './wallets/xdefi' ) ; case 'alphawallet' : return import ( './wallets/alphawallet' ) ; case 'ownbit' : return import ( './wallets/ownbit' ) ; case 'keepkey' : return import ( './wallets/keepkey/' ) ; case 'bitpie' : return import ( './wallets/bitpie' ) ; case 'gnosis' : return import ( './wallets/gnosis' ) ; case 'binance' : return import ( './wallets/binance-chain-wallet' ) ; case 'detectedwallet' : return import ( './wallets/detectedwallet' ) ; case 'tp' : return import ( './wallets/tp' ) ; case 'mathwallet' : return import ( './wallets/mathwallet' ) ; case 'blankwallet' : return import ( './wallets/blankwallet' ) ; case 'ronin' : return import ( './wallets/ronin' ) ; default : throw new Error ( `${ name } is not a valid walletName.` ) ; } } export default select ;