Merge branch 'master' of https://github.com/MetaMask/metamask-extension into cb-254
commit
4c15c0d3ba
@ -0,0 +1,33 @@ |
||||
const clone = require('clone') |
||||
|
||||
module.exports = getObjStructure |
||||
|
||||
// This will create an object that represents the structure of the given object
|
||||
// it replaces all values with the result of their type
|
||||
|
||||
// {
|
||||
// "data": {
|
||||
// "CurrencyController": {
|
||||
// "conversionDate": "number",
|
||||
// "conversionRate": "number",
|
||||
// "currentCurrency": "string"
|
||||
// }
|
||||
// }
|
||||
|
||||
function getObjStructure(obj) { |
||||
const structure = clone(obj) |
||||
return deepMap(structure, (value) => { |
||||
return value === null ? 'null' : typeof value |
||||
}) |
||||
} |
||||
|
||||
function deepMap(target = {}, visit) { |
||||
Object.entries(target).forEach(([key, value]) => { |
||||
if (typeof value === 'object' && value !== null) { |
||||
target[key] = deepMap(value, visit) |
||||
} else { |
||||
target[key] = visit(value) |
||||
} |
||||
}) |
||||
return target |
||||
} |
@ -0,0 +1,61 @@ |
||||
// next version number
|
||||
const version = 25 |
||||
|
||||
/* |
||||
|
||||
normalizes txParams on unconfirmed txs |
||||
|
||||
*/ |
||||
const ethUtil = require('ethereumjs-util') |
||||
const clone = require('clone') |
||||
|
||||
module.exports = { |
||||
version, |
||||
|
||||
migrate: async function (originalVersionedData) { |
||||
const versionedData = clone(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.TransactionController) { |
||||
if (newState.TransactionController.transactions) { |
||||
const transactions = newState.TransactionController.transactions |
||||
newState.TransactionController.transactions = transactions.map((txMeta) => { |
||||
if (txMeta.status !== 'unapproved') return txMeta |
||||
txMeta.txParams = normalizeTxParams(txMeta.txParams) |
||||
return txMeta |
||||
}) |
||||
} |
||||
} |
||||
|
||||
return newState |
||||
} |
||||
|
||||
function normalizeTxParams (txParams) { |
||||
// functions that handle normalizing of that key in txParams
|
||||
const whiteList = { |
||||
from: from => ethUtil.addHexPrefix(from).toLowerCase(), |
||||
to: to => ethUtil.addHexPrefix(txParams.to).toLowerCase(), |
||||
nonce: nonce => ethUtil.addHexPrefix(nonce), |
||||
value: value => ethUtil.addHexPrefix(value), |
||||
data: data => ethUtil.addHexPrefix(data), |
||||
gas: gas => ethUtil.addHexPrefix(gas), |
||||
gasPrice: gasPrice => ethUtil.addHexPrefix(gasPrice), |
||||
} |
||||
|
||||
// apply only keys in the whiteList
|
||||
const normalizedTxParams = {} |
||||
Object.keys(whiteList).forEach((key) => { |
||||
if (txParams[key]) normalizedTxParams[key] = whiteList[key](txParams[key]) |
||||
}) |
||||
|
||||
return normalizedTxParams |
||||
} |
@ -0,0 +1,29 @@ |
||||
// next version number
|
||||
const version = 0 |
||||
|
||||
/* |
||||
|
||||
description of migration and what it does |
||||
|
||||
*/ |
||||
|
||||
const clone = require('clone') |
||||
|
||||
module.exports = { |
||||
version, |
||||
|
||||
migrate: async function (originalVersionedData) { |
||||
const versionedData = clone(originalVersionedData) |
||||
versionedData.meta.version = version |
||||
const state = versionedData.data |
||||
const newState = transformState(state) |
||||
versionedData.data = newState |
||||
return versionedData |
||||
}, |
||||
} |
||||
|
||||
function transformState (state) { |
||||
const newState = state |
||||
// transform state here
|
||||
return newState |
||||
} |
@ -0,0 +1,49 @@ |
||||
const assert = require('assert') |
||||
const migration25 = require('../../../app/scripts/migrations/025') |
||||
const firstTimeState = { |
||||
meta: {}, |
||||
data: require('../../../app/scripts/first-time-state'), |
||||
} |
||||
|
||||
const storage = { |
||||
"meta": {}, |
||||
"data": { |
||||
"TransactionController": { |
||||
"transactions": [ |
||||
] |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
const transactions = [] |
||||
|
||||
|
||||
while (transactions.length <= 10) { |
||||
transactions.push({ txParams: { from: '0x8aCce2391c0d510a6c5E5d8f819a678f79b7e675', random: 'stuff', chainId: 2 }, status: 'unapproved' }) |
||||
transactions.push({ txParams: { from: '0x8aCce2391c0d510a6c5E5d8f819a678f79b7e675' }, status: 'confirmed' }) |
||||
} |
||||
|
||||
|
||||
storage.data.TransactionController.transactions = transactions |
||||
|
||||
describe('storage is migrated successfully and the txParams.from are lowercase', () => { |
||||
it('should lowercase the from for unapproved txs', (done) => { |
||||
migration25.migrate(storage) |
||||
.then((migratedData) => { |
||||
const migratedTransactions = migratedData.data.TransactionController.transactions |
||||
migratedTransactions.forEach((tx) => { |
||||
if (tx.status === 'unapproved') assert(!tx.txParams.random) |
||||
if (tx.status === 'unapproved') assert(!tx.txParams.chainId) |
||||
}) |
||||
done() |
||||
}).catch(done) |
||||
}) |
||||
|
||||
it('should migrate first time state', (done) => { |
||||
migration25.migrate(firstTimeState) |
||||
.then((migratedData) => { |
||||
assert.equal(migratedData.meta.version, 25) |
||||
done() |
||||
}).catch(done) |
||||
}) |
||||
}) |
@ -0,0 +1,17 @@ |
||||
const assert = require('assert') |
||||
const migrationTemplate = require('../../../app/scripts/migrations/template') |
||||
const properTime = (new Date()).getTime() |
||||
const storage = { |
||||
meta: {}, |
||||
data: {}, |
||||
} |
||||
|
||||
describe('storage is migrated successfully', () => { |
||||
it('should work', (done) => { |
||||
migrationTemplate.migrate(storage) |
||||
.then((migratedData) => { |
||||
assert.equal(migratedData.meta.version, 0) |
||||
done() |
||||
}).catch(done) |
||||
}) |
||||
}) |
Loading…
Reference in new issue