diff options
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/controllers/network/createLocalhostClient.js | 18 | ||||
-rw-r--r-- | app/scripts/controllers/transactions/lib/util.js | 8 | ||||
-rw-r--r-- | app/scripts/controllers/transactions/tx-state-manager.js | 30 |
3 files changed, 43 insertions, 13 deletions
diff --git a/app/scripts/controllers/network/createLocalhostClient.js b/app/scripts/controllers/network/createLocalhostClient.js index 09b1d3c1c..3a435e5d0 100644 --- a/app/scripts/controllers/network/createLocalhostClient.js +++ b/app/scripts/controllers/network/createLocalhostClient.js @@ -2,9 +2,12 @@ const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') const createFetchMiddleware = require('eth-json-rpc-middleware/fetch') const createBlockRefRewriteMiddleware = require('eth-json-rpc-middleware/block-ref-rewrite') const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') +const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware') const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') const BlockTracker = require('eth-block-tracker') +const inTest = process.env.IN_TEST === 'true' + module.exports = createLocalhostClient function createLocalhostClient () { @@ -13,9 +16,24 @@ function createLocalhostClient () { const blockTracker = new BlockTracker({ provider: blockProvider, pollingInterval: 1000 }) const networkMiddleware = mergeMiddleware([ + createEstimateGasMiddleware(), createBlockRefRewriteMiddleware({ blockTracker }), createBlockTrackerInspectorMiddleware({ blockTracker }), fetchMiddleware, ]) return { networkMiddleware, blockTracker } } + +function delay (time) { + return new Promise(resolve => setTimeout(resolve, time)) +} + + +function createEstimateGasMiddleware () { + return createAsyncMiddleware(async (req, _, next) => { + if (req.method === 'eth_estimateGas' && inTest) { + await delay(2000) + } + return next() + }) +} diff --git a/app/scripts/controllers/transactions/lib/util.js b/app/scripts/controllers/transactions/lib/util.js index 84f7592a0..5a8a0cefe 100644 --- a/app/scripts/controllers/transactions/lib/util.js +++ b/app/scripts/controllers/transactions/lib/util.js @@ -17,8 +17,8 @@ module.exports = { // functions that handle normalizing of that key in txParams const normalizers = { - from: from => addHexPrefix(from).toLowerCase(), - to: to => addHexPrefix(to).toLowerCase(), + from: (from, LowerCase = true) => LowerCase ? addHexPrefix(from).toLowerCase() : addHexPrefix(from), + to: (to, LowerCase = true) => LowerCase ? addHexPrefix(to).toLowerCase() : addHexPrefix(to), nonce: nonce => addHexPrefix(nonce), value: value => addHexPrefix(value), data: data => addHexPrefix(data), @@ -31,11 +31,11 @@ const normalizers = { @param txParams {object} @returns {object} normalized txParams */ -function normalizeTxParams (txParams) { +function normalizeTxParams (txParams, LowerCase) { // apply only keys in the normalizers const normalizedTxParams = {} for (const key in normalizers) { - if (txParams[key]) normalizedTxParams[key] = normalizers[key](txParams[key]) + if (txParams[key]) normalizedTxParams[key] = normalizers[key](txParams[key], LowerCase) } return normalizedTxParams } diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index 1a2cb5dee..2aa28c270 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -1,11 +1,10 @@ const extend = require('xtend') const EventEmitter = require('safe-event-emitter') const ObservableStore = require('obs-store') -const ethUtil = require('ethereumjs-util') const log = require('loglevel') const txStateHistoryHelper = require('./lib/tx-state-history-helper') const createId = require('../../lib/random-id') -const { getFinalStates } = require('./lib/util') +const { getFinalStates, normalizeTxParams } = require('./lib/util') /** TransactionStateManager is responsible for the state of a transaction and storing the transaction @@ -126,6 +125,11 @@ class TransactionStateManager extends EventEmitter { @returns {object} the txMeta */ addTx (txMeta) { + // normalize and validate txParams if present + if (txMeta.txParams) { + txMeta.txParams = this.normalizeAndValidateTxParams(txMeta.txParams) + } + this.once(`${txMeta.id}:signed`, function () { this.removeAllListeners(`${txMeta.id}:rejected`) }) @@ -175,13 +179,9 @@ class TransactionStateManager extends EventEmitter { @param [note] {string} - a note about the update for history */ updateTx (txMeta, note) { - // validate txParams + // normalize and validate txParams if present if (txMeta.txParams) { - if (typeof txMeta.txParams.data === 'undefined') { - delete txMeta.txParams.data - } - - this.validateTxParams(txMeta.txParams) + txMeta.txParams = this.normalizeAndValidateTxParams(txMeta.txParams) } // create txMeta snapshot for history @@ -214,6 +214,19 @@ class TransactionStateManager extends EventEmitter { } /** + * normalize and validate txParams members + * @param txParams {object} - txParams + */ + normalizeAndValidateTxParams (txParams) { + if (typeof txParams.data === 'undefined') { + delete txParams.data + } + txParams = normalizeTxParams(txParams, false) + this.validateTxParams(txParams) + return txParams + } + + /** validates txParams members by type @param txParams {object} - txParams to validate */ @@ -227,7 +240,6 @@ class TransactionStateManager extends EventEmitter { break default: if (typeof value !== 'string') throw new Error(`${key} in txParams is not a string. got: (${value})`) - if (!ethUtil.isHexPrefixed(value)) throw new Error(`${key} in txParams is not hex prefixed. got: (${value})`) break } }) |