1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/ui/pages/confirm-signature-request/index.js

228 lines
5.9 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
2023-02-01 06:54:18 +01:00
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { withRouter } from 'react-router-dom';
import log from 'loglevel';
import * as actions from '../../store/actions';
import txHelper from '../../helpers/utils/tx-helper';
import SignatureRequest from '../../components/app/signature-request';
import SignatureRequestSIWE from '../../components/app/signature-request-siwe';
import SignatureRequestOriginal from '../../components/app/signature-request-original';
import Loading from '../../components/ui/loading-screen';
2023-02-01 06:54:18 +01:00
import { useRouting } from '../../hooks/useRouting';
import { getTotalUnapprovedSignatureRequestCount } from '../../selectors';
import { MESSAGE_TYPE } from '../../../shared/constants/app';
import { TransactionStatus } from '../../../shared/constants/transaction';
import { getSendTo } from '../../ducks/send';
2023-02-01 06:54:18 +01:00
const SIGN_MESSAGE_TYPE = {
MESSAGE: 'message',
PERSONAL: 'personal',
TYPED: 'typed',
};
2023-02-01 06:54:18 +01:00
const signatureSelect = (txData) => {
const {
type,
msgParams: { version, siwe },
} = txData;
// Temporarily direct only v3 and v4 requests to new code.
if (
type === MESSAGE_TYPE.ETH_SIGN_TYPED_DATA &&
(version === 'V3' || version === 'V4')
) {
return SignatureRequest;
}
if (siwe?.isSIWEMessage) {
2023-02-01 06:54:18 +01:00
return SignatureRequestSIWE;
}
2023-02-01 06:54:18 +01:00
return SignatureRequestOriginal;
};
2023-02-01 06:54:18 +01:00
const stopPropagation = (event) => {
if (event?.stopPropagation) {
event.stopPropagation();
}
};
const ConfirmTxScreen = ({ match }) => {
const dispatch = useDispatch();
const { navigateToMostRecentOverviewPage } = useRouting();
const unapprovedMessagesTotal = useSelector(
getTotalUnapprovedSignatureRequestCount,
);
const sendTo = useSelector(getSendTo);
const {
unapprovedTxs,
identities,
currentNetworkTxList,
currentCurrency,
unapprovedMsgs,
unapprovedPersonalMsgs,
unapprovedTypedMessages,
network,
blockGasLimit,
provider: { chainId },
} = useSelector((state) => state.metamask);
const { txId: index } = useSelector((state) => state.appState);
const [prevValue, setPrevValues] = useState();
useEffect(() => {
const unconfTxList = txHelper(
2023-02-01 06:54:18 +01:00
unapprovedTxs || {},
{},
{},
{},
network,
chainId,
);
2023-02-01 06:54:18 +01:00
if (unconfTxList.length === 0 && !sendTo && unapprovedMessagesTotal === 0) {
navigateToMostRecentOverviewPage();
}
2023-02-01 06:54:18 +01:00
}, []);
2023-02-01 06:54:18 +01:00
useEffect(() => {
if (!prevValue) {
setPrevValues({ index, unapprovedTxs });
return;
}
let prevTx;
2023-02-01 06:54:18 +01:00
const { params: { id: transactionId } = {} } = match;
if (transactionId) {
prevTx = currentNetworkTxList.find(({ id }) => `${id}` === transactionId);
} else {
2023-02-01 06:54:18 +01:00
const { index: prevIndex, unapprovedTxs: prevUnapprovedTxs } = prevValue;
const prevUnconfTxList = txHelper(
prevUnapprovedTxs,
{},
{},
{},
network,
chainId,
);
const prevTxData = prevUnconfTxList[prevIndex] || {};
prevTx =
currentNetworkTxList.find(({ id }) => id === prevTxData.id) || {};
}
2023-02-01 06:54:18 +01:00
const unconfTxList = txHelper(
unapprovedTxs || {},
{},
{},
{},
network,
chainId,
);
if (prevTx && prevTx.status === TransactionStatus.dropped) {
2023-02-01 06:54:18 +01:00
dispatch(
2020-11-03 00:41:28 +01:00
actions.showModal({
name: 'TRANSACTION_CONFIRMED',
2023-02-01 06:54:18 +01:00
onSubmit: () => navigateToMostRecentOverviewPage(),
2020-11-03 00:41:28 +01:00
}),
);
return;
}
2023-02-01 06:54:18 +01:00
if (unconfTxList.length === 0 && !sendTo && unapprovedMessagesTotal === 0) {
navigateToMostRecentOverviewPage();
}
2017-03-22 23:14:33 +01:00
2023-02-01 06:54:18 +01:00
setPrevValues({ index, unapprovedTxs });
}, [
chainId,
currentNetworkTxList,
match,
network,
sendTo,
unapprovedMessagesTotal,
unapprovedTxs,
]);
const getTxData = () => {
const { params: { id: transactionId } = {} } = match;
const unconfTxList = txHelper(
unapprovedTxs || {},
unapprovedMsgs,
unapprovedPersonalMsgs,
unapprovedTypedMessages,
network,
chainId,
);
log.info(`rendering a combined ${unconfTxList.length} unconf msgs & txs`);
return transactionId
? unconfTxList.find(({ id }) => `${id}` === transactionId)
: unconfTxList[index];
};
const txData = getTxData() || {};
2023-02-01 06:54:18 +01:00
const { msgParams } = txData;
if (!msgParams) {
return <Loading />;
}
2023-02-01 06:54:18 +01:00
const signMessage = (type) => (event) => {
stopPropagation(event);
const params = txData.msgParams;
params.metamaskId = txData.id;
let action;
if (type === SIGN_MESSAGE_TYPE.MESSAGE) {
action = actions.signMsg;
} else if (type === SIGN_MESSAGE_TYPE.PERSONAL) {
action = actions.signPersonalMsg;
} else {
action = actions.signTypedMsg;
}
return dispatch(action?.(params));
};
2017-09-29 18:24:08 +02:00
2023-02-01 06:54:18 +01:00
const cancelMessage = (type) => (event) => {
stopPropagation(event);
let action;
if (type === SIGN_MESSAGE_TYPE.MESSAGE) {
action = actions.cancelMsg;
} else if (type === SIGN_MESSAGE_TYPE.PERSONAL) {
action = actions.cancelPersonalMsg;
} else {
action = actions.cancelTypedMsg;
}
2023-02-01 06:54:18 +01:00
return dispatch(action(txData));
};
2023-02-01 06:54:18 +01:00
const SigComponent = signatureSelect(txData);
return (
<SigComponent
txData={txData}
key={txData.id}
identities={identities}
currentCurrency={currentCurrency}
blockGasLimit={blockGasLimit}
signMessage={signMessage(SIGN_MESSAGE_TYPE.MESSAGE)}
signPersonalMessage={signMessage(SIGN_MESSAGE_TYPE.PERSONAL)}
signTypedMessage={signMessage(SIGN_MESSAGE_TYPE.TYPED)}
cancelMessage={cancelMessage(SIGN_MESSAGE_TYPE.MESSAGE)}
cancelPersonalMessage={cancelMessage(SIGN_MESSAGE_TYPE.PERSONAL)}
cancelTypedMessage={cancelMessage(SIGN_MESSAGE_TYPE.TYPED)}
/>
);
};
ConfirmTxScreen.propTypes = {
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string,
}),
}),
};
2017-02-24 01:00:43 +01:00
2023-02-01 06:54:18 +01:00
export default withRouter(ConfirmTxScreen);