'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.ERRORS = undefined;exports.







































validateAbi = validateAbi;exports.












































































validateAddress = validateAddress;exports.

















validateCode = validateCode;exports.















validateName = validateName;exports.











validatePositiveNumber = validatePositiveNumber;exports.



















validateDecimalsNumber = validateDecimalsNumber;exports.



















validateUint = validateUint;exports.





















isNullAddress = isNullAddress;var _bignumber = require('bignumber.js');var _bignumber2 = _interopRequireDefault(_bignumber);var _util = require('@parity/api/lib/util');var _util2 = _interopRequireDefault(_util);var _constants = require('./constants');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} // TODO: Convert to FormattedMessages as soon as comfortable with the impact, i.e. errors
// not being concatted into strings in components, all supporting a non-string format
var ERRORS = exports.ERRORS = { invalidAddress: 'address is an invalid network address', invalidAmount: 'the supplied amount should be a valid positive number', invalidAmountDecimals: 'the supplied amount exceeds the allowed decimals', duplicateAddress: 'the address is already in your address book', invalidChecksum: 'address has failed the checksum formatting', invalidName: 'name should not be blank and longer than 2', invalidAbi: 'abi should be a valid JSON array', invalidCode: 'code should be the compiled hex string', invalidNumber: 'invalid number format', negativeNumber: 'input number should be positive', decimalNumber: 'input number should not contain decimals', gasException: 'the transaction will throw an exception with the current values', gasBlockLimit: 'the transaction execution will exceed the block gas limit' }; // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.
function validateAbi(abi) {var abiError = null;var abiParsed = null;try {abiParsed = JSON.parse(abi);if (!_util2.default.isArray(abiParsed)) {abiError = ERRORS.invalidAbi;return { error: abiError, abi: abi, abiError: abiError, abiParsed: abiParsed };} // Validate each elements of the Array
    var invalidIndex = abiParsed.map(function (o) {return isValidAbiEvent(o) || isValidAbiFunction(o) || isAbiFallback(o);}).findIndex(function (valid) {return !valid;});if (invalidIndex !== -1) {var invalid = abiParsed[invalidIndex]; // TODO: Needs seperate error when using FormattedMessage (no concats)
      abiError = ERRORS.invalidAbi + ' (#' + invalidIndex + ': ' + (invalid.name || invalid.type) + ')';return { error: abiError, abi: abi, abiError: abiError, abiParsed: abiParsed };}abi = JSON.stringify(abiParsed);} catch (error) {abiError = ERRORS.invalidAbi;}return { error: abiError, abi: abi, abiError: abiError, abiParsed: abiParsed };}function isValidAbiFunction(object) {if (!object) {return false;}return (object.type === 'function' && object.name || object.type === 'constructor') && object.inputs && _util2.default.isArray(object.inputs);}function isAbiFallback(object) {if (!object) {return false;}return object.type === 'fallback';}function isValidAbiEvent(object) {if (!object) {return false;}return object.type === 'event' && object.name && object.inputs && _util2.default.isArray(object.inputs);}function validateAddress(address) {var addressError = null;if (!address) {addressError = ERRORS.invalidAddress;} else if (!_util2.default.isAddressValid(address)) {addressError = ERRORS.invalidAddress;} else {address = _util2.default.toChecksumAddress(address);}return { error: addressError, address: address, addressError: addressError };}function validateCode(code) {var codeError = null;if (!code || !code.length) {codeError = ERRORS.invalidCode;} else if (!_util2.default.isHex(code)) {codeError = ERRORS.invalidCode;}return { error: codeError, code: code, codeError: codeError };}function validateName(name) {var nameError = !name || name.trim().length < 2 ? ERRORS.invalidName : null;return { error: nameError, name: name, nameError: nameError };}function validatePositiveNumber(number) {var numberError = null;try {var v = new _bignumber2.default(number);if (v.lt(0)) {numberError = ERRORS.invalidAmount;}} catch (e) {numberError = ERRORS.invalidAmount;}return { error: numberError, number: number, numberError: numberError };}function validateDecimalsNumber(number) {var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;var numberError = null;try {var s = new _bignumber2.default(number).mul(base).toFixed();if (s.indexOf('.') !== -1) {numberError = ERRORS.invalidAmountDecimals;}} catch (e) {numberError = ERRORS.invalidAmount;}return { error: numberError, number: number, numberError: numberError };}function validateUint(value) {var valueError = null;try {var bn = new _bignumber2.default(value);if (bn.lt(0)) {valueError = ERRORS.negativeNumber;} else if (!bn.isInteger()) {valueError = ERRORS.decimalNumber;}} catch (e) {valueError = ERRORS.invalidNumber;}return { error: valueError, value: value, valueError: valueError };}function isNullAddress(address) {if (address && address.substr(0, 2) === '0x') {return isNullAddress(address.substr(2));}return address === _constants.NULL_ADDRESS;}