From 351424df7d608f4a8caa9ab8eae4c4bbc8cf301e Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Tue, 5 May 2020 21:04:05 -0700 Subject: [PATCH] Make permissions selectors less bad (#8529) * make getPermittedAccountsForCurrentTab more efficient * add missing docstrings --- ui/app/selectors/permissions.js | 71 ++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/ui/app/selectors/permissions.js b/ui/app/selectors/permissions.js index e7800d925..e707058ab 100644 --- a/ui/app/selectors/permissions.js +++ b/ui/app/selectors/permissions.js @@ -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} 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} 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} 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] }