From 4d7689b2dfd24dcfc5d94f26b10324108da7f779 Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Mon, 10 Jan 2022 09:07:26 -0800 Subject: [PATCH] Handle missing eth_accounts permission history in getLastConnectedInfo (#13257) This PR ensures that the `getLastConnectedInfo` selector handles missing `eth_accounts` permission history. Historically, `eth_accounts` was the only permission in existence, and the only way that a permission subject ended up with a permission history in the first place. This will no longer the case as of #11837, and we were perhaps never right to bake in this assumption to begin with. --- ui/selectors/permissions.js | 23 +++++++++++++++-------- ui/selectors/permissions.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/ui/selectors/permissions.js b/ui/selectors/permissions.js index e5f622a06..09d845d5c 100644 --- a/ui/selectors/permissions.js +++ b/ui/selectors/permissions.js @@ -238,16 +238,23 @@ export function activeTabHasPermissions(state) { ); } +/** + * Get the connected accounts history for all origins. + * + * @param {Record} state - The MetaMask state. + * @returns {Record }>} An object + * with account connection histories by origin. + */ export function getLastConnectedInfo(state) { const { permissionHistory = {} } = state.metamask; - return Object.keys(permissionHistory).reduce((acc, origin) => { - const ethAccountsHistory = JSON.parse( - JSON.stringify(permissionHistory[origin].eth_accounts), - ); - return { - ...acc, - [origin]: ethAccountsHistory, - }; + return Object.keys(permissionHistory).reduce((lastConnectedInfo, origin) => { + if (permissionHistory[origin].eth_accounts) { + lastConnectedInfo[origin] = JSON.parse( + JSON.stringify(permissionHistory[origin].eth_accounts), + ); + } + + return lastConnectedInfo; }, {}); } diff --git a/ui/selectors/permissions.test.js b/ui/selectors/permissions.test.js index 8b3ae6f33..0b439b85c 100644 --- a/ui/selectors/permissions.test.js +++ b/ui/selectors/permissions.test.js @@ -1,6 +1,7 @@ import { KOVAN_CHAIN_ID } from '../../shared/constants/network'; import { getConnectedSubjectsForSelectedAddress, + getLastConnectedInfo, getOrderedConnectedAccountsForActiveTab, getPermissionsForActiveTab, } from './permissions'; @@ -296,6 +297,33 @@ describe('selectors', () => { }); }); + describe('getLastConnectedInfo', () => { + it('retrieves the last connected info', () => { + const mockState = { + metamask: { + permissionHistory: { + a: { + foo: {}, + eth_accounts: { accounts: { 0x1: 1, 0x2: 2 } }, + }, + b: { + foo: {}, + eth_accounts: { accounts: { 0x2: 2 } }, + }, + c: { + foo: {}, + }, + }, + }, + }; + + expect(getLastConnectedInfo(mockState)).toStrictEqual({ + a: { accounts: { 0x1: 1, 0x2: 2 } }, + b: { accounts: { 0x2: 2 } }, + }); + }); + }); + describe('getPermissionsForActiveTab', () => { const mockState = { activeTab: {