diff --git a/app/scripts/migrations/092.test.ts b/app/scripts/migrations/092.test.ts new file mode 100644 index 000000000..b44c04602 --- /dev/null +++ b/app/scripts/migrations/092.test.ts @@ -0,0 +1,78 @@ +import { cloneDeep } from 'lodash'; +import { migrate, version } from './092'; + +const PREVIOUS_VERSION = version - 1; + +describe('migration #92', () => { + it('should update the version metadata', async () => { + const oldStorage = { + meta: { + version: PREVIOUS_VERSION, + }, + data: {}, + }; + + const newStorage = await migrate(oldStorage); + expect(newStorage.meta).toStrictEqual({ + version, + }); + }); + + it('should return state unaltered if there is no phishing controller state', async () => { + const oldData = { + other: 'data', + }; + const oldStorage = { + meta: { + version: PREVIOUS_VERSION, + }, + data: oldData, + }; + + const newStorage = await migrate(cloneDeep(oldStorage)); + expect(newStorage.data).toStrictEqual(oldData); + }); + + it('should return state unaltered if there is no phishing controller last fetched state', async () => { + const oldData = { + other: 'data', + PhishingController: { + whitelist: [], + }, + }; + const oldStorage = { + meta: { + version: PREVIOUS_VERSION, + }, + data: oldData, + }; + + const newStorage = await migrate(cloneDeep(oldStorage)); + expect(newStorage.data).toStrictEqual(oldData); + }); + + it('should remove both last fetched properties from phishing controller state', async () => { + const oldData = { + other: 'data', + PhishingController: { + whitelist: [], + hotlistLastFetched: 0, + stalelistLastFetched: 0, + }, + }; + const oldStorage = { + meta: { + version: PREVIOUS_VERSION, + }, + data: oldData, + }; + + const newStorage = await migrate(oldStorage); + expect(newStorage.data).toStrictEqual({ + other: 'data', + PhishingController: { + whitelist: [], + }, + }); + }); +}); diff --git a/app/scripts/migrations/092.ts b/app/scripts/migrations/092.ts new file mode 100644 index 000000000..bf5469614 --- /dev/null +++ b/app/scripts/migrations/092.ts @@ -0,0 +1,35 @@ +import { cloneDeep } from 'lodash'; +import { hasProperty, isObject } from '@metamask/utils'; + +export const version = 92; + +/** + * Delete `stalelistLastFetched` and `hotlistLastFetched` to force a phishing configuration refresh + * because the format has changed. + * + * @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; +}) { + const versionedData = cloneDeep(originalVersionedData); + versionedData.meta.version = version; + versionedData.data = transformState(versionedData.data); + return versionedData; +} + +function transformState(state: Record) { + if ( + hasProperty(state, 'PhishingController') && + isObject(state.PhishingController) + ) { + delete state.PhishingController.stalelistLastFetched; + delete state.PhishingController.hotlistLastFetched; + } + return state; +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index a37648e78..dc4fcd4e0 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -95,6 +95,7 @@ import * as m088 from './088'; import * as m089 from './089'; import * as m090 from './090'; import * as m091 from './091'; +import * as m092 from './092'; const migrations = [ m002, @@ -187,6 +188,7 @@ const migrations = [ m089, m090, m091, + m092, ]; export default migrations;