1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/app/scripts/migrations/069.js
Erik Marks fc1ffae406
Add subject types (#13026)
This PR introduces the concept of subject _types_ to be associated with each subject in the `SubjectMetadataController`, and used for control flow in our RPC stack (`setupProviderEngine` and so forth).

We already differentiate between "types" of subjects in various places on an ad hoc basis via boolean flags (e.g. `isInternal` in our RPC stack) or the presence/absence of certain values in the subject's metadata (specifically `metadata.extensionId`). The status quo is manageable if not ideal, but will start to become untenable with the introduction of Snaps in the near future.

Therefore, this PR establishes a `SUBJECT_TYPES` enum and adds the `subjectType` property to the metadata of each subject. A new migration is added to accomplish this. Finally, we specify and `INTERNAL` subject type to distinguish internal from external requests.
2021-12-08 15:37:29 -08:00

42 lines
1.1 KiB
JavaScript

import { cloneDeep } from 'lodash';
import { SUBJECT_TYPES } from '../../../shared/constants/app';
const version = 69;
/**
* Adds the `subjectType` property to all subject metadata.
*/
export default {
version,
async migrate(originalVersionedData) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
const state = versionedData.data;
const newState = transformState(state);
versionedData.data = newState;
return versionedData;
},
};
function transformState(state) {
if (typeof state?.SubjectMetadataController?.subjectMetadata === 'object') {
const {
SubjectMetadataController: { subjectMetadata },
} = state;
// mutate SubjectMetadataController.subjectMetadata in place
Object.values(subjectMetadata).forEach((metadata) => {
if (
metadata &&
typeof metadata === 'object' &&
!Array.isArray(metadata)
) {
metadata.subjectType = metadata.extensionId
? SUBJECT_TYPES.EXTENSION
: SUBJECT_TYPES.WEBSITE;
}
});
}
return state;
}