Add ethereum contract address validator

This commit is contained in:
Germán Martínez 2019-09-05 11:55:08 +02:00
parent 6372674fa6
commit 36b817d34f
3 changed files with 13 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import {
composeValidators, composeValidators,
required, required,
mustBeEthereumAddress, mustBeEthereumAddress,
mustBeEthereumContractAddress,
} from '~/components/forms/validator' } from '~/components/forms/validator'
import { getAddressFromENS } from '~/logic/wallets/getWeb3' import { getAddressFromENS } from '~/logic/wallets/getWeb3'
@ -19,6 +20,7 @@ type Props = {
testId?: string, testId?: string,
validators?: Function[], validators?: Function[],
inputAdornment?: React.Element, inputAdornment?: React.Element,
mustBeContract?: boolean,
} }
const isValidEnsName = (name) => /^([\w-]+\.)+(eth|test|xyz|luxe)$/.test(name) const isValidEnsName = (name) => /^([\w-]+\.)+(eth|test|xyz|luxe)$/.test(name)
@ -35,6 +37,7 @@ const AddressInput = ({
testId, testId,
inputAdornment, inputAdornment,
validators = [], validators = [],
mustBeContract,
}: Props): React.Element<*> => ( }: Props): React.Element<*> => (
<> <>
<Field <Field
@ -44,6 +47,7 @@ const AddressInput = ({
validate={composeValidators( validate={composeValidators(
required, required,
mustBeEthereumAddress, mustBeEthereumAddress,
mustBeContract && mustBeEthereumContractAddress,
...validators, ...validators,
)} )}
inputAdornment={inputAdornment} inputAdornment={inputAdornment}

View File

@ -66,6 +66,14 @@ export const mustBeEthereumAddress = simpleMemoize((address: Field) => {
return isAddress ? undefined : 'Address should be a valid Ethereum address or ENS name' return isAddress ? undefined : 'Address should be a valid Ethereum address or ENS name'
}) })
export const mustBeEthereumContractAddress = simpleMemoize(async (address: Field) => {
const contractCode: string = await getWeb3().eth.getCode(address)
return (!contractCode || contractCode.replace('0x', '').replace(/0/g, '') === '')
? 'Address should be a valid Ethereum contract address or ENS name'
: undefined
})
export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`) export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`)
export const ADDRESS_REPEATED_ERROR = 'Address already introduced' export const ADDRESS_REPEATED_ERROR = 'Address already introduced'

View File

@ -99,6 +99,7 @@ const SendCustomTx = ({
text="Recipient*" text="Recipient*"
className={classes.addressInput} className={classes.addressInput}
fieldMutator={mutators.setRecipient} fieldMutator={mutators.setRecipient}
mustBeContract
/> />
</Col> </Col>
</Row> </Row>