1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/ui/components/app/permission-page-container/permission-page-container.component.js
Olaf Tomalka 95c37e1ba3
feat: add yaml feature management (#18125)
* feat: add yaml feature management

Add yaml feature file per build type.
Also add method to parse yaml and set
enabled features env to true. The build
process will then replace any process.env[feature]
that exists on the config by its value

* chore: add example for desktop

* Added initial draft of build features

* [TMP] Sync between computers

* Is able to succesfully build stable extension with snaps feature

* Removing var context from builds.yml

* Add asssets to builds.yml

* Minor bug fixes and removing debug logs

* [WIP] Test changes

* Removed TODOs

* Fix regession bug

Also
* remove debug logs
* merge Variables.set and Variables.setMany with an overload

* Fix build, lint and a bunch of issues

* Update LavaMoat policies

* Re-add desktop build type

* Fix some tests

* Fix desktop build

* Define some env variables used by MV3

* Fix lint

* Fix remove-fenced-code tests

* Fix README typo

* Move new code

* Fix missing asset copy

* Move Jest env setup

* Fix path for test after rebase

* Fix code fences

* Fix fencing and LavaMoat policies

* Fix MMI code-fencing after rebase

* Fix MMI code fencing after merge

* Fix more MMI code fencing

---------

Co-authored-by: cryptotavares <joao.tavares@consensys.net>
Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
2023-04-25 16:32:51 +02:00

194 lines
5.5 KiB
JavaScript

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { isEqual } from 'lodash';
///: BEGIN:ONLY_INCLUDE_IN(snaps)
import { isObject } from '@metamask/utils';
import {
SnapCaveatType,
WALLET_SNAP_PERMISSION_KEY,
} from '@metamask/rpc-methods';
///: END:ONLY_INCLUDE_IN
import { MetaMetricsEventCategory } from '../../../../shared/constants/metametrics';
import { PageContainerFooter } from '../../ui/page-container';
import PermissionsConnectFooter from '../permissions-connect-footer';
///: BEGIN:ONLY_INCLUDE_IN(snaps)
import { RestrictedMethods } from '../../../../shared/constants/permissions';
///: END:ONLY_INCLUDE_IN
import { PermissionPageContainerContent } from '.';
export default class PermissionPageContainer extends Component {
static propTypes = {
approvePermissionsRequest: PropTypes.func.isRequired,
rejectPermissionsRequest: PropTypes.func.isRequired,
selectedIdentities: PropTypes.array,
allIdentitiesSelected: PropTypes.bool,
///: BEGIN:ONLY_INCLUDE_IN(snaps)
currentPermissions: PropTypes.object,
///: END:ONLY_INCLUDE_IN
request: PropTypes.object,
requestMetadata: PropTypes.object,
targetSubjectMetadata: PropTypes.shape({
name: PropTypes.string,
origin: PropTypes.string.isRequired,
subjectType: PropTypes.string.isRequired,
extensionId: PropTypes.string,
iconUrl: PropTypes.string,
}),
};
static defaultProps = {
request: {},
requestMetadata: {},
selectedIdentities: [],
allIdentitiesSelected: false,
///: BEGIN:ONLY_INCLUDE_IN(snaps)
currentPermissions: {},
///: END:ONLY_INCLUDE_IN
};
static contextTypes = {
t: PropTypes.func,
trackEvent: PropTypes.func,
};
state = {
selectedPermissions: this.getRequestedMethodState(
this.getRequestedMethodNames(this.props),
),
};
componentDidUpdate() {
const newMethodNames = this.getRequestedMethodNames(this.props);
if (!isEqual(Object.keys(this.state.selectedPermissions), newMethodNames)) {
// this should be a new request, so just overwrite
this.setState({
selectedPermissions: this.getRequestedMethodState(newMethodNames),
});
}
}
getRequestedMethodState(methodNames) {
return methodNames.reduce((acc, methodName) => {
///: BEGIN:ONLY_INCLUDE_IN(snaps)
if (methodName === RestrictedMethods.wallet_snap) {
acc[methodName] = this.getDedupedSnapPermissions();
return acc;
}
///: END:ONLY_INCLUDE_IN
acc[methodName] = true;
return acc;
}, {});
}
///: BEGIN:ONLY_INCLUDE_IN(snaps)
getDedupedSnapPermissions() {
const permission =
this.props.request.permissions[WALLET_SNAP_PERMISSION_KEY];
const requestedSnaps = permission?.caveats[0].value;
const currentSnaps =
this.props.currentPermissions[WALLET_SNAP_PERMISSION_KEY]?.caveats[0]
.value;
if (!isObject(currentSnaps)) {
return permission;
}
const requestedSnapKeys = requestedSnaps ? Object.keys(requestedSnaps) : [];
const currentSnapKeys = currentSnaps ? Object.keys(currentSnaps) : [];
const dedupedCaveats = requestedSnapKeys.reduce((acc, snapId) => {
if (!currentSnapKeys.includes(snapId)) {
acc[snapId] = {};
}
return acc;
}, {});
return {
...permission,
caveats: [{ type: SnapCaveatType.SnapIds, value: dedupedCaveats }],
};
}
///: END:ONLY_INCLUDE_IN
getRequestedMethodNames(props) {
return Object.keys(props.request.permissions || {});
}
componentDidMount() {
this.context.trackEvent({
category: MetaMetricsEventCategory.Auth,
event: 'Tab Opened',
properties: {
action: 'Connect',
legacy_event: true,
},
});
}
onCancel = () => {
const { request, rejectPermissionsRequest } = this.props;
rejectPermissionsRequest(request.metadata.id);
};
onSubmit = () => {
const {
request: _request,
approvePermissionsRequest,
rejectPermissionsRequest,
selectedIdentities,
} = this.props;
const request = {
..._request,
permissions: { ..._request.permissions },
approvedAccounts: selectedIdentities.map(
(selectedIdentity) => selectedIdentity.address,
),
};
Object.keys(this.state.selectedPermissions).forEach((key) => {
if (!this.state.selectedPermissions[key]) {
delete request.permissions[key];
}
});
if (Object.keys(request.permissions).length > 0) {
approvePermissionsRequest(request);
} else {
rejectPermissionsRequest(request.metadata.id);
}
};
render() {
const {
requestMetadata,
targetSubjectMetadata,
selectedIdentities,
allIdentitiesSelected,
} = this.props;
return (
<div className="page-container permission-approval-container">
<PermissionPageContainerContent
requestMetadata={requestMetadata}
subjectMetadata={targetSubjectMetadata}
selectedPermissions={this.state.selectedPermissions}
selectedIdentities={selectedIdentities}
allIdentitiesSelected={allIdentitiesSelected}
/>
<div className="permission-approval-container__footers">
<PermissionsConnectFooter />
<PageContainerFooter
cancelButtonType="default"
onCancel={() => this.onCancel()}
cancelText={this.context.t('cancel')}
onSubmit={() => this.onSubmit()}
submitText={this.context.t('connect')}
buttonSizeLarge={false}
/>
</div>
</div>
);
}
}