From a057136c83af1f26b229b0bdc533e67106a82fec Mon Sep 17 00:00:00 2001 From: William O'Beirne Date: Tue, 29 May 2018 14:39:20 -0400 Subject: [PATCH] Simplify Web3 Provider Identifier (#1861) * Check in progress * Revert some files to develop. --- .../WalletDecrypt/WalletDecrypt.tsx | 28 +++--------- common/utils/web3.ts | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 common/utils/web3.ts diff --git a/common/components/WalletDecrypt/WalletDecrypt.tsx b/common/components/WalletDecrypt/WalletDecrypt.tsx index 30bd9338..041731b0 100644 --- a/common/components/WalletDecrypt/WalletDecrypt.tsx +++ b/common/components/WalletDecrypt/WalletDecrypt.tsx @@ -43,10 +43,8 @@ import { donationAddressMap } from 'config'; import { isWeb3NodeAvailable } from 'libs/nodes/web3'; -import CipherIcon from 'assets/images/wallets/cipher.svg'; +import { getWeb3ProviderInfo } from 'utils/web3'; import LedgerIcon from 'assets/images/wallets/ledger.svg'; -import MetamaskIcon from 'assets/images/wallets/metamask.svg'; -import MistIcon from 'assets/images/wallets/mist.svg'; import TrezorIcon from 'assets/images/wallets/trezor.svg'; import ParitySignerIcon from 'assets/images/wallets/parity-signer.svg'; import { wikiLink as paritySignerHelpLink } from 'libs/wallet/non-deterministic/parity'; @@ -108,41 +106,25 @@ export interface InsecureWalletInfo extends BaseWalletInfo { // tslint:disable-next-line:no-empty-interface interface MiscWalletInfo extends InsecureWalletInfo {} -const WEB3_TYPES = { - CipherProvider: { - lid: 'X_CIPHER', - icon: CipherIcon - }, - MetamaskInpageProvider: { - lid: 'X_METAMASK', - icon: MetamaskIcon - }, - EthereumProvider: { - lid: 'X_MIST', - icon: MistIcon - } -}; - type SecureWallets = { [key in SecureWalletName]: SecureWalletInfo }; type InsecureWallets = { [key in InsecureWalletName]: InsecureWalletInfo }; type MiscWallet = { [key in MiscWalletName]: MiscWalletInfo }; type Wallets = SecureWallets & InsecureWallets & MiscWallet; -const WEB3_TYPE: keyof typeof WEB3_TYPES | false = - (window as any).web3 && (window as any).web3.currentProvider.constructor.name; - const SECURE_WALLETS = Object.values(SecureWalletName); const INSECURE_WALLETS = Object.values(InsecureWalletName); const MISC_WALLETS = Object.values(MiscWalletName); +const web3info = getWeb3ProviderInfo(); + const WalletDecrypt = withRouter( class WalletDecryptClass extends Component & Props, State> { // https://github.com/Microsoft/TypeScript/issues/13042 // index signature should become [key: Wallets] (from config) once typescript bug is fixed public WALLETS: Wallets = { [SecureWalletName.WEB3]: { - lid: WEB3_TYPE && WEB3_TYPES[WEB3_TYPE] ? WEB3_TYPES[WEB3_TYPE].lid : 'X_WEB3', - icon: WEB3_TYPE && WEB3_TYPES[WEB3_TYPE] && WEB3_TYPES[WEB3_TYPE].icon, + lid: web3info.lid, + icon: web3info.icon, description: 'ADD_WEB3DESC', component: Web3Decrypt, initialParams: {}, diff --git a/common/utils/web3.ts b/common/utils/web3.ts new file mode 100644 index 00000000..93f9b866 --- /dev/null +++ b/common/utils/web3.ts @@ -0,0 +1,43 @@ +import MetamaskIcon from 'assets/images/wallets/metamask.svg'; +import MistIcon from 'assets/images/wallets/mist.svg'; +import CipherIcon from 'assets/images/wallets/cipher.svg'; + +interface Web3ProviderInfo { + lid: string; + icon: string; +} + +const WEB3_CONFIGS: { + [classname: string]: Web3ProviderInfo; +} = { + CipherProvider: { + lid: 'X_CIPHER', + icon: CipherIcon + }, + MetamaskInpageProvider: { + lid: 'X_METAMASK', + icon: MetamaskIcon + }, + EthereumProvider: { + lid: 'X_MIST', + icon: MistIcon + } +}; + +const DEFAULT_WEB3_CONFIG: Web3ProviderInfo = { + lid: 'X_WEB3', + icon: '' +}; + +export function getWeb3ProviderInfo(): Web3ProviderInfo { + if (typeof window === 'undefined') { + return DEFAULT_WEB3_CONFIG; + } + + const className = (window as any).web3 && (window as any).web3.currentProvider.constructor.name; + if (className && WEB3_CONFIGS[className]) { + return WEB3_CONFIGS[className]; + } + + return DEFAULT_WEB3_CONFIG; +}