2020-01-27 23:42:03 +01:00
|
|
|
import { createSelector } from 'reselect'
|
2020-02-25 23:39:38 +01:00
|
|
|
|
2020-01-27 23:42:03 +01:00
|
|
|
import {
|
|
|
|
CAVEAT_NAMES,
|
|
|
|
} from '../../../app/scripts/controllers/permissions/enums'
|
|
|
|
|
2020-02-25 23:39:38 +01:00
|
|
|
// selectors
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-02-25 23:39:38 +01:00
|
|
|
/**
|
|
|
|
* Selects the permitted accounts from the eth_accounts permission given state
|
|
|
|
* and an origin.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
export function getPermittedAccounts (state, origin) {
|
|
|
|
return getAccountsFromPermission(
|
|
|
|
getAccountsPermissionFromDomain(
|
|
|
|
domainSelector(state, origin)
|
2020-01-27 23:42:03 +01:00
|
|
|
)
|
2020-02-25 23:39:38 +01:00
|
|
|
)
|
|
|
|
}
|
2020-01-27 23:42:03 +01:00
|
|
|
|
|
|
|
/**
|
2020-02-25 23:39:38 +01:00
|
|
|
* Returns a map of permitted accounts by origin for all origins.
|
|
|
|
* @param {Object} state - The current state.
|
|
|
|
* @returns {Object} Permitted accounts by origin.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-02-25 23:39:38 +01:00
|
|
|
export const getPermittedAccountsMap = createSelector(
|
|
|
|
allDomainsSelector,
|
|
|
|
(domains = {}) => {
|
|
|
|
return Object.keys(domains).reduce((acc, domainKey) => {
|
|
|
|
const accounts = getAccountsFromPermission(
|
|
|
|
getAccountsPermissionFromDomain(domains[domainKey])
|
2020-01-27 23:42:03 +01:00
|
|
|
)
|
2020-02-25 23:39:38 +01:00
|
|
|
if (accounts.length > 0) {
|
|
|
|
acc[domainKey] = accounts
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {})
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
|
|
|
)
|
2020-02-25 23:39:38 +01:00
|
|
|
|
|
|
|
// selector helpers
|
|
|
|
|
|
|
|
function getAccountsPermissionFromDomain (domain = {}) {
|
|
|
|
return (
|
|
|
|
Array.isArray(domain.permissions)
|
|
|
|
? domain.permissions.find(
|
|
|
|
(perm) => perm.parentCapability === 'eth_accounts'
|
|
|
|
)
|
|
|
|
: {}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAccountsFromPermission (accountsPermission) {
|
|
|
|
const accountsCaveat = getAccountsCaveatFromPermission(accountsPermission)
|
|
|
|
return (
|
|
|
|
accountsCaveat && Array.isArray(accountsCaveat.value)
|
|
|
|
? accountsCaveat.value
|
|
|
|
: []
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAccountsCaveatFromPermission (accountsPermission = {}) {
|
|
|
|
return (
|
|
|
|
Array.isArray(accountsPermission.caveats) &&
|
|
|
|
accountsPermission.caveats.find(
|
|
|
|
(c) => c.name === CAVEAT_NAMES.exposedAccounts
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function allDomainsSelector (state) {
|
|
|
|
return state.metamask.domains
|
|
|
|
}
|
|
|
|
|
|
|
|
function domainSelector (state, origin) {
|
|
|
|
return origin && state.metamask.domains && state.metamask.domains[origin]
|
|
|
|
}
|