Update watchAsset ERC20 validation (#5653)

* update ERC20 token valodation for watchAsset

* update ERC20 validation test descriptions
feature/default_network_editable
Esteban Miño 6 years ago committed by Whymarrh Whitby
parent 6883787c14
commit 4489a57f2f
  1. 4
      app/scripts/controllers/preferences.js
  2. 26
      test/unit/app/controllers/preferences-controller-test.js

@ -583,8 +583,8 @@ class PreferencesController {
*/ */
_validateERC20AssetParams (opts) { _validateERC20AssetParams (opts) {
const { rawAddress, symbol, decimals } = opts const { rawAddress, symbol, decimals } = opts
if (!rawAddress || !symbol || !decimals) throw new Error(`Cannot suggest token without address, symbol, and decimals`) if (!rawAddress || !symbol || typeof decimals === 'undefined') 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 (!(symbol.length < 7)) throw new Error(`Invalid symbol ${symbol} more than six characters`)
const numDecimals = parseInt(decimals, 10) const numDecimals = parseInt(decimals, 10)
if (isNaN(numDecimals) || numDecimals > 36 || numDecimals < 0) { if (isNaN(numDecimals) || numDecimals > 36 || numDecimals < 0) {
throw new Error(`Invalid decimals ${decimals} must be at least 0, and not over 36`) throw new Error(`Invalid decimals ${decimals} must be at least 0, and not over 36`)

@ -453,6 +453,32 @@ describe('preferences controller', function () {
const assetImages = preferencesController.getAssetImages() const assetImages = preferencesController.getAssetImages()
assert.ok(assetImages[address], `set image correctly`) 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 () { describe('setPasswordForgotten', function () {

Loading…
Cancel
Save