A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/ui/app/components/pages/create-account/connect-hardware/index.js

216 lines
6.0 KiB

6 years ago
const { Component } = require('react')
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const connect = require('react-redux').connect
6 years ago
const actions = require('../../../../actions')
6 years ago
const ConnectScreen = require('./connect-screen')
const AccountList = require('./account-list')
6 years ago
const { DEFAULT_ROUTE } = require('../../../../routes')
const { formatBalance } = require('../../../../util')
6 years ago
class ConnectHardwareForm extends Component {
constructor (props, context) {
super(props)
this.state = {
error: null,
btnText: context.t('connectToTrezor'),
6 years ago
selectedAccount: null,
6 years ago
accounts: [],
browserSupported: true,
6 years ago
}
}
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})
}
async componentDidMount () {
const unlocked = await this.props.checkHardwareStatus('trezor')
if (unlocked) {
this.getPage(0)
}
}
6 years ago
connectToTrezor = () => {
if (this.state.accounts.length) {
return null
}
this.setState({ btnText: this.context.t('connecting')})
6 years ago
this.getPage(0)
6 years ago
}
onAccountChange = (account) => {
6 years ago
this.setState({selectedAccount: account.toString(), error: null})
6 years ago
}
getPage = (page) => {
this.props
.connectHardware('trezor', page)
.then(accounts => {
if (accounts.length) {
const newState = {}
6 years ago
// Default to the first account
if (this.state.selectedAccount === null) {
accounts.forEach((a, i) => {
if (a.address.toLowerCase() === this.props.address) {
newState.selectedAccount = a.index.toString()
}
})
6 years ago
// 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) {
6 years ago
newState.selectedAccount = null
}
// Map accounts with balances
newState.accounts = accounts.map(account => {
const normalizedAddress = account.address.toLowerCase()
const balanceValue = this.props.accounts[normalizedAddress] && this.props.accounts[normalizedAddress].balance || null
account.balance = balanceValue ? formatBalance(balanceValue, 6) : '...'
return account
})
6 years ago
this.setState(newState)
6 years ago
}
})
.catch(e => {
if (e === 'Window blocked') {
this.setState({ browserSupported: false })
}
6 years ago
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: [],
})
}).catch(e => {
this.setState({ error: e.toString() })
})
}
6 years ago
onUnlockAccount = () => {
if (this.state.selectedAccount === null) {
this.setState({ error: this.context.t('accountSelectionRequired') })
6 years ago
}
6 years ago
this.props.unlockTrezorAccount(this.state.selectedAccount)
.then(_ => {
this.props.history.push(DEFAULT_ROUTE)
}).catch(e => {
this.setState({ error: e.toString() })
})
6 years ago
}
6 years ago
onCancel = () => {
this.props.history.push(DEFAULT_ROUTE)
}
6 years ago
renderError () {
return this.state.error
? h('span.error', { style: { marginBottom: 40 } }, this.state.error)
: null
}
renderContent () {
if (!this.state.accounts.length) {
return h(ConnectScreen, {
connectToTrezor: this.connectToTrezor,
btnText: this.state.btnText,
browserSupported: this.state.browserSupported,
6 years ago
})
}
return h(AccountList, {
accounts: this.state.accounts,
6 years ago
selectedAccount: this.state.selectedAccount,
6 years ago
onAccountChange: this.onAccountChange,
network: this.props.network,
getPage: this.getPage,
history: this.props.history,
6 years ago
onUnlockAccount: this.onUnlockAccount,
onForgetDevice: this.onForgetDevice,
6 years ago
onCancel: this.onCancel,
6 years ago
})
}
render () {
return h('div', [
6 years ago
this.renderError(),
this.renderContent(),
])
}
}
ConnectHardwareForm.propTypes = {
hideModal: PropTypes.func,
showImportPage: PropTypes.func,
showConnectPage: PropTypes.func,
connectHardware: PropTypes.func,
checkHardwareStatus: PropTypes.func,
forgetDevice: PropTypes.func,
6 years ago
unlockTrezorAccount: PropTypes.func,
numberOfExistingAccounts: PropTypes.number,
history: PropTypes.object,
t: PropTypes.func,
network: PropTypes.string,
accounts: PropTypes.object,
address: PropTypes.string,
6 years ago
}
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))
},
6 years ago
unlockTrezorAccount: index => {
return dispatch(actions.unlockTrezorAccount(index))
},
showImportPage: () => dispatch(actions.showImportPage()),
showConnectPage: () => dispatch(actions.showConnectPage()),
}
}
ConnectHardwareForm.contextTypes = {
t: PropTypes.func,
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(
ConnectHardwareForm
)