From 4489a57f2fd32ae4b9b5aa12aede289fa0b03fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Mi=C3=B1o?= Date: Mon, 5 Nov 2018 16:06:34 -0300 Subject: [PATCH] Update watchAsset ERC20 validation (#5653) * update ERC20 token valodation for watchAsset * update ERC20 validation test descriptions --- app/scripts/controllers/preferences.js | 4 +-- .../preferences-controller-test.js | 26 +++++++++++++++++++ 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 () {