1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-24 04:13:27 +02:00
metamask-extension/ui/app/pages/create-account/connect-hardware/index.js

294 lines
8.8 KiB
JavaScript
Raw Normal View History

2018-07-05 23:45:28 +02:00
const { Component } = require('react')
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const connect = require('react-redux').connect
const actions = require('../../../store/actions')
const { getMetaMaskAccounts } = require('../../../selectors/selectors')
2018-07-05 23:45:28 +02:00
const ConnectScreen = require('./connect-screen')
const AccountList = require('./account-list')
const { DEFAULT_ROUTE } = require('../../../helpers/constants/routes')
const { formatBalance } = require('../../../helpers/utils/util')
const { getPlatform } = require('../../../../../app/scripts/lib/util')
const { PLATFORM_FIREFOX } = require('../../../../../app/scripts/lib/enums')
2018-07-05 23:45:28 +02:00
class ConnectHardwareForm extends Component {
constructor (props, context) {
super(props)
this.state = {
error: null,
2018-07-06 02:59:31 +02:00
selectedAccount: null,
2018-07-05 23:45:28 +02:00
accounts: [],
browserSupported: true,
unlocked: false,
2018-08-14 01:29:43 +02:00
device: null,
2018-07-05 23:45:28 +02:00
}
}
2018-07-13 06:09:42 +02:00
componentWillReceiveProps (nextProps) {
const { accounts } = nextProps
const newAccounts = this.state.accounts.map(a => {
const normalizedAddress = a.address.toLowerCase()
const balanceValue = accounts[normalizedAddress] && accounts[normalizedAddress].balance || null
a.balance = balanceValue ? formatBalance(balanceValue, 6) : '...'
return a
})
this.setState({accounts: newAccounts})
}
componentDidMount () {
this.checkIfUnlocked()
}
async checkIfUnlocked () {
['trezor', 'ledger'].forEach(async device => {
2018-08-14 01:29:43 +02:00
const unlocked = await this.props.checkHardwareStatus(device, this.props.defaultHdPaths[device])
if (unlocked) {
this.setState({unlocked: true})
2018-08-14 07:26:18 +02:00
this.getPage(device, 0, this.props.defaultHdPaths[device])
}
})
}
connectToHardwareWallet = (device) => {
2018-08-21 03:51:15 +02:00
// Ledger hardware wallets are not supported on firefox
if (getPlatform() === PLATFORM_FIREFOX && device === 'ledger') {
2018-08-15 01:22:00 +02:00
this.setState({ browserSupported: false, error: null})
return null
}
2018-07-05 23:45:28 +02:00
if (this.state.accounts.length) {
return null
}
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) => {
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) => {
2018-07-06 02:59:31 +02: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 = () => {
this.setState({error: this.context.t('ledgerAccountRestriction') })
}
2018-07-19 08:31:13 +02:00
showTemporaryAlert () {
this.props.showAlert(this.context.t('hardwareWalletConnected'))
// Autohide the alert after 5 seconds
setTimeout(_ => {
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)
2018-07-05 23:45:28 +02:00
.then(accounts => {
if (accounts.length) {
2018-07-19 08:31:13 +02:00
// 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) {
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) {
2018-07-13 21:19:21 +02:00
accounts.forEach((a, i) => {
if (a.address.toLowerCase() === this.props.address) {
newState.selectedAccount = a.index.toString()
}
})
2018-07-06 02:59:31 +02:00
// If the page doesn't contain the selected account, let's deselect it
2018-07-09 23:24:52 +02:00
} 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
newState.accounts = accounts.map(account => {
2018-07-13 06:09:42 +02:00
const normalizedAddress = account.address.toLowerCase()
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
}
})
.catch(e => {
if (e === 'Window blocked') {
2018-08-15 01:22:00 +02:00
this.setState({ browserSupported: false, error: null})
2018-08-21 03:51:15 +02:00
} else if (e !== 'Window closed' && e !== 'Popup closed') {
2018-08-10 18:09:54 +02:00
this.setState({ error: e.toString() })
}
2018-07-05 23:45:28 +02:00
})
}
onForgetDevice = (device) => {
this.props.forgetDevice(device)
.then(_ => {
this.setState({
error: null,
selectedAccount: null,
accounts: [],
unlocked: false,
})
}).catch(e => {
this.setState({ error: e.toString() })
})
}
2018-08-11 11:02:02 +02:00
onUnlockAccount = (device) => {
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
2018-08-11 11:02:02 +02:00
this.props.unlockHardwareWalletAccount(this.state.selectedAccount, device)
2018-07-06 02:59:31 +02:00
.then(_ => {
Metametrics (#6171) * Add metametrics provider and util. * Add backend api and state for participating in metametrics. * Add frontend action for participating in metametrics. * Add metametrics opt-in screen. * Add metametrics events to first time flow. * Add metametrics events for route changes * Add metametrics events for send and confirm screens * Add metametrics events to dropdowns, transactions, log in and out, settings, sig requests and main screen * Ensures each log in is measured as a new visit by metametrics. * Ensure metametrics is called with an empty string for dimensions params if specified * Adds opt in metametrics modal after unlock for existing users * Adds settings page toggle for opting in and out of MetaMetrics * Switch metametrics dimensions to page level scope * Lint, test and translation fixes for metametrics. * Update design for metametrics opt-in screen * Complete responsive styling of metametrics-opt-in modal * Use new chart image on metrics opt in screens * Incorporate the metametrics opt-in screen into the new onboarding flow * Update e2e tests to accomodate metametrics changes * Mock out metametrics network requests in integration tests * Fix tx-list integration test to support metametrics provider. * Send number of tokens and accounts data with every metametrics event. * Update metametrics event descriptor schema and add new events. * Fix import tos bug and send gas button bug due to metametrics changes. * Various small fixes on the metametrics branch. * Add origin custom variable type to metametrics.util * Fix names of onboarding complete actions (metametrics). * Fix names of Metrics Options actions (metametrics). * Clean up code related to metametrics. * Fix bad merge conflict resolution and improve promise handling in sendMetaMetrics event and confrim tx base * Don't send a second metrics event if user has gone back during first time flow. * Collect metametrics on going back from onboarding create/import. * Add missing custom variable constants for metametrics * Fix metametrics provider * Make height of opt-in modal responsive. * Adjust text content for opt-in modal. * Update metametrics event names and clean up code in opt-in-modal * Put phishing warning step next to last in onboarding flow * Link terms of service on create and import screens of first time flow * Add subtext to options on the onboarding select action screen. * Fix styling of bullet points on end of onboarding screen. * Combine phishing warning and congratulations screens. * Fix placement of users if unlocking after an incomplete onboarding import flow. * Fix capitalization in opt-in screen * Fix last onboarding screen translations * Add link to 'Learn More' on the last screen of onboarding * Code clean up: metametrics branch * Update e2e tests for phishing warning step removal * e2e tests passing on metametrics branch * Different tracking urls for metametrics on development and prod
2019-03-05 16:45:01 +01:00
this.context.metricsEvent({
eventOpts: {
category: 'Accounts',
action: 'Connected Hardware Wallet',
name: 'Connected Account with: ' + device,
},
})
2018-07-06 02:59:31 +02:00
this.props.history.push(DEFAULT_ROUTE)
}).catch(e => {
Metametrics (#6171) * Add metametrics provider and util. * Add backend api and state for participating in metametrics. * Add frontend action for participating in metametrics. * Add metametrics opt-in screen. * Add metametrics events to first time flow. * Add metametrics events for route changes * Add metametrics events for send and confirm screens * Add metametrics events to dropdowns, transactions, log in and out, settings, sig requests and main screen * Ensures each log in is measured as a new visit by metametrics. * Ensure metametrics is called with an empty string for dimensions params if specified * Adds opt in metametrics modal after unlock for existing users * Adds settings page toggle for opting in and out of MetaMetrics * Switch metametrics dimensions to page level scope * Lint, test and translation fixes for metametrics. * Update design for metametrics opt-in screen * Complete responsive styling of metametrics-opt-in modal * Use new chart image on metrics opt in screens * Incorporate the metametrics opt-in screen into the new onboarding flow * Update e2e tests to accomodate metametrics changes * Mock out metametrics network requests in integration tests * Fix tx-list integration test to support metametrics provider. * Send number of tokens and accounts data with every metametrics event. * Update metametrics event descriptor schema and add new events. * Fix import tos bug and send gas button bug due to metametrics changes. * Various small fixes on the metametrics branch. * Add origin custom variable type to metametrics.util * Fix names of onboarding complete actions (metametrics). * Fix names of Metrics Options actions (metametrics). * Clean up code related to metametrics. * Fix bad merge conflict resolution and improve promise handling in sendMetaMetrics event and confrim tx base * Don't send a second metrics event if user has gone back during first time flow. * Collect metametrics on going back from onboarding create/import. * Add missing custom variable constants for metametrics * Fix metametrics provider * Make height of opt-in modal responsive. * Adjust text content for opt-in modal. * Update metametrics event names and clean up code in opt-in-modal * Put phishing warning step next to last in onboarding flow * Link terms of service on create and import screens of first time flow * Add subtext to options on the onboarding select action screen. * Fix styling of bullet points on end of onboarding screen. * Combine phishing warning and congratulations screens. * Fix placement of users if unlocking after an incomplete onboarding import flow. * Fix capitalization in opt-in screen * Fix last onboarding screen translations * Add link to 'Learn More' on the last screen of onboarding * Code clean up: metametrics branch * Update e2e tests for phishing warning step removal * e2e tests passing on metametrics branch * Different tracking urls for metametrics on development and prod
2019-03-05 16:45:01 +01:00
this.context.metricsEvent({
eventOpts: {
category: 'Accounts',
action: 'Connected Hardware Wallet',
name: 'Error connecting hardware wallet',
},
customVariables: {
error: e.toString(),
},
})
2018-07-06 02:59:31 +02:00
this.setState({ error: e.toString() })
})
2018-07-05 23:45:28 +02:00
}
2018-07-06 02:59:31 +02:00
onCancel = () => {
this.props.history.push(DEFAULT_ROUTE)
}
2018-07-05 23:45:28 +02:00
renderError () {
return this.state.error
2018-08-10 18:09:54 +02:00
? h('span.error', { style: { margin: '20px 20px 10px', display: 'block', textAlign: 'center' } }, this.state.error)
2018-07-05 23:45:28 +02:00
: null
}
renderContent () {
if (!this.state.accounts.length) {
return h(ConnectScreen, {
connectToHardwareWallet: this.connectToHardwareWallet,
browserSupported: this.state.browserSupported,
2018-07-05 23:45:28 +02:00
})
}
return h(AccountList, {
2018-08-14 01:29:43 +02:00
onPathChange: this.onPathChange,
selectedPath: this.props.defaultHdPaths[this.state.device],
device: this.state.device,
2018-07-05 23:45:28 +02:00
accounts: this.state.accounts,
2018-07-06 02:59:31 +02:00
selectedAccount: this.state.selectedAccount,
2018-07-05 23:45:28 +02:00
onAccountChange: this.onAccountChange,
network: this.props.network,
getPage: this.getPage,
history: this.props.history,
2018-07-06 02:59:31 +02:00
onUnlockAccount: this.onUnlockAccount,
onForgetDevice: this.onForgetDevice,
2018-07-06 02:59:31 +02:00
onCancel: this.onCancel,
2018-08-14 07:26:18 +02:00
onAccountRestriction: this.onAccountRestriction,
2018-07-05 23:45:28 +02:00
})
}
render () {
return h('div', [
2018-07-05 23:45:28 +02:00
this.renderError(),
this.renderContent(),
])
}
}
ConnectHardwareForm.propTypes = {
hideModal: PropTypes.func,
showImportPage: PropTypes.func,
showConnectPage: PropTypes.func,
connectHardware: PropTypes.func,
checkHardwareStatus: PropTypes.func,
forgetDevice: PropTypes.func,
2018-07-19 08:31:13 +02:00
showAlert: PropTypes.func,
hideAlert: PropTypes.func,
unlockHardwareWalletAccount: PropTypes.func,
2018-08-14 01:29:43 +02:00
setHardwareWalletDefaultHdPath: PropTypes.func,
2018-07-05 23:45:28 +02:00
numberOfExistingAccounts: PropTypes.number,
history: PropTypes.object,
t: PropTypes.func,
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,
2018-07-05 23:45:28 +02:00
}
const mapStateToProps = state => {
const {
metamask: { network, selectedAddress, identities = {} },
2018-07-05 23:45:28 +02:00
} = state
const accounts = getMetaMaskAccounts(state)
2018-07-05 23:45:28 +02:00
const numberOfExistingAccounts = Object.keys(identities).length
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,
numberOfExistingAccounts,
2018-08-14 01:29:43 +02:00
defaultHdPaths,
2018-07-05 23:45:28 +02:00
}
}
const mapDispatchToProps = dispatch => {
return {
2018-08-14 01:29:43 +02:00
setHardwareWalletDefaultHdPath: ({device, path}) => {
return dispatch(actions.setHardwareWalletDefaultHdPath({device, path}))
},
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))
},
forgetDevice: (deviceName) => {
return dispatch(actions.forgetDevice(deviceName))
},
2018-08-14 01:29:43 +02:00
unlockHardwareWalletAccount: (index, deviceName, hdPath) => {
return dispatch(actions.unlockHardwareWalletAccount(index, deviceName, hdPath))
2018-07-05 23:45:28 +02:00
},
showImportPage: () => dispatch(actions.showImportPage()),
showConnectPage: () => dispatch(actions.showConnectPage()),
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,
Metametrics (#6171) * Add metametrics provider and util. * Add backend api and state for participating in metametrics. * Add frontend action for participating in metametrics. * Add metametrics opt-in screen. * Add metametrics events to first time flow. * Add metametrics events for route changes * Add metametrics events for send and confirm screens * Add metametrics events to dropdowns, transactions, log in and out, settings, sig requests and main screen * Ensures each log in is measured as a new visit by metametrics. * Ensure metametrics is called with an empty string for dimensions params if specified * Adds opt in metametrics modal after unlock for existing users * Adds settings page toggle for opting in and out of MetaMetrics * Switch metametrics dimensions to page level scope * Lint, test and translation fixes for metametrics. * Update design for metametrics opt-in screen * Complete responsive styling of metametrics-opt-in modal * Use new chart image on metrics opt in screens * Incorporate the metametrics opt-in screen into the new onboarding flow * Update e2e tests to accomodate metametrics changes * Mock out metametrics network requests in integration tests * Fix tx-list integration test to support metametrics provider. * Send number of tokens and accounts data with every metametrics event. * Update metametrics event descriptor schema and add new events. * Fix import tos bug and send gas button bug due to metametrics changes. * Various small fixes on the metametrics branch. * Add origin custom variable type to metametrics.util * Fix names of onboarding complete actions (metametrics). * Fix names of Metrics Options actions (metametrics). * Clean up code related to metametrics. * Fix bad merge conflict resolution and improve promise handling in sendMetaMetrics event and confrim tx base * Don't send a second metrics event if user has gone back during first time flow. * Collect metametrics on going back from onboarding create/import. * Add missing custom variable constants for metametrics * Fix metametrics provider * Make height of opt-in modal responsive. * Adjust text content for opt-in modal. * Update metametrics event names and clean up code in opt-in-modal * Put phishing warning step next to last in onboarding flow * Link terms of service on create and import screens of first time flow * Add subtext to options on the onboarding select action screen. * Fix styling of bullet points on end of onboarding screen. * Combine phishing warning and congratulations screens. * Fix placement of users if unlocking after an incomplete onboarding import flow. * Fix capitalization in opt-in screen * Fix last onboarding screen translations * Add link to 'Learn More' on the last screen of onboarding * Code clean up: metametrics branch * Update e2e tests for phishing warning step removal * e2e tests passing on metametrics branch * Different tracking urls for metametrics on development and prod
2019-03-05 16:45:01 +01:00
metricsEvent: PropTypes.func,
2018-07-05 23:45:28 +02:00
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(
ConnectHardwareForm
)