2020-01-29 18:14:33 +01:00
|
|
|
import { cloneDeep } from 'lodash'
|
2020-05-12 17:00:24 +02:00
|
|
|
import { util } from '@metamask/controllers'
|
2019-09-21 19:36:06 +02:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
const version = 37
|
|
|
|
|
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) {
|
2020-01-29 18:14:33 +01:00
|
|
|
const versionedData = cloneDeep(originalVersionedData)
|
2019-09-21 19:36:06 +02:00
|
|
|
versionedData.meta.version = version
|
|
|
|
const state = versionedData.data
|
|
|
|
versionedData.data = transformState(state)
|
|
|
|
return versionedData
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function transformState(state) {
|
2019-09-21 19:36:06 +02:00
|
|
|
if (state.AddressBookController) {
|
|
|
|
const ab = state.AddressBookController.addressBook
|
|
|
|
|
|
|
|
const chainIds = new Set()
|
|
|
|
const newAddressBook = {}
|
|
|
|
|
|
|
|
// add all of the chainIds to a set
|
2020-07-21 23:10:45 +02:00
|
|
|
Object.values(ab).forEach((v) => {
|
|
|
|
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
|
2019-09-21 19:36:06 +02:00
|
|
|
newAddressBook[id] = {}
|
|
|
|
for (const address in ab) {
|
|
|
|
if (ab[address].chainId === id) {
|
|
|
|
ab[address].isEns = false
|
|
|
|
if (util.normalizeEnsName(ab[address].name)) {
|
|
|
|
ab[address].isEns = true
|
|
|
|
}
|
|
|
|
newAddressBook[id][address] = ab[address]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.AddressBookController.addressBook = newAddressBook
|
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|