1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 01:08:27 +00:00

move hex regexp to /utils/ethUtils

This commit is contained in:
Szymon Lesisz 2018-10-09 10:42:36 +02:00
parent 4da23c1db2
commit 51ed553e9a
3 changed files with 9 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import * as ValidationActions from 'actions/SendFormValidationActions';
import { initialState } from 'reducers/SendFormReducer';
import { findToken } from 'reducers/TokensReducer';
import * as reducerUtils from 'reducers/utils';
import * as ethUtils from 'utils/ethUtils';
import type {
Dispatch,
@ -397,8 +398,7 @@ const estimateGasPrice = (): AsyncAction => async (dispatch: Dispatch, getState:
}
const requestedData = state.data;
const re = /^(0x|0X)?[0-9A-Fa-f]+$/g;
if (!re.test(requestedData)) {
if (!ethUtils.isHex(requestedData)) {
// stop "calculatingGasLimit" process
dispatch(onGasLimitChange(requestedData.length > 0 ? state.gasLimit : network.defaultGasLimit.toString()));
return;

View File

@ -6,6 +6,7 @@ import EthereumjsUnits from 'ethereumjs-units';
import { findToken } from 'reducers/TokensReducer';
import { findDevice, getPendingAmount } from 'reducers/utils';
import * as SEND from 'actions/constants/send';
import * as ethUtils from 'utils/ethUtils';
import type {
Dispatch,
@ -19,7 +20,6 @@ const NUMBER_RE: RegExp = new RegExp('^(0|0\\.([0-9]+)?|[1-9][0-9]*\\.?([0-9]+)?
const UPPERCASE_RE = new RegExp('^(.*[A-Z].*)$');
const ABS_RE = new RegExp('^[0-9]+$');
const ETH_18_RE = new RegExp('^(0|0\\.([0-9]{0,18})?|[1-9][0-9]*\\.?([0-9]{0,18})?|\\.[0-9]{0,18})$');
const HEX_RE = new RegExp('^(0x|0X)?[0-9A-Fa-f]+$');
const dynamicRegexp = (decimals: number): RegExp => {
if (decimals > 0) {
return new RegExp(`^(0|0\\.([0-9]{0,${decimals}})?|[1-9][0-9]*\\.?([0-9]{0,${decimals}})?|\\.[0-9]{1,${decimals}})$`);
@ -326,7 +326,7 @@ export const nonceValidation = ($state: State): PayloadAction<State> => (dispatc
export const dataValidation = ($state: State): PayloadAction<State> => (): State => {
const state = { ...$state };
if (!state.touched.data || state.data.length === 0) return state;
if (!HEX_RE.test(state.data)) {
if (!ethUtils.isHex(state.data)) {
state.errors.data = 'Data is not valid hexadecimal';
}
return state;

View File

@ -39,4 +39,9 @@ export const validateAddress = (address: string): ?string => {
return 'Address is not a valid checksum';
}
return null;
};
export const isHex = (str: string): boolean => {
const regExp = /^(0x|0X)?[0-9A-Fa-f]+$/g;
return regExp.test(str);
};