You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
289 lines
7.8 KiB
289 lines
7.8 KiB
4 years ago
|
import { forOwn } from 'lodash';
|
||
4 years ago
|
import { CAVEAT_NAMES } from '../../shared/constants/permissions';
|
||
5 years ago
|
import {
|
||
4 years ago
|
getMetaMaskAccountsOrdered,
|
||
|
getOriginOfCurrentTab,
|
||
|
getSelectedAddress,
|
||
4 years ago
|
} from '.';
|
||
5 years ago
|
|
||
5 years ago
|
// selectors
|
||
5 years ago
|
|
||
5 years ago
|
/**
|
||
|
* Get the permission domains object.
|
||
|
*
|
||
|
* @param {Object} state - The current state.
|
||
|
* @returns {Object} The permissions domains object.
|
||
|
*/
|
||
4 years ago
|
export function getPermissionDomains(state) {
|
||
4 years ago
|
return state.metamask.domains || {};
|
||
5 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the permission domains metadata object.
|
||
|
*
|
||
|
* @param {Object} state - The current state.
|
||
|
* @returns {Object} The permission domains metadata object.
|
||
|
*/
|
||
4 years ago
|
export function getPermissionDomainsMetadata(state) {
|
||
4 years ago
|
return state.metamask.domainMetadata || {};
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* Selects the permitted accounts from the eth_accounts permission given state
|
||
|
* and an origin.
|
||
5 years ago
|
*
|
||
5 years ago
|
* @param {Object} state - The current state.
|
||
|
* @param {string} origin - The origin/domain to get the permitted accounts for.
|
||
|
* @returns {Array<string>} An empty array or an array of accounts.
|
||
|
*/
|
||
4 years ago
|
export function getPermittedAccounts(state, origin) {
|
||
5 years ago
|
return getAccountsFromPermission(
|
||
4 years ago
|
getAccountsPermissionFromDomain(domainSelector(state, origin)),
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
/**
|
||
|
* Selects the permitted accounts from the eth_accounts permission for the
|
||
|
* origin of the current tab.
|
||
|
*
|
||
|
* @param {Object} state - The current state.
|
||
|
* @returns {Array<string>} An empty array or an array of accounts.
|
||
|
*/
|
||
4 years ago
|
export function getPermittedAccountsForCurrentTab(state) {
|
||
4 years ago
|
return getPermittedAccounts(state, getOriginOfCurrentTab(state));
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
5 years ago
|
* Returns a map of permitted accounts by origin for all origins.
|
||
5 years ago
|
*
|
||
5 years ago
|
* @param {Object} state - The current state.
|
||
|
* @returns {Object} Permitted accounts by origin.
|
||
5 years ago
|
*/
|
||
4 years ago
|
export function getPermittedAccountsByOrigin(state) {
|
||
4 years ago
|
const domains = getPermissionDomains(state);
|
||
5 years ago
|
return Object.keys(domains).reduce((acc, domainKey) => {
|
||
|
const accounts = getAccountsFromPermission(
|
||
4 years ago
|
getAccountsPermissionFromDomain(domains[domainKey]),
|
||
4 years ago
|
);
|
||
5 years ago
|
if (accounts.length > 0) {
|
||
4 years ago
|
acc[domainKey] = accounts;
|
||
5 years ago
|
}
|
||
4 years ago
|
return acc;
|
||
|
}, {});
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
/**
|
||
|
* Returns an array of connected domain objects, with the following properties:
|
||
|
* - extensionId
|
||
|
* - key (i.e. origin)
|
||
|
* - name
|
||
|
* - icon
|
||
|
*
|
||
|
* @param {Object} state - The current state.
|
||
|
* @returns {Array<Object>} An array of connected domain objects.
|
||
|
*/
|
||
4 years ago
|
export function getConnectedDomainsForSelectedAddress(state) {
|
||
4 years ago
|
const { selectedAddress } = state.metamask;
|
||
|
const domains = getPermissionDomains(state);
|
||
|
const domainMetadata = getPermissionDomainsMetadata(state);
|
||
5 years ago
|
|
||
4 years ago
|
const connectedDomains = [];
|
||
5 years ago
|
|
||
|
forOwn(domains, (domainValue, domainKey) => {
|
||
4 years ago
|
const exposedAccounts = getAccountsFromDomain(domainValue);
|
||
5 years ago
|
if (!exposedAccounts.includes(selectedAddress)) {
|
||
4 years ago
|
return;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
const { extensionId, name, icon, host } = domainMetadata[domainKey] || {};
|
||
5 years ago
|
|
||
|
connectedDomains.push({
|
||
|
extensionId,
|
||
5 years ago
|
origin: domainKey,
|
||
5 years ago
|
name,
|
||
|
icon,
|
||
5 years ago
|
host,
|
||
4 years ago
|
});
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
return connectedDomains;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* Returns an object mapping addresses to objects mapping origins to connected
|
||
|
* domain info. Domain info objects have the following properties:
|
||
|
* - icon
|
||
|
* - name
|
||
|
*
|
||
|
* @param {Object} state - The current state.
|
||
|
* @returns {Object} A mapping of addresses to a mapping of origins to
|
||
|
* connected domain info.
|
||
|
*/
|
||
4 years ago
|
export function getAddressConnectedDomainMap(state) {
|
||
4 years ago
|
const domainMetadata = getPermissionDomainsMetadata(state);
|
||
|
const accountsMap = getPermittedAccountsByOrigin(state);
|
||
|
const addressConnectedIconMap = {};
|
||
5 years ago
|
|
||
|
Object.keys(accountsMap).forEach((domainKey) => {
|
||
4 years ago
|
const { icon, name } = domainMetadata[domainKey] || {};
|
||
5 years ago
|
|
||
|
accountsMap[domainKey].forEach((address) => {
|
||
4 years ago
|
const nameToRender = name || domainKey;
|
||
5 years ago
|
|
||
|
addressConnectedIconMap[address] = addressConnectedIconMap[address]
|
||
4 years ago
|
? {
|
||
|
...addressConnectedIconMap[address],
|
||
|
[domainKey]: { icon, name: nameToRender },
|
||
|
}
|
||
4 years ago
|
: { [domainKey]: { icon, name: nameToRender } };
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
return addressConnectedIconMap;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// selector helpers
|
||
|
|
||
4 years ago
|
function getAccountsFromDomain(domain) {
|
||
4 years ago
|
return getAccountsFromPermission(getAccountsPermissionFromDomain(domain));
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
function getAccountsPermissionFromDomain(domain = {}) {
|
||
|
return Array.isArray(domain.permissions)
|
||
|
? domain.permissions.find(
|
||
4 years ago
|
(perm) => perm.parentCapability === 'eth_accounts',
|
||
5 years ago
|
)
|
||
4 years ago
|
: {};
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
function getAccountsFromPermission(accountsPermission) {
|
||
4 years ago
|
const accountsCaveat = getAccountsCaveatFromPermission(accountsPermission);
|
||
4 years ago
|
return accountsCaveat && Array.isArray(accountsCaveat.value)
|
||
|
? accountsCaveat.value
|
||
4 years ago
|
: [];
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
function getAccountsCaveatFromPermission(accountsPermission = {}) {
|
||
5 years ago
|
return (
|
||
|
Array.isArray(accountsPermission.caveats) &&
|
||
|
accountsPermission.caveats.find(
|
||
4 years ago
|
(c) => c.name === CAVEAT_NAMES.exposedAccounts,
|
||
5 years ago
|
)
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
function domainSelector(state, origin) {
|
||
4 years ago
|
return origin && state.metamask.domains?.[origin];
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function getAccountToConnectToActiveTab(state) {
|
||
4 years ago
|
const selectedAddress = getSelectedAddress(state);
|
||
|
const connectedAccounts = getPermittedAccountsForCurrentTab(state);
|
||
5 years ago
|
|
||
4 years ago
|
const {
|
||
|
metamask: { identities },
|
||
4 years ago
|
} = state;
|
||
|
const numberOfAccounts = Object.keys(identities).length;
|
||
5 years ago
|
|
||
4 years ago
|
if (
|
||
|
connectedAccounts.length &&
|
||
|
connectedAccounts.length !== numberOfAccounts
|
||
|
) {
|
||
|
if (
|
||
|
connectedAccounts.findIndex((address) => address === selectedAddress) ===
|
||
|
-1
|
||
|
) {
|
||
4 years ago
|
return identities[selectedAddress];
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
return undefined;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getOrderedConnectedAccountsForActiveTab(state) {
|
||
|
const {
|
||
|
activeTab,
|
||
|
metamask: { permissionsHistory },
|
||
4 years ago
|
} = state;
|
||
5 years ago
|
|
||
4 years ago
|
const permissionsHistoryByAccount =
|
||
4 years ago
|
// eslint-disable-next-line camelcase
|
||
4 years ago
|
permissionsHistory[activeTab.origin]?.eth_accounts?.accounts;
|
||
|
const orderedAccounts = getMetaMaskAccountsOrdered(state);
|
||
|
const connectedAccounts = getPermittedAccountsForCurrentTab(state);
|
||
5 years ago
|
|
||
5 years ago
|
return orderedAccounts
|
||
|
.filter((account) => connectedAccounts.includes(account.address))
|
||
|
.map((account) => ({
|
||
|
...account,
|
||
5 years ago
|
lastActive: permissionsHistoryByAccount?.[account.address],
|
||
5 years ago
|
}))
|
||
4 years ago
|
.sort(
|
||
|
({ lastSelected: lastSelectedA }, { lastSelected: lastSelectedB }) => {
|
||
|
if (lastSelectedA === lastSelectedB) {
|
||
4 years ago
|
return 0;
|
||
4 years ago
|
} else if (lastSelectedA === undefined) {
|
||
4 years ago
|
return 1;
|
||
4 years ago
|
} else if (lastSelectedB === undefined) {
|
||
4 years ago
|
return -1;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
return lastSelectedB - lastSelectedA;
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getPermissionsForActiveTab(state) {
|
||
4 years ago
|
const { activeTab, metamask } = state;
|
||
|
const { domains = {} } = metamask;
|
||
5 years ago
|
|
||
|
return domains[activeTab.origin]?.permissions?.map(({ parentCapability }) => {
|
||
|
return {
|
||
|
key: parentCapability,
|
||
4 years ago
|
};
|
||
|
});
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function activeTabHasPermissions(state) {
|
||
4 years ago
|
const { activeTab, metamask } = state;
|
||
|
const { domains = {} } = metamask;
|
||
4 years ago
|
|
||
4 years ago
|
return Boolean(domains[activeTab.origin]?.permissions?.length > 0);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getLastConnectedInfo(state) {
|
||
4 years ago
|
const { permissionsHistory = {} } = state.metamask;
|
||
5 years ago
|
return Object.keys(permissionsHistory).reduce((acc, origin) => {
|
||
4 years ago
|
const ethAccountsHistory = JSON.parse(
|
||
|
JSON.stringify(permissionsHistory[origin].eth_accounts),
|
||
4 years ago
|
);
|
||
5 years ago
|
return {
|
||
|
...acc,
|
||
|
[origin]: ethAccountsHistory,
|
||
4 years ago
|
};
|
||
|
}, {});
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getPermissionsMetadataHostCounts(state) {
|
||
4 years ago
|
const metadata = getPermissionDomainsMetadata(state);
|
||
5 years ago
|
return Object.values(metadata).reduce((counts, { host }) => {
|
||
|
if (host) {
|
||
4 years ago
|
if (counts[host]) {
|
||
4 years ago
|
counts[host] += 1;
|
||
4 years ago
|
} else {
|
||
4 years ago
|
counts[host] = 1;
|
||
5 years ago
|
}
|
||
|
}
|
||
4 years ago
|
return counts;
|
||
|
}, {});
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getPermissionsRequests(state) {
|
||
4 years ago
|
return state.metamask.permissionsRequests || [];
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getFirstPermissionRequest(state) {
|
||
4 years ago
|
const requests = getPermissionsRequests(state);
|
||
|
return requests && requests[0] ? requests[0] : null;
|
||
5 years ago
|
}
|