Add basic validation for contract execute values (#3118)
This commit is contained in:
parent
e251fd49a1
commit
e4fcf4da2b
@ -19,7 +19,7 @@ import ActionDoneAll from 'material-ui/svg-icons/action/done-all';
|
||||
import ContentClear from 'material-ui/svg-icons/content/clear';
|
||||
|
||||
import { BusyStep, CompletedStep, Button, IdentityIcon, Modal, TxHash } from '../../ui';
|
||||
import { validateAddress } from '../../util/validation';
|
||||
import { validateAddress, validateUint } from '../../util/validation';
|
||||
|
||||
import DetailsStep from './DetailsStep';
|
||||
|
||||
@ -41,6 +41,7 @@ export default class ExecuteContract extends Component {
|
||||
state = {
|
||||
amount: '0',
|
||||
amountError: null,
|
||||
fromAddressError: null,
|
||||
func: null,
|
||||
funcError: null,
|
||||
values: [],
|
||||
@ -77,7 +78,8 @@ export default class ExecuteContract extends Component {
|
||||
|
||||
renderDialogActions () {
|
||||
const { onClose, fromAddress } = this.props;
|
||||
const { sending, step } = this.state;
|
||||
const { sending, step, fromAddressError, valuesError } = this.state;
|
||||
const hasError = fromAddressError || valuesError.find((error) => error);
|
||||
const cancelBtn = (
|
||||
<Button
|
||||
key='cancel'
|
||||
@ -92,7 +94,7 @@ export default class ExecuteContract extends Component {
|
||||
<Button
|
||||
key='postTransaction'
|
||||
label='post transaction'
|
||||
disabled={ sending }
|
||||
disabled={ sending || hasError }
|
||||
icon={ <IdentityIcon address={ fromAddress } button /> }
|
||||
onClick={ this.postTransaction } />
|
||||
];
|
||||
@ -177,12 +179,16 @@ export default class ExecuteContract extends Component {
|
||||
let valueError = null;
|
||||
|
||||
switch (input.kind.type) {
|
||||
case 'address':
|
||||
valueError = validateAddress(_value).addressError;
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
value = _value === 'true';
|
||||
break;
|
||||
|
||||
case 'address':
|
||||
valueError = validateAddress(_value).addressError;
|
||||
case 'uint':
|
||||
valueError = validateUint(_value).valueError;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -190,8 +196,8 @@ export default class ExecuteContract extends Component {
|
||||
valuesError[index] = valueError;
|
||||
|
||||
this.setState({
|
||||
values,
|
||||
valuesError
|
||||
values: [].concat(values),
|
||||
valuesError: [].concat(valuesError)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import BigNumber from 'bignumber.js';
|
||||
|
||||
import util from '../api/util';
|
||||
|
||||
export const ERRORS = {
|
||||
@ -22,7 +24,10 @@ export const ERRORS = {
|
||||
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'
|
||||
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'
|
||||
};
|
||||
|
||||
export function validateAbi (abi, api) {
|
||||
@ -88,3 +93,23 @@ export function validateName (name) {
|
||||
nameError
|
||||
};
|
||||
}
|
||||
|
||||
export function validateUint (value) {
|
||||
let valueError = null;
|
||||
|
||||
try {
|
||||
const bn = new BigNumber(value);
|
||||
if (bn.lt(0)) {
|
||||
valueError = ERRORS.negativeNumber;
|
||||
} else if (bn.toString().indexOf('.') !== -1) {
|
||||
valueError = ERRORS.decimalNumber;
|
||||
}
|
||||
} catch (e) {
|
||||
valueError = ERRORS.invalidNumber;
|
||||
}
|
||||
|
||||
return {
|
||||
value,
|
||||
valueError
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user