aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEsteban MiƱo <efmino@uc.cl>2018-11-06 03:06:34 +0800
committerWhymarrh Whitby <whymarrh.whitby@gmail.com>2018-11-06 03:06:34 +0800
commit4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1 (patch)
treee90e28e3ffe696fe51750f01cd7faccbe3d3bea8
parent6883787c14ba5e3a26a865887e0626a01e96e56a (diff)
downloadtangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.tar
tangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.tar.gz
tangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.tar.bz2
tangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.tar.lz
tangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.tar.xz
tangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.tar.zst
tangerine-wallet-browser-4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1.zip
Update watchAsset ERC20 validation (#5653)
* update ERC20 token valodation for watchAsset * update ERC20 validation test descriptions
-rw-r--r--app/scripts/controllers/preferences.js4
-rw-r--r--test/unit/app/controllers/preferences-controller-test.js26
2 files changed, 28 insertions, 2 deletions
diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js
index eaeaee499..dc6fecaf5 100644
--- a/app/scripts/controllers/preferences.js
+++ b/app/scripts/controllers/preferences.js
@@ -583,8 +583,8 @@ class PreferencesController {
*/
_validateERC20AssetParams (opts) {
const { rawAddress, symbol, decimals } = opts
- if (!rawAddress || !symbol || !decimals) throw new Error(`Cannot suggest token without address, symbol, and decimals`)
- if (!(symbol.length < 6)) throw new Error(`Invalid symbol ${symbol} more than five characters`)
+ if (!rawAddress || !symbol || typeof decimals === 'undefined') throw new Error(`Cannot suggest token without address, symbol, and decimals`)
+ if (!(symbol.length < 7)) throw new Error(`Invalid symbol ${symbol} more than six characters`)
const numDecimals = parseInt(decimals, 10)
if (isNaN(numDecimals) || numDecimals > 36 || numDecimals < 0) {
throw new Error(`Invalid decimals ${decimals} must be at least 0, and not over 36`)
diff --git a/test/unit/app/controllers/preferences-controller-test.js b/test/unit/app/controllers/preferences-controller-test.js
index c64c47ae9..674cf5167 100644
--- a/test/unit/app/controllers/preferences-controller-test.js
+++ b/test/unit/app/controllers/preferences-controller-test.js
@@ -453,6 +453,32 @@ describe('preferences controller', function () {
const assetImages = preferencesController.getAssetImages()
assert.ok(assetImages[address], `set image correctly`)
})
+ it('should validate ERC20 asset correctly', async function () {
+ const validateSpy = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpy({rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC', decimals: 0}) } catch (e) {}
+ assert.equal(validateSpy.threw(), false, 'correct options object')
+ const validateSpyAddress = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpyAddress({symbol: 'ABC', decimals: 0}) } catch (e) {}
+ assert.equal(validateSpyAddress.threw(), true, 'options object with no address')
+ const validateSpySymbol = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpySymbol({rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', decimals: 0}) } catch (e) {}
+ assert.equal(validateSpySymbol.threw(), true, 'options object with no symbol')
+ const validateSpyDecimals = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpyDecimals({rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC'}) } catch (e) {}
+ assert.equal(validateSpyDecimals.threw(), true, 'options object with no decimals')
+ const validateSpyInvalidSymbol = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpyInvalidSymbol({rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 0}) } catch (e) {}
+ assert.equal(validateSpyInvalidSymbol.threw(), true, 'options object with invalid symbol')
+ const validateSpyInvalidDecimals1 = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpyInvalidDecimals1({rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: -1}) } catch (e) {}
+ assert.equal(validateSpyInvalidDecimals1.threw(), true, 'options object with decimals less than zero')
+ const validateSpyInvalidDecimals2 = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpyInvalidDecimals2({rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 38}) } catch (e) {}
+ assert.equal(validateSpyInvalidDecimals2.threw(), true, 'options object with decimals more than 36')
+ const validateSpyInvalidAddress = sandbox.spy(preferencesController._validateERC20AssetParams)
+ try { validateSpyInvalidAddress({rawAddress: '0x123', symbol: 'ABC', decimals: 0}) } catch (e) {}
+ assert.equal(validateSpyInvalidAddress.threw(), true, 'options object with address invalid')
+ })
})
describe('setPasswordForgotten', function () {