1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Migration 89: ensure providerConfig in state has an id property (#20181)

* Migration 89: ensure providerConfig in state has an id property

* Exit transformState function early if providerConfig already has an id

* Update migrations/index.js

* Code cleanup
This commit is contained in:
Dan J Miller 2023-07-25 21:17:57 -02:30 committed by GitHub
parent d6cb724323
commit 4927ee735a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 297 additions and 0 deletions

View File

@ -0,0 +1,224 @@
import { migrate, version } from './089';
jest.mock('uuid', () => {
const actual = jest.requireActual('uuid');
return {
...actual,
v4: jest.fn(),
};
});
describe('migration #89', () => {
it('should update the version metadata', async () => {
const oldStorage = {
meta: {
version: 88,
},
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: 88,
},
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: 88,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if the providerConfig already has an id', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
},
},
providerConfig: {
id: 'test',
},
},
};
const oldStorage = {
meta: {
version: 88,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if there is no network config with the same rpcUrl and the providerConfig', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
rpcUrl: 'http://foo.bar',
},
},
providerConfig: {
rpcUrl: 'http://baz.buzz',
},
},
};
const oldStorage = {
meta: {
version: 88,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should update the provider config to have the id of a network config with the same rpcUrl', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
rpcUrl: 'http://foo.bar',
id: 'test',
},
},
providerConfig: {
rpcUrl: 'http://foo.bar',
},
},
};
const oldStorage = {
meta: {
version: 88,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
rpcUrl: 'http://foo.bar',
id: 'test',
},
},
providerConfig: {
rpcUrl: 'http://foo.bar',
id: 'test',
},
},
});
});
it('should update the provider config to have the id of a network config with the same rpcUrl, even if there are other networks with the same chainId', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
rpcUrl: 'http://fizz.buzz',
id: 'FAILEDtest',
chainId: 1,
},
id2: {
foo: 'bar',
rpcUrl: 'http://foo.bar',
id: 'PASSEDtest',
},
id3: {
foo: 'bar',
rpcUrl: 'http://baz.buzz',
id: 'FAILEDtest',
chainId: 1,
},
},
providerConfig: {
rpcUrl: 'http://foo.bar',
chainId: 1,
},
},
};
const oldStorage = {
meta: {
version: 88,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
rpcUrl: 'http://fizz.buzz',
id: 'FAILEDtest',
chainId: 1,
},
id2: {
foo: 'bar',
rpcUrl: 'http://foo.bar',
id: 'PASSEDtest',
},
id3: {
foo: 'bar',
rpcUrl: 'http://baz.buzz',
id: 'FAILEDtest',
chainId: 1,
},
},
providerConfig: {
rpcUrl: 'http://foo.bar',
id: 'PASSEDtest',
chainId: 1,
},
},
});
});
});

View File

@ -0,0 +1,71 @@
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';
export const version = 89;
/**
* Add an `id` to the `providerConfig` object.
*
* @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 { networkConfigurations, providerConfig } = state.NetworkController;
if (!isObject(networkConfigurations)) {
return state;
}
if (providerConfig.id) {
return state;
}
let newProviderConfigId;
for (const networkConfigurationId of Object.keys(networkConfigurations)) {
const networkConfiguration =
networkConfigurations[networkConfigurationId];
if (!isObject(networkConfiguration)) {
return state;
}
if (networkConfiguration.rpcUrl === providerConfig.rpcUrl) {
newProviderConfigId = networkConfiguration.id;
break;
}
}
if (!newProviderConfigId) {
return state;
}
state.NetworkController.providerConfig = {
...providerConfig,
id: newProviderConfigId,
};
return {
...state,
NetworkController: state.NetworkController,
};
}
return state;
}

View File

@ -92,6 +92,7 @@ import * as m085 from './085';
import * as m086 from './086';
import * as m087 from './087';
import * as m088 from './088';
import * as m089 from './089';
const migrations = [
m002,
@ -181,6 +182,7 @@ const migrations = [
m086,
m087,
m088,
m089,
];
export default migrations;