1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 07:23:21 +01:00
metamask-extension/ui/app/pages/confirm-transaction/conf-tx.js

272 lines
7.7 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types'
2019-12-03 21:50:55 +01:00
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
2020-02-24 23:58:26 +01:00
import { compose } from 'redux'
import log from 'loglevel'
import * as actions from '../../store/actions'
import txHelper from '../../../lib/tx-helper'
import SignatureRequest from '../../components/app/signature-request'
import SignatureRequestOriginal from '../../components/app/signature-request-original'
import Loading from '../../components/ui/loading-screen'
import { getMostRecentOverviewPage } from '../../ducks/history/history'
import { MESSAGE_TYPE } from '../../../../shared/constants/app'
import { TRANSACTION_STATUSES } from '../../../../shared/constants/transaction'
2020-11-03 00:41:28 +01:00
function mapStateToProps(state) {
const { metamask, appState } = state
2017-12-05 04:27:42 +01:00
const {
unapprovedMsgCount,
unapprovedPersonalMsgCount,
2018-04-04 01:59:32 +02:00
unapprovedTypedMessagesCount,
2017-12-05 04:27:42 +01:00
} = metamask
2020-11-03 00:41:28 +01:00
const { txId } = appState
2017-12-05 04:27:42 +01:00
return {
identities: state.metamask.identities,
mostRecentOverviewPage: getMostRecentOverviewPage(state),
unapprovedTxs: state.metamask.unapprovedTxs,
unapprovedMsgs: state.metamask.unapprovedMsgs,
2017-02-23 01:23:13 +01:00
unapprovedPersonalMsgs: state.metamask.unapprovedPersonalMsgs,
2017-09-29 18:24:08 +02:00
unapprovedTypedMessages: state.metamask.unapprovedTypedMessages,
index: txId,
warning: state.appState.warning,
2016-09-08 21:56:04 +02:00
network: state.metamask.network,
currentCurrency: state.metamask.currentCurrency,
blockGasLimit: state.metamask.currentBlockGasLimit,
2017-12-05 04:27:42 +01:00
unapprovedMsgCount,
unapprovedPersonalMsgCount,
2018-04-04 01:59:32 +02:00
unapprovedTypedMessagesCount,
send: state.metamask.send,
currentNetworkTxList: state.metamask.currentNetworkTxList,
}
}
class ConfirmTxScreen extends Component {
static propTypes = {
mostRecentOverviewPage: PropTypes.string.isRequired,
unapprovedMsgCount: PropTypes.number,
unapprovedPersonalMsgCount: PropTypes.number,
unapprovedTypedMessagesCount: PropTypes.number,
network: PropTypes.string,
index: PropTypes.number,
unapprovedTxs: PropTypes.object,
unapprovedMsgs: PropTypes.object,
unapprovedPersonalMsgs: PropTypes.object,
unapprovedTypedMessages: PropTypes.object,
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string,
}),
}),
currentNetworkTxList: PropTypes.array,
currentCurrency: PropTypes.string,
blockGasLimit: PropTypes.string,
history: PropTypes.object,
identities: PropTypes.object,
dispatch: PropTypes.func.isRequired,
send: PropTypes.shape({
to: PropTypes.string,
}).isRequired,
}
2020-11-03 00:41:28 +01:00
getUnapprovedMessagesTotal() {
const {
unapprovedMsgCount = 0,
unapprovedPersonalMsgCount = 0,
unapprovedTypedMessagesCount = 0,
} = this.props
2020-11-03 00:41:28 +01:00
return (
unapprovedTypedMessagesCount +
unapprovedMsgCount +
unapprovedPersonalMsgCount
)
}
2020-11-03 00:41:28 +01:00
getTxData() {
const {
network,
index,
unapprovedTxs,
unapprovedMsgs,
unapprovedPersonalMsgs,
unapprovedTypedMessages,
match: { params: { id: transactionId } = {} },
} = this.props
const unconfTxList = txHelper(
unapprovedTxs,
unapprovedMsgs,
unapprovedPersonalMsgs,
unapprovedTypedMessages,
network,
)
log.info(`rendering a combined ${unconfTxList.length} unconf msgs & txs`)
return transactionId
? unconfTxList.find(({ id }) => `${id}` === transactionId)
: unconfTxList[index]
}
2020-11-03 00:41:28 +01:00
signatureSelect(type, version) {
// Temporarily direct only v3 and v4 requests to new code.
2020-11-03 00:41:28 +01:00
if (
type === MESSAGE_TYPE.ETH_SIGN_TYPED_DATA &&
(version === 'V3' || version === 'V4')
) {
return SignatureRequest
}
return SignatureRequestOriginal
}
2020-11-03 00:41:28 +01:00
signMessage(msgData, event) {
log.info('conf-tx.js: signing message')
const params = msgData.msgParams
params.metamaskId = msgData.id
this.stopPropagation(event)
return this.props.dispatch(actions.signMsg(params))
}
2020-11-03 00:41:28 +01:00
stopPropagation(event) {
if (event.stopPropagation) {
event.stopPropagation()
}
}
2020-11-03 00:41:28 +01:00
signPersonalMessage(msgData, event) {
log.info('conf-tx.js: signing personal message')
const params = msgData.msgParams
params.metamaskId = msgData.id
this.stopPropagation(event)
return this.props.dispatch(actions.signPersonalMsg(params))
}
2020-11-03 00:41:28 +01:00
signTypedMessage(msgData, event) {
log.info('conf-tx.js: signing typed message')
const params = msgData.msgParams
params.metamaskId = msgData.id
this.stopPropagation(event)
return this.props.dispatch(actions.signTypedMsg(params))
}
2020-11-03 00:41:28 +01:00
cancelMessage(msgData, event) {
log.info('canceling message')
this.stopPropagation(event)
return this.props.dispatch(actions.cancelMsg(msgData))
}
2020-11-03 00:41:28 +01:00
cancelPersonalMessage(msgData, event) {
log.info('canceling personal message')
this.stopPropagation(event)
return this.props.dispatch(actions.cancelPersonalMsg(msgData))
}
2020-11-03 00:41:28 +01:00
cancelTypedMessage(msgData, event) {
log.info('canceling typed message')
this.stopPropagation(event)
return this.props.dispatch(actions.cancelTypedMsg(msgData))
}
2017-09-08 00:03:25 +02:00
2020-11-03 00:41:28 +01:00
componentDidMount() {
const {
unapprovedTxs = {},
history,
mostRecentOverviewPage,
network,
send,
} = this.props
const unconfTxList = txHelper(unapprovedTxs, {}, {}, {}, network)
2020-11-03 00:41:28 +01:00
if (
unconfTxList.length === 0 &&
!send.to &&
this.getUnapprovedMessagesTotal() === 0
) {
history.push(mostRecentOverviewPage)
}
}
2020-11-03 00:41:28 +01:00
componentDidUpdate(prevProps) {
const {
unapprovedTxs = {},
network,
currentNetworkTxList,
send,
history,
match: { params: { id: transactionId } = {} },
mostRecentOverviewPage,
} = this.props
let prevTx
if (transactionId) {
prevTx = currentNetworkTxList.find(({ id }) => `${id}` === transactionId)
} else {
const { index: prevIndex, unapprovedTxs: prevUnapprovedTxs } = prevProps
const prevUnconfTxList = txHelper(prevUnapprovedTxs, {}, {}, {}, network)
const prevTxData = prevUnconfTxList[prevIndex] || {}
prevTx = currentNetworkTxList.find(({ id }) => id === prevTxData.id) || {}
}
const unconfTxList = txHelper(unapprovedTxs, {}, {}, {}, network)
if (prevTx && prevTx.status === TRANSACTION_STATUSES.DROPPED) {
2020-11-03 00:41:28 +01:00
this.props.dispatch(
actions.showModal({
name: 'TRANSACTION_CONFIRMED',
onSubmit: () => history.push(mostRecentOverviewPage),
}),
)
return
}
2020-11-03 00:41:28 +01:00
if (
unconfTxList.length === 0 &&
!send.to &&
this.getUnapprovedMessagesTotal() === 0
) {
this.props.history.push(mostRecentOverviewPage)
}
2017-03-22 23:14:33 +01:00
}
2020-11-03 00:41:28 +01:00
render() {
const { currentCurrency, blockGasLimit } = this.props
const txData = this.getTxData() || {}
2020-11-03 00:41:28 +01:00
const {
msgParams,
type,
msgParams: { version },
} = txData
log.debug('msgParams detected, rendering pending msg')
2017-09-29 18:24:08 +02:00
if (!msgParams) {
2020-11-03 00:41:28 +01:00
return <Loading />
}
const SigComponent = this.signatureSelect(type, version)
return (
<SigComponent
txData={txData}
key={txData.id}
identities={this.props.identities}
currentCurrency={currentCurrency}
blockGasLimit={blockGasLimit}
signMessage={this.signMessage.bind(this, txData)}
signPersonalMessage={this.signPersonalMessage.bind(this, txData)}
signTypedMessage={this.signTypedMessage.bind(this, txData)}
cancelMessage={this.cancelMessage.bind(this, txData)}
cancelPersonalMessage={this.cancelPersonalMessage.bind(this, txData)}
cancelTypedMessage={this.cancelTypedMessage.bind(this, txData)}
/>
)
}
2017-02-24 01:00:43 +01:00
}
2020-11-03 00:41:28 +01:00
export default compose(withRouter, connect(mapStateToProps))(ConfirmTxScreen)