2019-11-23 19:14:09 +01:00
|
|
|
import React, { Component } from 'react'
|
2020-01-09 04:34:58 +01:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import * as actions from '../../../store/actions'
|
2020-05-02 21:41:17 +02:00
|
|
|
import { getMetaMaskAccounts } from '../../../selectors'
|
2020-01-09 04:34:58 +01:00
|
|
|
import { formatBalance } from '../../../helpers/utils/util'
|
2020-06-01 19:54:32 +02:00
|
|
|
import { getMostRecentOverviewPage } from '../../../ducks/history/history'
|
2020-08-18 21:18:25 +02:00
|
|
|
import SelectHardware from './select-hardware'
|
|
|
|
import AccountList from './account-list'
|
2018-07-05 23:45:28 +02:00
|
|
|
|
|
|
|
class ConnectHardwareForm extends Component {
|
2019-12-19 23:00:22 +01:00
|
|
|
state = {
|
|
|
|
error: null,
|
|
|
|
selectedAccount: null,
|
|
|
|
accounts: [],
|
|
|
|
browserSupported: true,
|
|
|
|
unlocked: false,
|
|
|
|
device: null,
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2018-07-13 06:09:42 +02:00
|
|
|
const { accounts } = nextProps
|
2020-02-15 21:34:12 +01:00
|
|
|
const newAccounts = this.state.accounts.map((a) => {
|
2018-07-13 06:09:42 +02:00
|
|
|
const normalizedAddress = a.address.toLowerCase()
|
2020-11-03 00:41:28 +01:00
|
|
|
const balanceValue =
|
|
|
|
(accounts[normalizedAddress] && accounts[normalizedAddress].balance) ||
|
|
|
|
null
|
2018-07-13 06:09:42 +02:00
|
|
|
a.balance = balanceValue ? formatBalance(balanceValue, 6) : '...'
|
|
|
|
return a
|
|
|
|
})
|
2019-12-03 21:50:55 +01:00
|
|
|
this.setState({ accounts: newAccounts })
|
2018-07-13 06:09:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2018-07-23 07:18:39 +02:00
|
|
|
this.checkIfUnlocked()
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async checkIfUnlocked() {
|
2020-08-14 16:27:42 +02:00
|
|
|
for (const device of ['trezor', 'ledger']) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const unlocked = await this.props.checkHardwareStatus(
|
|
|
|
device,
|
|
|
|
this.props.defaultHdPaths[device],
|
|
|
|
)
|
2018-08-11 03:54:34 +02:00
|
|
|
if (unlocked) {
|
2019-12-03 21:50:55 +01:00
|
|
|
this.setState({ unlocked: true })
|
2018-08-14 07:26:18 +02:00
|
|
|
this.getPage(device, 0, this.props.defaultHdPaths[device])
|
2018-08-11 03:54:34 +02:00
|
|
|
}
|
2020-08-14 16:27:42 +02:00
|
|
|
}
|
2018-07-12 03:21:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 03:54:34 +02:00
|
|
|
connectToHardwareWallet = (device) => {
|
2020-09-02 18:28:14 +02:00
|
|
|
this.setState({ device })
|
2018-07-05 23:45:28 +02:00
|
|
|
if (this.state.accounts.length) {
|
2020-08-12 21:06:57 +02:00
|
|
|
return
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
2018-08-14 01:29:43 +02:00
|
|
|
|
|
|
|
// Default values
|
2018-08-14 07:26:18 +02:00
|
|
|
this.getPage(device, 0, this.props.defaultHdPaths[device])
|
2018-08-14 01:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onPathChange = (path) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
this.props.setHardwareWalletDefaultHdPath({
|
|
|
|
device: this.state.device,
|
|
|
|
path,
|
|
|
|
})
|
2018-08-14 07:26:18 +02:00
|
|
|
this.getPage(this.state.device, 0, path)
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onAccountChange = (account) => {
|
2019-12-03 21:50:55 +01:00
|
|
|
this.setState({ selectedAccount: account.toString(), error: null })
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 07:26:18 +02:00
|
|
|
onAccountRestriction = () => {
|
2019-12-03 21:50:55 +01:00
|
|
|
this.setState({ error: this.context.t('ledgerAccountRestriction') })
|
2018-08-14 07:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
showTemporaryAlert() {
|
2018-07-19 08:31:13 +02:00
|
|
|
this.props.showAlert(this.context.t('hardwareWalletConnected'))
|
|
|
|
// Autohide the alert after 5 seconds
|
2020-02-15 21:34:12 +01:00
|
|
|
setTimeout((_) => {
|
2018-07-19 08:31:13 +02:00
|
|
|
this.props.hideAlert()
|
|
|
|
}, 5000)
|
|
|
|
}
|
|
|
|
|
2018-08-14 07:26:18 +02:00
|
|
|
getPage = (device, page, hdPath) => {
|
2018-07-05 23:45:28 +02:00
|
|
|
this.props
|
2018-08-14 01:29:43 +02:00
|
|
|
.connectHardware(device, page, hdPath)
|
2020-02-15 21:34:12 +01:00
|
|
|
.then((accounts) => {
|
2018-07-05 23:45:28 +02:00
|
|
|
if (accounts.length) {
|
2018-07-19 08:31:13 +02:00
|
|
|
// If we just loaded the accounts for the first time
|
2018-07-23 07:18:39 +02:00
|
|
|
// (device previously locked) show the global alert
|
|
|
|
if (this.state.accounts.length === 0 && !this.state.unlocked) {
|
2018-07-19 08:31:13 +02:00
|
|
|
this.showTemporaryAlert()
|
|
|
|
}
|
|
|
|
|
2018-08-15 01:22:00 +02:00
|
|
|
const newState = { unlocked: true, device, error: null }
|
2018-07-06 02:59:31 +02:00
|
|
|
// Default to the first account
|
|
|
|
if (this.state.selectedAccount === null) {
|
2019-05-08 21:51:33 +02:00
|
|
|
accounts.forEach((a) => {
|
2018-07-13 21:19:21 +02:00
|
|
|
if (a.address.toLowerCase() === this.props.address) {
|
|
|
|
newState.selectedAccount = a.index.toString()
|
|
|
|
}
|
|
|
|
})
|
2020-11-03 00:41:28 +01:00
|
|
|
// If the page doesn't contain the selected account, let's deselect it
|
|
|
|
} else if (
|
|
|
|
!accounts.filter(
|
|
|
|
(a) => a.index.toString() === this.state.selectedAccount,
|
|
|
|
).length
|
|
|
|
) {
|
2018-07-06 02:59:31 +02:00
|
|
|
newState.selectedAccount = null
|
|
|
|
}
|
2018-07-09 23:24:52 +02:00
|
|
|
|
|
|
|
// Map accounts with balances
|
2020-02-15 21:34:12 +01:00
|
|
|
newState.accounts = accounts.map((account) => {
|
2018-07-13 06:09:42 +02:00
|
|
|
const normalizedAddress = account.address.toLowerCase()
|
2020-11-03 00:41:28 +01:00
|
|
|
const balanceValue =
|
|
|
|
(this.props.accounts[normalizedAddress] &&
|
|
|
|
this.props.accounts[normalizedAddress].balance) ||
|
|
|
|
null
|
|
|
|
account.balance = balanceValue
|
|
|
|
? formatBalance(balanceValue, 6)
|
|
|
|
: '...'
|
2018-07-09 23:24:52 +02:00
|
|
|
return account
|
|
|
|
})
|
|
|
|
|
2018-07-06 02:59:31 +02:00
|
|
|
this.setState(newState)
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
})
|
2020-02-15 21:34:12 +01:00
|
|
|
.catch((e) => {
|
2019-11-23 02:54:59 +01:00
|
|
|
const errorMessage = e.message
|
|
|
|
if (errorMessage === 'Window blocked') {
|
2019-12-03 21:50:55 +01:00
|
|
|
this.setState({ browserSupported: false, error: null })
|
2020-09-02 18:28:14 +02:00
|
|
|
} else if (e.indexOf('U2F') > -1) {
|
|
|
|
this.setState({ error: 'U2F' })
|
2020-11-03 00:41:28 +01:00
|
|
|
} else if (
|
|
|
|
errorMessage !== 'Window closed' &&
|
|
|
|
errorMessage !== 'Popup closed'
|
|
|
|
) {
|
2019-11-23 02:54:59 +01:00
|
|
|
this.setState({ error: errorMessage })
|
2018-07-13 08:03:54 +02:00
|
|
|
}
|
2018-07-05 23:45:28 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-11 03:54:34 +02:00
|
|
|
onForgetDevice = (device) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
this.props
|
|
|
|
.forgetDevice(device)
|
2020-02-15 21:34:12 +01:00
|
|
|
.then((_) => {
|
2019-07-31 22:17:11 +02:00
|
|
|
this.setState({
|
|
|
|
error: null,
|
|
|
|
selectedAccount: null,
|
|
|
|
accounts: [],
|
|
|
|
unlocked: false,
|
|
|
|
})
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2019-11-23 02:54:59 +01:00
|
|
|
this.setState({ error: e.message })
|
2018-07-12 03:21:36 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-11 11:02:02 +02:00
|
|
|
onUnlockAccount = (device) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
history,
|
|
|
|
mostRecentOverviewPage,
|
|
|
|
unlockHardwareWalletAccount,
|
|
|
|
} = this.props
|
2018-07-06 02:59:31 +02:00
|
|
|
|
|
|
|
if (this.state.selectedAccount === null) {
|
|
|
|
this.setState({ error: this.context.t('accountSelectionRequired') })
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
2018-07-06 02:59:31 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
unlockHardwareWalletAccount(this.state.selectedAccount, device)
|
2020-02-15 21:34:12 +01:00
|
|
|
.then((_) => {
|
2019-07-31 22:17:11 +02:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Accounts',
|
|
|
|
action: 'Connected Hardware Wallet',
|
2020-08-19 18:27:05 +02:00
|
|
|
name: `Connected Account with: ${device}`,
|
2019-07-31 22:17:11 +02:00
|
|
|
},
|
|
|
|
})
|
2020-06-01 19:54:32 +02:00
|
|
|
history.push(mostRecentOverviewPage)
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2019-07-31 22:17:11 +02:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Accounts',
|
|
|
|
action: 'Connected Hardware Wallet',
|
|
|
|
name: 'Error connecting hardware wallet',
|
|
|
|
},
|
|
|
|
customVariables: {
|
2019-11-23 02:54:59 +01:00
|
|
|
error: e.message,
|
2019-07-31 22:17:11 +02:00
|
|
|
},
|
|
|
|
})
|
2019-11-23 02:54:59 +01:00
|
|
|
this.setState({ error: e.message })
|
2019-03-05 16:45:01 +01:00
|
|
|
})
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 02:59:31 +02:00
|
|
|
onCancel = () => {
|
2020-06-01 19:54:32 +02:00
|
|
|
const { history, mostRecentOverviewPage } = this.props
|
|
|
|
history.push(mostRecentOverviewPage)
|
2018-07-06 02:59:31 +02:00
|
|
|
}
|
2018-07-05 23:45:28 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderError() {
|
2020-09-02 18:28:14 +02:00
|
|
|
if (this.state.error === 'U2F') {
|
|
|
|
return (
|
2020-11-03 00:41:28 +01:00
|
|
|
<p className="hw-connect__error">
|
|
|
|
{this.context.t('troubleConnectingToWallet', [
|
|
|
|
this.state.device,
|
2020-09-02 18:28:14 +02:00
|
|
|
// eslint-disable-next-line react/jsx-key
|
|
|
|
<a
|
|
|
|
href="https://metamask.zendesk.com/hc/en-us/articles/360020394612-How-to-connect-a-Trezor-or-Ledger-Hardware-Wallet"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className="hw-connect__link"
|
|
|
|
style={{ marginLeft: '5px', marginRight: '5px' }}
|
|
|
|
>
|
|
|
|
{this.context.t('walletConnectionGuide')}
|
2020-11-03 00:41:28 +01:00
|
|
|
</a>,
|
|
|
|
])}
|
2020-09-02 18:28:14 +02:00
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
return this.state.error ? (
|
|
|
|
<span className="hw-connect__error">{this.state.error}</span>
|
|
|
|
) : null
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderContent() {
|
2018-07-05 23:45:28 +02:00
|
|
|
if (!this.state.accounts.length) {
|
2019-11-23 19:14:09 +01:00
|
|
|
return (
|
2020-05-28 20:47:48 +02:00
|
|
|
<SelectHardware
|
2019-11-23 19:14:09 +01:00
|
|
|
connectToHardwareWallet={this.connectToHardwareWallet}
|
|
|
|
browserSupported={this.state.browserSupported}
|
|
|
|
/>
|
|
|
|
)
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:14:09 +01:00
|
|
|
return (
|
|
|
|
<AccountList
|
|
|
|
onPathChange={this.onPathChange}
|
|
|
|
selectedPath={this.props.defaultHdPaths[this.state.device]}
|
|
|
|
device={this.state.device}
|
|
|
|
accounts={this.state.accounts}
|
|
|
|
selectedAccount={this.state.selectedAccount}
|
|
|
|
onAccountChange={this.onAccountChange}
|
|
|
|
network={this.props.network}
|
|
|
|
getPage={this.getPage}
|
|
|
|
onUnlockAccount={this.onUnlockAccount}
|
|
|
|
onForgetDevice={this.onForgetDevice}
|
|
|
|
onCancel={this.onCancel}
|
|
|
|
onAccountRestriction={this.onAccountRestriction}
|
|
|
|
/>
|
|
|
|
)
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2019-11-23 19:14:09 +01:00
|
|
|
return (
|
2020-05-28 20:47:48 +02:00
|
|
|
<>
|
2019-11-23 19:14:09 +01:00
|
|
|
{this.renderError()}
|
|
|
|
{this.renderContent()}
|
2020-05-28 20:47:48 +02:00
|
|
|
</>
|
2019-11-23 19:14:09 +01:00
|
|
|
)
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectHardwareForm.propTypes = {
|
|
|
|
connectHardware: PropTypes.func,
|
2018-07-12 03:21:36 +02:00
|
|
|
checkHardwareStatus: PropTypes.func,
|
|
|
|
forgetDevice: PropTypes.func,
|
2018-07-19 08:31:13 +02:00
|
|
|
showAlert: PropTypes.func,
|
|
|
|
hideAlert: PropTypes.func,
|
2018-08-11 03:54:34 +02:00
|
|
|
unlockHardwareWalletAccount: PropTypes.func,
|
2018-08-14 01:29:43 +02:00
|
|
|
setHardwareWalletDefaultHdPath: PropTypes.func,
|
2018-07-05 23:45:28 +02:00
|
|
|
history: PropTypes.object,
|
|
|
|
network: PropTypes.string,
|
|
|
|
accounts: PropTypes.object,
|
2018-07-13 21:19:21 +02:00
|
|
|
address: PropTypes.string,
|
2018-08-14 01:29:43 +02:00
|
|
|
defaultHdPaths: PropTypes.object,
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: PropTypes.string.isRequired,
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const mapStateToProps = (state) => {
|
2018-07-05 23:45:28 +02:00
|
|
|
const {
|
2019-12-08 04:10:47 +01:00
|
|
|
metamask: { network, selectedAddress },
|
2018-07-05 23:45:28 +02:00
|
|
|
} = state
|
2018-11-30 23:51:24 +01:00
|
|
|
const accounts = getMetaMaskAccounts(state)
|
2018-08-14 01:29:43 +02:00
|
|
|
const {
|
|
|
|
appState: { defaultHdPaths },
|
|
|
|
} = state
|
2018-07-05 23:45:28 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
network,
|
|
|
|
accounts,
|
|
|
|
address: selectedAddress,
|
2018-08-14 01:29:43 +02:00
|
|
|
defaultHdPaths,
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const mapDispatchToProps = (dispatch) => {
|
2018-07-05 23:45:28 +02:00
|
|
|
return {
|
2019-12-03 21:50:55 +01:00
|
|
|
setHardwareWalletDefaultHdPath: ({ device, path }) => {
|
|
|
|
return dispatch(actions.setHardwareWalletDefaultHdPath({ device, path }))
|
2018-08-14 01:29:43 +02:00
|
|
|
},
|
|
|
|
connectHardware: (deviceName, page, hdPath) => {
|
2018-08-14 07:26:18 +02:00
|
|
|
return dispatch(actions.connectHardware(deviceName, page, hdPath))
|
2018-07-05 23:45:28 +02:00
|
|
|
},
|
2018-08-14 01:29:43 +02:00
|
|
|
checkHardwareStatus: (deviceName, hdPath) => {
|
|
|
|
return dispatch(actions.checkHardwareStatus(deviceName, hdPath))
|
2018-07-12 03:21:36 +02:00
|
|
|
},
|
|
|
|
forgetDevice: (deviceName) => {
|
|
|
|
return dispatch(actions.forgetDevice(deviceName))
|
|
|
|
},
|
2018-08-14 01:29:43 +02:00
|
|
|
unlockHardwareWalletAccount: (index, deviceName, hdPath) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return dispatch(
|
|
|
|
actions.unlockHardwareWalletAccount(index, deviceName, hdPath),
|
|
|
|
)
|
2018-07-05 23:45:28 +02:00
|
|
|
},
|
2018-07-19 08:31:13 +02:00
|
|
|
showAlert: (msg) => dispatch(actions.showAlert(msg)),
|
|
|
|
hideAlert: () => dispatch(actions.hideAlert()),
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectHardwareForm.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2019-03-05 16:45:01 +01:00
|
|
|
metricsEvent: PropTypes.func,
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(ConnectHardwareForm)
|