1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Merge pull request #3887 from MetaMask/i#3886

transactions - remove unnecessary keys on txParams
This commit is contained in:
kumavis 2018-04-05 15:06:29 -07:00 committed by GitHub
commit 741c6097a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 27 deletions

View File

@ -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,25 +315,24 @@ module.exports = class TransactionController extends EventEmitter {
//
_normalizeTxParams (txParams) {
delete txParams.chainId
if ( !txParams.to ) {
delete txParams.to
} else {
txParams.to = ethUtil.addHexPrefix(txParams.to)
}
txParams.from = ethUtil.addHexPrefix(txParams.from).toLowerCase()
if (!txParams.data) {
delete txParams.data
} else {
txParams.data = ethUtil.addHexPrefix(txParams.data)
// 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),
}
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) {

View File

@ -239,19 +239,20 @@ describe('Transaction Controller', function () {
from: 'a7df1beDBF813f57096dF77FCd515f0B3900e402',
to: null,
data: '68656c6c6f20776f726c64',
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(!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')
txParams.to = 'a7df1beDBF813f57096dF77FCd515f0B3900e402'
txController._normalizeTxParams(txParams)
assert.equal(txParams.to.slice(0, 2), '0x', 'to should be hexPrefixd')
normalizedTxParams = txController._normalizeTxParams(txParams)
assert.equal(normalizedTxParams.to.slice(0, 2), '0x', 'to should be hexPrefixd')
})
})