aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
authorEsteban MIno <efmino@uc.cl>2018-08-07 06:28:47 +0800
committerEsteban MIno <efmino@uc.cl>2018-08-07 06:28:47 +0800
commit78ad3c38e2c9cfce8b0756c7d0df8264316d1d21 (patch)
tree92053cc46d2106c6838a5044818ed3b809d0bdba /app/scripts/controllers
parent9ac9f53a73357238ed2ee0ce57c65de592cfd968 (diff)
downloadtangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.tar
tangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.tar.gz
tangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.tar.bz2
tangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.tar.lz
tangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.tar.xz
tangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.tar.zst
tangerine-wallet-browser-78ad3c38e2c9cfce8b0756c7d0df8264316d1d21.zip
add suggested token params validation
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/preferences.js29
1 files changed, 25 insertions, 4 deletions
diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js
index 521a68a66..3bbd48f06 100644
--- a/app/scripts/controllers/preferences.js
+++ b/app/scripts/controllers/preferences.js
@@ -1,5 +1,6 @@
const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
+const isValidAddress = require('ethereumjs-util').isValidAddress
const extend = require('xtend')
@@ -55,9 +56,12 @@ class PreferencesController {
}
addSuggestedToken (tokenOpts) {
- // TODO: Validate params
+ this._validateSuggestedTokenParams(tokenOpts)
const suggested = this.getSuggestedTokens()
- suggested[tokenOpts.address] = tokenOpts
+ const { rawAddress, symbol, decimals } = tokenOpts
+ const address = normalizeAddress(rawAddress)
+ const newEntry = { address, symbol, decimals }
+ suggested[address] = newEntry
this.store.updateState({ suggestedTokens: suggested })
}
@@ -71,10 +75,10 @@ class PreferencesController {
*/
requestAddToken (req, res, next, end) {
if (req.method === 'eth_watchToken') {
- // TODO: Validate params!
const [ rawAddress, symbol, decimals ] = req.params
+ this._validateSuggestedTokenParams({ rawAddress, symbol, decimals })
const tokenOpts = {
- address: rawAddress,
+ rawAddress,
decimals,
symbol,
}
@@ -423,6 +427,23 @@ class PreferencesController {
//
// PRIVATE METHODS
//
+
+ /**
+ * Validates that the passed options for suggested token have all required properties.
+ *
+ * @param {Object} opts The options object to validate
+ * @throws {string} Throw a custom error indicating that address, symbol and/or decimals
+ * doesn't fulfill requirements
+ *
+ */
+ _validateSuggestedTokenParams (opts) {
+ const { rawAddress, symbol, decimals } = opts
+ if (!rawAddress || !symbol || !decimals) throw new Error(`Cannot suggest token without address, symbol, and decimals`)
+ if (!(symbol.length < 5)) throw new Error(`Invalid symbol ${symbol} more than four characters`)
+ const numDecimals = parseInt(decimals, 10)
+ if (isNaN(numDecimals) || numDecimals > 18 || numDecimals < 0) throw new Error(`Invalid decimals ${decimals}`)
+ if (!isValidAddress(rawAddress)) throw new Error(`Invalid address ${rawAddress}`)
+ }
}
module.exports = PreferencesController