mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-25 03:20:23 +01:00
31cf7c10a4
# Permission System 2.0 ## Background This PR migrates the extension permission system to [the new `PermissionController`](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions). The original permission system, based on [`rpc-cap`](https://github.com/MetaMask/rpc-cap), introduced [`ZCAP-LD`](https://w3c-ccg.github.io/zcap-ld/)-like permissions to our JSON-RPC stack. We used it to [implement](https://github.com/MetaMask/metamask-extension/pull/7004) what we called "LoginPerSite" in [version 7.7.0](https://github.com/MetaMask/metamask-extension/releases/tag/v7.7.0) of the extension, which enabled the user to choose which accounts, if any, should be exposed to each dapp. While that was a worthwhile feature in and of itself, we wanted a permission _system_ in order to enable everything we are going to with Snaps. Unfortunately, the original permission system was difficult to use, and necessitated the creation of the original `PermissionsController` (note the "s"), which was more or less a wrapper for `rpc-cap`. With this PR, we shake off the yoke of the original permission system, in favor of the modular, self-contained, ergonomic, and more mature permission system 2.0. Note that [the `PermissionController` readme](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions/README.md) explains how the new permission system works. The `PermissionController` and `SubjectMetadataController` are currently shipped via `@metamask/snap-controllers`. This is a temporary state of affairs, and we'll move them to `@metamask/controllers` once they've landed in prod. ## Changes in Detail First, the changes in this PR are not as big as they seem. Roughly half of the additions in this PR are fixtures in the test for the new migration (number 68), and a significant portion of the remaining ~2500 lines are due to find-and-replace changes in other test fixtures and UI files. - The extension `PermissionsController` has been deleted, and completely replaced with the new `PermissionController` from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers). - The original `PermissionsController` "domain metadata" functionality is now managed by the new `SubjectMetadataController`, also from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers). - The permission activity and history log controller has been renamed `PermissionLogController` and has its own top-level state key, but is otherwise functionally equivalent to the existing implementation. - Migration number 68 has been added to account for the new state changes. - The tests in `app/scripts/controllers/permissions` have been migrated from `mocha` to `jest`. Reviewers should focus their attention on the following files: - `app/scripts/` - `metamask-controller.js` - This is where most of the integration work for the new `PermissionController` occurs. Some functions that were internal to the original controller were moved here. - `controllers/permissions/` - `selectors.js` - These selectors are for `ControllerMessenger` selector subscriptions. The actual subscriptions occur in `metamask-controller.js`. See the `ControllerMessenger` implementation for details. - `specifications.js` - The caveat and permission specifications are required by the new `PermissionController`, and are used to specify the `eth_accounts` permission and its JSON-RPC method implementation. See the `PermissionController` readme for details. - `migrations/068.js` - The new state should be cross-referenced with the controllers that manage it. The accompanying tests should also be thoroughly reviewed. Some files may appear new but have just moved and/or been renamed: - `app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js` - This was previously implemented in `controllers/permissions/permissionsMethodMiddleware.js`. - `test/mocks/permissions.js` - A truncated version of `test/mocks/permission-controller.js`. Co-authored-by: Mark Stacey <markjstacey@gmail.com>
310 lines
9.2 KiB
JavaScript
310 lines
9.2 KiB
JavaScript
import React, { Component } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import copyToClipboard from 'copy-to-clipboard';
|
|
import classnames from 'classnames';
|
|
|
|
import AccountListItem from '../../components/app/account-list-item';
|
|
import Button from '../../components/ui/button';
|
|
import Identicon from '../../components/ui/identicon';
|
|
import Tooltip from '../../components/ui/tooltip';
|
|
import Copy from '../../components/ui/icon/copy-icon.component';
|
|
|
|
import { SECOND } from '../../../shared/constants/time';
|
|
import { conversionUtil } from '../../../shared/modules/conversion.utils';
|
|
|
|
export default class ConfirmDecryptMessage extends Component {
|
|
static contextTypes = {
|
|
t: PropTypes.func.isRequired,
|
|
metricsEvent: PropTypes.func.isRequired,
|
|
};
|
|
|
|
static propTypes = {
|
|
fromAccount: PropTypes.shape({
|
|
address: PropTypes.string.isRequired,
|
|
balance: PropTypes.string,
|
|
name: PropTypes.string,
|
|
}).isRequired,
|
|
clearConfirmTransaction: PropTypes.func.isRequired,
|
|
cancelDecryptMessage: PropTypes.func.isRequired,
|
|
decryptMessage: PropTypes.func.isRequired,
|
|
decryptMessageInline: PropTypes.func.isRequired,
|
|
conversionRate: PropTypes.number,
|
|
history: PropTypes.object.isRequired,
|
|
mostRecentOverviewPage: PropTypes.string.isRequired,
|
|
requesterAddress: PropTypes.string,
|
|
txData: PropTypes.object,
|
|
subjectMetadata: PropTypes.object,
|
|
};
|
|
|
|
state = {
|
|
fromAccount: this.props.fromAccount,
|
|
copyToClipboardPressed: false,
|
|
hasCopied: false,
|
|
};
|
|
|
|
copyMessage = () => {
|
|
copyToClipboard(this.state.rawMessage);
|
|
this.context.metricsEvent({
|
|
eventOpts: {
|
|
category: 'Messages',
|
|
action: 'Decrypt Message Copy',
|
|
name: 'Copy',
|
|
},
|
|
});
|
|
this.setState({ hasCopied: true });
|
|
setTimeout(() => this.setState({ hasCopied: false }), SECOND * 3);
|
|
};
|
|
|
|
renderHeader = () => {
|
|
return (
|
|
<div className="request-decrypt-message__header">
|
|
<div className="request-decrypt-message__header-background" />
|
|
|
|
<div className="request-decrypt-message__header__text">
|
|
{this.context.t('decryptRequest')}
|
|
</div>
|
|
|
|
<div className="request-decrypt-message__header__tip-container">
|
|
<div className="request-decrypt-message__header__tip" />
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
renderAccount = () => {
|
|
const { fromAccount } = this.state;
|
|
const { t } = this.context;
|
|
|
|
return (
|
|
<div className="request-decrypt-message__account">
|
|
<div className="request-decrypt-message__account-text">
|
|
{`${t('account')}:`}
|
|
</div>
|
|
|
|
<div className="request-decrypt-message__account-item">
|
|
<AccountListItem account={fromAccount} />
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
renderBalance = () => {
|
|
const { conversionRate } = this.props;
|
|
const {
|
|
fromAccount: { balance },
|
|
} = this.state;
|
|
const { t } = this.context;
|
|
|
|
const balanceInEther = conversionUtil(balance, {
|
|
fromNumericBase: 'hex',
|
|
toNumericBase: 'dec',
|
|
fromDenomination: 'WEI',
|
|
numberOfDecimals: 6,
|
|
conversionRate,
|
|
});
|
|
|
|
return (
|
|
<div className="request-decrypt-message__balance">
|
|
<div className="request-decrypt-message__balance-text">
|
|
{`${t('balance')}:`}
|
|
</div>
|
|
<div className="request-decrypt-message__balance-value">
|
|
{`${balanceInEther} ETH`}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
renderRequestIcon = () => {
|
|
const { requesterAddress } = this.props;
|
|
|
|
return (
|
|
<div className="request-decrypt-message__request-icon">
|
|
<Identicon diameter={40} address={requesterAddress} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
renderAccountInfo = () => {
|
|
return (
|
|
<div className="request-decrypt-message__account-info">
|
|
{this.renderAccount()}
|
|
{this.renderRequestIcon()}
|
|
{this.renderBalance()}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
renderBody = () => {
|
|
const { decryptMessageInline, subjectMetadata, txData } = this.props;
|
|
const { t } = this.context;
|
|
|
|
const targetSubjectMetadata = subjectMetadata[txData.msgParams.origin];
|
|
const name = targetSubjectMetadata?.name || txData.msgParams.origin;
|
|
const notice = t('decryptMessageNotice', [txData.msgParams.origin]);
|
|
|
|
const {
|
|
hasCopied,
|
|
hasDecrypted,
|
|
hasError,
|
|
rawMessage,
|
|
errorMessage,
|
|
copyToClipboardPressed,
|
|
} = this.state;
|
|
|
|
return (
|
|
<div className="request-decrypt-message__body">
|
|
{this.renderAccountInfo()}
|
|
<div className="request-decrypt-message__visual">
|
|
<section>
|
|
{targetSubjectMetadata?.iconUrl ? (
|
|
<img
|
|
className="request-decrypt-message__visual-identicon"
|
|
src={targetSubjectMetadata.iconUrl}
|
|
alt=""
|
|
/>
|
|
) : (
|
|
<i className="request-decrypt-message__visual-identicon--default">
|
|
{name.charAt(0).toUpperCase()}
|
|
</i>
|
|
)}
|
|
<div className="request-decrypt-message__notice">{notice}</div>
|
|
</section>
|
|
</div>
|
|
<div className="request-decrypt-message__message">
|
|
<div className="request-decrypt-message__message-text">
|
|
{!hasDecrypted && !hasError ? txData.msgParams.data : rawMessage}
|
|
{hasError ? errorMessage : ''}
|
|
</div>
|
|
<div
|
|
className={classnames({
|
|
'request-decrypt-message__message-cover': true,
|
|
'request-decrypt-message__message-lock--pressed':
|
|
hasDecrypted || hasError,
|
|
})}
|
|
/>
|
|
<div
|
|
className={classnames({
|
|
'request-decrypt-message__message-lock': true,
|
|
'request-decrypt-message__message-lock--pressed':
|
|
hasDecrypted || hasError,
|
|
})}
|
|
onClick={(event) => {
|
|
decryptMessageInline(txData, event).then((result) => {
|
|
if (result.error) {
|
|
this.setState({
|
|
hasError: true,
|
|
errorMessage: this.context.t('decryptInlineError', [
|
|
result.error,
|
|
]),
|
|
});
|
|
} else {
|
|
this.setState({
|
|
hasDecrypted: true,
|
|
rawMessage: result.rawData,
|
|
});
|
|
}
|
|
});
|
|
}}
|
|
>
|
|
<img src="images/lock.svg" alt="" />
|
|
<div className="request-decrypt-message__message-lock-text">
|
|
{t('decryptMetamask')}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{hasDecrypted ? (
|
|
<div
|
|
className={classnames({
|
|
'request-decrypt-message__message-copy': true,
|
|
'request-decrypt-message__message-copy--pressed': copyToClipboardPressed,
|
|
})}
|
|
onClick={() => this.copyMessage()}
|
|
onMouseDown={() => this.setState({ copyToClipboardPressed: true })}
|
|
onMouseUp={() => this.setState({ copyToClipboardPressed: false })}
|
|
>
|
|
<Tooltip
|
|
position="bottom"
|
|
title={hasCopied ? t('copiedExclamation') : t('copyToClipboard')}
|
|
wrapperClassName="request-decrypt-message__message-copy-tooltip"
|
|
style={{ display: 'flex', alignItems: 'center' }}
|
|
>
|
|
<div className="request-decrypt-message__message-copy-text">
|
|
{t('decryptCopy')}
|
|
</div>
|
|
<Copy size={17} color="#3098DC" />
|
|
</Tooltip>
|
|
</div>
|
|
) : (
|
|
<div></div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
renderFooter = () => {
|
|
const {
|
|
cancelDecryptMessage,
|
|
clearConfirmTransaction,
|
|
decryptMessage,
|
|
history,
|
|
mostRecentOverviewPage,
|
|
txData,
|
|
} = this.props;
|
|
const { metricsEvent, t } = this.context;
|
|
|
|
return (
|
|
<div className="request-decrypt-message__footer">
|
|
<Button
|
|
type="secondary"
|
|
large
|
|
className="request-decrypt-message__footer__cancel-button"
|
|
onClick={async (event) => {
|
|
await cancelDecryptMessage(txData, event);
|
|
metricsEvent({
|
|
eventOpts: {
|
|
category: 'Messages',
|
|
action: 'Decrypt Message Request',
|
|
name: 'Cancel',
|
|
},
|
|
});
|
|
clearConfirmTransaction();
|
|
history.push(mostRecentOverviewPage);
|
|
}}
|
|
>
|
|
{t('cancel')}
|
|
</Button>
|
|
<Button
|
|
type="primary"
|
|
large
|
|
className="request-decrypt-message__footer__sign-button"
|
|
onClick={async (event) => {
|
|
await decryptMessage(txData, event);
|
|
metricsEvent({
|
|
eventOpts: {
|
|
category: 'Messages',
|
|
action: 'Decrypt Message Request',
|
|
name: 'Confirm',
|
|
},
|
|
});
|
|
clearConfirmTransaction();
|
|
history.push(mostRecentOverviewPage);
|
|
}}
|
|
>
|
|
{t('decrypt')}
|
|
</Button>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
render = () => {
|
|
return (
|
|
<div className="request-decrypt-message__container">
|
|
{this.renderHeader()}
|
|
{this.renderBody()}
|
|
{this.renderFooter()}
|
|
</div>
|
|
);
|
|
};
|
|
}
|