aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/blacklist.js5
-rw-r--r--app/scripts/controllers/currency.js23
-rw-r--r--app/scripts/controllers/infura.js18
-rw-r--r--app/scripts/controllers/shapeshift.js15
-rw-r--r--app/scripts/controllers/transactions.js60
5 files changed, 86 insertions, 35 deletions
diff --git a/app/scripts/controllers/blacklist.js b/app/scripts/controllers/blacklist.js
index 33c31dab9..df41c90c0 100644
--- a/app/scripts/controllers/blacklist.js
+++ b/app/scripts/controllers/blacklist.js
@@ -41,9 +41,9 @@ class BlacklistController {
scheduleUpdates () {
if (this._phishingUpdateIntervalRef) return
- this.updatePhishingList()
+ this.updatePhishingList().catch(log.warn)
this._phishingUpdateIntervalRef = setInterval(() => {
- this.updatePhishingList()
+ this.updatePhishingList().catch(log.warn)
}, POLLING_INTERVAL)
}
@@ -57,4 +57,3 @@ class BlacklistController {
}
module.exports = BlacklistController
-
diff --git a/app/scripts/controllers/currency.js b/app/scripts/controllers/currency.js
index 930fc52e8..36b8808aa 100644
--- a/app/scripts/controllers/currency.js
+++ b/app/scripts/controllers/currency.js
@@ -43,20 +43,19 @@ class CurrencyController {
this.store.updateState({ conversionDate })
}
- updateConversionRate () {
- const currentCurrency = this.getCurrentCurrency()
- return fetch(`https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`)
- .then(response => response.json())
- .then((parsedResponse) => {
+ async updateConversionRate () {
+ let currentCurrency
+ try {
+ currentCurrency = this.getCurrentCurrency()
+ const response = await fetch(`https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`)
+ const parsedResponse = await response.json()
this.setConversionRate(Number(parsedResponse.bid))
this.setConversionDate(Number(parsedResponse.timestamp))
- }).catch((err) => {
- if (err) {
- console.warn(`MetaMask - Failed to query currency conversion:`, currentCurrency, err)
- this.setConversionRate(0)
- this.setConversionDate('N/A')
- }
- })
+ } catch (err) {
+ log.warn(`MetaMask - Failed to query currency conversion:`, currentCurrency, err)
+ this.setConversionRate(0)
+ this.setConversionDate('N/A')
+ }
}
scheduleConversionInterval () {
diff --git a/app/scripts/controllers/infura.js b/app/scripts/controllers/infura.js
index 10adb1004..c6b4c9de2 100644
--- a/app/scripts/controllers/infura.js
+++ b/app/scripts/controllers/infura.js
@@ -19,15 +19,13 @@ class InfuraController {
// Responsible for retrieving the status of Infura's nodes. Can return either
// ok, degraded, or down.
- checkInfuraNetworkStatus () {
- return fetch('https://api.infura.io/v1/status/metamask')
- .then(response => response.json())
- .then((parsedResponse) => {
- this.store.updateState({
- infuraNetworkStatus: parsedResponse,
- })
- return parsedResponse
- })
+ async checkInfuraNetworkStatus () {
+ const response = await fetch('https://api.infura.io/v1/status/metamask')
+ const parsedResponse = await response.json()
+ this.store.updateState({
+ infuraNetworkStatus: parsedResponse,
+ })
+ return parsedResponse
}
scheduleInfuraNetworkCheck () {
@@ -35,7 +33,7 @@ class InfuraController {
clearInterval(this.conversionInterval)
}
this.conversionInterval = setInterval(() => {
- this.checkInfuraNetworkStatus()
+ this.checkInfuraNetworkStatus().catch(log.warn)
}, POLLING_INTERVAL)
}
}
diff --git a/app/scripts/controllers/shapeshift.js b/app/scripts/controllers/shapeshift.js
index 3d955c01f..3bbfaa1c5 100644
--- a/app/scripts/controllers/shapeshift.js
+++ b/app/scripts/controllers/shapeshift.js
@@ -45,18 +45,19 @@ class ShapeshiftController {
})
}
- updateTx (tx) {
- const url = `https://shapeshift.io/txStat/${tx.depositAddress}`
- return fetch(url)
- .then((response) => {
- return response.json()
- }).then((json) => {
+ async updateTx (tx) {
+ try {
+ const url = `https://shapeshift.io/txStat/${tx.depositAddress}`
+ const response = await fetch(url)
+ const json = await response.json()
tx.response = json
if (tx.response.status === 'complete') {
tx.time = new Date().getTime()
}
return tx
- })
+ } catch (err) {
+ log.warn(err)
+ }
}
saveTx (tx) {
diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js
index 31e53554d..336b0d8f7 100644
--- a/app/scripts/controllers/transactions.js
+++ b/app/scripts/controllers/transactions.js
@@ -185,9 +185,10 @@ module.exports = class TransactionController extends EventEmitter {
async addUnapprovedTransaction (txParams) {
// validate
- await this.txGasUtil.validateTxParams(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
@@ -215,7 +216,6 @@ module.exports = class TransactionController extends EventEmitter {
}
txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16))
txParams.value = txParams.value || '0x0'
- if (txParams.to === null) delete txParams.to
// set gasLimit
return await this.txGasUtil.analyzeGasUsage(txMeta)
}
@@ -314,6 +314,60 @@ module.exports = class TransactionController extends EventEmitter {
// PRIVATE METHODS
//
+ _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
+ }
+
+ _validateTxParams (txParams) {
+ this._validateFrom(txParams)
+ this._validateRecipient(txParams)
+ if ('value' in txParams) {
+ const value = txParams.value.toString()
+ if (value.includes('-')) {
+ throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`)
+ }
+
+ if (value.includes('.')) {
+ throw new Error(`Invalid transaction value of ${txParams.value} number must be in wei`)
+ }
+ }
+ }
+
+ _validateFrom (txParams) {
+ if ( !(typeof txParams.from === 'string') ) throw new Error(`Invalid from address ${txParams.from} not a string`)
+ if (!ethUtil.isValidAddress(txParams.from)) throw new Error('Invalid from address')
+ }
+
+ _validateRecipient (txParams) {
+ if (txParams.to === '0x' || txParams.to === null ) {
+ if (txParams.data) {
+ delete txParams.to
+ } else {
+ throw new Error('Invalid recipient address')
+ }
+ } else if ( txParams.to !== undefined && !ethUtil.isValidAddress(txParams.to) ) {
+ throw new Error('Invalid recipient address')
+ }
+ return txParams
+ }
+
_markNonceDuplicatesDropped (txId) {
this.txStateManager.setTxStatusConfirmed(txId)
// get the confirmed transactions nonce and from address