From 530e8c132fbac85d96709e83bbce60a7a2d4d738 Mon Sep 17 00:00:00 2001 From: ryanml Date: Fri, 19 Mar 2021 10:17:39 -0700 Subject: [PATCH] Fixing incorrectly typed token decimal attribute (#10666) --- app/scripts/controllers/preferences.js | 2 +- app/scripts/migrations/054.js | 75 +++ app/scripts/migrations/054.test.js | 687 +++++++++++++++++++++++++ app/scripts/migrations/index.js | 1 + 4 files changed, 764 insertions(+), 1 deletion(-) create mode 100644 app/scripts/migrations/054.js create mode 100644 app/scripts/migrations/054.test.js diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index 240aaaa18..fba7a400a 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -378,7 +378,7 @@ export default class PreferencesController { */ async addToken(rawAddress, symbol, decimals, image) { const address = normalizeAddress(rawAddress); - const newEntry = { address, symbol, decimals }; + const newEntry = { address, symbol, decimals: Number(decimals) }; const { tokens, hiddenTokens } = this.store.getState(); const assetImages = this.getAssetImages(); const updatedHiddenTokens = hiddenTokens.filter( diff --git a/app/scripts/migrations/054.js b/app/scripts/migrations/054.js new file mode 100644 index 000000000..105a46750 --- /dev/null +++ b/app/scripts/migrations/054.js @@ -0,0 +1,75 @@ +import { cloneDeep } from 'lodash'; + +const version = 54; + +function isValidDecimals(decimals) { + return ( + typeof decimals === 'number' || + (typeof decimals === 'string' && decimals.match(/^(0x)?\d+$/u)) + ); +} + +/** + * Migrates preference tokens with decimals typed as string to number. + * It also removes any tokens with corrupted or inconvertible decimal values. + */ +export default { + version, + async migrate(originalVersionedData) { + const versionedData = cloneDeep(originalVersionedData); + versionedData.meta.version = version; + const state = versionedData.data; + const newState = transformState(state); + versionedData.data = newState; + return versionedData; + }, +}; + +function transformState(state) { + const newState = state; + + if (!newState.PreferencesController) { + return newState; + } + + const tokens = newState.PreferencesController.tokens || []; + // Filter out any tokens with corrupted decimal values + const validTokens = tokens.filter(({ decimals }) => + isValidDecimals(decimals), + ); + for (const token of validTokens) { + // In the case of a decimal value type string, convert to a number. + if (typeof token.decimals === 'string') { + // eslint-disable-next-line radix + token.decimals = parseInt(token.decimals); + } + } + newState.PreferencesController.tokens = validTokens; + + const { accountTokens } = newState.PreferencesController; + if (accountTokens && typeof accountTokens === 'object') { + for (const address of Object.keys(accountTokens)) { + const networkTokens = accountTokens[address]; + if (networkTokens && typeof networkTokens === 'object') { + for (const network of Object.keys(networkTokens)) { + const tokensOnNetwork = networkTokens[network] || []; + // Filter out any tokens with corrupted decimal values + const validTokensOnNetwork = tokensOnNetwork.filter(({ decimals }) => + isValidDecimals(decimals), + ); + // In the case of a decimal value type string, convert to a number. + for (const token of validTokensOnNetwork) { + if (typeof token.decimals === 'string') { + // eslint-disable-next-line radix + token.decimals = parseInt(token.decimals); + } + } + networkTokens[network] = validTokensOnNetwork; + } + } + } + } + newState.PreferencesController.accountTokens = accountTokens; + + return newState; +} diff --git a/app/scripts/migrations/054.test.js b/app/scripts/migrations/054.test.js new file mode 100644 index 000000000..b0d78bcfd --- /dev/null +++ b/app/scripts/migrations/054.test.js @@ -0,0 +1,687 @@ +import { strict as assert } from 'assert'; +import { + MAINNET_CHAIN_ID, + ROPSTEN_CHAIN_ID, +} from '../../../shared/constants/network'; +import migration54 from './054'; + +describe('migration #54', function () { + it('should update the version metadata', async function () { + const oldStorage = { + meta: { + version: 53, + }, + data: {}, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.meta, { + version: 54, + }); + }); + + it('should retype instance of 0 decimal values to numbers [tokens]', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + accountTokens: [], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + accountTokens: [], + }, + }); + }); + + it('should do nothing if all decimal value typings are correct [tokens]', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + accountTokens: [], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + accountTokens: [], + }, + }); + }); + + it('should retype instance of 0 decimal values to numbers [accountTokens]', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + }, + tokens: [], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }, + tokens: [], + }, + }); + }); + + it('should do nothing if all decimal value typings are correct [accountTokens]', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }, + tokens: [], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }, + tokens: [], + }, + }); + }); + + it('should retype instance of 0 decimal values to numbers [accountTokens and tokens]', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + }, + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }, + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }); + }); + + it('should retype instance of 0 decimal values to numbers, and remove tokens with corrupted decimal values [accountTokens and tokens]', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 'corrupted_decimal?', + symbol: 'SOR', + }, + ], + }, + }, + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '18xx', + symbol: 'SOR', + }, + ], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + accountTokens: { + '0x1111': { + [MAINNET_CHAIN_ID]: [ + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + '0x1112': { + [ROPSTEN_CHAIN_ID]: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + ], + }, + }, + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + ], + }, + }); + }); +}); diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index b30410202..00565e4d8 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -58,6 +58,7 @@ const migrations = [ require('./051').default, require('./052').default, require('./053').default, + require('./054').default, ]; export default migrations;