1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 20:02:58 +01:00
metamask-extension/app/scripts/migrations/091.ts
Dan J Miller b825ee8e37
Fix migration 88 to handle the case where chainId keys can be undefined (#20345)
* Fix migration 88 to handle the case where chainId keys can be undefined

* Add migration 91 to delete network configurations that have no chainId

* Lint fix

* Update migration number

* Update migration 91 description

* Update version numbers in 091.test.js

* Fix 088.test.ts typescript problem

* Fix 088.test.ts typescript problem

* Update app/scripts/migrations/091.ts

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Change app/scripts/migrations/091.test.js to typescript

* clone oldstorage for test result comparisons in 091.test.js

* Lint fix

* Add missing test case

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2023-08-01 20:24:02 -02:30

56 lines
1.7 KiB
TypeScript

import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';
export const version = 91;
/**
* Delete network configurations if they do not have a chain id
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
* @param originalVersionedData.meta - State metadata.
* @param originalVersionedData.meta.version - The current state version.
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(originalVersionedData: {
meta: { version: number };
data: Record<string, unknown>;
}) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
versionedData.data = transformState(versionedData.data);
return versionedData;
}
function transformState(state: Record<string, unknown>) {
if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController) &&
hasProperty(state.NetworkController, 'networkConfigurations') &&
isObject(state.NetworkController.networkConfigurations)
) {
const { networkConfigurations } = state.NetworkController;
for (const [networkConfigurationId, networkConfiguration] of Object.entries(
networkConfigurations,
)) {
if (isObject(networkConfiguration)) {
if (!networkConfiguration.chainId) {
delete networkConfigurations[networkConfigurationId];
}
}
}
state.NetworkController = {
...state.NetworkController,
networkConfigurations,
};
return {
...state,
NetworkController: state.NetworkController,
};
}
return state;
}