2022-03-14 19:26:02 +01:00
|
|
|
import deepFreeze from 'deep-freeze-strict';
|
2022-08-18 17:07:34 +02:00
|
|
|
import React from 'react';
|
2023-02-28 08:25:58 +01:00
|
|
|
|
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
2022-11-30 13:19:33 +01:00
|
|
|
import { getRpcCaveatOrigins } from '@metamask/snaps-controllers/dist/snaps/endowments/rpc';
|
2022-12-01 14:38:56 +01:00
|
|
|
import { SnapCaveatType } from '@metamask/snaps-utils';
|
|
|
|
import { isNonEmptyArray } from '@metamask/controller-utils';
|
2022-08-18 17:07:34 +02:00
|
|
|
///: END:ONLY_INCLUDE_IN
|
2023-03-20 12:32:03 +01:00
|
|
|
import classnames from 'classnames';
|
2022-03-14 19:26:02 +01:00
|
|
|
import {
|
|
|
|
RestrictedMethods,
|
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
|
|
|
EndowmentPermissions,
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
} from '../../../shared/constants/permissions';
|
2023-03-20 12:32:03 +01:00
|
|
|
import Tooltip from '../../components/ui/tooltip';
|
|
|
|
import {
|
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
2023-03-29 21:17:57 +02:00
|
|
|
Text,
|
2023-03-20 12:32:03 +01:00
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
} from '../../components/component-library';
|
2023-04-04 18:48:04 +02:00
|
|
|
import { ICON_NAMES } from '../../components/component-library/icon/deprecated';
|
2023-03-17 12:00:05 +01:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
2023-04-03 19:33:54 +02:00
|
|
|
import { Color, FONT_WEIGHT, TextVariant } from '../constants/design-system';
|
2023-03-17 12:00:05 +01:00
|
|
|
import {
|
|
|
|
coinTypeToProtocolName,
|
|
|
|
getSnapDerivationPathName,
|
|
|
|
getSnapName,
|
|
|
|
} from './util';
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
2022-03-14 19:26:02 +01:00
|
|
|
|
|
|
|
const UNKNOWN_PERMISSION = Symbol('unknown');
|
|
|
|
|
2023-03-29 21:17:57 +02:00
|
|
|
export const PERMISSION_DESCRIPTIONS = deepFreeze({
|
|
|
|
[RestrictedMethods.eth_accounts]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_ethereumAccounts'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.EYE,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 2,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2022-03-14 19:26:02 +01:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_confirm]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_customConfirmation'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_customConfirmationDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.SECURITY_TICK,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 3,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_dialog]: ({ t }) => ({
|
2022-12-01 16:46:06 +01:00
|
|
|
label: t('permission_dialog'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_dialogDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.MESSAGES,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 3,
|
2022-12-01 16:46:06 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_notify]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_notifications'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_notificationsDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.NOTIFICATION,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 3,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_getBip32PublicKey]: ({
|
|
|
|
t,
|
|
|
|
permissionValue,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}) =>
|
|
|
|
permissionValue.caveats[0].value.map(({ path, curve }, i) => {
|
2023-02-08 15:28:48 +01:00
|
|
|
const baseDescription = {
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.SECURITY_SEARCH,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 1,
|
2023-03-29 21:17:57 +02:00
|
|
|
id: `public-key-access-bip32-${path
|
|
|
|
.join('-')
|
|
|
|
?.replace(/'/gu, 'h')}-${curve}-${i}`,
|
|
|
|
message: t('snapInstallWarningPublicKeyAccess', [
|
|
|
|
<Text
|
|
|
|
key="1"
|
|
|
|
color={Color.primaryDefault}
|
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="span"
|
|
|
|
>
|
|
|
|
{getSnapName(targetSubjectMetadata?.origin)}
|
|
|
|
</Text>,
|
|
|
|
<b key="2">
|
|
|
|
{getSnapDerivationPathName(path, curve) ??
|
|
|
|
`${path.join('/')} (${curve})`}
|
|
|
|
</b>,
|
|
|
|
]),
|
2023-02-08 15:28:48 +01:00
|
|
|
};
|
|
|
|
|
2023-02-06 21:49:56 +01:00
|
|
|
const friendlyName = getSnapDerivationPathName(path, curve);
|
|
|
|
if (friendlyName) {
|
|
|
|
return {
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2023-02-06 21:49:56 +01:00
|
|
|
label: t('permission_viewNamedBip32PublicKeys', [
|
|
|
|
<span className="permission-label-item" key={path.join('/')}>
|
|
|
|
{friendlyName}
|
|
|
|
</span>,
|
|
|
|
path.join('/'),
|
|
|
|
]),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_viewBip32PublicKeysDescription', [
|
|
|
|
<span
|
|
|
|
className="tooltip-label-item"
|
|
|
|
key={`description-${path.join('/')}`}
|
|
|
|
>
|
|
|
|
{friendlyName}
|
|
|
|
</span>,
|
|
|
|
path.join('/'),
|
|
|
|
]),
|
2023-02-06 21:49:56 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2023-02-06 21:49:56 +01:00
|
|
|
label: t('permission_viewBip32PublicKeys', [
|
|
|
|
<span className="permission-label-item" key={path.join('/')}>
|
|
|
|
{path.join('/')}
|
|
|
|
</span>,
|
|
|
|
curve,
|
|
|
|
]),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_viewBip32PublicKeysDescription', [
|
|
|
|
<span
|
|
|
|
className="tooltip-label-item"
|
|
|
|
key={`description-${path.join('/')}`}
|
|
|
|
>
|
|
|
|
{path.join('/')}
|
|
|
|
</span>,
|
|
|
|
path.join('/'),
|
|
|
|
]),
|
2023-02-06 21:49:56 +01:00
|
|
|
};
|
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_getBip32Entropy]: ({
|
|
|
|
t,
|
|
|
|
permissionValue,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}) =>
|
|
|
|
permissionValue.caveats[0].value.map(({ path, curve }, i) => {
|
2023-02-08 15:28:48 +01:00
|
|
|
const baseDescription = {
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.KEY,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 1,
|
2023-03-29 21:17:57 +02:00
|
|
|
id: `key-access-bip32-${path
|
|
|
|
.join('-')
|
|
|
|
?.replace(/'/gu, 'h')}-${curve}-${i}`,
|
|
|
|
message: t('snapInstallWarningKeyAccess', [
|
|
|
|
<Text
|
|
|
|
key="1"
|
|
|
|
color={Color.primaryDefault}
|
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="span"
|
|
|
|
>
|
|
|
|
{getSnapName(targetSubjectMetadata?.origin)}
|
|
|
|
</Text>,
|
|
|
|
<b key="2">
|
|
|
|
{getSnapDerivationPathName(path, curve) ??
|
|
|
|
`${path.join('/')} (${curve})`}
|
|
|
|
</b>,
|
|
|
|
]),
|
2023-02-08 15:28:48 +01:00
|
|
|
};
|
|
|
|
|
2023-02-06 21:49:56 +01:00
|
|
|
const friendlyName = getSnapDerivationPathName(path, curve);
|
|
|
|
if (friendlyName) {
|
|
|
|
return {
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2023-02-06 21:49:56 +01:00
|
|
|
label: t('permission_manageNamedBip32Keys', [
|
|
|
|
<span className="permission-label-item" key={path.join('/')}>
|
|
|
|
{friendlyName}
|
|
|
|
</span>,
|
|
|
|
path.join('/'),
|
|
|
|
]),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_manageBip32KeysDescription', [
|
|
|
|
<span
|
|
|
|
className="tooltip-label-item"
|
|
|
|
key={`description-${path.join('/')}`}
|
|
|
|
>
|
|
|
|
{friendlyName}
|
|
|
|
</span>,
|
|
|
|
curve,
|
|
|
|
]),
|
2023-02-06 21:49:56 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2023-02-06 21:49:56 +01:00
|
|
|
label: t('permission_manageBip32Keys', [
|
|
|
|
<span className="permission-label-item" key={path.join('/')}>
|
|
|
|
{path.join('/')}
|
|
|
|
</span>,
|
|
|
|
curve,
|
|
|
|
]),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_manageBip32KeysDescription', [
|
|
|
|
<span
|
|
|
|
className="tooltip-label-item"
|
|
|
|
key={`description-${path.join('/')}`}
|
|
|
|
>
|
|
|
|
{path.join('/')}
|
|
|
|
</span>,
|
|
|
|
curve,
|
|
|
|
]),
|
2023-02-06 21:49:56 +01:00
|
|
|
};
|
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_getBip44Entropy]: ({
|
|
|
|
t,
|
|
|
|
permissionValue,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}) =>
|
|
|
|
permissionValue.caveats[0].value.map(({ coinType }, i) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_manageBip44Keys', [
|
|
|
|
<span className="permission-label-item" key={`coin-type-${coinType}`}>
|
|
|
|
{coinTypeToProtocolName(coinType) ||
|
2023-03-20 12:32:03 +01:00
|
|
|
t('unrecognizedProtocol', [coinType])}
|
|
|
|
</span>,
|
|
|
|
]),
|
|
|
|
description: t('permission_manageBip44KeysDescription', [
|
|
|
|
<span
|
|
|
|
className="tooltip-label-item"
|
|
|
|
key={`description-coin-type-${coinType}`}
|
|
|
|
>
|
|
|
|
{coinTypeToProtocolName(coinType) ||
|
|
|
|
t('unrecognizedProtocol', [coinType])}
|
2022-12-01 14:38:56 +01:00
|
|
|
</span>,
|
|
|
|
]),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.KEY,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 1,
|
2023-03-29 21:17:57 +02:00
|
|
|
id: `key-access-bip44-${coinType}-${i}`,
|
|
|
|
message: t('snapInstallWarningKeyAccess', [
|
|
|
|
<Text
|
|
|
|
key="1"
|
|
|
|
color={Color.primaryDefault}
|
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="span"
|
|
|
|
>
|
|
|
|
{getSnapName(targetSubjectMetadata?.origin)}
|
|
|
|
</Text>,
|
|
|
|
<b key="2">
|
|
|
|
{coinTypeToProtocolName(coinType) ||
|
|
|
|
t('unrecognizedProtocol', [coinType])}
|
|
|
|
</b>,
|
|
|
|
]),
|
2022-12-01 14:38:56 +01:00
|
|
|
})),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_getEntropy]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_getEntropy'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_getEntropyDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.SECURITY_KEY,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 3,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.snap_manageState]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_manageState'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_manageStateDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.ADD_SQUARE,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 3,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[RestrictedMethods.wallet_snap]: ({ t, permissionValue }) => {
|
2023-03-08 19:29:23 +01:00
|
|
|
const snaps = permissionValue.caveats[0].value;
|
2023-02-08 15:28:48 +01:00
|
|
|
const baseDescription = {
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.FLASH,
|
2023-02-08 15:28:48 +01:00
|
|
|
};
|
2023-03-20 12:32:03 +01:00
|
|
|
|
2023-03-08 19:29:23 +01:00
|
|
|
return Object.keys(snaps).map((snapId) => {
|
2023-03-17 12:00:05 +01:00
|
|
|
const friendlyName = getSnapName(snapId);
|
2023-03-08 19:29:23 +01:00
|
|
|
if (friendlyName) {
|
|
|
|
return {
|
|
|
|
...baseDescription,
|
|
|
|
label: t('permission_accessNamedSnap', [
|
|
|
|
<span className="permission-label-item" key={snapId}>
|
|
|
|
{friendlyName}
|
|
|
|
</span>,
|
|
|
|
]),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_accessSnapDescription', [friendlyName]),
|
2023-03-08 19:29:23 +01:00
|
|
|
};
|
|
|
|
}
|
2023-03-20 12:32:03 +01:00
|
|
|
|
2023-02-06 11:05:57 +01:00
|
|
|
return {
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2023-03-08 19:29:23 +01:00
|
|
|
label: t('permission_accessSnap', [snapId]),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_accessSnapDescription', [snapId]),
|
2023-02-06 11:05:57 +01:00
|
|
|
};
|
2023-03-08 19:29:23 +01:00
|
|
|
});
|
2023-02-06 11:05:57 +01:00
|
|
|
},
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:network-access']]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_accessNetwork'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_accessNetworkDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.GLOBAL,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 2,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:webassembly']]: ({ t }) => ({
|
2023-03-10 12:38:58 +01:00
|
|
|
label: t('permission_webAssembly'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_webAssemblyDescription'),
|
2023-03-10 12:38:58 +01:00
|
|
|
leftIcon: 'fas fa-microchip',
|
|
|
|
rightIcon: null,
|
|
|
|
weight: 2,
|
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:long-running']]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_longRunning'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_longRunningDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.LINK,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 3,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:transaction-insight']]: ({
|
2022-12-01 14:38:56 +01:00
|
|
|
t,
|
|
|
|
permissionValue,
|
2023-03-29 21:17:57 +02:00
|
|
|
}) => {
|
2023-02-08 15:28:48 +01:00
|
|
|
const baseDescription = {
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.SPEEDOMETER,
|
2023-03-20 12:32:03 +01:00
|
|
|
weight: 3,
|
2023-02-08 15:28:48 +01:00
|
|
|
};
|
|
|
|
|
2022-12-01 14:38:56 +01:00
|
|
|
const result = [
|
|
|
|
{
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_transactionInsight'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_transactionInsightDescription'),
|
2022-12-01 14:38:56 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (
|
|
|
|
isNonEmptyArray(permissionValue.caveats) &&
|
|
|
|
permissionValue.caveats[0].type === SnapCaveatType.TransactionOrigin &&
|
|
|
|
permissionValue.caveats[0].value
|
|
|
|
) {
|
|
|
|
result.push({
|
2023-02-08 15:28:48 +01:00
|
|
|
...baseDescription,
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_transactionInsightOrigin'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_transactionInsightOriginDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.EXPLORE,
|
2022-12-01 14:38:56 +01:00
|
|
|
});
|
|
|
|
}
|
2023-02-08 15:28:48 +01:00
|
|
|
|
2022-12-01 14:38:56 +01:00
|
|
|
return result;
|
2022-05-18 13:49:26 +02:00
|
|
|
},
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:cronjob']]: ({ t }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_cronjob'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_cronjobDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.CLOCK,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 2,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:ethereum-provider']]: ({
|
|
|
|
t,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_ethereumProvider'),
|
2023-03-20 12:32:03 +01:00
|
|
|
description: t('permission_ethereumProviderDescription'),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.ETHEREUM,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 1,
|
2023-03-29 21:17:57 +02:00
|
|
|
id: 'ethereum-provider-access',
|
|
|
|
message: t('ethereumProviderAccess', [targetSubjectMetadata?.origin]),
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2023-03-29 21:17:57 +02:00
|
|
|
[EndowmentPermissions['endowment:rpc']]: ({ t, permissionValue }) => {
|
2023-03-20 12:32:03 +01:00
|
|
|
const baseDescription = {
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.HIERARCHY,
|
2023-03-20 12:32:03 +01:00
|
|
|
weight: 2,
|
|
|
|
};
|
|
|
|
|
2022-12-01 14:38:56 +01:00
|
|
|
const { snaps, dapps } = getRpcCaveatOrigins(permissionValue);
|
2022-11-30 13:19:33 +01:00
|
|
|
|
2023-03-20 12:32:03 +01:00
|
|
|
const results = [];
|
2022-12-01 14:38:56 +01:00
|
|
|
if (snaps) {
|
2023-03-20 12:32:03 +01:00
|
|
|
results.push({
|
|
|
|
...baseDescription,
|
|
|
|
label: t('permission_rpc', [t('otherSnaps')]),
|
|
|
|
description: t('permission_rpcDescription', [t('otherSnaps')]),
|
|
|
|
});
|
2022-12-01 14:38:56 +01:00
|
|
|
}
|
2022-11-30 13:19:33 +01:00
|
|
|
|
2022-12-01 14:38:56 +01:00
|
|
|
if (dapps) {
|
2023-03-20 12:32:03 +01:00
|
|
|
results.push({
|
|
|
|
...baseDescription,
|
|
|
|
label: t('permission_rpc', [t('websites')]),
|
|
|
|
description: t('permission_rpcDescription', [t('websites')]),
|
|
|
|
});
|
2022-12-01 14:38:56 +01:00
|
|
|
}
|
2022-11-30 13:19:33 +01:00
|
|
|
|
2023-03-20 12:32:03 +01:00
|
|
|
return results;
|
2022-11-30 13:19:33 +01:00
|
|
|
},
|
2022-03-14 19:26:02 +01:00
|
|
|
///: END:ONLY_INCLUDE_IN
|
2023-03-29 21:17:57 +02:00
|
|
|
[UNKNOWN_PERMISSION]: ({ t, permissionName }) => ({
|
2022-12-01 14:38:56 +01:00
|
|
|
label: t('permission_unknown', [permissionName ?? 'undefined']),
|
2023-04-03 19:33:54 +02:00
|
|
|
leftIcon: ICON_NAMES.QUESTION,
|
2023-02-08 15:28:48 +01:00
|
|
|
weight: 4,
|
2022-12-01 14:38:56 +01:00
|
|
|
}),
|
2022-03-14 19:26:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} PermissionLabelObject
|
2022-03-14 19:26:02 +01:00
|
|
|
* @property {string} label - The text label.
|
2023-03-20 12:32:03 +01:00
|
|
|
* @property {string} [description] - An optional description, shown when the
|
|
|
|
* `rightIcon` is hovered.
|
2022-03-14 19:26:02 +01:00
|
|
|
* @property {string} leftIcon - The left icon.
|
|
|
|
* @property {string} rightIcon - The right icon.
|
2023-02-08 15:28:48 +01:00
|
|
|
* @property {number} weight - The weight of the permission.
|
|
|
|
* @property {string} permissionName - The name of the permission.
|
|
|
|
* @property {string} permissionValue - The raw value of the permission.
|
2022-03-14 19:26:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-03-29 21:17:57 +02:00
|
|
|
* @typedef {object} PermissionDescriptionParamsObject
|
|
|
|
* @property {Function} t - The translation function.
|
|
|
|
* @property {string} permissionName - The name of the permission.
|
|
|
|
* @property {object} permissionValue - The permission object.
|
|
|
|
* @property {object} targetSubjectMetadata - Subject metadata.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PermissionDescriptionParamsObject} params - The permission description params object.
|
|
|
|
* @param {Function} params.t - The translation function.
|
|
|
|
* @param {string} params.permissionName - The name of the permission to request
|
|
|
|
* @param {object} params.permissionValue - The value of the permission to request
|
2022-12-01 14:38:56 +01:00
|
|
|
* @returns {PermissionLabelObject[]}
|
2022-03-14 19:26:02 +01:00
|
|
|
*/
|
2023-03-29 21:17:57 +02:00
|
|
|
export const getPermissionDescription = ({
|
2022-08-18 17:07:34 +02:00
|
|
|
t,
|
|
|
|
permissionName,
|
|
|
|
permissionValue,
|
2023-04-05 15:34:33 +02:00
|
|
|
targetSubjectMetadata,
|
2023-03-29 21:17:57 +02:00
|
|
|
}) => {
|
2022-03-14 19:26:02 +01:00
|
|
|
let value = PERMISSION_DESCRIPTIONS[UNKNOWN_PERMISSION];
|
|
|
|
|
|
|
|
if (Object.hasOwnProperty.call(PERMISSION_DESCRIPTIONS, permissionName)) {
|
|
|
|
value = PERMISSION_DESCRIPTIONS[permissionName];
|
|
|
|
}
|
|
|
|
|
2023-04-05 15:34:33 +02:00
|
|
|
const result = value({
|
|
|
|
t,
|
|
|
|
permissionName,
|
|
|
|
permissionValue,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
});
|
2022-12-01 14:38:56 +01:00
|
|
|
if (!Array.isArray(result)) {
|
2023-02-08 15:28:48 +01:00
|
|
|
return [{ ...result, permissionName, permissionValue }];
|
2022-12-01 14:38:56 +01:00
|
|
|
}
|
2023-02-08 15:28:48 +01:00
|
|
|
|
|
|
|
return result.map((item) => ({
|
|
|
|
...item,
|
|
|
|
permissionName,
|
|
|
|
permissionValue,
|
|
|
|
}));
|
2022-03-14 19:26:02 +01:00
|
|
|
};
|
2023-02-08 15:28:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the weighted permissions from a permissions object. The weight is used to
|
|
|
|
* sort the permissions in the UI.
|
|
|
|
*
|
|
|
|
* @param {Function} t - The translation function
|
|
|
|
* @param {object} permissions - The permissions object.
|
2023-04-05 15:34:33 +02:00
|
|
|
* @param {object} targetSubjectMetadata - The subject metadata.
|
2023-02-08 15:28:48 +01:00
|
|
|
* @returns {PermissionLabelObject[]}
|
|
|
|
*/
|
2023-04-05 15:34:33 +02:00
|
|
|
export function getWeightedPermissions(t, permissions, targetSubjectMetadata) {
|
2023-02-08 15:28:48 +01:00
|
|
|
return Object.entries(permissions)
|
|
|
|
.reduce(
|
|
|
|
(target, [permissionName, permissionValue]) =>
|
|
|
|
target.concat(
|
2023-04-05 15:34:33 +02:00
|
|
|
getPermissionDescription({
|
|
|
|
t,
|
|
|
|
permissionName,
|
|
|
|
permissionValue,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}),
|
2023-02-08 15:28:48 +01:00
|
|
|
),
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
.sort((left, right) => left.weight - right.weight);
|
|
|
|
}
|
2023-03-20 12:32:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the right icon for a permission. If a description is provided, the icon
|
|
|
|
* will be wrapped in a tooltip. Otherwise, the icon will be rendered as-is. If
|
|
|
|
* there's no right icon, this function will return null.
|
|
|
|
*
|
|
|
|
* If the weight is 1, the icon will be rendered with a warning color.
|
|
|
|
*
|
|
|
|
* @param {PermissionLabelObject} permission - The permission object.
|
|
|
|
* @param {JSX.Element | string} permission.rightIcon - The right icon.
|
|
|
|
* @param {string} permission.description - The description.
|
|
|
|
* @param {number} permission.weight - The weight.
|
|
|
|
* @returns {JSX.Element | null} The right icon, or null if there's no
|
|
|
|
* right icon.
|
|
|
|
*/
|
|
|
|
export function getRightIcon({ rightIcon, description, weight }) {
|
|
|
|
if (rightIcon && description) {
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
wrapperClassName={classnames(
|
|
|
|
'permission__tooltip-icon',
|
|
|
|
weight === 1 && 'permission__tooltip-icon__warning',
|
|
|
|
)}
|
|
|
|
html={<div>{description}</div>}
|
|
|
|
position="bottom"
|
|
|
|
>
|
|
|
|
{typeof rightIcon === 'string' ? (
|
|
|
|
<i className={rightIcon} />
|
|
|
|
) : (
|
|
|
|
rightIcon
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rightIcon) {
|
|
|
|
if (typeof rightIcon === 'string') {
|
|
|
|
return (
|
|
|
|
<i className={classnames(rightIcon, 'permission__tooltip-icon')} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rightIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|