mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
parent
6075e86838
commit
aea5c5824f
@ -1604,7 +1604,7 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
updateAndSetCustomRpc: this.updateAndSetCustomRpc.bind(this),
|
updateAndSetCustomRpc: this.updateAndSetCustomRpc.bind(this),
|
||||||
delCustomRpc: this.delCustomRpc.bind(this),
|
delCustomRpc: this.delCustomRpc.bind(this),
|
||||||
addCustomNetwork: this.addCustomNetwork.bind(this),
|
addCustomNetwork: this.addCustomNetwork.bind(this),
|
||||||
requestUserApproval: this.requestUserApproval.bind(this),
|
requestAddNetworkApproval: this.requestAddNetworkApproval.bind(this),
|
||||||
// PreferencesController
|
// PreferencesController
|
||||||
setSelectedAddress: preferencesController.setSelectedAddress.bind(
|
setSelectedAddress: preferencesController.setSelectedAddress.bind(
|
||||||
preferencesController,
|
preferencesController,
|
||||||
@ -2053,7 +2053,7 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async requestUserApproval(customRpc, originIsMetaMask) {
|
async requestAddNetworkApproval(customRpc, originIsMetaMask) {
|
||||||
try {
|
try {
|
||||||
await this.approvalController.addAndShowApprovalRequest({
|
await this.approvalController.addAndShowApprovalRequest({
|
||||||
origin: 'metamask',
|
origin: 'metamask',
|
||||||
|
@ -28,7 +28,7 @@ import {
|
|||||||
ENVIRONMENT_TYPE_POPUP,
|
ENVIRONMENT_TYPE_POPUP,
|
||||||
MESSAGE_TYPE,
|
MESSAGE_TYPE,
|
||||||
} from '../../../../shared/constants/app';
|
} from '../../../../shared/constants/app';
|
||||||
import { requestUserApproval } from '../../../store/actions';
|
import { requestAddNetworkApproval } from '../../../store/actions';
|
||||||
import Popover from '../../ui/popover';
|
import Popover from '../../ui/popover';
|
||||||
import ConfirmationPage from '../../../pages/confirmation/confirmation';
|
import ConfirmationPage from '../../../pages/confirmation/confirmation';
|
||||||
import { FEATURED_RPCS } from '../../../../shared/constants/network';
|
import { FEATURED_RPCS } from '../../../../shared/constants/network';
|
||||||
@ -239,7 +239,7 @@ const AddNetwork = () => {
|
|||||||
type="inline"
|
type="inline"
|
||||||
className="add-network__add-button"
|
className="add-network__add-button"
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
await dispatch(requestUserApproval(item, true));
|
await dispatch(requestAddNetworkApproval(item, true));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{t('add')}
|
{t('add')}
|
||||||
@ -277,7 +277,7 @@ const AddNetwork = () => {
|
|||||||
)}
|
)}
|
||||||
{showPopover && (
|
{showPopover && (
|
||||||
<Popover>
|
<Popover>
|
||||||
<ConfirmationPage />
|
<ConfirmationPage redirectToHomeOnZeroConfirmations={false} />
|
||||||
</Popover>
|
</Popover>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
@ -5,6 +5,8 @@ import React, {
|
|||||||
useReducer,
|
useReducer,
|
||||||
useState,
|
useState,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
import { isEqual } from 'lodash';
|
import { isEqual } from 'lodash';
|
||||||
@ -24,7 +26,6 @@ import { getUnapprovedTemplatedConfirmations } from '../../selectors';
|
|||||||
import NetworkDisplay from '../../components/app/network-display/network-display';
|
import NetworkDisplay from '../../components/app/network-display/network-display';
|
||||||
import Callout from '../../components/ui/callout';
|
import Callout from '../../components/ui/callout';
|
||||||
import SiteOrigin from '../../components/ui/site-origin';
|
import SiteOrigin from '../../components/ui/site-origin';
|
||||||
import { addCustomNetwork } from '../../store/actions';
|
|
||||||
import ConfirmationFooter from './components/confirmation-footer';
|
import ConfirmationFooter from './components/confirmation-footer';
|
||||||
import { getTemplateValues, getTemplateAlerts } from './templates';
|
import { getTemplateValues, getTemplateAlerts } from './templates';
|
||||||
|
|
||||||
@ -116,7 +117,9 @@ function useAlertState(pendingConfirmation) {
|
|||||||
return [alertState, dismissAlert];
|
return [alertState, dismissAlert];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ConfirmationPage() {
|
export default function ConfirmationPage({
|
||||||
|
redirectToHomeOnZeroConfirmations = true,
|
||||||
|
}) {
|
||||||
const t = useI18nContext();
|
const t = useI18nContext();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
@ -130,28 +133,35 @@ export default function ConfirmationPage() {
|
|||||||
const pendingConfirmation = pendingConfirmations[currentPendingConfirmation];
|
const pendingConfirmation = pendingConfirmations[currentPendingConfirmation];
|
||||||
const originMetadata = useOriginMetadata(pendingConfirmation?.origin) || {};
|
const originMetadata = useOriginMetadata(pendingConfirmation?.origin) || {};
|
||||||
const [alertState, dismissAlert] = useAlertState(pendingConfirmation);
|
const [alertState, dismissAlert] = useAlertState(pendingConfirmation);
|
||||||
const [stayOnPage, setStayOnPage] = useState(false);
|
|
||||||
|
|
||||||
// Generating templatedValues is potentially expensive, and if done on every render
|
// Generating templatedValues is potentially expensive, and if done on every render
|
||||||
// will result in a new object. Avoiding calling this generation unnecessarily will
|
// will result in a new object. Avoiding calling this generation unnecessarily will
|
||||||
// improve performance and prevent unnecessary draws.
|
// improve performance and prevent unnecessary draws.
|
||||||
const templatedValues = useMemo(() => {
|
const templatedValues = useMemo(() => {
|
||||||
return pendingConfirmation
|
return pendingConfirmation
|
||||||
? getTemplateValues(pendingConfirmation, t, dispatch)
|
? getTemplateValues(pendingConfirmation, t, dispatch, history)
|
||||||
: {};
|
: {};
|
||||||
}, [pendingConfirmation, t, dispatch]);
|
}, [pendingConfirmation, t, dispatch, history]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// If the number of pending confirmations reduces to zero when the user
|
// If the number of pending confirmations reduces to zero when the user
|
||||||
// return them to the default route. Otherwise, if the number of pending
|
// return them to the default route. Otherwise, if the number of pending
|
||||||
// confirmations reduces to a number that is less than the currently
|
// confirmations reduces to a number that is less than the currently
|
||||||
// viewed index, reset the index.
|
// viewed index, reset the index.
|
||||||
if (pendingConfirmations.length === 0) {
|
if (
|
||||||
!stayOnPage && history.push(DEFAULT_ROUTE);
|
pendingConfirmations.length === 0 &&
|
||||||
|
redirectToHomeOnZeroConfirmations
|
||||||
|
) {
|
||||||
|
history.push(DEFAULT_ROUTE);
|
||||||
} else if (pendingConfirmations.length <= currentPendingConfirmation) {
|
} else if (pendingConfirmations.length <= currentPendingConfirmation) {
|
||||||
setCurrentPendingConfirmation(pendingConfirmations.length - 1);
|
setCurrentPendingConfirmation(pendingConfirmations.length - 1);
|
||||||
}
|
}
|
||||||
}, [pendingConfirmations, history, currentPendingConfirmation, stayOnPage]);
|
}, [
|
||||||
|
pendingConfirmations,
|
||||||
|
history,
|
||||||
|
currentPendingConfirmation,
|
||||||
|
redirectToHomeOnZeroConfirmations,
|
||||||
|
]);
|
||||||
if (!pendingConfirmation) {
|
if (!pendingConfirmation) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -237,18 +247,15 @@ export default function ConfirmationPage() {
|
|||||||
</Callout>
|
</Callout>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
onApprove={() => {
|
onApprove={templatedValues.onApprove}
|
||||||
templatedValues.onApprove.apply();
|
onCancel={templatedValues.onCancel}
|
||||||
pendingConfirmation.origin === 'metamask' &&
|
|
||||||
dispatch(addCustomNetwork(pendingConfirmation.requestData));
|
|
||||||
}}
|
|
||||||
onCancel={() => {
|
|
||||||
templatedValues.onCancel.apply();
|
|
||||||
pendingConfirmation.origin === 'metamask' && setStayOnPage(true);
|
|
||||||
}}
|
|
||||||
approveText={templatedValues.approvalText}
|
approveText={templatedValues.approvalText}
|
||||||
cancelText={templatedValues.cancelText}
|
cancelText={templatedValues.cancelText}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConfirmationPage.propTypes = {
|
||||||
|
redirectToHomeOnZeroConfirmations: PropTypes.bool,
|
||||||
|
};
|
||||||
|
@ -9,6 +9,8 @@ import {
|
|||||||
DISPLAY,
|
DISPLAY,
|
||||||
COLORS,
|
COLORS,
|
||||||
} from '../../../helpers/constants/design-system';
|
} from '../../../helpers/constants/design-system';
|
||||||
|
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
|
||||||
|
|
||||||
import fetchWithCache from '../../../helpers/utils/fetch-with-cache';
|
import fetchWithCache from '../../../helpers/utils/fetch-with-cache';
|
||||||
|
|
||||||
const UNRECOGNIZED_CHAIN = {
|
const UNRECOGNIZED_CHAIN = {
|
||||||
@ -142,7 +144,7 @@ async function getAlerts(pendingApproval) {
|
|||||||
return alerts;
|
return alerts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getValues(pendingApproval, t, actions) {
|
function getValues(pendingApproval, t, actions, history) {
|
||||||
const originIsMetaMask = pendingApproval.origin === 'metamask';
|
const originIsMetaMask = pendingApproval.origin === 'metamask';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -320,12 +322,16 @@ function getValues(pendingApproval, t, actions) {
|
|||||||
],
|
],
|
||||||
approvalText: t('approveButtonText'),
|
approvalText: t('approveButtonText'),
|
||||||
cancelText: t('cancel'),
|
cancelText: t('cancel'),
|
||||||
onApprove: () =>
|
onApprove: async () => {
|
||||||
actions.resolvePendingApproval(
|
await actions.resolvePendingApproval(
|
||||||
pendingApproval.id,
|
pendingApproval.id,
|
||||||
pendingApproval.requestData,
|
pendingApproval.requestData,
|
||||||
),
|
);
|
||||||
|
if (originIsMetaMask) {
|
||||||
|
actions.addCustomNetwork(pendingApproval.requestData);
|
||||||
|
history.push(DEFAULT_ROUTE);
|
||||||
|
}
|
||||||
|
},
|
||||||
onCancel: () =>
|
onCancel: () =>
|
||||||
actions.rejectPendingApproval(
|
actions.rejectPendingApproval(
|
||||||
pendingApproval.id,
|
pendingApproval.id,
|
||||||
|
@ -3,6 +3,7 @@ import { MESSAGE_TYPE } from '../../../../shared/constants/app';
|
|||||||
import {
|
import {
|
||||||
rejectPendingApproval,
|
rejectPendingApproval,
|
||||||
resolvePendingApproval,
|
resolvePendingApproval,
|
||||||
|
addCustomNetwork,
|
||||||
} from '../../../store/actions';
|
} from '../../../store/actions';
|
||||||
import addEthereumChain from './add-ethereum-chain';
|
import addEthereumChain from './add-ethereum-chain';
|
||||||
import switchEthereumChain from './switch-ethereum-chain';
|
import switchEthereumChain from './switch-ethereum-chain';
|
||||||
@ -106,6 +107,7 @@ function getAttenuatedDispatch(dispatch) {
|
|||||||
dispatch(rejectPendingApproval(...args)),
|
dispatch(rejectPendingApproval(...args)),
|
||||||
resolvePendingApproval: (...args) =>
|
resolvePendingApproval: (...args) =>
|
||||||
dispatch(resolvePendingApproval(...args)),
|
dispatch(resolvePendingApproval(...args)),
|
||||||
|
addCustomNetwork: (...args) => dispatch(addCustomNetwork(...args)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,8 +117,9 @@ function getAttenuatedDispatch(dispatch) {
|
|||||||
* @param {object} pendingApproval - The pending confirmation object
|
* @param {object} pendingApproval - The pending confirmation object
|
||||||
* @param {Function} t - Translation function
|
* @param {Function} t - Translation function
|
||||||
* @param {Function} dispatch - Redux dispatch function
|
* @param {Function} dispatch - Redux dispatch function
|
||||||
|
* @param history
|
||||||
*/
|
*/
|
||||||
export function getTemplateValues(pendingApproval, t, dispatch) {
|
export function getTemplateValues(pendingApproval, t, dispatch, history) {
|
||||||
const fn = APPROVAL_TEMPLATES[pendingApproval.type]?.getValues;
|
const fn = APPROVAL_TEMPLATES[pendingApproval.type]?.getValues;
|
||||||
if (!fn) {
|
if (!fn) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -125,7 +128,7 @@ export function getTemplateValues(pendingApproval, t, dispatch) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const safeActions = getAttenuatedDispatch(dispatch);
|
const safeActions = getAttenuatedDispatch(dispatch);
|
||||||
const values = fn(pendingApproval, t, safeActions);
|
const values = fn(pendingApproval, t, safeActions, history);
|
||||||
const extraneousKeys = omit(values, ALLOWED_TEMPLATE_KEYS);
|
const extraneousKeys = omit(values, ALLOWED_TEMPLATE_KEYS);
|
||||||
const safeValues = pick(values, ALLOWED_TEMPLATE_KEYS);
|
const safeValues = pick(values, ALLOWED_TEMPLATE_KEYS);
|
||||||
if (extraneousKeys.length > 0) {
|
if (extraneousKeys.length > 0) {
|
||||||
|
@ -142,7 +142,7 @@ export default class Home extends PureComponent {
|
|||||||
newTokensImported: PropTypes.string,
|
newTokensImported: PropTypes.string,
|
||||||
setNewTokensImported: PropTypes.func.isRequired,
|
setNewTokensImported: PropTypes.func.isRequired,
|
||||||
newCustomNetworkAdded: PropTypes.object,
|
newCustomNetworkAdded: PropTypes.object,
|
||||||
setNewCustomNetworkAdded: PropTypes.func,
|
clearNewCustomNetworkAdded: PropTypes.func,
|
||||||
setRpcTarget: PropTypes.func,
|
setRpcTarget: PropTypes.func,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -282,7 +282,7 @@ export default class Home extends PureComponent {
|
|||||||
newTokensImported,
|
newTokensImported,
|
||||||
setNewTokensImported,
|
setNewTokensImported,
|
||||||
newCustomNetworkAdded,
|
newCustomNetworkAdded,
|
||||||
setNewCustomNetworkAdded,
|
clearNewCustomNetworkAdded,
|
||||||
setRpcTarget,
|
setRpcTarget,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
return (
|
return (
|
||||||
@ -507,7 +507,7 @@ export default class Home extends PureComponent {
|
|||||||
newCustomNetworkAdded.ticker,
|
newCustomNetworkAdded.ticker,
|
||||||
newCustomNetworkAdded.chainName,
|
newCustomNetworkAdded.chainName,
|
||||||
);
|
);
|
||||||
setNewCustomNetworkAdded();
|
clearNewCustomNetworkAdded();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography
|
<Typography
|
||||||
@ -520,7 +520,7 @@ export default class Home extends PureComponent {
|
|||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
type="secondary"
|
type="secondary"
|
||||||
onClick={() => setNewCustomNetworkAdded()}
|
onClick={() => clearNewCustomNetworkAdded()}
|
||||||
>
|
>
|
||||||
<Typography
|
<Typography
|
||||||
variant={TYPOGRAPHY.H6}
|
variant={TYPOGRAPHY.H6}
|
||||||
|
@ -186,7 +186,7 @@ const mapDispatchToProps = (dispatch) => ({
|
|||||||
setNewTokensImported: (newTokens) => {
|
setNewTokensImported: (newTokens) => {
|
||||||
dispatch(setNewTokensImported(newTokens));
|
dispatch(setNewTokensImported(newTokens));
|
||||||
},
|
},
|
||||||
setNewCustomNetworkAdded: () => {
|
clearNewCustomNetworkAdded: () => {
|
||||||
dispatch(setNewCustomNetworkAdded({}));
|
dispatch(setNewCustomNetworkAdded({}));
|
||||||
},
|
},
|
||||||
setRpcTarget: (rpcUrl, chainId, ticker, nickname) => {
|
setRpcTarget: (rpcUrl, chainId, ticker, nickname) => {
|
||||||
|
@ -3788,10 +3788,10 @@ export function addCustomNetwork(customRpc) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function requestUserApproval(customRpc, originIsMetaMask) {
|
export function requestAddNetworkApproval(customRpc, originIsMetaMask) {
|
||||||
return async (dispatch) => {
|
return async (dispatch) => {
|
||||||
try {
|
try {
|
||||||
await promisifiedBackground.requestUserApproval(
|
await promisifiedBackground.requestAddNetworkApproval(
|
||||||
customRpc,
|
customRpc,
|
||||||
originIsMetaMask,
|
originIsMetaMask,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user