1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 16:18:07 +01:00
metamask-extension/ui/app/selectors/permissions.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

import { createSelector } from 'reselect'
import {
CAVEAT_NAMES,
} from '../../../app/scripts/controllers/permissions/enums'
const permissionsSelector = (state, origin) => {
return origin && state.metamask.domains && state.metamask.domains[origin]
}
// all permissions for the origin probably too expensive for deep equality check
const accountsPermissionSelector = createSelector(
permissionsSelector,
(domain = {}) => {
return (
Array.isArray(domain.permissions)
? domain.permissions.find(
perm => perm.parentCapability === 'eth_accounts'
)
: {}
)
}
)
/**
* Selects the permitted accounts from an eth_accounts permission.
* Expects input from accountsPermissionsSelector.
* @returns - An empty array or an array of accounts.
*/
export const getPermittedAccounts = createSelector(
accountsPermissionSelector, // deep equal check performed on this output
(accountsPermission = {}) => {
const accountsCaveat = (
Array.isArray(accountsPermission.caveats) &&
accountsPermission.caveats.find(
c => c.name === CAVEAT_NAMES.exposedAccounts
)
)
return (
accountsCaveat && Array.isArray(accountsCaveat.value)
? accountsCaveat.value
: []
)
}
)