1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Adopt ApprovalType from core (#18567)

This commit is contained in:
Vinicius Stevam 2023-05-10 06:36:01 +01:00 committed by GitHub
parent e991439718
commit df3db6e559
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 39 additions and 43 deletions

View File

@ -9,6 +9,9 @@ import debounce from 'debounce-stream';
import log from 'loglevel'; import log from 'loglevel';
import browser from 'webextension-polyfill'; import browser from 'webextension-polyfill';
import { storeAsStream } from '@metamask/obs-store'; import { storeAsStream } from '@metamask/obs-store';
///: BEGIN:ONLY_INCLUDE_IN(snaps)
import { ApprovalType } from '@metamask/controller-utils';
///: END:ONLY_INCLUDE_IN
import PortStream from 'extension-port-stream'; import PortStream from 'extension-port-stream';
import { ethErrors } from 'eth-rpc-errors'; import { ethErrors } from 'eth-rpc-errors';
@ -18,9 +21,6 @@ import {
ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_FULLSCREEN,
EXTENSION_MESSAGES, EXTENSION_MESSAGES,
PLATFORM_FIREFOX, PLATFORM_FIREFOX,
///: BEGIN:ONLY_INCLUDE_IN(snaps)
MESSAGE_TYPE,
///: END:ONLY_INCLUDE_IN
} from '../../shared/constants/app'; } from '../../shared/constants/app';
import { import {
REJECT_NOTIFICATION_CLOSE, REJECT_NOTIFICATION_CLOSE,
@ -800,11 +800,11 @@ export function setupController(
({ id, type }) => { ({ id, type }) => {
switch (type) { switch (type) {
///: BEGIN:ONLY_INCLUDE_IN(snaps) ///: BEGIN:ONLY_INCLUDE_IN(snaps)
case MESSAGE_TYPE.SNAP_DIALOG_ALERT: case ApprovalType.SnapDialogAlert:
case MESSAGE_TYPE.SNAP_DIALOG_PROMPT: case ApprovalType.SnapDialogPrompt:
controller.approvalController.accept(id, null); controller.approvalController.accept(id, null);
break; break;
case MESSAGE_TYPE.SNAP_DIALOG_CONFIRMATION: case ApprovalType.SnapDialogConfirmation:
controller.approvalController.accept(id, false); controller.approvalController.accept(id, false);
break; break;
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN

View File

@ -2,6 +2,7 @@ import EventEmitter from 'events';
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import log from 'loglevel'; import log from 'loglevel';
import { ApprovalType } from '@metamask/controller-utils';
import { METAMASK_CONTROLLER_EVENTS } from '../metamask-controller'; import { METAMASK_CONTROLLER_EVENTS } from '../metamask-controller';
import { MINUTE } from '../../../shared/constants/time'; import { MINUTE } from '../../../shared/constants/time';
import { AUTO_LOCK_TIMEOUT_ALARM } from '../../../shared/constants/alarms'; import { AUTO_LOCK_TIMEOUT_ALARM } from '../../../shared/constants/alarms';
@ -13,8 +14,6 @@ import {
ORIGIN_METAMASK, ORIGIN_METAMASK,
} from '../../../shared/constants/app'; } from '../../../shared/constants/app';
const APPROVAL_REQUEST_TYPE = 'unlock';
export default class AppStateController extends EventEmitter { export default class AppStateController extends EventEmitter {
/** /**
* @param {object} opts * @param {object} opts
@ -408,7 +407,7 @@ export default class AppStateController extends EventEmitter {
{ {
id: this._approvalRequestId, id: this._approvalRequestId,
origin: ORIGIN_METAMASK, origin: ORIGIN_METAMASK,
type: APPROVAL_REQUEST_TYPE, type: ApprovalType.Unlock,
}, },
true, true,
) )

View File

@ -5,6 +5,7 @@ import EthQuery from 'ethjs-query';
import { ethErrors } from 'eth-rpc-errors'; import { ethErrors } from 'eth-rpc-errors';
import { Common, Hardfork } from '@ethereumjs/common'; import { Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory } from '@ethereumjs/tx'; import { TransactionFactory } from '@ethereumjs/tx';
import { ApprovalType } from '@metamask/controller-utils';
import NonceTracker from 'nonce-tracker'; import NonceTracker from 'nonce-tracker';
import log from 'loglevel'; import log from 'loglevel';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
@ -51,10 +52,7 @@ import {
determineTransactionType, determineTransactionType,
isEIP1559Transaction, isEIP1559Transaction,
} from '../../../../shared/modules/transaction.utils'; } from '../../../../shared/modules/transaction.utils';
import { import { ORIGIN_METAMASK } from '../../../../shared/constants/app';
ORIGIN_METAMASK,
MESSAGE_TYPE,
} from '../../../../shared/constants/app';
import { import {
calcGasTotal, calcGasTotal,
getSwapsTokensReceivedFromTxMeta, getSwapsTokensReceivedFromTxMeta,
@ -2651,7 +2649,7 @@ export default class TransactionController extends EventEmitter {
_requestApproval(txMeta) { _requestApproval(txMeta) {
const id = this._getApprovalId(txMeta); const id = this._getApprovalId(txMeta);
const { origin } = txMeta; const { origin } = txMeta;
const type = MESSAGE_TYPE.TRANSACTION; const type = ApprovalType.Transaction;
const requestData = { txId: txMeta.id }; const requestData = { txId: txMeta.id };
this.messagingSystem this.messagingSystem

View File

@ -3,6 +3,7 @@ import EventEmitter from 'events';
import { toBuffer } from 'ethereumjs-util'; import { toBuffer } from 'ethereumjs-util';
import { TransactionFactory } from '@ethereumjs/tx'; import { TransactionFactory } from '@ethereumjs/tx';
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import { ApprovalType } from '@metamask/controller-utils';
import sinon from 'sinon'; import sinon from 'sinon';
import { import {
@ -29,10 +30,7 @@ import {
GasRecommendations, GasRecommendations,
} from '../../../../shared/constants/gas'; } from '../../../../shared/constants/gas';
import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller'; import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller';
import { import { ORIGIN_METAMASK } from '../../../../shared/constants/app';
MESSAGE_TYPE,
ORIGIN_METAMASK,
} from '../../../../shared/constants/app';
import { NetworkStatus } from '../../../../shared/constants/network'; import { NetworkStatus } from '../../../../shared/constants/network';
import { TRANSACTION_ENVELOPE_TYPE_NAMES } from '../../../../shared/lib/transactions-controller-utils'; import { TRANSACTION_ENVELOPE_TYPE_NAMES } from '../../../../shared/lib/transactions-controller-utils';
import TransactionController from '.'; import TransactionController from '.';
@ -513,7 +511,7 @@ describe('Transaction Controller', function () {
id: String(txMeta.id), id: String(txMeta.id),
origin: ORIGIN_METAMASK, origin: ORIGIN_METAMASK,
requestData: { txId: txMeta.id }, requestData: { txId: txMeta.id },
type: MESSAGE_TYPE.TRANSACTION, type: ApprovalType.Transaction,
}, },
true, // Show popup true, // Show popup
]); ]);
@ -551,7 +549,7 @@ describe('Transaction Controller', function () {
id: String(secondTxMeta.id), id: String(secondTxMeta.id),
origin: ORIGIN_METAMASK, origin: ORIGIN_METAMASK,
requestData: { txId: secondTxMeta.id }, requestData: { txId: secondTxMeta.id },
type: MESSAGE_TYPE.TRANSACTION, type: ApprovalType.Transaction,
}, },
true, // Show popup true, // Show popup
]); ]);

View File

@ -1,6 +1,7 @@
import { ethErrors, errorCodes } from 'eth-rpc-errors'; import { ethErrors, errorCodes } from 'eth-rpc-errors';
import validUrl from 'valid-url'; import validUrl from 'valid-url';
import { omit } from 'lodash'; import { omit } from 'lodash';
import { ApprovalType } from '@metamask/controller-utils';
import { import {
MESSAGE_TYPE, MESSAGE_TYPE,
UNKNOWN_TICKER_SYMBOL, UNKNOWN_TICKER_SYMBOL,
@ -158,7 +159,7 @@ async function addEthereumChainHandler(
try { try {
await requestUserApproval({ await requestUserApproval({
origin, origin,
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, type: ApprovalType.SwitchEthereumChain,
requestData: { requestData: {
rpcUrl: existingNetwork.rpcUrl, rpcUrl: existingNetwork.rpcUrl,
chainId: existingNetwork.chainId, chainId: existingNetwork.chainId,
@ -244,7 +245,7 @@ async function addEthereumChainHandler(
try { try {
await requestUserApproval({ await requestUserApproval({
origin, origin,
type: MESSAGE_TYPE.ADD_ETHEREUM_CHAIN, type: ApprovalType.AddEthereumChain,
requestData: { requestData: {
chainId: _chainId, chainId: _chainId,
rpcPrefs: { blockExplorerUrl: firstValidBlockExplorerUrl }, rpcPrefs: { blockExplorerUrl: firstValidBlockExplorerUrl },
@ -275,7 +276,7 @@ async function addEthereumChainHandler(
try { try {
await requestUserApproval({ await requestUserApproval({
origin, origin,
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, type: ApprovalType.SwitchEthereumChain,
requestData: { requestData: {
rpcUrl: firstValidRPCUrl, rpcUrl: firstValidRPCUrl,
chainId: _chainId, chainId: _chainId,

View File

@ -1,5 +1,6 @@
import { ethErrors } from 'eth-rpc-errors'; import { ethErrors } from 'eth-rpc-errors';
import { omit } from 'lodash'; import { omit } from 'lodash';
import { ApprovalType } from '@metamask/controller-utils';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
import { import {
CHAIN_ID_TO_TYPE_MAP, CHAIN_ID_TO_TYPE_MAP,
@ -109,7 +110,7 @@ async function switchEthereumChainHandler(
try { try {
const approvedRequestData = await requestUserApproval({ const approvedRequestData = await requestUserApproval({
origin, origin,
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, type: ApprovalType.SwitchEthereumChain,
requestData, requestData,
}); });
if ( if (

View File

@ -1,6 +1,7 @@
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
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 { ApprovalType } from '@metamask/controller-utils';
import { I18nContext } from '../../../contexts/i18n'; import { I18nContext } from '../../../contexts/i18n';
import Box from '../../ui/box'; import Box from '../../ui/box';
import { import {
@ -26,7 +27,6 @@ import {
import { import {
ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_FULLSCREEN,
ENVIRONMENT_TYPE_POPUP, ENVIRONMENT_TYPE_POPUP,
MESSAGE_TYPE,
ORIGIN_METAMASK, ORIGIN_METAMASK,
} from '../../../../shared/constants/app'; } from '../../../../shared/constants/app';
import { requestUserApproval } from '../../../store/actions'; import { requestUserApproval } from '../../../store/actions';
@ -66,7 +66,7 @@ const AddNetwork = () => {
unapprovedConfirmations?.find((confirmation) => { unapprovedConfirmations?.find((confirmation) => {
return ( return (
confirmation.origin === 'metamask' && confirmation.origin === 'metamask' &&
confirmation.type === MESSAGE_TYPE.ADD_ETHEREUM_CHAIN confirmation.type === ApprovalType.AddEthereumChain
); );
}); });
if (!showPopover && anAddNetworkConfirmationFromMetaMaskExists) { if (!showPopover && anAddNetworkConfirmationFromMetaMaskExists) {
@ -258,7 +258,7 @@ const AddNetwork = () => {
await dispatch( await dispatch(
requestUserApproval({ requestUserApproval({
origin: ORIGIN_METAMASK, origin: ORIGIN_METAMASK,
type: MESSAGE_TYPE.ADD_ETHEREUM_CHAIN, type: ApprovalType.AddEthereumChain,
requestData: { requestData: {
chainId: item.chainId, chainId: item.chainId,
rpcUrl: item.rpcUrl, rpcUrl: item.rpcUrl,

View File

@ -3,7 +3,6 @@ import PropTypes from 'prop-types';
import classnames from 'classnames'; import classnames from 'classnames';
import { ObjectInspector } from 'react-inspector'; import { ObjectInspector } from 'react-inspector';
import LedgerInstructionField from '../ledger-instruction-field'; import LedgerInstructionField from '../ledger-instruction-field';
import { MESSAGE_TYPE } from '../../../../shared/constants/app'; import { MESSAGE_TYPE } from '../../../../shared/constants/app';
import { import {
getURLHostName, getURLHostName,

View File

@ -11,7 +11,7 @@ import { useHistory } from 'react-router-dom';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
import { produce } from 'immer'; import { produce } from 'immer';
import { MESSAGE_TYPE } from '../../../shared/constants/app'; import { ApprovalType } from '@metamask/controller-utils';
import Box from '../../components/ui/box'; import Box from '../../components/ui/box';
import MetaMaskTemplateRenderer from '../../components/app/metamask-template-renderer'; import MetaMaskTemplateRenderer from '../../components/app/metamask-template-renderer';
import ConfirmationWarningModal from '../../components/app/confirmation-warning-modal'; import ConfirmationWarningModal from '../../components/app/confirmation-warning-modal';
@ -198,9 +198,9 @@ export default function ConfirmationPage({
getSnapName(pendingConfirmation?.origin, targetSubjectMetadata); getSnapName(pendingConfirmation?.origin, targetSubjectMetadata);
const SNAP_DIALOG_TYPE = [ const SNAP_DIALOG_TYPE = [
MESSAGE_TYPE.SNAP_DIALOG_ALERT, ApprovalType.SnapDialogAlert,
MESSAGE_TYPE.SNAP_DIALOG_CONFIRMATION, ApprovalType.SnapDialogConfirmation,
MESSAGE_TYPE.SNAP_DIALOG_PROMPT, ApprovalType.SnapDialogPrompt,
]; ];
const isSnapDialog = SNAP_DIALOG_TYPE.includes(pendingConfirmation?.type); const isSnapDialog = SNAP_DIALOG_TYPE.includes(pendingConfirmation?.type);
@ -208,7 +208,7 @@ export default function ConfirmationPage({
const INPUT_STATE_CONFIRMATIONS = [ const INPUT_STATE_CONFIRMATIONS = [
///: BEGIN:ONLY_INCLUDE_IN(snaps) ///: BEGIN:ONLY_INCLUDE_IN(snaps)
MESSAGE_TYPE.SNAP_DIALOG_PROMPT, ApprovalType.SnapDialogPrompt,
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN
]; ];
@ -284,7 +284,7 @@ export default function ConfirmationPage({
setShowWarningModal(true); setShowWarningModal(true);
} else { } else {
const inputState = hasInputState(pendingConfirmation.type) const inputState = hasInputState(pendingConfirmation.type)
? inputStates[MESSAGE_TYPE.SNAP_DIALOG_PROMPT] ? inputStates[ApprovalType.SnapDialogPrompt]
: null; : null;
// submit result is an array of errors or empty on success // submit result is an array of errors or empty on success
const submitResult = await templatedValues.onSubmit(inputState); const submitResult = await templatedValues.onSubmit(inputState);

View File

@ -1,5 +1,5 @@
import { omit, pick } from 'lodash'; import { omit, pick } from 'lodash';
import { MESSAGE_TYPE } from '../../../../shared/constants/app'; import { ApprovalType } from '@metamask/controller-utils';
import { import {
rejectPendingApproval, rejectPendingApproval,
resolvePendingApproval, resolvePendingApproval,
@ -15,16 +15,16 @@ import snapPrompt from './snaps/snap-prompt/snap-prompt';
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN
const APPROVAL_TEMPLATES = { const APPROVAL_TEMPLATES = {
[MESSAGE_TYPE.ADD_ETHEREUM_CHAIN]: addEthereumChain, [ApprovalType.AddEthereumChain]: addEthereumChain,
[MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN]: switchEthereumChain, [ApprovalType.SwitchEthereumChain]: switchEthereumChain,
///: BEGIN:ONLY_INCLUDE_IN(snaps) ///: BEGIN:ONLY_INCLUDE_IN(snaps)
[MESSAGE_TYPE.SNAP_DIALOG_ALERT]: snapAlert, [ApprovalType.SnapDialogAlert]: snapAlert,
[MESSAGE_TYPE.SNAP_DIALOG_CONFIRMATION]: snapConfirmation, [ApprovalType.SnapDialogConfirmation]: snapConfirmation,
[MESSAGE_TYPE.SNAP_DIALOG_PROMPT]: snapPrompt, [ApprovalType.SnapDialogPrompt]: snapPrompt,
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN
}; };
export const TEMPLATED_CONFIRMATION_MESSAGE_TYPES = export const TEMPLATED_CONFIRMATION_APPROVAL_TYPES =
Object.keys(APPROVAL_TEMPLATES); Object.keys(APPROVAL_TEMPLATES);
const ALLOWED_TEMPLATE_KEYS = [ const ALLOWED_TEMPLATE_KEYS = [

View File

@ -63,7 +63,7 @@ import {
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN
} from '../helpers/utils/util'; } from '../helpers/utils/util';
import { TEMPLATED_CONFIRMATION_MESSAGE_TYPES } from '../pages/confirmation/templates'; import { TEMPLATED_CONFIRMATION_APPROVAL_TYPES } from '../pages/confirmation/templates';
import { STATIC_MAINNET_TOKEN_LIST } from '../../shared/constants/tokens'; import { STATIC_MAINNET_TOKEN_LIST } from '../../shared/constants/tokens';
import { DAY } from '../../shared/constants/time'; import { DAY } from '../../shared/constants/time';
import { TERMS_OF_USE_LAST_UPDATED } from '../../shared/constants/terms'; import { TERMS_OF_USE_LAST_UPDATED } from '../../shared/constants/terms';
@ -551,7 +551,7 @@ export function getUnapprovedConfirmations(state) {
export function getUnapprovedTemplatedConfirmations(state) { export function getUnapprovedTemplatedConfirmations(state) {
const unapprovedConfirmations = getUnapprovedConfirmations(state); const unapprovedConfirmations = getUnapprovedConfirmations(state);
return unapprovedConfirmations.filter((approval) => return unapprovedConfirmations.filter((approval) =>
TEMPLATED_CONFIRMATION_MESSAGE_TYPES.includes(approval.type), TEMPLATED_CONFIRMATION_APPROVAL_TYPES.includes(approval.type),
); );
} }