1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/helpers/utils/permission.js
Frederik Bolding de955f3faa
[FLASK] snaps-monorepo@0.24.1 (#16525)
* snaps-monorepo@0.24.0

* Fix imports and regen LavaMoat policies

* Bump iframe-execution-environment

* Fix tests

* Add permissions

* Bump patch

* Bump test-snaps

* [FLASK] Fix update e2e test to catch snaps with caveats (#16546)

* changed snap to update to bip32

* small changes to test

* Fix lint

* Fix E2E

* Update copy

* Update icon

Co-authored-by: Bowen Sanders <bowensanders@gmail.com>
2022-11-22 13:07:08 +01:00

162 lines
4.9 KiB
JavaScript

import deepFreeze from 'deep-freeze-strict';
///: BEGIN:ONLY_INCLUDE_IN(flask)
import React from 'react';
///: END:ONLY_INCLUDE_IN
import {
RestrictedMethods,
///: BEGIN:ONLY_INCLUDE_IN(flask)
EndowmentPermissions,
PermissionNamespaces,
///: END:ONLY_INCLUDE_IN
} from '../../../shared/constants/permissions';
///: BEGIN:ONLY_INCLUDE_IN(flask)
import { coinTypeToProtocolName } from './util';
///: END:ONLY_INCLUDE_IN
const UNKNOWN_PERMISSION = Symbol('unknown');
const PERMISSION_DESCRIPTIONS = deepFreeze({
[RestrictedMethods.eth_accounts]: {
label: (t) => t('permission_ethereumAccounts'),
leftIcon: 'fas fa-eye',
rightIcon: null,
},
///: BEGIN:ONLY_INCLUDE_IN(flask)
[RestrictedMethods.snap_confirm]: {
label: (t) => t('permission_customConfirmation'),
leftIcon: 'fas fa-user-check',
rightIcon: null,
},
[RestrictedMethods.snap_notify]: {
leftIcon: 'fas fa-bell',
label: (t) => t('permission_notifications'),
rightIcon: null,
},
[RestrictedMethods.snap_getBip32PublicKey]: {
label: (t, _, permissionValue) => {
return permissionValue.caveats[0].value.map(({ path, curve }) =>
t('permission_viewBip32PublicKeys', [
<span className="permission-label-item" key={path.join('/')}>
{path.join('/')}
</span>,
curve,
]),
);
},
leftIcon: 'fas fa-eye',
rightIcon: null,
},
[RestrictedMethods.snap_getBip32Entropy]: {
label: (t, _, permissionValue) => {
return permissionValue.caveats[0].value.map(({ path, curve }) =>
t('permission_manageBip32Keys', [
<span className="permission-label-item" key={path.join('/')}>
{path.join('/')}
</span>,
curve,
]),
);
},
leftIcon: 'fas fa-door-open',
rightIcon: null,
},
[RestrictedMethods.snap_getBip44Entropy]: {
label: (t, _, permissionValue) => {
return permissionValue.caveats[0].value.map(({ coinType }) =>
t('permission_manageBip44Keys', [
<span className="permission-label-item" key={`coin-type-${coinType}`}>
{coinTypeToProtocolName(coinType) ||
`${coinType} (Unrecognized protocol)`}
</span>,
]),
);
},
leftIcon: 'fas fa-door-open',
rightIcon: null,
},
[RestrictedMethods.snap_getEntropy]: {
label: (t) => t('permission_getEntropy'),
leftIcon: 'fas fa-key',
rightIcon: null,
},
[RestrictedMethods.snap_manageState]: {
label: (t) => t('permission_manageState'),
leftIcon: 'fas fa-download',
rightIcon: null,
},
[RestrictedMethods['wallet_snap_*']]: {
label: (t, permissionName) => {
const snapId = permissionName.split('_').slice(-1);
return t('permission_accessSnap', [snapId]);
},
leftIcon: 'fas fa-bolt',
rightIcon: null,
},
[EndowmentPermissions['endowment:network-access']]: {
label: (t) => t('permission_accessNetwork'),
leftIcon: 'fas fa-wifi',
rightIcon: null,
},
[EndowmentPermissions['endowment:long-running']]: {
label: (t) => t('permission_longRunning'),
leftIcon: 'fas fa-infinity',
rightIcon: null,
},
[EndowmentPermissions['endowment:transaction-insight']]: {
label: (t) => t('permission_transactionInsight'),
leftIcon: 'fas fa-info',
rightIcon: null,
},
[EndowmentPermissions['endowment:cronjob']]: {
label: (t) => t('permission_cronjob'),
leftIcon: 'fas fa-clock',
rightIcon: null,
},
[EndowmentPermissions['endowment:ethereum-provider']]: {
label: (t) => t('permission_ethereumProvider'),
leftIcon: 'fab fa-ethereum',
rightIcon: null,
},
///: END:ONLY_INCLUDE_IN
[UNKNOWN_PERMISSION]: {
label: (t, permissionName) =>
t('permission_unknown', [permissionName ?? 'undefined']),
leftIcon: 'fas fa-times-circle',
rightIcon: null,
},
});
/**
* @typedef {object} PermissionLabelObject
* @property {string} label - The text label.
* @property {string} leftIcon - The left icon.
* @property {string} rightIcon - The right icon.
*/
/**
* @param {Function} t - The translation function
* @param {string} permissionName - The name of the permission to request
* @param {object} permissionValue - The value of the permission to request
* @returns {(permissionName:string) => PermissionLabelObject}
*/
export const getPermissionDescription = (
t,
permissionName,
permissionValue,
) => {
let value = PERMISSION_DESCRIPTIONS[UNKNOWN_PERMISSION];
if (Object.hasOwnProperty.call(PERMISSION_DESCRIPTIONS, permissionName)) {
value = PERMISSION_DESCRIPTIONS[permissionName];
}
///: BEGIN:ONLY_INCLUDE_IN(flask)
for (const namespace of Object.keys(PermissionNamespaces)) {
if (permissionName.startsWith(namespace)) {
value = PERMISSION_DESCRIPTIONS[PermissionNamespaces[namespace]];
}
}
///: END:ONLY_INCLUDE_IN
return { ...value, label: value.label(t, permissionName, permissionValue) };
};