1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Make permissions selectors less bad (#8529)

* make getPermittedAccountsForCurrentTab more efficient

* add missing docstrings
This commit is contained in:
Erik Marks 2020-05-05 21:04:05 -07:00 committed by GitHub
parent 8b308884b7
commit 351424df7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,9 +6,30 @@ import {
// selectors
/**
* 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 || {}
}
/**
* 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.
@ -21,8 +42,23 @@ export function getPermittedAccounts (state, origin) {
)
}
/**
* 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,
getOriginOfCurrentTab(state)
)
}
/**
* Returns a map of permitted accounts by origin for all origins.
*
* @param {Object} state - The current state.
* @returns {Object} Permitted accounts by origin.
*/
@ -39,6 +75,16 @@ export function getPermittedAccountsByOrigin (state) {
}, {})
}
/**
* 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.
*/
export function getConnectedDomainsForSelectedAddress (state) {
const {
selectedAddress,
@ -71,15 +117,18 @@ export function getConnectedDomainsForSelectedAddress (state) {
return connectedDomains
}
export function getPermittedAccountsForCurrentTab (state) {
const permittedAccountsMap = getPermittedAccountsByOrigin(state)
const originOfCurrentTab = getOriginOfCurrentTab(state)
return permittedAccountsMap[originOfCurrentTab] || []
}
/**
* 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.
*/
export function getAddressConnectedDomainMap (state) {
const domainMetadata = getPermissionDomainsMetadata(state)
const accountsMap = getPermittedAccountsByOrigin(state)
const addressConnectedIconMap = {}
@ -136,14 +185,6 @@ function getAccountsCaveatFromPermission (accountsPermission = {}) {
)
}
export function getPermissionDomains (state) {
return state.metamask.domains || {}
}
export function getPermissionDomainsMetadata (state) {
return state.metamask.domainMetadata || {}
}
function domainSelector (state, origin) {
return origin && state.metamask?.domains[origin]
}