mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-25 11:28:51 +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>
404 lines
14 KiB
JavaScript
404 lines
14 KiB
JavaScript
import { KOVAN_CHAIN_ID } from '../../shared/constants/network';
|
|
import {
|
|
getConnectedSubjectsForSelectedAddress,
|
|
getOrderedConnectedAccountsForActiveTab,
|
|
getPermissionsForActiveTab,
|
|
} from './permissions';
|
|
|
|
describe('selectors', () => {
|
|
describe('getConnectedSubjectsForSelectedAddress', () => {
|
|
it('should return the list of connected subjects when there is 1 connected account', () => {
|
|
const mockState = {
|
|
metamask: {
|
|
selectedAddress: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
subjectMetadata: {
|
|
'peepeth.com': {
|
|
iconUrl: 'https://peepeth.com/favicon-32x32.png',
|
|
name: 'Peepeth',
|
|
},
|
|
'https://remix.ethereum.org': {
|
|
iconUrl: 'https://remix.ethereum.org/icon.png',
|
|
name: 'Remix - Ethereum IDE',
|
|
},
|
|
},
|
|
subjects: {
|
|
'peepeth.com': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
|
|
},
|
|
],
|
|
date: 1585676177970,
|
|
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
|
|
invoker: 'peepeth.com',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
'https://remix.ethereum.org': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
|
|
},
|
|
],
|
|
date: 1585685128948,
|
|
id: '6b9615cc-64e4-4317-afab-3c4f8ee0244a',
|
|
invoker: 'https://remix.ethereum.org',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
const extensionId = undefined;
|
|
expect(getConnectedSubjectsForSelectedAddress(mockState)).toStrictEqual([
|
|
{
|
|
extensionId,
|
|
iconUrl: 'https://peepeth.com/favicon-32x32.png',
|
|
origin: 'peepeth.com',
|
|
name: 'Peepeth',
|
|
},
|
|
{
|
|
extensionId,
|
|
name: 'Remix - Ethereum IDE',
|
|
iconUrl: 'https://remix.ethereum.org/icon.png',
|
|
origin: 'https://remix.ethereum.org',
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('should return the list of connected subjects when there are 2 connected accounts', () => {
|
|
const mockState = {
|
|
metamask: {
|
|
selectedAddress: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
subjectMetadata: {
|
|
'peepeth.com': {
|
|
iconUrl: 'https://peepeth.com/favicon-32x32.png',
|
|
name: 'Peepeth',
|
|
},
|
|
'https://remix.ethereum.org': {
|
|
iconUrl: 'https://remix.ethereum.org/icon.png',
|
|
name: 'Remix - Ethereum IDE',
|
|
},
|
|
},
|
|
subjects: {
|
|
'peepeth.com': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
|
|
},
|
|
],
|
|
date: 1585676177970,
|
|
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
|
|
invoker: 'peepeth.com',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
'https://remix.ethereum.org': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: [
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
],
|
|
},
|
|
],
|
|
date: 1585685128948,
|
|
id: '6b9615cc-64e4-4317-afab-3c4f8ee0244a',
|
|
invoker: 'https://remix.ethereum.org',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
const extensionId = undefined;
|
|
expect(getConnectedSubjectsForSelectedAddress(mockState)).toStrictEqual([
|
|
{
|
|
extensionId,
|
|
name: 'Remix - Ethereum IDE',
|
|
iconUrl: 'https://remix.ethereum.org/icon.png',
|
|
origin: 'https://remix.ethereum.org',
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('getConnectedAccountsForActiveTab', () => {
|
|
const mockState = {
|
|
activeTab: {
|
|
title: 'Eth Sign Tests',
|
|
origin: 'https://remix.ethereum.org',
|
|
protocol: 'https:',
|
|
url: 'https://remix.ethereum.org/',
|
|
},
|
|
metamask: {
|
|
provider: {
|
|
chainId: KOVAN_CHAIN_ID,
|
|
},
|
|
accounts: {
|
|
0x7250739de134d33ec7ab1ee592711e15098c9d2d: {
|
|
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
},
|
|
0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5: {
|
|
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
},
|
|
0xb3958fb96c8201486ae20be1d5c9f58083df343a: {
|
|
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
|
|
},
|
|
0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc: {
|
|
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
|
|
},
|
|
0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4: {
|
|
address: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
},
|
|
},
|
|
cachedBalances: {},
|
|
subjects: {
|
|
'https://remix.ethereum.org': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: [
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
|
|
'0xb3958fb96c8201486ae20be1d5c9f58083df343a',
|
|
],
|
|
},
|
|
],
|
|
date: 1586359844177,
|
|
id: '3aa65a8b-3bcb-4944-941b-1baa5fe0ed8b',
|
|
invoker: 'https://remix.ethereum.org',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
'peepeth.com': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
|
|
},
|
|
],
|
|
date: 1585676177970,
|
|
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
|
|
invoker: 'peepeth.com',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
identities: {
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': {
|
|
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
name: 'Really Long Name That Should Be Truncated',
|
|
},
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': {
|
|
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
lastSelected: 1586359844192,
|
|
name: 'Account 1',
|
|
},
|
|
'0xb3958fb96c8201486ae20be1d5c9f58083df343a': {
|
|
lastSelected: 1586359844193,
|
|
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
|
|
name: 'Account 2',
|
|
},
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': {
|
|
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
|
|
lastSelected: 1586359844192,
|
|
name: 'Account 3',
|
|
},
|
|
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4': {
|
|
address: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
name: 'Account 4',
|
|
},
|
|
},
|
|
keyrings: [
|
|
{
|
|
accounts: [
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
|
|
'0xb3958fb96c8201486ae20be1d5c9f58083df343a',
|
|
],
|
|
},
|
|
],
|
|
permissionHistory: {
|
|
'https://remix.ethereum.org': {
|
|
eth_accounts: {
|
|
accounts: {
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': 1586359844192,
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': 1586359844192,
|
|
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4': 1586359844192,
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': 1586359844192,
|
|
'0xb3958fb96c8201486ae20be1d5c9f58083df343a': 1586359844192,
|
|
},
|
|
lastApproved: 1586359844192,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
it('should return connected accounts sorted by last selected, then by keyring controller order', () => {
|
|
expect(getOrderedConnectedAccountsForActiveTab(mockState)).toStrictEqual([
|
|
{
|
|
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
|
|
name: 'Account 2',
|
|
lastActive: 1586359844192,
|
|
lastSelected: 1586359844193,
|
|
},
|
|
{
|
|
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
name: 'Account 1',
|
|
lastActive: 1586359844192,
|
|
lastSelected: 1586359844192,
|
|
},
|
|
{
|
|
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
|
|
name: 'Account 3',
|
|
lastActive: 1586359844192,
|
|
lastSelected: 1586359844192,
|
|
},
|
|
{
|
|
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
name: 'Really Long Name That Should Be Truncated',
|
|
lastActive: 1586359844192,
|
|
},
|
|
{
|
|
address: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
name: 'Account 4',
|
|
lastActive: 1586359844192,
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('getPermissionsForActiveTab', () => {
|
|
const mockState = {
|
|
activeTab: {
|
|
title: 'Eth Sign Tests',
|
|
origin: 'https://remix.ethereum.org',
|
|
protocol: 'https:',
|
|
url: 'https://remix.ethereum.org/',
|
|
},
|
|
metamask: {
|
|
identities: {
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': {
|
|
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
name: 'Really Long Name That Should Be Truncated',
|
|
},
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': {
|
|
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
name: 'Account 1',
|
|
},
|
|
'0xb3958fb96c8201486ae20be1d5c9f58083df343a': {
|
|
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
|
|
name: 'Account 2',
|
|
},
|
|
},
|
|
subjects: {
|
|
'https://remix.ethereum.org': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: [
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
|
|
],
|
|
},
|
|
],
|
|
date: 1586359844177,
|
|
id: '3aa65a8b-3bcb-4944-941b-1baa5fe0ed8b',
|
|
invoker: 'https://remix.ethereum.org',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
'peepeth.com': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
|
|
},
|
|
],
|
|
date: 1585676177970,
|
|
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
|
|
invoker: 'peepeth.com',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
'uniswap.exchange': {
|
|
permissions: {
|
|
eth_accounts: {
|
|
caveats: [
|
|
{
|
|
type: 'restrictReturnedAccounts',
|
|
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
|
|
},
|
|
],
|
|
date: 1585616816623,
|
|
id: 'ce625215-f2e9-48e7-93ca-21ba193244ff',
|
|
invoker: 'uniswap.exchange',
|
|
parentCapability: 'eth_accounts',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
subjectMetadata: {
|
|
'https://remix.ethereum.org': {
|
|
iconUrl: 'https://remix.ethereum.org/icon.png',
|
|
name: 'Remix - Ethereum IDE',
|
|
},
|
|
},
|
|
permissionHistory: {
|
|
'https://remix.ethereum.org': {
|
|
eth_accounts: {
|
|
accounts: {
|
|
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': 1586359844192,
|
|
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': 1586359844192,
|
|
},
|
|
lastApproved: 1586359844192,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
it('should return a list of permissions strings', () => {
|
|
expect(getPermissionsForActiveTab(mockState)).toStrictEqual([
|
|
{
|
|
key: 'eth_accounts',
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
});
|