2021-02-04 19:15:23 +01:00
|
|
|
import { cloneDeep } from 'lodash';
|
|
|
|
import { util } from '@metamask/controllers';
|
2019-09-21 19:36:06 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const version = 37;
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2019-09-21 19:36:06 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export default {
|
2019-09-21 19:36:06 +02:00
|
|
|
version,
|
2020-11-03 00:41:28 +01:00
|
|
|
async migrate(originalVersionedData) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const versionedData = cloneDeep(originalVersionedData);
|
|
|
|
versionedData.meta.version = version;
|
|
|
|
const state = versionedData.data;
|
|
|
|
versionedData.data = transformState(state);
|
|
|
|
return versionedData;
|
2019-09-21 19:36:06 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-21 19:36:06 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function transformState(state) {
|
2019-09-21 19:36:06 +02:00
|
|
|
if (state.AddressBookController) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const ab = state.AddressBookController.addressBook;
|
2019-09-21 19:36:06 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const chainIds = new Set();
|
|
|
|
const newAddressBook = {};
|
2019-09-21 19:36:06 +02:00
|
|
|
|
|
|
|
// add all of the chainIds to a set
|
2020-07-21 23:10:45 +02:00
|
|
|
Object.values(ab).forEach((v) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
chainIds.add(v.chainId);
|
|
|
|
});
|
2019-09-21 19:36:06 +02:00
|
|
|
|
|
|
|
// fill the chainId object with the entries with the matching chainId
|
|
|
|
for (const id of chainIds.values()) {
|
2020-11-03 00:41:28 +01:00
|
|
|
// make an empty object entry for each chainId
|
2021-02-04 19:15:23 +01:00
|
|
|
newAddressBook[id] = {};
|
2019-09-21 19:36:06 +02:00
|
|
|
for (const address in ab) {
|
|
|
|
if (ab[address].chainId === id) {
|
2021-02-04 19:15:23 +01:00
|
|
|
ab[address].isEns = false;
|
2019-09-21 19:36:06 +02:00
|
|
|
if (util.normalizeEnsName(ab[address].name)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
ab[address].isEns = true;
|
2019-09-21 19:36:06 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
newAddressBook[id][address] = ab[address];
|
2019-09-21 19:36:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
state.AddressBookController.addressBook = newAddressBook;
|
2019-09-21 19:36:06 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return state;
|
2019-09-21 19:36:06 +02:00
|
|
|
}
|