From df3db6e5596b4b5e59923d9263a6f4f5da432a6d Mon Sep 17 00:00:00 2001 From: Vinicius Stevam <45455812+vinistevam@users.noreply.github.com> Date: Wed, 10 May 2023 06:36:01 +0100 Subject: [PATCH] Adopt ApprovalType from core (#18567) --- app/scripts/background.js | 12 ++++++------ app/scripts/controllers/app-state.js | 5 ++--- app/scripts/controllers/transactions/index.js | 8 +++----- app/scripts/controllers/transactions/index.test.js | 10 ++++------ .../handlers/add-ethereum-chain.js | 7 ++++--- .../handlers/switch-ethereum-chain.js | 3 ++- ui/components/app/add-network/add-network.js | 6 +++--- .../signature-request-original.component.js | 1 - ui/pages/confirmation/confirmation.js | 12 ++++++------ ui/pages/confirmation/templates/index.js | 14 +++++++------- ui/selectors/selectors.js | 4 ++-- 11 files changed, 39 insertions(+), 43 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 082a13b67..059b1bdd0 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -9,6 +9,9 @@ import debounce from 'debounce-stream'; import log from 'loglevel'; import browser from 'webextension-polyfill'; 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 { ethErrors } from 'eth-rpc-errors'; @@ -18,9 +21,6 @@ import { ENVIRONMENT_TYPE_FULLSCREEN, EXTENSION_MESSAGES, PLATFORM_FIREFOX, - ///: BEGIN:ONLY_INCLUDE_IN(snaps) - MESSAGE_TYPE, - ///: END:ONLY_INCLUDE_IN } from '../../shared/constants/app'; import { REJECT_NOTIFICATION_CLOSE, @@ -800,11 +800,11 @@ export function setupController( ({ id, type }) => { switch (type) { ///: BEGIN:ONLY_INCLUDE_IN(snaps) - case MESSAGE_TYPE.SNAP_DIALOG_ALERT: - case MESSAGE_TYPE.SNAP_DIALOG_PROMPT: + case ApprovalType.SnapDialogAlert: + case ApprovalType.SnapDialogPrompt: controller.approvalController.accept(id, null); break; - case MESSAGE_TYPE.SNAP_DIALOG_CONFIRMATION: + case ApprovalType.SnapDialogConfirmation: controller.approvalController.accept(id, false); break; ///: END:ONLY_INCLUDE_IN diff --git a/app/scripts/controllers/app-state.js b/app/scripts/controllers/app-state.js index 63eaf085d..92166eac2 100644 --- a/app/scripts/controllers/app-state.js +++ b/app/scripts/controllers/app-state.js @@ -2,6 +2,7 @@ import EventEmitter from 'events'; import { ObservableStore } from '@metamask/obs-store'; import { v4 as uuid } from 'uuid'; import log from 'loglevel'; +import { ApprovalType } from '@metamask/controller-utils'; import { METAMASK_CONTROLLER_EVENTS } from '../metamask-controller'; import { MINUTE } from '../../../shared/constants/time'; import { AUTO_LOCK_TIMEOUT_ALARM } from '../../../shared/constants/alarms'; @@ -13,8 +14,6 @@ import { ORIGIN_METAMASK, } from '../../../shared/constants/app'; -const APPROVAL_REQUEST_TYPE = 'unlock'; - export default class AppStateController extends EventEmitter { /** * @param {object} opts @@ -408,7 +407,7 @@ export default class AppStateController extends EventEmitter { { id: this._approvalRequestId, origin: ORIGIN_METAMASK, - type: APPROVAL_REQUEST_TYPE, + type: ApprovalType.Unlock, }, true, ) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index e31215171..a3bad2f62 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -5,6 +5,7 @@ import EthQuery from 'ethjs-query'; import { ethErrors } from 'eth-rpc-errors'; import { Common, Hardfork } from '@ethereumjs/common'; import { TransactionFactory } from '@ethereumjs/tx'; +import { ApprovalType } from '@metamask/controller-utils'; import NonceTracker from 'nonce-tracker'; import log from 'loglevel'; import BigNumber from 'bignumber.js'; @@ -51,10 +52,7 @@ import { determineTransactionType, isEIP1559Transaction, } from '../../../../shared/modules/transaction.utils'; -import { - ORIGIN_METAMASK, - MESSAGE_TYPE, -} from '../../../../shared/constants/app'; +import { ORIGIN_METAMASK } from '../../../../shared/constants/app'; import { calcGasTotal, getSwapsTokensReceivedFromTxMeta, @@ -2651,7 +2649,7 @@ export default class TransactionController extends EventEmitter { _requestApproval(txMeta) { const id = this._getApprovalId(txMeta); const { origin } = txMeta; - const type = MESSAGE_TYPE.TRANSACTION; + const type = ApprovalType.Transaction; const requestData = { txId: txMeta.id }; this.messagingSystem diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index 12e39d5ac..1203c6c58 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -3,6 +3,7 @@ import EventEmitter from 'events'; import { toBuffer } from 'ethereumjs-util'; import { TransactionFactory } from '@ethereumjs/tx'; import { ObservableStore } from '@metamask/obs-store'; +import { ApprovalType } from '@metamask/controller-utils'; import sinon from 'sinon'; import { @@ -29,10 +30,7 @@ import { GasRecommendations, } from '../../../../shared/constants/gas'; import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller'; -import { - MESSAGE_TYPE, - ORIGIN_METAMASK, -} from '../../../../shared/constants/app'; +import { ORIGIN_METAMASK } from '../../../../shared/constants/app'; import { NetworkStatus } from '../../../../shared/constants/network'; import { TRANSACTION_ENVELOPE_TYPE_NAMES } from '../../../../shared/lib/transactions-controller-utils'; import TransactionController from '.'; @@ -513,7 +511,7 @@ describe('Transaction Controller', function () { id: String(txMeta.id), origin: ORIGIN_METAMASK, requestData: { txId: txMeta.id }, - type: MESSAGE_TYPE.TRANSACTION, + type: ApprovalType.Transaction, }, true, // Show popup ]); @@ -551,7 +549,7 @@ describe('Transaction Controller', function () { id: String(secondTxMeta.id), origin: ORIGIN_METAMASK, requestData: { txId: secondTxMeta.id }, - type: MESSAGE_TYPE.TRANSACTION, + type: ApprovalType.Transaction, }, true, // Show popup ]); diff --git a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js index f63412b61..03b3f5714 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js @@ -1,6 +1,7 @@ import { ethErrors, errorCodes } from 'eth-rpc-errors'; import validUrl from 'valid-url'; import { omit } from 'lodash'; +import { ApprovalType } from '@metamask/controller-utils'; import { MESSAGE_TYPE, UNKNOWN_TICKER_SYMBOL, @@ -158,7 +159,7 @@ async function addEthereumChainHandler( try { await requestUserApproval({ origin, - type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, + type: ApprovalType.SwitchEthereumChain, requestData: { rpcUrl: existingNetwork.rpcUrl, chainId: existingNetwork.chainId, @@ -244,7 +245,7 @@ async function addEthereumChainHandler( try { await requestUserApproval({ origin, - type: MESSAGE_TYPE.ADD_ETHEREUM_CHAIN, + type: ApprovalType.AddEthereumChain, requestData: { chainId: _chainId, rpcPrefs: { blockExplorerUrl: firstValidBlockExplorerUrl }, @@ -275,7 +276,7 @@ async function addEthereumChainHandler( try { await requestUserApproval({ origin, - type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, + type: ApprovalType.SwitchEthereumChain, requestData: { rpcUrl: firstValidRPCUrl, chainId: _chainId, diff --git a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js index 4755a2c3d..6d71ba602 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js @@ -1,5 +1,6 @@ import { ethErrors } from 'eth-rpc-errors'; import { omit } from 'lodash'; +import { ApprovalType } from '@metamask/controller-utils'; import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; import { CHAIN_ID_TO_TYPE_MAP, @@ -109,7 +110,7 @@ async function switchEthereumChainHandler( try { const approvedRequestData = await requestUserApproval({ origin, - type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, + type: ApprovalType.SwitchEthereumChain, requestData, }); if ( diff --git a/ui/components/app/add-network/add-network.js b/ui/components/app/add-network/add-network.js index d8eae27ee..d0eca654b 100644 --- a/ui/components/app/add-network/add-network.js +++ b/ui/components/app/add-network/add-network.js @@ -1,6 +1,7 @@ import React, { useContext, useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; +import { ApprovalType } from '@metamask/controller-utils'; import { I18nContext } from '../../../contexts/i18n'; import Box from '../../ui/box'; import { @@ -26,7 +27,6 @@ import { import { ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_POPUP, - MESSAGE_TYPE, ORIGIN_METAMASK, } from '../../../../shared/constants/app'; import { requestUserApproval } from '../../../store/actions'; @@ -66,7 +66,7 @@ const AddNetwork = () => { unapprovedConfirmations?.find((confirmation) => { return ( confirmation.origin === 'metamask' && - confirmation.type === MESSAGE_TYPE.ADD_ETHEREUM_CHAIN + confirmation.type === ApprovalType.AddEthereumChain ); }); if (!showPopover && anAddNetworkConfirmationFromMetaMaskExists) { @@ -258,7 +258,7 @@ const AddNetwork = () => { await dispatch( requestUserApproval({ origin: ORIGIN_METAMASK, - type: MESSAGE_TYPE.ADD_ETHEREUM_CHAIN, + type: ApprovalType.AddEthereumChain, requestData: { chainId: item.chainId, rpcUrl: item.rpcUrl, diff --git a/ui/components/app/signature-request-original/signature-request-original.component.js b/ui/components/app/signature-request-original/signature-request-original.component.js index cfabd20bb..0cc27dd42 100644 --- a/ui/components/app/signature-request-original/signature-request-original.component.js +++ b/ui/components/app/signature-request-original/signature-request-original.component.js @@ -3,7 +3,6 @@ import PropTypes from 'prop-types'; import classnames from 'classnames'; import { ObjectInspector } from 'react-inspector'; import LedgerInstructionField from '../ledger-instruction-field'; - import { MESSAGE_TYPE } from '../../../../shared/constants/app'; import { getURLHostName, diff --git a/ui/pages/confirmation/confirmation.js b/ui/pages/confirmation/confirmation.js index 83a5b1cba..4666a4cbf 100644 --- a/ui/pages/confirmation/confirmation.js +++ b/ui/pages/confirmation/confirmation.js @@ -11,7 +11,7 @@ import { useHistory } from 'react-router-dom'; import { isEqual } from 'lodash'; import { produce } from 'immer'; -import { MESSAGE_TYPE } from '../../../shared/constants/app'; +import { ApprovalType } from '@metamask/controller-utils'; import Box from '../../components/ui/box'; import MetaMaskTemplateRenderer from '../../components/app/metamask-template-renderer'; import ConfirmationWarningModal from '../../components/app/confirmation-warning-modal'; @@ -198,9 +198,9 @@ export default function ConfirmationPage({ getSnapName(pendingConfirmation?.origin, targetSubjectMetadata); const SNAP_DIALOG_TYPE = [ - MESSAGE_TYPE.SNAP_DIALOG_ALERT, - MESSAGE_TYPE.SNAP_DIALOG_CONFIRMATION, - MESSAGE_TYPE.SNAP_DIALOG_PROMPT, + ApprovalType.SnapDialogAlert, + ApprovalType.SnapDialogConfirmation, + ApprovalType.SnapDialogPrompt, ]; const isSnapDialog = SNAP_DIALOG_TYPE.includes(pendingConfirmation?.type); @@ -208,7 +208,7 @@ export default function ConfirmationPage({ const INPUT_STATE_CONFIRMATIONS = [ ///: BEGIN:ONLY_INCLUDE_IN(snaps) - MESSAGE_TYPE.SNAP_DIALOG_PROMPT, + ApprovalType.SnapDialogPrompt, ///: END:ONLY_INCLUDE_IN ]; @@ -284,7 +284,7 @@ export default function ConfirmationPage({ setShowWarningModal(true); } else { const inputState = hasInputState(pendingConfirmation.type) - ? inputStates[MESSAGE_TYPE.SNAP_DIALOG_PROMPT] + ? inputStates[ApprovalType.SnapDialogPrompt] : null; // submit result is an array of errors or empty on success const submitResult = await templatedValues.onSubmit(inputState); diff --git a/ui/pages/confirmation/templates/index.js b/ui/pages/confirmation/templates/index.js index 2dc7a50c5..bd2b733a6 100644 --- a/ui/pages/confirmation/templates/index.js +++ b/ui/pages/confirmation/templates/index.js @@ -1,5 +1,5 @@ import { omit, pick } from 'lodash'; -import { MESSAGE_TYPE } from '../../../../shared/constants/app'; +import { ApprovalType } from '@metamask/controller-utils'; import { rejectPendingApproval, resolvePendingApproval, @@ -15,16 +15,16 @@ import snapPrompt from './snaps/snap-prompt/snap-prompt'; ///: END:ONLY_INCLUDE_IN const APPROVAL_TEMPLATES = { - [MESSAGE_TYPE.ADD_ETHEREUM_CHAIN]: addEthereumChain, - [MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN]: switchEthereumChain, + [ApprovalType.AddEthereumChain]: addEthereumChain, + [ApprovalType.SwitchEthereumChain]: switchEthereumChain, ///: BEGIN:ONLY_INCLUDE_IN(snaps) - [MESSAGE_TYPE.SNAP_DIALOG_ALERT]: snapAlert, - [MESSAGE_TYPE.SNAP_DIALOG_CONFIRMATION]: snapConfirmation, - [MESSAGE_TYPE.SNAP_DIALOG_PROMPT]: snapPrompt, + [ApprovalType.SnapDialogAlert]: snapAlert, + [ApprovalType.SnapDialogConfirmation]: snapConfirmation, + [ApprovalType.SnapDialogPrompt]: snapPrompt, ///: END:ONLY_INCLUDE_IN }; -export const TEMPLATED_CONFIRMATION_MESSAGE_TYPES = +export const TEMPLATED_CONFIRMATION_APPROVAL_TYPES = Object.keys(APPROVAL_TEMPLATES); const ALLOWED_TEMPLATE_KEYS = [ diff --git a/ui/selectors/selectors.js b/ui/selectors/selectors.js index de7a4b8dc..85cb57be3 100644 --- a/ui/selectors/selectors.js +++ b/ui/selectors/selectors.js @@ -63,7 +63,7 @@ import { ///: END:ONLY_INCLUDE_IN } 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 { DAY } from '../../shared/constants/time'; import { TERMS_OF_USE_LAST_UPDATED } from '../../shared/constants/terms'; @@ -551,7 +551,7 @@ export function getUnapprovedConfirmations(state) { export function getUnapprovedTemplatedConfirmations(state) { const unapprovedConfirmations = getUnapprovedConfirmations(state); return unapprovedConfirmations.filter((approval) => - TEMPLATED_CONFIRMATION_MESSAGE_TYPES.includes(approval.type), + TEMPLATED_CONFIRMATION_APPROVAL_TYPES.includes(approval.type), ); }