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.
feature/default_network_editable
Erik Marks 3 years ago committed by GitHub
parent 0aad0d55a4
commit 4d7689b2df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      ui/selectors/permissions.js
  2. 28
      ui/selectors/permissions.test.js

@ -238,16 +238,23 @@ export function activeTabHasPermissions(state) {
);
}
/**
* Get the connected accounts history for all origins.
*
* @param {Record<string, unknown>} state - The MetaMask state.
* @returns {Record<string, { accounts: Record<string, number> }>} 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;
}, {});
}

@ -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: {

Loading…
Cancel
Save