mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-12 20:57:12 +01:00
51cffa15dd
* Migrate to new controller packages `@metamask/controllers` is deprecated, and most of the controllers that lived here are now located in their own package ([1]). This commit replaces `@metamask/controllers` in `package.json` with references to these packages and updates `import` lines to match. [1]: https://github.com/MetaMask/controllers/pull/831 * Support GitHub registry for draft PRs (#16549) * Add additional allowed host to lockfile linter * Update LavaMoat policies * Add policy exception for nanoid * Add additional nanoid overrides * Update LavaMoat policies again * Bump controller packages * Update lavamoat * Bump controller packages * Update packages to v1.0.0 * Expand gitignore comment * Unpin controller dependencies, using ^ range instead Co-authored-by: Mark Stacey <markjstacey@gmail.com>
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
import { cloneDeep } from 'lodash';
|
|
import { normalizeEnsName } from '@metamask/controller-utils';
|
|
|
|
const version = 37;
|
|
|
|
/**
|
|
* The purpose of this migration is to update the address book state
|
|
* to the new schema with chainId as a key.
|
|
* and to add the isEns flag to all entries
|
|
*/
|
|
export default {
|
|
version,
|
|
async migrate(originalVersionedData) {
|
|
const versionedData = cloneDeep(originalVersionedData);
|
|
versionedData.meta.version = version;
|
|
const state = versionedData.data;
|
|
versionedData.data = transformState(state);
|
|
return versionedData;
|
|
},
|
|
};
|
|
|
|
function transformState(state) {
|
|
if (state.AddressBookController) {
|
|
const ab = state.AddressBookController.addressBook;
|
|
|
|
const chainIds = new Set();
|
|
const newAddressBook = {};
|
|
|
|
// add all of the chainIds to a set
|
|
Object.values(ab).forEach((v) => {
|
|
chainIds.add(v.chainId);
|
|
});
|
|
|
|
// fill the chainId object with the entries with the matching chainId
|
|
for (const id of chainIds.values()) {
|
|
// make an empty object entry for each chainId
|
|
newAddressBook[id] = {};
|
|
for (const address in ab) {
|
|
if (ab[address].chainId === id) {
|
|
ab[address].isEns = false;
|
|
if (normalizeEnsName(ab[address].name)) {
|
|
ab[address].isEns = true;
|
|
}
|
|
newAddressBook[id][address] = ab[address];
|
|
}
|
|
}
|
|
}
|
|
|
|
state.AddressBookController.addressBook = newAddressBook;
|
|
}
|
|
|
|
return state;
|
|
}
|