import { IS_CHROME , CHECK_METAMASK_INSTALLED_URL , WALLET_BRAND_CONTENT , KEYRING_CLASS , KEYRINGS_LOGOS , KEYRING_PURPLE_LOGOS , } from 'consts' ; import { Account } from 'background/service/preference' ; export const noop = ( ) => { } ; export * from './WalletContext' ; export * from './WindowContext' ; export * from './hooks' ; export * from './webapi' ; export * from './time' ; export * from './number' ; const UI_TYPE = { Tab : 'index' , Pop : 'popup' , Notification : 'notification' , } ; type UiTypeCheck = { isTab : boolean ; isNotification : boolean ; isPop : boolean ; } ; export const getUiType = ( ) : UiTypeCheck => { const { pathname } = window . location ; return Object . entries ( UI_TYPE ) . reduce ( ( m , [ key , value ] ) => { m [ `is${ key }` ] = pathname === `/${ value }.html` ; return m ; } , { } as UiTypeCheck ) ; } ; export const hex2Text = ( hex ) => { try { return hex . startsWith ( '0x' ) ? decodeURIComponent ( hex . replace ( /^0x/ , '' ) . replace ( /[0-9a-f]{2}/g , '%$&' ) ) : hex ; } catch { return hex ; } } ; export const getUITypeName = ( ) : string => { const UIType = getUiType ( ) ; if ( UIType . isPop ) return 'popup' ; if ( UIType . isNotification ) return 'notification' ; if ( UIType . isTab ) return 'tab' ; return '' ; } ; param origin returns export const getOriginName = ( origin ) => { const matches = origin . replace ( /https?:\/\// , '' ) . match ( /^([^.]+\.)?(\S+)\./ ) ; return matches ? matches [ 2 ] || origin : origin ; } ; export const hashCode = ( str ) => { if ( ! str ) return 0 ; let hash = 0 , i , chr , len ; if ( str . length === 0 ) return hash ; for ( i = 0 , len = str . length ; i < len ; i ++ ) { chr = str . charCodeAt ( i ) ; hash = ( hash << 5 ) - hash + chr ; hash |= 0 ; } return hash ; } ; export const isMetaMaskActive = async ( ) => { let url = '' ; if ( IS_CHROME ) { url = CHECK_METAMASK_INSTALLED_URL . Chrome ; } if ( ! url ) return false ; try { const res = await window . fetch ( url ) ; await res . text ( ) ; return true ; } catch ( e ) { return false ; } } ; export const ellipsisOverflowedText = ( str , length = 5 , removeLastComma = false ) => { if ( str . length <= length ) return str ; let cut = str . substring ( 0 , length ) ; if ( removeLastComma ) { if ( cut . endsWith ( ',' ) ) { cut = cut . substring ( 0 , length - 1 ) ; } } return `${ cut }...` ; } ; export const isSameAddress = ( a , b ) => { if ( ! a || ! b ) return false ; return a . toLowerCase ( ) === b . toLowerCase ( ) ; } ; export const icons = { mnemonic : KEYRING_PURPLE_LOGOS [ KEYRING_CLASS . MNEMONIC ] , privatekey : KEYRING_PURPLE_LOGOS [ KEYRING_CLASS . PRIVATE_KEY ] , watch : KEYRING_PURPLE_LOGOS [ KEYRING_CLASS . WATCH ] , } ; export const getAccountIcon = ( account ) => { if ( account ) { if ( WALLET_BRAND_CONTENT [ account ?. brandName ] ) { return WALLET_BRAND_CONTENT [ account ?. brandName ] . image ; } switch ( account . type ) { case KEYRING_CLASS . MNEMONIC || 'HD Key Tree' : return icons . mnemonic ; case KEYRING_CLASS . PRIVATE_KEY : return icons . privatekey ; case KEYRING_CLASS . WATCH : return icons . watch ; } return KEYRINGS_LOGOS [ account . type ] ; } return '' ; } ;