1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +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 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

View File

@ -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,
)

View File

@ -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

View File

@ -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
]);

View File

@ -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,

View File

@ -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 (

View File

@ -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,

View File

@ -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,

View File

@ -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);

View File

@ -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 = [

View File

@ -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),
);
}