1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/ui/app/components/signature-request.js

251 lines
5.8 KiB
JavaScript
Raw Normal View History

2017-10-25 18:01:58 +02:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const Identicon = require('./identicon')
const connect = require('react-redux').connect
const ethUtil = require('ethereumjs-util')
2017-10-26 18:59:22 +02:00
const classnames = require('classnames')
2017-10-25 18:01:58 +02:00
const AccountDropdownMini = require('./dropdowns/account-dropdown-mini')
const actions = require('../actions')
const t = require('../../i18n')
2017-10-25 18:01:58 +02:00
const { conversionUtil } = require('../conversion-util')
const {
getSelectedAccount,
getCurrentAccountWithSendEtherInfo,
getSelectedAddress,
accountsWithSendEtherInfoSelector,
conversionRateSelector,
} = require('../selectors.js')
function mapStateToProps (state) {
return {
balance: getSelectedAccount(state).balance,
selectedAccount: getCurrentAccountWithSendEtherInfo(state),
selectedAddress: getSelectedAddress(state),
requester: null,
requesterAddress: null,
accounts: accountsWithSendEtherInfoSelector(state),
2017-11-02 13:15:59 +01:00
conversionRate: conversionRateSelector(state),
2017-10-25 18:01:58 +02:00
}
}
function mapDispatchToProps (dispatch) {
return {
2017-11-02 13:15:59 +01:00
goHome: () => dispatch(actions.goHome()),
2017-10-25 18:01:58 +02:00
}
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(SignatureRequest)
inherits(SignatureRequest, Component)
function SignatureRequest (props) {
Component.call(this)
this.state = {
selectedAccount: props.selectedAccount,
accountDropdownOpen: false,
}
}
SignatureRequest.prototype.renderHeader = function () {
return h('div.request-signature__header', [
h('div.request-signature__header-background'),
2018-01-23 10:48:03 +01:00
h('div.request-signature__header__text', t('sigRequest')),
2017-10-25 18:01:58 +02:00
h('div.request-signature__header__tip-container', [
h('div.request-signature__header__tip'),
]),
])
}
SignatureRequest.prototype.renderAccountDropdown = function () {
const {
selectedAccount,
accountDropdownOpen,
} = this.state
const {
accounts,
} = this.props
return h('div.request-signature__account', [
2018-01-29 21:29:01 +01:00
h('div.request-signature__account-text', [t('account') + ':']),
2017-10-25 18:01:58 +02:00
h(AccountDropdownMini, {
selectedAccount,
accounts,
onSelect: selectedAccount => this.setState({ selectedAccount }),
dropdownOpen: accountDropdownOpen,
openDropdown: () => this.setState({ accountDropdownOpen: true }),
closeDropdown: () => this.setState({ accountDropdownOpen: false }),
2017-11-02 13:15:59 +01:00
}),
2017-10-25 18:01:58 +02:00
])
}
SignatureRequest.prototype.renderBalance = function () {
const { balance, conversionRate } = this.props
const balanceInEther = conversionUtil(balance, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromDenomination: 'WEI',
numberOfDecimals: 6,
conversionRate,
})
return h('div.request-signature__balance', [
2018-01-23 10:48:03 +01:00
h('div.request-signature__balance-text', [t('balance')]),
2017-10-25 18:01:58 +02:00
h('div.request-signature__balance-value', `${balanceInEther} ETH`),
])
}
SignatureRequest.prototype.renderAccountInfo = function () {
return h('div.request-signature__account-info', [
this.renderAccountDropdown(),
2017-11-02 13:15:59 +01:00
2017-10-25 18:01:58 +02:00
this.renderRequestIcon(),
this.renderBalance(),
])
}
SignatureRequest.prototype.renderRequestIcon = function () {
const { requesterAddress } = this.props
return h('div.request-signature__request-icon', [
h(Identicon, {
diameter: 40,
address: requesterAddress,
2017-11-02 13:15:59 +01:00
}),
2017-10-25 18:01:58 +02:00
])
}
SignatureRequest.prototype.renderRequestInfo = function () {
return h('div.request-signature__request-info', [
h('div.request-signature__headline', [
2018-01-23 10:48:03 +01:00
t('yourSigRequested'),
2017-11-02 13:15:59 +01:00
]),
2017-10-25 18:01:58 +02:00
])
}
SignatureRequest.prototype.msgHexToText = function (hex) {
try {
const stripped = ethUtil.stripHexPrefix(hex)
const buff = Buffer.from(stripped, 'hex')
return buff.toString('utf8')
} catch (e) {
return hex
}
}
SignatureRequest.prototype.renderBody = function () {
let rows
2018-01-29 21:29:01 +01:00
let notice = t('youSign') + ':'
2017-10-25 18:01:58 +02:00
const { txData } = this.props
const { type, msgParams: { data } } = txData
if (type === 'personal_sign') {
2018-01-23 10:48:03 +01:00
rows = [{ name: t('message'), value: this.msgHexToText(data) }]
2017-11-02 13:15:59 +01:00
} else if (type === 'eth_signTypedData') {
2017-10-25 18:01:58 +02:00
rows = data
2017-11-02 13:15:59 +01:00
} else if (type === 'eth_sign') {
2018-01-23 10:48:03 +01:00
rows = [{ name: t('message'), value: data }]
notice = t('signNotice')
2017-10-26 02:28:56 +02:00
}
2017-10-25 18:01:58 +02:00
return h('div.request-signature__body', {}, [
this.renderAccountInfo(),
this.renderRequestInfo(),
2017-10-26 18:59:22 +02:00
h('div.request-signature__notice', {
className: classnames({
'request-signature__notice': type === 'personal_sign' || type === 'eth_signTypedData',
'request-signature__warning': type === 'eth_sign',
2017-11-02 13:15:59 +01:00
}),
2017-10-26 18:59:22 +02:00
}, [notice]),
2017-10-25 18:01:58 +02:00
h('div.request-signature__rows', [
...rows.map(({ name, value }) => {
return h('div.request-signature__row', [
2017-11-02 13:15:59 +01:00
h('div.request-signature__row-title', [`${name}:`]),
2017-10-25 18:01:58 +02:00
h('div.request-signature__row-value', value),
])
}),
]),
])
}
SignatureRequest.prototype.renderFooter = function () {
const {
signPersonalMessage,
signTypedMessage,
cancelPersonalMessage,
cancelTypedMessage,
2017-10-26 02:28:56 +02:00
signMessage,
cancelMessage,
2017-10-25 18:01:58 +02:00
} = this.props
const { txData } = this.props
const { type } = txData
let cancel
let sign
if (type === 'personal_sign') {
cancel = cancelPersonalMessage
sign = signPersonalMessage
2017-11-02 13:15:59 +01:00
} else if (type === 'eth_signTypedData') {
2017-10-25 18:01:58 +02:00
cancel = cancelTypedMessage
sign = signTypedMessage
2017-11-02 13:15:59 +01:00
} else if (type === 'eth_sign') {
2017-10-26 02:28:56 +02:00
cancel = cancelMessage
sign = signMessage
}
2017-10-25 18:01:58 +02:00
return h('div.request-signature__footer', [
2018-03-26 07:19:42 +02:00
h('button.btn-secondary--lg.request-signature__footer__cancel-button', {
2017-10-25 18:01:58 +02:00
onClick: cancel,
2018-01-29 21:29:01 +01:00
}, t('cancel')),
2018-03-26 07:19:42 +02:00
h('button.btn-primary--lg', {
2017-10-25 18:01:58 +02:00
onClick: sign,
2018-01-29 21:29:01 +01:00
}, t('sign')),
2017-10-25 18:01:58 +02:00
])
}
SignatureRequest.prototype.render = function () {
return (
h('div.request-signature__container', [
this.renderHeader(),
this.renderBody(),
this.renderFooter(),
])
)
}