2021-02-04 19:15:23 +01:00
|
|
|
import { cloneDeep } from 'lodash';
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const version = 48;
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Delete NetworkController.settings
|
2020-10-12 23:09:08 +02:00
|
|
|
* 2a. Migrate NetworkController.provider to Rinkeby if set to type 'rpc' or
|
|
|
|
* 'localhost'.
|
2020-10-06 19:57:02 +02:00
|
|
|
* 2b. Re-key provider.rpcTarget to provider.rpcUrl
|
2020-10-12 21:05:40 +02:00
|
|
|
* 3. Add localhost network to frequentRpcListDetail.
|
2020-10-12 23:09:08 +02:00
|
|
|
* 4. Delete CachedBalancesController.cachedBalances
|
|
|
|
* 5. Convert transactions metamaskNetworkId to decimal if they are hex
|
2020-10-13 01:35:55 +02:00
|
|
|
* 6. Convert address book keys from decimal to hex
|
2020-10-13 15:18:24 +02:00
|
|
|
* 7. Delete localhost key in IncomingTransactionsController
|
2020-10-13 16:07:00 +02:00
|
|
|
* 8. Merge 'localhost' tokens into 'rpc' tokens
|
2020-10-06 19:57:02 +02:00
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
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;
|
2020-10-06 19:57:02 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const hexRegEx = /^0x[0-9a-f]+$/iu;
|
|
|
|
const chainIdRegEx = /^0x[1-9a-f]+[0-9a-f]*$/iu;
|
2020-10-12 23:09:08 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function transformState(state = {}) {
|
2020-10-06 19:57:02 +02:00
|
|
|
// 1. Delete NetworkController.settings
|
2021-02-04 19:15:23 +01:00
|
|
|
delete state.NetworkController?.settings;
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2020-10-12 23:09:08 +02:00
|
|
|
// 2. Migrate NetworkController.provider to Rinkeby or rename rpcTarget key
|
2021-02-04 19:15:23 +01:00
|
|
|
const provider = state.NetworkController?.provider || {};
|
2020-11-03 00:41:28 +01:00
|
|
|
const isCustomRpcWithInvalidChainId =
|
|
|
|
provider.type === 'rpc' &&
|
|
|
|
(typeof provider.chainId !== 'string' ||
|
2021-02-04 19:15:23 +01:00
|
|
|
!chainIdRegEx.test(provider.chainId));
|
2020-10-12 23:09:08 +02:00
|
|
|
if (isCustomRpcWithInvalidChainId || provider.type === 'localhost') {
|
|
|
|
state.NetworkController.provider = {
|
|
|
|
type: 'rinkeby',
|
|
|
|
rpcUrl: '',
|
|
|
|
chainId: '0x4',
|
|
|
|
nickname: '',
|
|
|
|
rpcPrefs: {},
|
|
|
|
ticker: 'ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 19:57:02 +02:00
|
|
|
} else if (state.NetworkController?.provider) {
|
|
|
|
if ('rpcTarget' in state.NetworkController.provider) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const rpcUrl = state.NetworkController.provider.rpcTarget;
|
|
|
|
state.NetworkController.provider.rpcUrl = rpcUrl;
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
delete state.NetworkController?.provider?.rpcTarget;
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:05:40 +02:00
|
|
|
// 3. Add localhost network to frequentRpcListDetail.
|
|
|
|
if (!state.PreferencesController) {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.PreferencesController = {};
|
2020-10-12 21:05:40 +02:00
|
|
|
}
|
|
|
|
if (!state.PreferencesController.frequentRpcListDetail) {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.PreferencesController.frequentRpcListDetail = [];
|
2020-10-12 21:05:40 +02:00
|
|
|
}
|
|
|
|
state.PreferencesController.frequentRpcListDetail.unshift({
|
|
|
|
rpcUrl: 'http://localhost:8545',
|
|
|
|
chainId: '0x539',
|
|
|
|
ticker: 'ETH',
|
|
|
|
nickname: 'Localhost 8545',
|
|
|
|
rpcPrefs: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-12 21:05:40 +02:00
|
|
|
|
2020-10-12 23:09:08 +02:00
|
|
|
// 4. Delete CachedBalancesController.cachedBalances
|
2021-02-04 19:15:23 +01:00
|
|
|
delete state.CachedBalancesController?.cachedBalances;
|
2020-10-12 23:09:08 +02:00
|
|
|
|
|
|
|
// 5. Convert transactions metamaskNetworkId to decimal if they are hex
|
2021-02-04 19:15:23 +01:00
|
|
|
const transactions = state.TransactionController?.transactions;
|
2020-10-12 23:09:08 +02:00
|
|
|
if (Array.isArray(transactions)) {
|
|
|
|
transactions.forEach((transaction) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metamaskNetworkId = transaction?.metamaskNetworkId;
|
2020-10-12 23:09:08 +02:00
|
|
|
if (
|
|
|
|
typeof metamaskNetworkId === 'string' &&
|
|
|
|
hexRegEx.test(metamaskNetworkId)
|
|
|
|
) {
|
2020-11-03 00:41:28 +01:00
|
|
|
transaction.metamaskNetworkId = parseInt(
|
|
|
|
metamaskNetworkId,
|
|
|
|
16,
|
2021-02-04 19:15:23 +01:00
|
|
|
).toString(10);
|
2020-10-12 23:09:08 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-12 23:09:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:35:55 +02:00
|
|
|
// 6. Convert address book keys from decimal to hex
|
2021-02-04 19:15:23 +01:00
|
|
|
const addressBook = state.AddressBookController?.addressBook || {};
|
2020-10-13 01:35:55 +02:00
|
|
|
Object.keys(addressBook).forEach((networkKey) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (/^\d+$/iu.test(networkKey)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const chainId = `0x${parseInt(networkKey, 10).toString(16)}`;
|
|
|
|
updateChainIds(addressBook[networkKey], chainId);
|
2020-10-13 01:35:55 +02:00
|
|
|
|
|
|
|
if (addressBook[chainId]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
mergeAddressBookKeys(addressBook, networkKey, chainId);
|
2020-10-13 01:35:55 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
addressBook[chainId] = addressBook[networkKey];
|
2020-10-13 01:35:55 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
delete addressBook[networkKey];
|
2020-10-13 01:35:55 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-13 01:35:55 +02:00
|
|
|
|
2020-10-13 15:18:24 +02:00
|
|
|
// 7. Delete localhost key in IncomingTransactionsController
|
|
|
|
delete state.IncomingTransactionsController
|
2021-02-04 19:15:23 +01:00
|
|
|
?.incomingTxLastFetchedBlocksByNetwork?.localhost;
|
2020-10-13 15:18:24 +02:00
|
|
|
|
2020-10-13 16:07:00 +02:00
|
|
|
// 8. Merge 'localhost' tokens into 'rpc' tokens
|
2021-02-04 19:15:23 +01:00
|
|
|
const accountTokens = state.PreferencesController?.accountTokens;
|
2020-10-13 16:07:00 +02:00
|
|
|
if (accountTokens) {
|
|
|
|
Object.keys(accountTokens).forEach((account) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const localhostTokens = accountTokens[account]?.localhost || [];
|
2020-10-13 16:07:00 +02:00
|
|
|
|
|
|
|
if (localhostTokens.length > 0) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const rpcTokens = accountTokens[account].rpc || [];
|
2020-10-13 16:07:00 +02:00
|
|
|
|
|
|
|
if (rpcTokens.length > 0) {
|
2020-11-03 00:41:28 +01:00
|
|
|
accountTokens[account].rpc = mergeTokenArrays(
|
|
|
|
localhostTokens,
|
|
|
|
rpcTokens,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-13 16:07:00 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
accountTokens[account].rpc = localhostTokens;
|
2020-10-13 16:07:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
delete accountTokens[account]?.localhost;
|
|
|
|
});
|
2020-10-13 16:07:00 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return state;
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
2020-10-13 01:35:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges the two given keys for the given address book in place.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function mergeAddressBookKeys(addressBook, networkKey, chainIdKey) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const networkKeyEntries = addressBook[networkKey] || {};
|
2020-10-13 01:35:55 +02:00
|
|
|
// For the new entries, start by copying the existing entries for the chainId
|
2021-02-04 19:15:23 +01:00
|
|
|
const newEntries = { ...addressBook[chainIdKey] };
|
2020-10-13 01:35:55 +02:00
|
|
|
|
|
|
|
// For each address of the old/networkId key entries
|
|
|
|
Object.keys(networkKeyEntries).forEach((address) => {
|
|
|
|
if (newEntries[address] && typeof newEntries[address] === 'object') {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mergedEntry = {};
|
2020-10-13 01:35:55 +02:00
|
|
|
|
|
|
|
// Collect all keys from both entries and merge the corresponding chainId
|
|
|
|
// entry with the networkId entry
|
|
|
|
new Set([
|
|
|
|
...Object.keys(newEntries[address]),
|
|
|
|
...Object.keys(networkKeyEntries[address] || {}),
|
|
|
|
]).forEach((key) => {
|
|
|
|
// Use non-empty value for the current key, if any
|
2020-11-03 00:41:28 +01:00
|
|
|
mergedEntry[key] =
|
2021-02-04 19:15:23 +01:00
|
|
|
newEntries[address][key] || networkKeyEntries[address]?.[key] || '';
|
|
|
|
});
|
2020-10-13 01:35:55 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
newEntries[address] = mergedEntry;
|
2020-10-13 01:35:55 +02:00
|
|
|
} else if (
|
|
|
|
networkKeyEntries[address] &&
|
|
|
|
typeof networkKeyEntries[address] === 'object'
|
|
|
|
) {
|
|
|
|
// If there is no corresponding chainId entry, just use the networkId entry
|
|
|
|
// directly
|
2021-02-04 19:15:23 +01:00
|
|
|
newEntries[address] = networkKeyEntries[address];
|
2020-10-13 01:35:55 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-13 01:35:55 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
addressBook[chainIdKey] = newEntries;
|
2020-10-13 01:35:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the chainId key values to the given chainId in place for all values
|
|
|
|
* of the given networkEntries object.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function updateChainIds(networkEntries, chainId) {
|
2020-10-13 01:35:55 +02:00
|
|
|
Object.values(networkEntries).forEach((entry) => {
|
|
|
|
if (entry && typeof entry === 'object') {
|
2021-02-04 19:15:23 +01:00
|
|
|
entry.chainId = chainId;
|
2020-10-13 01:35:55 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-13 01:35:55 +02:00
|
|
|
}
|
2020-10-13 16:07:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges the two given, non-empty arrays of token objects and returns a new
|
|
|
|
* array.
|
|
|
|
*
|
|
|
|
* @returns {Array<Object>}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function mergeTokenArrays(localhostTokens, rpcTokens) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const localhostTokensMap = tokenArrayToMap(localhostTokens);
|
|
|
|
const rpcTokensMap = tokenArrayToMap(rpcTokens);
|
2020-10-13 16:07:00 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const mergedTokens = [];
|
2020-10-13 16:07:00 +02:00
|
|
|
new Set([
|
|
|
|
...Object.keys(localhostTokensMap),
|
|
|
|
...Object.keys(rpcTokensMap),
|
|
|
|
]).forEach((tokenAddress) => {
|
|
|
|
mergedTokens.push({
|
|
|
|
...localhostTokensMap[tokenAddress],
|
|
|
|
...rpcTokensMap[tokenAddress],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-10-13 16:07:00 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return mergedTokens;
|
2020-10-13 16:07:00 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function tokenArrayToMap(array) {
|
2020-10-13 16:07:00 +02:00
|
|
|
return array.reduce((map, token) => {
|
|
|
|
if (token?.address && typeof token?.address === 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
map[token.address] = token;
|
2020-10-13 16:07:00 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return map;
|
|
|
|
}, {});
|
2020-10-13 16:07:00 +02:00
|
|
|
}
|
|
|
|
}
|