1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

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.
This commit is contained in:
Erik Marks 2022-01-10 09:07:26 -08:00 committed by GitHub
parent 0aad0d55a4
commit 4d7689b2df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 8 deletions

View File

@ -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;
}, {});
}

View File

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