2021-02-22 17:20:42 +01:00
|
|
|
import { omit, pick } from 'lodash';
|
2023-05-10 07:36:01 +02:00
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
2021-02-22 17:20:42 +01:00
|
|
|
import {
|
|
|
|
rejectPendingApproval,
|
|
|
|
resolvePendingApproval,
|
2023-03-09 22:00:28 +01:00
|
|
|
setNewNetworkAdded,
|
|
|
|
upsertNetworkConfiguration,
|
2021-02-22 17:20:42 +01:00
|
|
|
} from '../../../store/actions';
|
|
|
|
import addEthereumChain from './add-ethereum-chain';
|
|
|
|
import switchEthereumChain from './switch-ethereum-chain';
|
2023-07-12 10:29:54 +02:00
|
|
|
import success from './success';
|
|
|
|
import error from './error';
|
2023-04-25 16:32:51 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(snaps)
|
|
|
|
import snapAlert from './snaps/snap-alert/snap-alert';
|
|
|
|
import snapConfirmation from './snaps/snap-confirmation/snap-confirmation';
|
|
|
|
import snapPrompt from './snaps/snap-prompt/snap-prompt';
|
2022-02-15 01:02:51 +01:00
|
|
|
///: END:ONLY_INCLUDE_IN
|
2021-02-22 17:20:42 +01:00
|
|
|
|
|
|
|
const APPROVAL_TEMPLATES = {
|
2023-05-10 07:36:01 +02:00
|
|
|
[ApprovalType.AddEthereumChain]: addEthereumChain,
|
|
|
|
[ApprovalType.SwitchEthereumChain]: switchEthereumChain,
|
2023-07-12 10:29:54 +02:00
|
|
|
// Use ApprovalType from utils controller
|
|
|
|
[ApprovalType.ResultSuccess]: success,
|
|
|
|
[ApprovalType.ResultError]: error,
|
2023-04-25 16:32:51 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(snaps)
|
2023-05-10 07:36:01 +02:00
|
|
|
[ApprovalType.SnapDialogAlert]: snapAlert,
|
|
|
|
[ApprovalType.SnapDialogConfirmation]: snapConfirmation,
|
|
|
|
[ApprovalType.SnapDialogPrompt]: snapPrompt,
|
2022-02-15 01:02:51 +01:00
|
|
|
///: END:ONLY_INCLUDE_IN
|
2021-02-22 17:20:42 +01:00
|
|
|
};
|
|
|
|
|
2023-05-10 07:36:01 +02:00
|
|
|
export const TEMPLATED_CONFIRMATION_APPROVAL_TYPES =
|
2022-07-31 20:26:40 +02:00
|
|
|
Object.keys(APPROVAL_TEMPLATES);
|
2021-03-23 22:12:32 +01:00
|
|
|
|
2021-02-22 17:20:42 +01:00
|
|
|
const ALLOWED_TEMPLATE_KEYS = [
|
|
|
|
'cancelText',
|
2022-12-01 16:46:06 +01:00
|
|
|
'content',
|
2021-02-22 17:20:42 +01:00
|
|
|
'onCancel',
|
2022-12-01 16:46:06 +01:00
|
|
|
'onSubmit',
|
2022-01-28 18:39:32 +01:00
|
|
|
'networkDisplay',
|
2022-12-01 16:46:06 +01:00
|
|
|
'submitText',
|
2023-03-08 17:33:27 +01:00
|
|
|
'loadingText',
|
2021-02-22 17:20:42 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} PendingApproval
|
2021-02-22 17:20:42 +01:00
|
|
|
* @property {string} id - The randomly generated id of the approval
|
|
|
|
* @property {string} origin - The origin of the site requesting this approval
|
|
|
|
* @property {number} time - The time the approval was requested
|
|
|
|
* @property {string} type - The type of approval being requested
|
2022-07-27 15:28:05 +02:00
|
|
|
* @property {object} requestData - The data submitted with the request
|
2021-02-22 17:20:42 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getTemplateAlerts calls the getAlerts function exported by the template if
|
|
|
|
* it exists, and then returns the result of that function. In the confirmation
|
|
|
|
* page the alerts returned from the getAlerts method will be set into the
|
|
|
|
* alertState state object.
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} pendingApproval - the object representing the confirmation
|
2023-05-19 14:13:34 +02:00
|
|
|
* @param {object} state - The state object consist of required info to determine alerts.
|
2021-02-22 17:20:42 +01:00
|
|
|
*/
|
2023-05-19 14:13:34 +02:00
|
|
|
export async function getTemplateAlerts(pendingApproval, state) {
|
2021-02-22 17:20:42 +01:00
|
|
|
const fn = APPROVAL_TEMPLATES[pendingApproval.type]?.getAlerts;
|
2023-05-19 14:13:34 +02:00
|
|
|
const results = fn ? await fn(pendingApproval, state) : [];
|
2021-02-22 17:20:42 +01:00
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
throw new Error(`Template alerts must be an array, received: ${results}`);
|
|
|
|
}
|
|
|
|
if (results.some((result) => result?.id === undefined)) {
|
|
|
|
throw new Error(
|
|
|
|
`Template alert entries must be objects with an id key. Received: ${results}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The function call to return state must be a promise returning function
|
|
|
|
* this "NOOP" is here to conform to the requirements for templates without
|
|
|
|
* state.
|
|
|
|
*/
|
|
|
|
async function emptyState() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getTemplateState calls the getState function exported by the template if
|
|
|
|
* it exists, and then returns the result of that function. In the confirmation
|
|
|
|
* page the object returned from the getState method will be set into the
|
|
|
|
* confirmationState state object. Note, this state is not consumed by the page
|
|
|
|
* itself.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} pendingApproval - the object representing the confirmation
|
2021-02-22 17:20:42 +01:00
|
|
|
*/
|
|
|
|
export async function getTemplateState(pendingApproval) {
|
|
|
|
const fn = APPROVAL_TEMPLATES[pendingApproval.type]?.getState ?? emptyState;
|
|
|
|
const result = await fn(pendingApproval);
|
|
|
|
if (typeof result !== 'object' || Array.isArray(result)) {
|
|
|
|
throw new Error(`Template state must be an object, received: ${result}`);
|
|
|
|
} else if (result === null || result === undefined) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We do not want to pass the entire dispatch function to the template.
|
|
|
|
* This function should return an object of actions that we generally consider
|
|
|
|
* to be safe for templates to invoke. In the future we could put these behind
|
|
|
|
* permission sets so that snaps that wish to manipulate state must ask for
|
|
|
|
* explicit permission to do so.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-02-22 17:20:42 +01:00
|
|
|
* @param {Function} dispatch - Redux dispatch function
|
|
|
|
*/
|
|
|
|
function getAttenuatedDispatch(dispatch) {
|
|
|
|
return {
|
|
|
|
rejectPendingApproval: (...args) =>
|
|
|
|
dispatch(rejectPendingApproval(...args)),
|
|
|
|
resolvePendingApproval: (...args) =>
|
|
|
|
dispatch(resolvePendingApproval(...args)),
|
2023-03-09 22:00:28 +01:00
|
|
|
upsertNetworkConfiguration: (...args) =>
|
|
|
|
dispatch(upsertNetworkConfiguration(...args)),
|
|
|
|
setNewNetworkAdded: (...args) => dispatch(setNewNetworkAdded(...args)),
|
2021-02-22 17:20:42 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the templated values to be consumed in the confirmation page
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-12-01 16:46:06 +01:00
|
|
|
* @param {object} pendingApproval - The pending confirmation object.
|
|
|
|
* @param {Function} t - Translation function.
|
|
|
|
* @param {Function} dispatch - Redux dispatch function.
|
|
|
|
* @param {object} history - The application's history object.
|
|
|
|
* @param {Function} setInputState - A function that can be used to record the
|
|
|
|
* state of input fields in the templated component.
|
2021-02-22 17:20:42 +01:00
|
|
|
*/
|
2022-12-01 16:46:06 +01:00
|
|
|
export function getTemplateValues(
|
|
|
|
pendingApproval,
|
|
|
|
t,
|
|
|
|
dispatch,
|
|
|
|
history,
|
|
|
|
setInputState,
|
|
|
|
) {
|
2021-02-22 17:20:42 +01:00
|
|
|
const fn = APPROVAL_TEMPLATES[pendingApproval.type]?.getValues;
|
|
|
|
if (!fn) {
|
|
|
|
throw new Error(
|
|
|
|
`MESSAGE_TYPE: '${pendingApproval.type}' is not specified in approval templates`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const safeActions = getAttenuatedDispatch(dispatch);
|
2022-12-01 16:46:06 +01:00
|
|
|
const values = fn(pendingApproval, t, safeActions, history, setInputState);
|
2021-02-22 17:20:42 +01:00
|
|
|
const extraneousKeys = omit(values, ALLOWED_TEMPLATE_KEYS);
|
|
|
|
const safeValues = pick(values, ALLOWED_TEMPLATE_KEYS);
|
|
|
|
if (extraneousKeys.length > 0) {
|
|
|
|
throw new Error(
|
|
|
|
`Received extraneous keys from ${
|
|
|
|
pendingApproval.type
|
|
|
|
}.getValues. These keys are not passed to the confirmation page: ${Object.keys(
|
|
|
|
extraneousKeys,
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return safeValues;
|
|
|
|
}
|