2020-05-02 21:41:17 +02:00
|
|
|
import { forOwn } from 'lodash'
|
2020-01-27 23:42:03 +01:00
|
|
|
import {
|
|
|
|
CAVEAT_NAMES,
|
|
|
|
} from '../../../app/scripts/controllers/permissions/enums'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { getMetaMaskAccountsOrdered, getOriginOfCurrentTab, getSelectedAddress } from '.'
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-02-25 23:39:38 +01:00
|
|
|
// selectors
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-05-06 06:04:05 +02:00
|
|
|
/**
|
|
|
|
* Get the permission domains object.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The current state.
|
|
|
|
* @returns {Object} The permissions domains object.
|
|
|
|
*/
|
|
|
|
export function getPermissionDomains (state) {
|
|
|
|
return state.metamask.domains || {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the permission domains metadata object.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The current state.
|
|
|
|
* @returns {Object} The permission domains metadata object.
|
|
|
|
*/
|
|
|
|
export function getPermissionDomainsMetadata (state) {
|
|
|
|
return state.metamask.domainMetadata || {}
|
|
|
|
}
|
|
|
|
|
2020-02-25 23:39:38 +01:00
|
|
|
/**
|
|
|
|
* Selects the permitted accounts from the eth_accounts permission given state
|
|
|
|
* and an origin.
|
2020-05-06 06:04:05 +02:00
|
|
|
*
|
2020-02-25 23:39:38 +01:00
|
|
|
* @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(
|
2020-07-14 17:20:41 +02:00
|
|
|
domainSelector(state, origin),
|
|
|
|
),
|
2020-02-25 23:39:38 +01:00
|
|
|
)
|
|
|
|
}
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-05-06 06:04:05 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
export function getPermittedAccountsForCurrentTab (state) {
|
|
|
|
return getPermittedAccounts(
|
|
|
|
state,
|
2020-07-14 17:20:41 +02:00
|
|
|
getOriginOfCurrentTab(state),
|
2020-05-06 06:04:05 +02: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.
|
2020-05-06 06:04:05 +02:00
|
|
|
*
|
2020-02-25 23:39:38 +01:00
|
|
|
* @param {Object} state - The current state.
|
|
|
|
* @returns {Object} Permitted accounts by origin.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-05-02 01:48:32 +02:00
|
|
|
export function getPermittedAccountsByOrigin (state) {
|
2020-05-02 21:41:17 +02:00
|
|
|
const domains = getPermissionDomains(state)
|
2020-05-02 01:48:32 +02:00
|
|
|
return Object.keys(domains).reduce((acc, domainKey) => {
|
|
|
|
const accounts = getAccountsFromPermission(
|
2020-07-14 17:20:41 +02:00
|
|
|
getAccountsPermissionFromDomain(domains[domainKey]),
|
2020-05-02 01:48:32 +02:00
|
|
|
)
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
acc[domainKey] = accounts
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
}
|
2020-02-25 23:39:38 +01:00
|
|
|
|
2020-05-06 06:04:05 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-05-02 21:41:17 +02:00
|
|
|
export function getConnectedDomainsForSelectedAddress (state) {
|
|
|
|
const {
|
|
|
|
selectedAddress,
|
|
|
|
} = state.metamask
|
|
|
|
const domains = getPermissionDomains(state)
|
|
|
|
const domainMetadata = getPermissionDomainsMetadata(state)
|
|
|
|
|
|
|
|
const connectedDomains = []
|
|
|
|
|
|
|
|
forOwn(domains, (domainValue, domainKey) => {
|
|
|
|
const exposedAccounts = getAccountsFromDomain(domainValue)
|
|
|
|
if (!exposedAccounts.includes(selectedAddress)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
extensionId,
|
|
|
|
name,
|
|
|
|
icon,
|
2020-06-09 22:56:24 +02:00
|
|
|
host,
|
2020-05-02 21:41:17 +02:00
|
|
|
} = domainMetadata[domainKey] || {}
|
|
|
|
|
|
|
|
connectedDomains.push({
|
|
|
|
extensionId,
|
2020-06-09 22:56:24 +02:00
|
|
|
origin: domainKey,
|
2020-05-02 21:41:17 +02:00
|
|
|
name,
|
|
|
|
icon,
|
2020-06-09 22:56:24 +02:00
|
|
|
host,
|
2020-05-02 21:41:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return connectedDomains
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:04:05 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-05-02 21:41:17 +02:00
|
|
|
export function getAddressConnectedDomainMap (state) {
|
|
|
|
const domainMetadata = getPermissionDomainsMetadata(state)
|
|
|
|
const accountsMap = getPermittedAccountsByOrigin(state)
|
|
|
|
const addressConnectedIconMap = {}
|
|
|
|
|
|
|
|
Object.keys(accountsMap).forEach((domainKey) => {
|
|
|
|
|
|
|
|
const { icon, name } = domainMetadata[domainKey] || {}
|
|
|
|
|
|
|
|
accountsMap[domainKey].forEach((address) => {
|
|
|
|
|
|
|
|
const nameToRender = name || domainKey
|
|
|
|
|
|
|
|
addressConnectedIconMap[address] = addressConnectedIconMap[address]
|
|
|
|
? { ...addressConnectedIconMap[address], [domainKey]: { icon, name: nameToRender } }
|
|
|
|
: { [domainKey]: { icon, name: nameToRender } }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return addressConnectedIconMap
|
|
|
|
}
|
|
|
|
|
2020-02-25 23:39:38 +01:00
|
|
|
// selector helpers
|
|
|
|
|
2020-05-02 21:41:17 +02:00
|
|
|
function getAccountsFromDomain (domain) {
|
|
|
|
return getAccountsFromPermission(
|
2020-07-14 17:20:41 +02:00
|
|
|
getAccountsPermissionFromDomain(domain),
|
2020-05-02 21:41:17 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-25 23:39:38 +01:00
|
|
|
function getAccountsPermissionFromDomain (domain = {}) {
|
|
|
|
return (
|
|
|
|
Array.isArray(domain.permissions)
|
|
|
|
? domain.permissions.find(
|
2020-07-14 17:20:41 +02:00
|
|
|
(perm) => perm.parentCapability === 'eth_accounts',
|
2020-02-25 23:39:38 +01:00
|
|
|
)
|
|
|
|
: {}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2020-07-14 17:20:41 +02:00
|
|
|
(c) => c.name === CAVEAT_NAMES.exposedAccounts,
|
2020-02-25 23:39:38 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function domainSelector (state, origin) {
|
2020-05-12 18:29:45 +02:00
|
|
|
return origin && state.metamask.domains?.[origin]
|
2020-02-25 23:39:38 +01:00
|
|
|
}
|
2020-05-15 20:53:52 +02:00
|
|
|
|
|
|
|
export function getAccountToConnectToActiveTab (state) {
|
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
const connectedAccounts = getPermittedAccountsForCurrentTab(state)
|
|
|
|
|
|
|
|
const { metamask: { identities } } = state
|
|
|
|
const numberOfAccounts = Object.keys(identities).length
|
|
|
|
|
|
|
|
if (connectedAccounts.length && connectedAccounts.length !== numberOfAccounts) {
|
|
|
|
if (connectedAccounts.findIndex((address) => address === selectedAddress) === -1) {
|
|
|
|
return identities[selectedAddress]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOrderedConnectedAccountsForActiveTab (state) {
|
2020-05-18 18:31:11 +02:00
|
|
|
const { activeTab, metamask: { permissionsHistory } } = state
|
2020-05-15 20:53:52 +02:00
|
|
|
|
2020-10-22 19:07:02 +02:00
|
|
|
const permissionsHistoryByAccount = permissionsHistory[activeTab.origin]?.eth_accounts?.accounts
|
2020-05-18 18:31:11 +02:00
|
|
|
const orderedAccounts = getMetaMaskAccountsOrdered(state)
|
|
|
|
const connectedAccounts = getPermittedAccountsForCurrentTab(state)
|
2020-05-15 20:53:52 +02:00
|
|
|
|
2020-05-18 18:31:11 +02:00
|
|
|
return orderedAccounts
|
|
|
|
.filter((account) => connectedAccounts.includes(account.address))
|
|
|
|
.map((account) => ({
|
|
|
|
...account,
|
2020-06-05 18:07:23 +02:00
|
|
|
lastActive: permissionsHistoryByAccount?.[account.address],
|
2020-05-15 20:53:52 +02:00
|
|
|
}))
|
2020-05-18 18:31:11 +02:00
|
|
|
.sort(({ lastSelected: lastSelectedA }, { lastSelected: lastSelectedB }) => {
|
2020-05-15 20:53:52 +02:00
|
|
|
if (lastSelectedA === lastSelectedB) {
|
|
|
|
return 0
|
|
|
|
} else if (lastSelectedA === undefined) {
|
|
|
|
return 1
|
|
|
|
} else if (lastSelectedB === undefined) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastSelectedB - lastSelectedA
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPermissionsForActiveTab (state) {
|
|
|
|
const { activeTab, metamask } = state
|
|
|
|
const {
|
|
|
|
domains = {},
|
|
|
|
} = metamask
|
|
|
|
|
|
|
|
return domains[activeTab.origin]?.permissions?.map(({ parentCapability }) => {
|
|
|
|
return {
|
|
|
|
key: parentCapability,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-06-05 22:24:38 +02:00
|
|
|
|
|
|
|
export function getLastConnectedInfo (state) {
|
|
|
|
const { permissionsHistory = {} } = state.metamask
|
|
|
|
return Object.keys(permissionsHistory).reduce((acc, origin) => {
|
2020-08-19 18:27:05 +02:00
|
|
|
const ethAccountsHistory = JSON.parse(JSON.stringify(permissionsHistory[origin].eth_accounts))
|
2020-06-05 22:24:38 +02:00
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[origin]: ethAccountsHistory,
|
|
|
|
}
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:56:24 +02:00
|
|
|
export function getPermissionsMetadataHostCounts (state) {
|
|
|
|
const metadata = getPermissionDomainsMetadata(state)
|
|
|
|
return Object.values(metadata).reduce((counts, { host }) => {
|
|
|
|
if (host) {
|
2020-08-14 13:47:43 +02:00
|
|
|
if (counts[host]) {
|
2020-06-09 22:56:24 +02:00
|
|
|
counts[host] += 1
|
2020-08-14 13:47:43 +02:00
|
|
|
} else {
|
|
|
|
counts[host] = 1
|
2020-06-09 22:56:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return counts
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:24:38 +02:00
|
|
|
export function getPermissionsRequests (state) {
|
|
|
|
return state.metamask.permissionsRequests || []
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPermissionsRequestCount (state) {
|
|
|
|
const permissionsRequests = getPermissionsRequests(state)
|
|
|
|
return permissionsRequests.length
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFirstPermissionRequest (state) {
|
|
|
|
const requests = getPermissionsRequests(state)
|
|
|
|
return requests && requests[0] ? requests[0] : null
|
|
|
|
}
|