2023-04-11 21:16:31 +02:00
|
|
|
import { cloneDeep } from 'lodash';
|
2023-04-19 16:08:16 +02:00
|
|
|
import { hasProperty, isObject } from '@metamask/utils';
|
2023-04-11 21:16:31 +02:00
|
|
|
|
|
|
|
export const version = 84;
|
|
|
|
|
|
|
|
/**
|
2023-04-19 16:08:16 +02:00
|
|
|
* The `network` property in state was replaced with `networkId` and `networkStatus`.
|
2023-04-11 21:16:31 +02:00
|
|
|
*
|
|
|
|
* @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>) {
|
2023-04-19 16:08:16 +02:00
|
|
|
if (
|
|
|
|
!hasProperty(state, 'NetworkController') ||
|
|
|
|
!isObject(state.NetworkController) ||
|
|
|
|
!hasProperty(state.NetworkController, 'network')
|
|
|
|
) {
|
2023-04-11 21:16:31 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:08:16 +02:00
|
|
|
const NetworkController = { ...state.NetworkController };
|
2023-04-11 21:16:31 +02:00
|
|
|
|
2023-04-19 16:08:16 +02:00
|
|
|
if (NetworkController.network === 'loading') {
|
|
|
|
NetworkController.networkId = null;
|
|
|
|
NetworkController.networkStatus = 'unknown';
|
|
|
|
} else {
|
|
|
|
NetworkController.networkId = NetworkController.network;
|
|
|
|
NetworkController.networkStatus = 'available';
|
2023-04-11 21:16:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-19 16:08:16 +02:00
|
|
|
delete NetworkController.network;
|
|
|
|
|
|
|
|
return { ...state, NetworkController };
|
2023-04-11 21:16:31 +02:00
|
|
|
}
|