From 343f0e9e80af804f256a5fa1a55b136c8241c368 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Wed, 4 Apr 2018 20:18:44 -0700 Subject: transactions - remove unnecessary keys on txParams --- app/scripts/controllers/transactions.js | 12 ++++++++++++ test/unit/tx-controller-test.js | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index a73a8b36d..0b78d62f1 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -315,6 +315,18 @@ module.exports = class TransactionController extends EventEmitter { // _normalizeTxParams (txParams) { + const acceptableKeys = [ + 'from', + 'to', + 'nonce', + 'value', + 'data', + 'gas', + 'gasPrice', + ] + Object.keys(txParams).forEach((key) => { + if (!acceptableKeys.includes(key)) delete txParams[key] + }) delete txParams.chainId if ( !txParams.to ) { diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 3fec9758f..e552464cf 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -239,6 +239,7 @@ describe('Transaction Controller', function () { from: 'a7df1beDBF813f57096dF77FCd515f0B3900e402', to: null, data: '68656c6c6f20776f726c64', + random: 'hello world', } txController._normalizeTxParams(txParams) @@ -247,7 +248,7 @@ describe('Transaction Controller', function () { assert(!txParams.to, 'their should be no to address if null') assert.equal(txParams.from.slice(0, 2), '0x', 'from should be hexPrefixd') assert.equal(txParams.data.slice(0, 2), '0x', 'data should be hexPrefixd') - + assert(!('random' in txParams), 'their should be no random key in txParams') txParams.to = 'a7df1beDBF813f57096dF77FCd515f0B3900e402' txController._normalizeTxParams(txParams) -- cgit v1.2.3 From c02da0f27ca4a4239ebae4cfd3348a656e258b86 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Thu, 5 Apr 2018 12:12:02 -0700 Subject: transactions - _normalizeTxParams will now return a new object for txParams --- app/scripts/controllers/transactions.js | 49 ++++++++++++--------------------- test/unit/tx-controller-test.js | 18 ++++++------ 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index 0b78d62f1..336b0d8f7 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -185,10 +185,10 @@ module.exports = class TransactionController extends EventEmitter { async addUnapprovedTransaction (txParams) { // validate - this._validateTxParams(txParams) - this._normalizeTxParams(txParams) + const normalizedTxParams = this._normalizeTxParams(txParams) + this._validateTxParams(normalizedTxParams) // construct txMeta - let txMeta = this.txStateManager.generateTxMeta({txParams}) + let txMeta = this.txStateManager.generateTxMeta({ txParams: normalizedTxParams }) this.addTx(txMeta) this.emit('newUnapprovedTx', txMeta) // add default tx params @@ -315,37 +315,24 @@ module.exports = class TransactionController extends EventEmitter { // _normalizeTxParams (txParams) { - const acceptableKeys = [ - 'from', - 'to', - 'nonce', - 'value', - 'data', - 'gas', - 'gasPrice', - ] - Object.keys(txParams).forEach((key) => { - if (!acceptableKeys.includes(key)) delete txParams[key] - }) - delete txParams.chainId - - if ( !txParams.to ) { - delete txParams.to - } else { - txParams.to = ethUtil.addHexPrefix(txParams.to) + // 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), } - txParams.from = ethUtil.addHexPrefix(txParams.from).toLowerCase() - if (!txParams.data) { - delete txParams.data - } else { - txParams.data = ethUtil.addHexPrefix(txParams.data) - } - - if (txParams.value) txParams.value = ethUtil.addHexPrefix(txParams.value) + // apply only keys in the whiteList + const normalizedTxParams = {} + Object.keys(whiteList).forEach((key) => { + if (txParams[key]) normalizedTxParams[key] = whiteList[key](txParams[key]) + }) - if (txParams.gas) txParams.gas = ethUtil.addHexPrefix(txParams.gas) - if (txParams.gasPrice) txParams.gas = ethUtil.addHexPrefix(txParams.gas) + return normalizedTxParams } _validateTxParams (txParams) { diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index e552464cf..824574ff2 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -242,17 +242,17 @@ describe('Transaction Controller', function () { random: 'hello world', } - txController._normalizeTxParams(txParams) + let normalizedTxParams = txController._normalizeTxParams(txParams) - assert(!txParams.chainId, 'their should be no chainId') - assert(!txParams.to, 'their should be no to address if null') - assert.equal(txParams.from.slice(0, 2), '0x', 'from should be hexPrefixd') - assert.equal(txParams.data.slice(0, 2), '0x', 'data should be hexPrefixd') - assert(!('random' in txParams), 'their should be no random key in txParams') - txParams.to = 'a7df1beDBF813f57096dF77FCd515f0B3900e402' + assert(!normalizedTxParams.chainId, 'their should be no chainId') + assert(!normalizedTxParams.to, 'their should be no to address if null') + assert.equal(normalizedTxParams.from.slice(0, 2), '0x', 'from should be hexPrefixd') + assert.equal(normalizedTxParams.data.slice(0, 2), '0x', 'data should be hexPrefixd') + assert(!('random' in normalizedTxParams), 'their should be no random key in normalizedTxParams') - txController._normalizeTxParams(txParams) - assert.equal(txParams.to.slice(0, 2), '0x', 'to should be hexPrefixd') + txParams.to = 'a7df1beDBF813f57096dF77FCd515f0B3900e402' + normalizedTxParams = txController._normalizeTxParams(txParams) + assert.equal(normalizedTxParams.to.slice(0, 2), '0x', 'to should be hexPrefixd') }) }) -- cgit v1.2.3