import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import * as actions from '../../../store/actions'; import { getCurrentChainId, getMetaMaskAccounts, getRpcPrefsForCurrentProvider, getMetaMaskAccountsConnected, } from '../../../selectors'; import { formatBalance } from '../../../helpers/utils/util'; import { getMostRecentOverviewPage } from '../../../ducks/history/history'; import SelectHardware from './select-hardware'; import AccountList from './account-list'; const U2F_ERROR = 'U2F'; const LEDGER_LIVE_PATH = `m/44'/60'/0'/0/0`; const MEW_PATH = `m/44'/60'/0'`; const BIP44_PATH = `m/44'/60'/0'/0`; const HD_PATHS = [ { name: 'Ledger Live', value: LEDGER_LIVE_PATH }, { name: 'Legacy (MEW / MyCrypto)', value: MEW_PATH }, { name: `BIP44 Standard (e.g. MetaMask, Trezor)`, value: BIP44_PATH }, ]; class ConnectHardwareForm extends Component { state = { error: null, selectedAccounts: [], accounts: [], browserSupported: true, unlocked: false, device: null, }; UNSAFE_componentWillReceiveProps(nextProps) { const { accounts } = nextProps; const newAccounts = this.state.accounts.map((a) => { const normalizedAddress = a.address.toLowerCase(); const balanceValue = accounts[normalizedAddress]?.balance || null; a.balance = balanceValue ? formatBalance(balanceValue, 6) : '...'; return a; }); this.setState({ accounts: newAccounts }); } componentDidMount() { this.checkIfUnlocked(); } async checkIfUnlocked() { for (const device of ['trezor', 'ledger']) { const path = this.props.defaultHdPaths[device]; const unlocked = await this.props.checkHardwareStatus(device, path); if (unlocked) { this.setState({ unlocked: true }); this.getPage(device, 0, path); } } } connectToHardwareWallet = (device) => { this.setState({ device }); if (this.state.accounts.length) { return; } // Default values this.getPage(device, 0, this.props.defaultHdPaths[device]); }; onPathChange = (path) => { this.props.setHardwareWalletDefaultHdPath({ device: this.state.device, path, }); this.setState({ selectedAccounts: [], }); this.getPage(this.state.device, 0, path); }; onAccountChange = (account) => { let { selectedAccounts } = this.state; if (selectedAccounts.includes(account)) { selectedAccounts = selectedAccounts.filter((acc) => account !== acc); } else { selectedAccounts.push(account); } this.setState({ selectedAccounts, error: null }); }; onAccountRestriction = () => { this.setState({ error: this.context.t('ledgerAccountRestriction') }); }; showTemporaryAlert() { this.props.showAlert(this.context.t('hardwareWalletConnected')); // Autohide the alert after 5 seconds setTimeout((_) => { this.props.hideAlert(); }, 5000); } getPage = (device, page, hdPath) => { this.props .connectHardware(device, page, hdPath) .then((accounts) => { if (accounts.length) { // If we just loaded the accounts for the first time // (device previously locked) show the global alert if (this.state.accounts.length === 0 && !this.state.unlocked) { this.showTemporaryAlert(); } // Map accounts with balances const newAccounts = accounts.map((account) => { const normalizedAddress = account.address.toLowerCase(); const balanceValue = this.props.accounts[normalizedAddress]?.balance || null; account.balance = balanceValue ? formatBalance(balanceValue, 6) : '...'; return account; }); this.setState({ accounts: newAccounts, unlocked: true, device, error: null, }); } }) .catch((e) => { const errorMessage = typeof e === 'string' ? e : e.message; if (errorMessage === 'Window blocked') { this.setState({ browserSupported: false, error: null }); } else if (errorMessage.includes(U2F_ERROR)) { this.setState({ error: U2F_ERROR }); } else if ( errorMessage !== 'Window closed' && errorMessage !== 'Popup closed' ) { this.setState({ error: errorMessage }); } }); }; onForgetDevice = (device) => { this.props .forgetDevice(device) .then((_) => { this.setState({ error: null, selectedAccounts: [], accounts: [], unlocked: false, }); }) .catch((e) => { this.setState({ error: e.message }); }); }; onUnlockAccounts = (device, path) => { const { history, mostRecentOverviewPage, unlockHardwareWalletAccounts, } = this.props; const { selectedAccounts } = this.state; if (selectedAccounts.length === 0) { this.setState({ error: this.context.t('accountSelectionRequired') }); } const description = MEW_PATH === path ? this.context.t('hardwareWalletLegacyDescription') : ''; return unlockHardwareWalletAccounts( selectedAccounts, device, path || null, description, ) .then((_) => { this.context.metricsEvent({ eventOpts: { category: 'Accounts', action: 'Connected Hardware Wallet', name: `Connected Account with: ${device}`, }, }); history.push(mostRecentOverviewPage); }) .catch((e) => { this.context.metricsEvent({ eventOpts: { category: 'Accounts', action: 'Connected Hardware Wallet', name: 'Error connecting hardware wallet', }, customVariables: { error: e.message, }, }); this.setState({ error: e.message }); }); }; onCancel = () => { const { history, mostRecentOverviewPage } = this.props; history.push(mostRecentOverviewPage); }; renderError() { if (this.state.error === U2F_ERROR) { return (
{this.context.t('troubleConnectingToWallet', [ this.state.device, // eslint-disable-next-line react/jsx-key {this.context.t('walletConnectionGuide')} , ])}
); } return this.state.error ? ( {this.state.error} ) : null; } renderContent() { if (!this.state.accounts.length) { return (