Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for localised Snap manifests #21909

Merged
merged 69 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
dbdfb88
Add support for localised Snap manifests
Mrtenz Nov 21, 2023
cfb868f
Update LavaMoat policies
metamaskbot Nov 21, 2023
dd1f514
Remove unused import
Mrtenz Nov 21, 2023
cb5bb9a
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Jan 25, 2024
25caea4
remove snaps metadata constant
hmalik88 Jan 25, 2024
0236821
add fence
hmalik88 Jan 25, 2024
c631e4f
update useTransactionInsights with a new selector
hmalik88 Jan 25, 2024
8ada300
fixed fences and regenerated lavamoat policies
hmalik88 Jan 25, 2024
8b00de0
more fixes
hmalik88 Jan 26, 2024
33c0eba
remove prop
hmalik88 Jan 26, 2024
b75b0c3
lint fix
hmalik88 Jan 26, 2024
217b38c
remove unused message
hmalik88 Jan 26, 2024
dad9c75
fix selector
hmalik88 Jan 26, 2024
588b84a
remove unused messages
hmalik88 Jan 26, 2024
285c6a0
fix fence
hmalik88 Jan 26, 2024
1891d3e
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Jan 30, 2024
efa8c11
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Feb 1, 2024
316c85a
revert fence change
hmalik88 Feb 5, 2024
6a0db9d
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Feb 5, 2024
b5b8573
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Feb 6, 2024
045ece9
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Feb 7, 2024
b8d6a4a
remove upstreamed code
hmalik88 Feb 7, 2024
31502d2
add snaps metadata selector
hmalik88 Feb 7, 2024
a3c5c2e
fix selector
hmalik88 Feb 7, 2024
7bf2143
remove unused import
hmalik88 Feb 8, 2024
238da7d
add guard to selector
hmalik88 Feb 15, 2024
7e9c8ce
reorder selectors
hmalik88 Feb 15, 2024
939d1d8
revert changes
hmalik88 Feb 19, 2024
f539710
revert more changes
hmalik88 Feb 19, 2024
d40b561
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Feb 19, 2024
de1e717
revert permission utils
hmalik88 Feb 19, 2024
3e28af6
update
hmalik88 Mar 12, 2024
985804c
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 12, 2024
7b613ed
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 12, 2024
14b121d
lint fix
hmalik88 Mar 13, 2024
32cf174
update with getSnapName func
hmalik88 Mar 13, 2024
e82615d
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 13, 2024
1ee0cca
revert messages
hmalik88 Mar 13, 2024
b033b59
revert lavamoat policies
hmalik88 Mar 13, 2024
6cc9085
fix lint error, regenerate lavamoat policies
hmalik88 Mar 13, 2024
73d32c1
address PR comments
hmalik88 Mar 13, 2024
9d1b806
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 13, 2024
fffaff8
use correct selector
hmalik88 Mar 13, 2024
5a11aed
update snap keyring with new getSnapName func
hmalik88 Mar 14, 2024
f52db29
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 14, 2024
2d70bfb
update policies
hmalik88 Mar 14, 2024
cdcd78c
lint fix
hmalik88 Mar 14, 2024
55b3acb
update snapshot
hmalik88 Mar 14, 2024
07c3d2d
update implementation to accomodate for non-snap origins
hmalik88 Mar 14, 2024
a46262e
update selector usage
hmalik88 Mar 14, 2024
01380e8
applying pr suggestions
hmalik88 Mar 14, 2024
33dd279
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 14, 2024
6154ffc
add missing param
hmalik88 Mar 14, 2024
dbe9557
update mockstore
hmalik88 Mar 14, 2024
4d98e62
fix mock store for error template store
hmalik88 Mar 14, 2024
c387e6e
update mock store for success template
hmalik88 Mar 14, 2024
0a47e49
update snap permission list test with mock store
hmalik88 Mar 14, 2024
2de1e50
some more fixes
hmalik88 Mar 14, 2024
3372577
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 14, 2024
ba2c90d
update getSnapName functions to fallback to snap id
hmalik88 Mar 14, 2024
e24df44
test fixes
hmalik88 Mar 14, 2024
fd0fd07
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 14, 2024
81e7a48
update selector test
hmalik88 Mar 14, 2024
07b8470
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 14, 2024
0003a3e
refactor per PR comments
hmalik88 Mar 15, 2024
4e3eaca
Merge branch 'develop' into mrtenz/manifest-localisation
hmalik88 Mar 15, 2024
f259765
lint fix
hmalik88 Mar 15, 2024
90f06b6
pass subject origin to permission descriptions for future use
hmalik88 Mar 15, 2024
3d69eb5
fix prop usage
hmalik88 Mar 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import {
SnapController,
IframeExecutionService,
} from '@metamask/snaps-controllers';
import { getLocalizedSnapManifest } from '@metamask/snaps-utils';
///: END:ONLY_INCLUDE_IN

///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
Expand Down Expand Up @@ -2230,10 +2231,16 @@ export default class MetamaskController extends EventEmitter {
this.controllerMessenger.subscribe(
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
`${this.snapController.name}:snapAdded`,
(snap, svgIcon = null) => {
const {
manifest: { proposedName },
version,
} = snap;
const { manifest, localizationFiles, version } = snap;

// In case the Snap uses a localized manifest, we need to get the
// proposed name from the localized manifest.
const { proposedName } = getLocalizedSnapManifest(
manifest,
'en',
localizationFiles,
);

this.subjectMetadataController.addSubjectMetadata({
subjectType: SubjectType.Snap,
name: proposedName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import Box from '../../../ui/box/box';
import { SnapUIRenderer } from '../../snaps/snap-ui-renderer';
import { SnapDelineator } from '../../snaps/snap-delineator';
import { DelineatorType } from '../../../../helpers/constants/snaps';
import { getSnapName } from '../../../../helpers/utils/util';
import { Copyable } from '../../snaps/copyable';
import { getTargetSubjectMetadata } from '../../../../selectors';
import { getSnapMetadata } from '../../../../selectors';
///: BEGIN:ONLY_INCLUDE_IN(build-flask)
import { trackInsightSnapUsage } from '../../../../store/actions';
///: END:ONLY_INCLUDE_IN
Expand Down Expand Up @@ -72,12 +71,10 @@ export const SnapInsight = ({
isLoading = insights.loading;
///: END:ONLY_INCLUDE_IN

const targetSubjectMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snapId),
const { name: snapName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const snapName = getSnapName(snapId, targetSubjectMetadata);

const hasNoData = !error && !isLoading && !content;
return (
<Box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,10 @@ import {
TextColor,
TextVariant,
} from '../../../../helpers/constants/design-system';
import {
formatDate,
getSnapName,
removeSnapIdPrefix,
} from '../../../../helpers/utils/util';
import { formatDate, removeSnapIdPrefix } from '../../../../helpers/utils/util';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import { useOriginMetadata } from '../../../../hooks/useOriginMetadata';
import {
getSnapRegistryData,
getTargetSubjectMetadata,
} from '../../../../selectors';
import { getSnapMetadata, getSnapRegistryData } from '../../../../selectors';
import {
disableSnap,
enableSnap,
Expand Down Expand Up @@ -58,12 +51,14 @@ const SnapAuthorshipExpanded = ({ snapId, className, snap }) => {
? `https://www.npmjs.com/package/${packageName}${versionPath}`
: packageName;

const subjectMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snapId),
);
const snapRegistryData = useSelector((state) =>
getSnapRegistryData(state, snapId),
);

const { name: friendlyName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const { website = undefined } = snapRegistryData?.metadata ?? {};
const [safeWebsite, setSafeWebsite] = useState(null);

Expand All @@ -75,13 +70,12 @@ const SnapAuthorshipExpanded = ({ snapId, className, snap }) => {
setSafeWebsite(website);
}
};

if (website) {
performPhishingCheck();
}
}, [website]);

const friendlyName = snapId && getSnapName(snapId, subjectMetadata);

const versionHistory = snap?.versionHistory ?? [];
const installInfo = versionHistory.length
? versionHistory[versionHistory.length - 1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {
BlockSize,
FontWeight,
} from '../../../../helpers/constants/design-system';
import { removeSnapIdPrefix } from '../../../../helpers/utils/util';
import {
getSnapName,
removeSnapIdPrefix,
} from '../../../../helpers/utils/util';
getSnapMetadata,
getTargetSubjectMetadata,
} from '../../../../selectors';

import { Text, Box } from '../../../component-library';
import { getTargetSubjectMetadata } from '../../../../selectors';
import SnapAvatar from '../snap-avatar';
import SnapVersion from '../snap-version/snap-version';

Expand All @@ -40,15 +40,18 @@ const SnapAuthorshipHeader = ({
getTargetSubjectMetadata(state, snapId),
);

const { name: snapName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const versionPath = subjectMetadata?.version
? `/v/${subjectMetadata?.version}`
: '';

const url = isNPM
? `https://www.npmjs.com/package/${packageName}${versionPath}`
: packageName;

const friendlyName = snapId && getSnapName(snapId, subjectMetadata);

return (
<Box
className={classnames('snaps-authorship-header', className)}
Expand All @@ -72,7 +75,7 @@ const SnapAuthorshipHeader = ({
style={{ overflow: 'hidden' }}
>
<Text ellipsis fontWeight={FontWeight.Medium}>
{friendlyName}
{snapName}
</Text>
<Text
ellipsis
Expand Down
10 changes: 7 additions & 3 deletions ui/components/app/snaps/snap-avatar/snap-avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
JustifyContent,
BackgroundColor,
} from '../../../../helpers/constants/design-system';
import { getSnapName } from '../../../../helpers/utils/util';
import {
AvatarFavicon,
BadgeWrapper,
Expand All @@ -20,7 +19,10 @@ import {
IconName,
IconSize,
} from '../../../component-library';
import { getTargetSubjectMetadata } from '../../../../selectors';
import {
getSnapMetadata,
getTargetSubjectMetadata,
} from '../../../../selectors';

const SnapAvatar = ({
snapId,
Expand All @@ -33,7 +35,9 @@ const SnapAvatar = ({
getTargetSubjectMetadata(state, snapId),
);

const friendlyName = snapId && getSnapName(snapId, subjectMetadata);
const { name: friendlyName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const iconUrl = subjectMetadata?.iconUrl;

Expand Down
12 changes: 5 additions & 7 deletions ui/components/app/snaps/snap-connect-cell/snap-connect-cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Display,
FontWeight,
} from '../../../../helpers/constants/design-system';
import { getSnapName } from '../../../../helpers/utils/util';
import {
Icon,
IconName,
Expand All @@ -19,14 +18,13 @@ import {
import Tooltip from '../../../ui/tooltip/tooltip';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import SnapAvatar from '../snap-avatar/snap-avatar';
import { getTargetSubjectMetadata } from '../../../../selectors';
import { getSnapManifest } from '../../../../selectors';

export default function SnapConnectCell({ origin, snapId }) {
const t = useI18nContext();
const snapMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snapId),
const { name: snapName } = useSelector((state) =>
getSnapManifest(state, snapId),
);
const friendlyName = getSnapName(snapId, snapMetadata);

return (
<Box
Expand All @@ -40,7 +38,7 @@ export default function SnapConnectCell({ origin, snapId }) {
<Text>
{t('connectSnap', [
<Text as={ValidTag.Span} key="1" fontWeight={FontWeight.Bold}>
{friendlyName}
{snapName}
</Text>,
])}
</Text>
Expand All @@ -51,7 +49,7 @@ export default function SnapConnectCell({ origin, snapId }) {
<div>
{t('snapConnectionWarning', [
<b key="0">{origin}</b>,
<b key="1">{friendlyName}</b>,
<b key="1">{snapName}</b>,
])}
</div>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { Box, Text } from '../../../component-library';
import { SnapUIRenderer } from '../snap-ui-renderer';
import { getTargetSubjectMetadata } from '../../../../selectors';
import { getSnapName } from '../../../../helpers/utils/util';
import { getSnapMetadata } from '../../../../selectors';
import { SnapDelineator } from '../snap-delineator';
import { DelineatorType } from '../../../../helpers/constants/snaps';
import { TextVariant } from '../../../../helpers/constants/design-system';
Expand All @@ -14,11 +13,10 @@ import { useSnapHome } from './useSnapHome';

export const SnapHomeRenderer = ({ snapId }) => {
const t = useI18nContext();
const targetSubjectMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snapId),
const { name: snapName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const snapName = getSnapName(snapId, targetSubjectMetadata);
const { data, error, loading } = useSnapHome({ snapId });

const content = !loading && !error && data?.content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ import {
BorderColor,
BorderRadius,
} from '../../../../helpers/constants/design-system';
import {
getSnapName,
removeSnapIdPrefix,
} from '../../../../helpers/utils/util';
import { removeSnapIdPrefix } from '../../../../helpers/utils/util';

import { Box, Text } from '../../../component-library';
import { getTargetSubjectMetadata } from '../../../../selectors';
import { getSnapMetadata } from '../../../../selectors';
import SnapAvatar from '../snap-avatar';

const SnapLegacyAuthorshipHeader = ({
Expand All @@ -30,13 +27,10 @@ const SnapLegacyAuthorshipHeader = ({
marginRight,
}) => {
const packageName = snapId && removeSnapIdPrefix(snapId);

const subjectMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snapId),
const { name: friendlyName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const friendlyName = snapId && getSnapName(snapId, subjectMetadata);

return (
<Box
className={classnames('snap-legacy-authorship-header', className)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { getWeightedPermissions } from '../../../../helpers/utils/permission';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import PermissionCell from '../../permission-cell';
import Box from '../../../ui/box';
import { getSnapMetadata } from '../../../../selectors';

export default function SnapPermissionsList({
snapId,
snapName,
permissions,
targetSubjectMetadata,
showOptions,
}) {
const t = useI18nContext();
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
const state = useSelector((value) => value);

/**
* Get the snap name from the snap ID.
*
* This is used to get the names for permissions which include snap IDs as
* caveat.
*
* @param id - The snap ID.
* @returns {string | undefined} The snap name if it exists, or `undefined`.
*/
const getSnapName = (id) => {
const snap = getSnapMetadata(state, id);
return snap?.name;
};

return (
<Box paddingTop={2} paddingBottom={2} className="snap-permissions-list">
{getWeightedPermissions(t, permissions, targetSubjectMetadata).map(
(permission, index) => {
return (
<PermissionCell
snapId={snapId}
permissionName={permission.permissionName}
title={permission.label}
description={permission.description}
weight={permission.weight}
avatarIcon={permission.leftIcon}
dateApproved={permission?.permissionValue?.date}
key={`${permission.permissionName}-${index}`}
showOptions={showOptions}
/>
);
},
)}
{getWeightedPermissions(
t,
permissions,
targetSubjectMetadata,
snapName,
getSnapName,
).map((permission, index) => {
return (
<PermissionCell
snapId={snapId}
snapName={snapName}
permissionName={permission.permissionName}
title={permission.label}
description={permission.description}
weight={permission.weight}
avatarIcon={permission.leftIcon}
dateApproved={permission?.permissionValue?.date}
key={`${permission.permissionName}-${index}`}
showOptions={showOptions}
/>
);
})}
</Box>
);
}

SnapPermissionsList.propTypes = {
snapId: PropTypes.string.isRequired,
snapName: PropTypes.string.isRequired,
permissions: PropTypes.object.isRequired,
targetSubjectMetadata: PropTypes.object.isRequired,
showOptions: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
import { SnapDelineator } from '../snap-delineator';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import Box from '../../../ui/box';
import { getSnapName } from '../../../../helpers/utils/util';
import { getTargetSubjectMetadata } from '../../../../selectors';
import { getSnapMetadata } from '../../../../selectors';
import { Text } from '../../../component-library';
import { Copyable } from '../copyable';
import { DelineatorType } from '../../../../helpers/constants/snaps';
Expand Down Expand Up @@ -100,12 +99,10 @@ export const SnapUIRenderer = ({
boxProps,
}) => {
const t = useI18nContext();
const targetSubjectMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snapId),
const { name: snapName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const snapName = getSnapName(snapId, targetSubjectMetadata);

if (isLoading) {
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
return (
<SnapDelineator
Expand Down
Loading
Loading