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

View File

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