Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force an update of the phishing warning configuration #20381

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 20 additions & 53 deletions app/scripts/migrations/092.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { InfuraNetworkType, NetworkType } from '@metamask/controller-utils';
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: 91,
version: PREVIOUS_VERSION,
},
data: {},
};
Expand All @@ -16,95 +18,60 @@ describe('migration #92', () => {
});
});

it('should return state unaltered if there is no network controller state', async () => {
it('should return state unaltered if there is no phishing controller state', async () => {
const oldData = {
other: 'data',
};
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});

it('should return state unaltered if there is no network controller providerConfig state', async () => {
it('should return state unaltered if there is no phishing controller last fetched state', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
},
},
PhishingController: {
whitelist: [],
},
};
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});

it('should return state unaltered if there is already a ticker in the providerConfig state', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
ticker: 'GoerliETH',
type: InfuraNetworkType.goerli,
chainId: '5',
nickname: 'Goerli Testnet',
id: 'goerli',
},
},
};
const oldStorage = {
meta: {
version: 91,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});

it('should update the provider config to have a ticker set to "ETH" if none is currently present', async () => {
it('should remove both last fetched properties from phishing controller state', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
},
PhishingController: {
whitelist: [],
hotlistLastFetched: 0,
stalelistLastFetched: 0,
},
};
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
ticker: 'ETH',
},
PhishingController: {
whitelist: [],
},
});
});
Expand Down
26 changes: 6 additions & 20 deletions app/scripts/migrations/092.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { hasProperty, isObject } from '@metamask/utils';
export const version = 92;

/**
* Add ticker to the providerConfig object if missing
* 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.
Expand All @@ -24,26 +25,11 @@ export async function migrate(originalVersionedData: {

function transformState(state: Record<string, unknown>) {
if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController) &&
hasProperty(state.NetworkController, 'providerConfig') &&
isObject(state.NetworkController.providerConfig)
hasProperty(state, 'PhishingController') &&
isObject(state.PhishingController)
) {
const { providerConfig } = state.NetworkController;

if (providerConfig.ticker) {
return state;
}

state.NetworkController.providerConfig = {
ticker: 'ETH',
...providerConfig,
};

return {
...state,
NetworkController: state.NetworkController,
};
delete state.PhishingController.stalelistLastFetched;
delete state.PhishingController.hotlistLastFetched;
}
return state;
}
113 changes: 113 additions & 0 deletions app/scripts/migrations/093.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { InfuraNetworkType, NetworkType } from '@metamask/controller-utils';
import { migrate, version } from './093';

const PREVIOUS_VERSION = version - 1;

describe('migration #93', () => {
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 network controller state', async () => {
const oldData = {
other: 'data',
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});

it('should return state unaltered if there is no network controller providerConfig state', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
},
},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});

it('should return state unaltered if there is already a ticker in the providerConfig state', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
ticker: 'GoerliETH',
type: InfuraNetworkType.goerli,
chainId: '5',
nickname: 'Goerli Testnet',
id: 'goerli',
},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});

it('should update the provider config to have a ticker set to "ETH" if none is currently present', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
ticker: 'ETH',
},
},
});
});
});
49 changes: 49 additions & 0 deletions app/scripts/migrations/093.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { cloneDeep } from 'lodash';
import { hasProperty, isObject } from '@metamask/utils';

export const version = 93;

/**
* Add ticker to the providerConfig object if missing
*
* @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, 'providerConfig') &&
isObject(state.NetworkController.providerConfig)
) {
const { providerConfig } = state.NetworkController;

if (providerConfig.ticker) {
return state;
}

state.NetworkController.providerConfig = {
ticker: 'ETH',
...providerConfig,
};

return {
...state,
NetworkController: state.NetworkController,
};
}
return state;
}
2 changes: 2 additions & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import * as m089 from './089';
import * as m090 from './090';
import * as m091 from './091';
import * as m092 from './092';
import * as m093 from './093';

const migrations = [
m002,
Expand Down Expand Up @@ -189,5 +190,6 @@ const migrations = [
m090,
m091,
m092,
m093,
];
export default migrations;
Loading