1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-25 21:00:23 +02:00
metamask-extension/ui/app/components/pages/create-account/connect-hardware/index.js

244 lines
6.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
2018-07-06 02:59:31 +02:00
const actions = require('../../../../actions')
2018-07-05 23:45:28 +02:00
const ConnectScreen = require('./connect-screen')
const AccountList = require('./account-list')
2018-07-06 02:59:31 +02:00
const { DEFAULT_ROUTE } = require('../../../../routes')
2018-07-09 23:24:52 +02:00
const { formatBalance } = require('../../../../util')
2018-07-05 23:45:28 +02:00
class ConnectHardwareForm extends Component {
constructor (props, context) {
super(props)
this.state = {
error: null,
btnText: context.t('connectToTrezor'),
2018-07-06 02:59:31 +02:00
selectedAccount: null,
2018-07-05 23:45:28 +02:00
accounts: [],
browserSupported: true,
unlocked: false,
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 () {
const unlocked = await this.props.checkHardwareStatus('trezor')
if (unlocked) {
this.setState({unlocked: true})
this.getPage(0)
}
}
2018-07-05 23:45:28 +02:00
connectToTrezor = () => {
if (this.state.accounts.length) {
return null
}
this.setState({ btnText: this.context.t('connecting')})
2018-07-06 02:59:31 +02:00
this.getPage(0)
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-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-07-05 23:45:28 +02:00
getPage = (page) => {
this.props
.connectHardware('trezor', page)
.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-10 18:09:54 +02:00
const newState = { unlocked: true, 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') {
this.setState({ browserSupported: false })
2018-08-10 18:09:54 +02:00
} else {
this.setState({ error: e.toString() })
}
2018-07-05 23:45:28 +02:00
this.setState({ btnText: this.context.t('connectToTrezor') })
})
}
onForgetDevice = () => {
this.props.forgetDevice('trezor')
.then(_ => {
this.setState({
error: null,
btnText: this.context.t('connectToTrezor'),
selectedAccount: null,
accounts: [],
unlocked: false,
})
}).catch(e => {
this.setState({ error: e.toString() })
})
}
2018-07-06 02:59:31 +02:00
onUnlockAccount = () => {
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
this.props.unlockTrezorAccount(this.state.selectedAccount)
.then(_ => {
this.props.history.push(DEFAULT_ROUTE)
}).catch(e => {
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, {
connectToTrezor: this.connectToTrezor,
btnText: this.state.btnText,
browserSupported: this.state.browserSupported,
2018-07-05 23:45:28 +02:00
})
}
return h(AccountList, {
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-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,
2018-07-05 23:45:28 +02:00
unlockTrezorAccount: PropTypes.func,
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-07-05 23:45:28 +02:00
}
const mapStateToProps = state => {
const {
metamask: { network, selectedAddress, identities = {}, accounts = [] },
} = state
const numberOfExistingAccounts = Object.keys(identities).length
return {
network,
accounts,
address: selectedAddress,
numberOfExistingAccounts,
}
}
const mapDispatchToProps = dispatch => {
return {
connectHardware: (deviceName, page) => {
return dispatch(actions.connectHardware(deviceName, page))
},
checkHardwareStatus: (deviceName) => {
return dispatch(actions.checkHardwareStatus(deviceName))
},
forgetDevice: (deviceName) => {
return dispatch(actions.forgetDevice(deviceName))
},
2018-07-05 23:45:28 +02:00
unlockTrezorAccount: index => {
return dispatch(actions.unlockTrezorAccount(index))
},
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,
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(
ConnectHardwareForm
)