2021-02-04 19:15:23 +01:00
|
|
|
import { ethErrors, errorCodes } from 'eth-rpc-errors';
|
|
|
|
import deepFreeze from 'deep-freeze-strict';
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2021-08-31 21:27:13 +02:00
|
|
|
import { ApprovalController, ControllerMessenger } from '@metamask/controllers';
|
2020-12-14 17:04:26 +01:00
|
|
|
|
2021-03-16 22:00:08 +01:00
|
|
|
import _getRestrictedMethods from '../../app/scripts/controllers/permissions/restrictedMethods';
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2021-03-16 22:00:08 +01:00
|
|
|
import { CAVEAT_NAMES } from '../../shared/constants/permissions';
|
2020-03-16 18:13:22 +01:00
|
|
|
import {
|
2020-05-28 05:35:09 +02:00
|
|
|
CAVEAT_TYPES,
|
2020-03-16 18:13:22 +01:00
|
|
|
NOTIFICATION_NAMES,
|
2021-03-16 22:00:08 +01:00
|
|
|
} from '../../app/scripts/controllers/permissions/enums';
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* README
|
|
|
|
* This file contains three primary kinds of mocks:
|
|
|
|
* - Mocks for initializing a permissions controller and getting a permissions
|
|
|
|
* middleware
|
|
|
|
* - Functions for getting various mock objects consumed or produced by
|
|
|
|
* permissions controller methods
|
|
|
|
* - Immutable mock values like Ethereum accounts and expected states
|
|
|
|
*/
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const noop = () => undefined;
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock Permissions Controller and Middleware
|
|
|
|
*/
|
|
|
|
|
|
|
|
const keyringAccounts = deepFreeze([
|
|
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
|
|
|
|
'0xc42edfcc21ed14dda456aa0756c153f7985d8813',
|
2020-04-16 18:16:53 +02:00
|
|
|
'0x7ae1cdd37bcbdb0e1f491974da8022bfdbf9c2bf',
|
|
|
|
'0xcc74c7a59194e5d9268476955650d1e285be703c',
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2020-04-16 20:20:01 +02:00
|
|
|
const getIdentities = () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return keyringAccounts.reduce((identities, address, index) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
identities[address] = { address, name: `Account ${index}` };
|
|
|
|
return identities;
|
|
|
|
}, {});
|
|
|
|
};
|
2020-04-16 20:20:01 +02:00
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
// perm controller initialization helper
|
|
|
|
const getRestrictedMethods = (permController) => {
|
|
|
|
return {
|
|
|
|
// the actual, production restricted methods
|
|
|
|
..._getRestrictedMethods(permController),
|
|
|
|
|
|
|
|
// our own dummy method for testing
|
2020-11-03 00:41:28 +01:00
|
|
|
test_method: {
|
2020-03-16 18:13:22 +01:00
|
|
|
description: `This method is only for testing.`,
|
|
|
|
method: (req, res, __, end) => {
|
|
|
|
if (req.params[0]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
res.result = 1;
|
2020-03-16 18:13:22 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
res.result = 0;
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
end();
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets default mock constructor options for a permissions controller.
|
|
|
|
*
|
|
|
|
* @returns {Object} A PermissionsController constructor options object.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getPermControllerOpts() {
|
2020-03-16 18:13:22 +01:00
|
|
|
return {
|
2020-12-14 17:04:26 +01:00
|
|
|
approvals: new ApprovalController({
|
2021-08-31 21:27:13 +02:00
|
|
|
messenger: new ControllerMessenger(),
|
2020-12-14 17:04:26 +01:00
|
|
|
showApprovalRequest: noop,
|
|
|
|
}),
|
2020-12-08 20:48:47 +01:00
|
|
|
getKeyringAccounts: async () => [...keyringAccounts],
|
|
|
|
getUnlockPromise: () => Promise.resolve(),
|
2020-03-23 17:25:55 +01:00
|
|
|
getRestrictedMethods,
|
2020-12-08 20:48:47 +01:00
|
|
|
isUnlocked: () => true,
|
2020-03-16 18:13:22 +01:00
|
|
|
notifyDomain: noop,
|
|
|
|
notifyAllDomains: noop,
|
2020-04-16 18:16:53 +02:00
|
|
|
preferences: {
|
|
|
|
getState: () => {
|
2020-04-16 20:20:01 +02:00
|
|
|
return {
|
|
|
|
identities: getIdentities(),
|
|
|
|
selectedAddress: keyringAccounts[0],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-16 18:16:53 +02:00
|
|
|
},
|
|
|
|
subscribe: noop,
|
|
|
|
},
|
2020-12-08 20:48:47 +01:00
|
|
|
showPermissionRequest: noop,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Promise-wrapped permissions controller middleware function.
|
|
|
|
*
|
|
|
|
* @param {PermissionsController} permController - The permissions controller to get a
|
|
|
|
* middleware for.
|
|
|
|
* @param {string} origin - The origin for the middleware.
|
|
|
|
* @param {string} extensionId - The extension id for the middleware.
|
|
|
|
* @returns {Function} A Promise-wrapped middleware function with convenient default args.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getPermissionsMiddleware(permController, origin, extensionId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const middleware = permController.createMiddleware({ origin, extensionId });
|
2020-03-16 18:13:22 +01:00
|
|
|
return (req, res = {}, next = noop, end) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-08-15 13:58:11 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2021-02-04 19:15:23 +01:00
|
|
|
end = end || _end;
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
middleware(req, res, next, end);
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
// emulates json-rpc-engine error handling
|
2020-11-03 00:41:28 +01:00
|
|
|
function _end(err) {
|
2020-03-16 18:13:22 +01:00
|
|
|
if (err || res.error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
reject(err || res.error);
|
2020-03-16 18:13:22 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve(res);
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} notifications - An object that will store notifications produced
|
|
|
|
* by the permissions controller.
|
|
|
|
* @returns {Function} A function passed to the permissions controller at initialization,
|
|
|
|
* for recording notifications.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getNotifyDomain = (notifications = {}) => (
|
|
|
|
origin,
|
|
|
|
notification,
|
|
|
|
) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
notifications[origin].push(notification);
|
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} notifications - An object that will store notifications produced
|
|
|
|
* by the permissions controller.
|
|
|
|
* @returns {Function} A function passed to the permissions controller at initialization,
|
|
|
|
* for recording notifications.
|
|
|
|
*/
|
|
|
|
export const getNotifyAllDomains = (notifications = {}) => (notification) => {
|
|
|
|
Object.keys(notifications).forEach((origin) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
notifications[origin].push(notification);
|
|
|
|
});
|
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants and Mock Objects
|
|
|
|
* - e.g. permissions, caveats, and permission requests
|
|
|
|
*/
|
|
|
|
|
2020-06-09 22:56:24 +02:00
|
|
|
const DOMAINS = {
|
|
|
|
a: { origin: 'https://foo.xyz', host: 'foo.xyz' },
|
|
|
|
b: { origin: 'https://bar.abc', host: 'bar.abc' },
|
|
|
|
c: { origin: 'https://baz.def', host: 'baz.def' },
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
const PERM_NAMES = {
|
|
|
|
eth_accounts: 'eth_accounts',
|
|
|
|
test_method: 'test_method',
|
|
|
|
does_not_exist: 'does_not_exist',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2020-05-28 05:35:09 +02:00
|
|
|
const ACCOUNTS = {
|
|
|
|
a: {
|
|
|
|
permitted: keyringAccounts.slice(0, 3),
|
|
|
|
primary: keyringAccounts[0],
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
permitted: [keyringAccounts[0]],
|
|
|
|
primary: keyringAccounts[0],
|
|
|
|
},
|
|
|
|
c: {
|
|
|
|
permitted: [keyringAccounts[1]],
|
|
|
|
primary: keyringAccounts[1],
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers for getting mock caveats.
|
|
|
|
*/
|
|
|
|
const CAVEATS = {
|
|
|
|
/**
|
|
|
|
* Gets a correctly formatted eth_accounts exposedAccounts caveat.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} accounts - The accounts for the caveat
|
|
|
|
* @returns {Object} An eth_accounts exposedAccounts caveats
|
|
|
|
*/
|
|
|
|
eth_accounts: (accounts) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
type: CAVEAT_TYPES.limitResponseLength,
|
|
|
|
value: 1,
|
|
|
|
name: CAVEAT_NAMES.primaryAccountOnly,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: CAVEAT_TYPES.filterResponse,
|
|
|
|
value: accounts,
|
|
|
|
name: CAVEAT_NAMES.exposedAccounts,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Each function here corresponds to what would be a type or interface consumed
|
|
|
|
* by permissions controller functions if we used TypeScript.
|
|
|
|
*/
|
|
|
|
const PERMS = {
|
|
|
|
/**
|
|
|
|
* The argument to approvePermissionsRequest
|
|
|
|
* @param {string} id - The rpc-cap permissions request id.
|
|
|
|
* @param {Object} permissions - The approved permissions, request-formatted.
|
|
|
|
*/
|
|
|
|
approvedRequest: (id, permissions = {}) => {
|
|
|
|
return {
|
|
|
|
permissions: { ...permissions },
|
|
|
|
metadata: { id },
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requested permissions objects, as passed to wallet_requestPermissions.
|
|
|
|
*/
|
|
|
|
requests: {
|
|
|
|
/**
|
|
|
|
* @returns {Object} A permissions request object with eth_accounts
|
|
|
|
*/
|
|
|
|
eth_accounts: () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return { eth_accounts: {} };
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Object} A permissions request object with test_method
|
|
|
|
*/
|
|
|
|
test_method: () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return { test_method: {} };
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Object} A permissions request object with does_not_exist
|
|
|
|
*/
|
|
|
|
does_not_exist: () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return { does_not_exist: {} };
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalized permission requests, as returned by finalizePermissionsRequest
|
|
|
|
*/
|
|
|
|
finalizedRequests: {
|
|
|
|
/**
|
|
|
|
* @param {Array<string>} accounts - The accounts for the eth_accounts permission caveat
|
|
|
|
* @returns {Object} A finalized permissions request object with eth_accounts and its caveat
|
|
|
|
*/
|
|
|
|
eth_accounts: (accounts) => {
|
|
|
|
return {
|
|
|
|
eth_accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
caveats: CAVEATS.eth_accounts(accounts),
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Object} A finalized permissions request object with test_method
|
|
|
|
*/
|
|
|
|
test_method: () => {
|
|
|
|
return {
|
|
|
|
test_method: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partial members of res.result for successful:
|
|
|
|
* - wallet_requestPermissions
|
|
|
|
* - wallet_getPermissions
|
|
|
|
*/
|
|
|
|
granted: {
|
|
|
|
/**
|
|
|
|
* @param {Array<string>} accounts - The accounts for the eth_accounts permission caveat
|
|
|
|
* @returns {Object} A granted permissions object with eth_accounts and its caveat
|
|
|
|
*/
|
|
|
|
eth_accounts: (accounts) => {
|
|
|
|
return {
|
|
|
|
parentCapability: PERM_NAMES.eth_accounts,
|
2020-05-28 05:35:09 +02:00
|
|
|
caveats: CAVEATS.eth_accounts(accounts),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Object} A granted permissions object with test_method
|
|
|
|
*/
|
|
|
|
test_method: () => {
|
|
|
|
return {
|
|
|
|
parentCapability: PERM_NAMES.test_method,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Objects with function values for getting correctly formatted permissions,
|
|
|
|
* caveats, errors, permissions requests etc.
|
|
|
|
*/
|
|
|
|
export const getters = deepFreeze({
|
|
|
|
CAVEATS,
|
|
|
|
|
|
|
|
PERMS,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getters for errors by the method or workflow that throws them.
|
|
|
|
*/
|
|
|
|
ERRORS: {
|
|
|
|
validatePermittedAccounts: {
|
|
|
|
invalidParam: () => {
|
|
|
|
return {
|
|
|
|
name: 'Error',
|
|
|
|
message: 'Must provide non-empty array of account(s).',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
nonKeyringAccount: (account) => {
|
|
|
|
return {
|
|
|
|
name: 'Error',
|
|
|
|
message: `Unknown account: ${account}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
finalizePermissionsRequest: {
|
|
|
|
grantEthAcountsFailure: (origin) => {
|
|
|
|
return {
|
|
|
|
// name: 'EthereumRpcError',
|
|
|
|
message: `Failed to add 'eth_accounts' to '${origin}'.`,
|
2021-01-21 07:06:41 +01:00
|
|
|
code: errorCodes.rpc.internal,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-04-17 00:23:36 +02:00
|
|
|
addPermittedAccount: {
|
|
|
|
alreadyPermitted: () => {
|
|
|
|
return {
|
2020-04-20 20:34:56 +02:00
|
|
|
message: 'Account is already permitted for origin',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-17 00:23:36 +02:00
|
|
|
},
|
|
|
|
invalidOrigin: () => {
|
|
|
|
return {
|
|
|
|
message: 'Unrecognized domain',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-17 00:23:36 +02:00
|
|
|
},
|
|
|
|
noEthAccountsPermission: () => {
|
|
|
|
return {
|
2020-04-20 20:34:56 +02:00
|
|
|
message: `Origin does not have 'eth_accounts' permission`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-20 20:34:56 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
removePermittedAccount: {
|
|
|
|
notPermitted: () => {
|
|
|
|
return {
|
|
|
|
message: 'Account is not permitted for origin',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-20 20:34:56 +02:00
|
|
|
},
|
|
|
|
invalidOrigin: () => {
|
|
|
|
return {
|
|
|
|
message: 'Unrecognized domain',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-20 20:34:56 +02:00
|
|
|
},
|
|
|
|
noEthAccountsPermission: () => {
|
|
|
|
return {
|
|
|
|
message: `Origin does not have 'eth_accounts' permission`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-04-17 00:23:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-04-16 18:16:53 +02:00
|
|
|
_handleAccountSelected: {
|
2020-03-16 18:13:22 +01:00
|
|
|
invalidParams: () => {
|
|
|
|
return {
|
|
|
|
name: 'Error',
|
2020-04-16 18:16:53 +02:00
|
|
|
message: 'Selected account should be a non-empty string.',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
approvePermissionsRequest: {
|
|
|
|
noPermsRequested: () => {
|
|
|
|
return {
|
|
|
|
message: 'Must request at least one permission.',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
rejectPermissionsRequest: {
|
|
|
|
rejection: () => {
|
|
|
|
return {
|
|
|
|
message: ethErrors.provider.userRejectedRequest().message,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
methodNotFound: (methodName) => {
|
|
|
|
return {
|
|
|
|
message: `The method '${methodName}' does not exist / is not available.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
createMiddleware: {
|
|
|
|
badOrigin: () => {
|
|
|
|
return {
|
|
|
|
message: 'Must provide non-empty string origin.',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
rpcCap: {
|
|
|
|
unauthorized: () => {
|
|
|
|
return {
|
|
|
|
code: 4100,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
pendingApprovals: {
|
|
|
|
duplicateOriginOrId: (id, origin) => {
|
|
|
|
return {
|
2020-05-05 16:03:21 +02:00
|
|
|
message: `Pending approval with id '${id}' or origin '${origin}' already exists.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
2020-12-14 17:04:26 +01:00
|
|
|
requestAlreadyPending: (origin) => {
|
2020-03-16 18:13:22 +01:00
|
|
|
return {
|
2020-12-14 17:04:26 +01:00
|
|
|
message: `Request of type 'wallet_requestPermissions' already pending for origin ${origin}. Please wait.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
2020-03-23 17:25:55 +01:00
|
|
|
|
|
|
|
eth_requestAccounts: {
|
|
|
|
requestAlreadyPending: () => {
|
|
|
|
return {
|
|
|
|
message: 'Already processing eth_requestAccounts. Please wait.',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-23 17:25:55 +01:00
|
|
|
},
|
|
|
|
},
|
2020-06-04 21:15:52 +02:00
|
|
|
|
|
|
|
notifyAccountsChanged: {
|
|
|
|
invalidOrigin: (origin) => {
|
|
|
|
return {
|
|
|
|
message: `Invalid origin: '${origin}'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-06-04 21:15:52 +02:00
|
|
|
},
|
|
|
|
invalidAccounts: () => {
|
|
|
|
return {
|
|
|
|
message: 'Invalid accounts',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-06-04 21:15:52 +02:00
|
|
|
},
|
|
|
|
},
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getters for notifications produced by the permissions controller.
|
|
|
|
*/
|
|
|
|
NOTIFICATIONS: {
|
|
|
|
/**
|
|
|
|
* Gets a removed accounts notification.
|
|
|
|
*
|
|
|
|
* @returns {Object} An accountsChanged notification with an empty array as its result
|
|
|
|
*/
|
|
|
|
removedAccounts: () => {
|
|
|
|
return {
|
|
|
|
method: NOTIFICATION_NAMES.accountsChanged,
|
2020-12-08 20:48:47 +01:00
|
|
|
params: [],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a new accounts notification.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} accounts - The accounts added to the notification.
|
|
|
|
* @returns {Object} An accountsChanged notification with the given accounts as its result
|
|
|
|
*/
|
|
|
|
newAccounts: (accounts) => {
|
|
|
|
return {
|
|
|
|
method: NOTIFICATION_NAMES.accountsChanged,
|
2020-12-08 20:48:47 +01:00
|
|
|
params: accounts,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getters for mock RPC request objects.
|
|
|
|
*/
|
|
|
|
RPC_REQUESTS: {
|
|
|
|
/**
|
|
|
|
* Gets an arbitrary RPC request object.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @param {string} method - The request method
|
|
|
|
* @param {Array<any>} params - The request parameters
|
|
|
|
* @param {string} [id] - The request id
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
|
|
|
custom: (origin, method, params = [], id) => {
|
|
|
|
const req = {
|
|
|
|
origin,
|
|
|
|
method,
|
|
|
|
params,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
if (id !== undefined) {
|
2021-02-04 19:15:23 +01:00
|
|
|
req.id = id;
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return req;
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an eth_accounts RPC request object.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
|
|
|
eth_accounts: (origin) => {
|
|
|
|
return {
|
|
|
|
origin,
|
|
|
|
method: 'eth_accounts',
|
|
|
|
params: [],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a test_method RPC request object.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @param {boolean} param - The request param
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
|
|
|
test_method: (origin, param = false) => {
|
|
|
|
return {
|
|
|
|
origin,
|
|
|
|
method: 'test_method',
|
|
|
|
params: [param],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an eth_requestAccounts RPC request object.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
|
|
|
eth_requestAccounts: (origin) => {
|
|
|
|
return {
|
|
|
|
origin,
|
|
|
|
method: 'eth_requestAccounts',
|
|
|
|
params: [],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a wallet_requestPermissions RPC request object,
|
|
|
|
* for a single permission.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @param {string} permissionName - The name of the permission to request
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
|
|
|
requestPermission: (origin, permissionName) => {
|
|
|
|
return {
|
|
|
|
origin,
|
|
|
|
method: 'wallet_requestPermissions',
|
2020-08-19 18:27:05 +02:00
|
|
|
params: [PERMS.requests[permissionName]()],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a wallet_requestPermissions RPC request object,
|
|
|
|
* for multiple permissions.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @param {Object} permissions - A permission request object
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
|
|
|
requestPermissions: (origin, permissions = {}) => {
|
|
|
|
return {
|
|
|
|
origin,
|
|
|
|
method: 'wallet_requestPermissions',
|
2020-08-19 18:27:05 +02:00
|
|
|
params: [permissions],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-12-08 20:48:47 +01:00
|
|
|
* Gets a metamask_sendDomainMetadata RPC request object.
|
2020-03-16 18:13:22 +01:00
|
|
|
*
|
|
|
|
* @param {string} origin - The origin of the request
|
|
|
|
* @param {Object} name - The domainMetadata name
|
|
|
|
* @param {Array<any>} [args] - Any other data for the request's domainMetadata
|
|
|
|
* @returns {Object} An RPC request object
|
|
|
|
*/
|
2020-12-08 20:48:47 +01:00
|
|
|
metamask_sendDomainMetadata: (origin, name, ...args) => {
|
2020-03-16 18:13:22 +01:00
|
|
|
return {
|
|
|
|
origin,
|
2020-12-08 20:48:47 +01:00
|
|
|
method: 'metamask_sendDomainMetadata',
|
2021-01-22 19:27:07 +01:00
|
|
|
params: {
|
2020-03-16 18:13:22 +01:00
|
|
|
...args,
|
|
|
|
name,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Objects with immutable mock values.
|
|
|
|
*/
|
|
|
|
export const constants = deepFreeze({
|
2020-04-16 20:20:01 +02:00
|
|
|
ALL_ACCOUNTS: keyringAccounts,
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
DUMMY_ACCOUNT: '0xabc',
|
|
|
|
|
2020-04-16 18:16:53 +02:00
|
|
|
EXTRA_ACCOUNT: keyringAccounts[3],
|
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
REQUEST_IDS: {
|
|
|
|
a: '1',
|
|
|
|
b: '2',
|
|
|
|
c: '3',
|
|
|
|
},
|
|
|
|
|
2020-06-09 22:56:24 +02:00
|
|
|
DOMAINS: { ...DOMAINS },
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2020-05-28 05:35:09 +02:00
|
|
|
ACCOUNTS: { ...ACCOUNTS },
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
PERM_NAMES: { ...PERM_NAMES },
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
RESTRICTED_METHODS: ['eth_accounts', 'test_method'],
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock permissions history objects.
|
|
|
|
*/
|
|
|
|
EXPECTED_HISTORIES: {
|
|
|
|
case1: [
|
|
|
|
{
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.a.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 1,
|
|
|
|
accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
[ACCOUNTS.a.permitted[0]]: 1,
|
|
|
|
[ACCOUNTS.a.permitted[1]]: 1,
|
|
|
|
[ACCOUNTS.a.permitted[2]]: 1,
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.a.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 2,
|
|
|
|
accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
[ACCOUNTS.a.permitted[0]]: 2,
|
|
|
|
[ACCOUNTS.a.permitted[1]]: 1,
|
|
|
|
[ACCOUNTS.a.permitted[2]]: 1,
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
case2: [
|
|
|
|
{
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.a.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 1,
|
|
|
|
accounts: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
case3: [
|
|
|
|
{
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.a.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.test_method]: { lastApproved: 1 },
|
|
|
|
},
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.b.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 1,
|
|
|
|
accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
[ACCOUNTS.b.permitted[0]]: 1,
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.c.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.test_method]: { lastApproved: 1 },
|
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 1,
|
|
|
|
accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
[ACCOUNTS.c.permitted[0]]: 1,
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.a.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.test_method]: { lastApproved: 2 },
|
|
|
|
},
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.b.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 1,
|
|
|
|
accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
[ACCOUNTS.b.permitted[0]]: 1,
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.c.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.test_method]: { lastApproved: 1 },
|
|
|
|
[PERM_NAMES.eth_accounts]: {
|
|
|
|
lastApproved: 2,
|
|
|
|
accounts: {
|
2020-05-28 05:35:09 +02:00
|
|
|
[ACCOUNTS.c.permitted[0]]: 1,
|
|
|
|
[ACCOUNTS.b.permitted[0]]: 2,
|
2020-03-16 18:13:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
case4: [
|
|
|
|
{
|
2020-06-09 22:56:24 +02:00
|
|
|
[DOMAINS.a.origin]: {
|
2020-03-16 18:13:22 +01:00
|
|
|
[PERM_NAMES.test_method]: {
|
|
|
|
lastApproved: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|