Add basic validation for contract execute values (#3118)

This commit is contained in:
Jaco Greeff 2016-11-03 12:23:25 +01:00 committed by Gav Wood
parent e251fd49a1
commit e4fcf4da2b
2 changed files with 39 additions and 8 deletions

View File

@ -19,7 +19,7 @@ import ActionDoneAll from 'material-ui/svg-icons/action/done-all';
import ContentClear from 'material-ui/svg-icons/content/clear'; import ContentClear from 'material-ui/svg-icons/content/clear';
import { BusyStep, CompletedStep, Button, IdentityIcon, Modal, TxHash } from '../../ui'; import { BusyStep, CompletedStep, Button, IdentityIcon, Modal, TxHash } from '../../ui';
import { validateAddress } from '../../util/validation'; import { validateAddress, validateUint } from '../../util/validation';
import DetailsStep from './DetailsStep'; import DetailsStep from './DetailsStep';
@ -41,6 +41,7 @@ export default class ExecuteContract extends Component {
state = { state = {
amount: '0', amount: '0',
amountError: null, amountError: null,
fromAddressError: null,
func: null, func: null,
funcError: null, funcError: null,
values: [], values: [],
@ -77,7 +78,8 @@ export default class ExecuteContract extends Component {
renderDialogActions () { renderDialogActions () {
const { onClose, fromAddress } = this.props; 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 = ( const cancelBtn = (
<Button <Button
key='cancel' key='cancel'
@ -92,7 +94,7 @@ export default class ExecuteContract extends Component {
<Button <Button
key='postTransaction' key='postTransaction'
label='post transaction' label='post transaction'
disabled={ sending } disabled={ sending || hasError }
icon={ <IdentityIcon address={ fromAddress } button /> } icon={ <IdentityIcon address={ fromAddress } button /> }
onClick={ this.postTransaction } /> onClick={ this.postTransaction } />
]; ];
@ -177,12 +179,16 @@ export default class ExecuteContract extends Component {
let valueError = null; let valueError = null;
switch (input.kind.type) { switch (input.kind.type) {
case 'address':
valueError = validateAddress(_value).addressError;
break;
case 'bool': case 'bool':
value = _value === 'true'; value = _value === 'true';
break; break;
case 'address': case 'uint':
valueError = validateAddress(_value).addressError; valueError = validateUint(_value).valueError;
break; break;
} }
@ -190,8 +196,8 @@ export default class ExecuteContract extends Component {
valuesError[index] = valueError; valuesError[index] = valueError;
this.setState({ this.setState({
values, values: [].concat(values),
valuesError valuesError: [].concat(valuesError)
}); });
} }

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import util from '../api/util'; import util from '../api/util';
export const ERRORS = { export const ERRORS = {
@ -22,7 +24,10 @@ export const ERRORS = {
invalidChecksum: 'address has failed the checksum formatting', invalidChecksum: 'address has failed the checksum formatting',
invalidName: 'name should not be blank and longer than 2', invalidName: 'name should not be blank and longer than 2',
invalidAbi: 'abi should be a valid JSON array', 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) { export function validateAbi (abi, api) {
@ -88,3 +93,23 @@ export function validateName (name) {
nameError 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
};
}