Fix the input error message in contract interaction (#2253) (#2297)

Co-authored-by: Daniel Sanchez <daniel.sanchez@gnosis.pm>
This commit is contained in:
katspaugh 2021-05-20 11:09:12 +02:00 committed by GitHub
parent b0fadee951
commit 1f9bb3aef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import {
mustBeUrl, mustBeUrl,
minValue, minValue,
mustBeEthereumAddress, mustBeEthereumAddress,
mustBeAddressHash,
minMaxLength, minMaxLength,
uniqueAddress, uniqueAddress,
differentFrom, differentFrom,
@ -129,8 +130,24 @@ describe('Forms > Validators', () => {
}) })
}) })
describe('mustBeAddressHash validator', () => {
const MUST_BE_ETH_ADDRESS_ERR_MSG = 'Must be a valid address'
it('Returns undefined for a valid ethereum address', async () => {
expect(await mustBeAddressHash('0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe')).toBeUndefined()
})
it('Returns an error message for an address with an invalid checksum', async () => {
expect(await mustBeAddressHash('0xde0b295669a9FD93d5F28D9Ec85E40f4cb697BAe')).toEqual(MUST_BE_ETH_ADDRESS_ERR_MSG)
})
it('Returns an error message for non-address and non-domain string', async () => {
expect(await mustBeAddressHash('notanaddress')).toEqual(MUST_BE_ETH_ADDRESS_ERR_MSG)
})
})
describe('mustBeEthereumAddress validator', () => { describe('mustBeEthereumAddress validator', () => {
const MUST_BE_ETH_ADDRESS_ERR_MSG = 'Must be a valid address, ENS or Unstoppable domain' const MUST_BE_ETH_ADDRESS_OR_DOMAIN_ERR_MSG = 'Must be a valid address, ENS or Unstoppable domain'
it('Returns undefined for a valid ethereum address', async () => { it('Returns undefined for a valid ethereum address', async () => {
expect(await mustBeEthereumAddress('0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe')).toBeUndefined() expect(await mustBeEthereumAddress('0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe')).toBeUndefined()
@ -138,13 +155,9 @@ describe('Forms > Validators', () => {
it('Returns an error message for an address with an invalid checksum', async () => { it('Returns an error message for an address with an invalid checksum', async () => {
expect(await mustBeEthereumAddress('0xde0b295669a9FD93d5F28D9Ec85E40f4cb697BAe')).toEqual( expect(await mustBeEthereumAddress('0xde0b295669a9FD93d5F28D9Ec85E40f4cb697BAe')).toEqual(
MUST_BE_ETH_ADDRESS_ERR_MSG, MUST_BE_ETH_ADDRESS_OR_DOMAIN_ERR_MSG,
) )
}) })
it('Returns an error message for non-address string', async () => {
expect(await mustBeEthereumAddress('notanaddress')).toEqual(MUST_BE_ETH_ADDRESS_ERR_MSG)
})
}) })
describe('minMaxLength validator', () => { describe('minMaxLength validator', () => {

View File

@ -63,19 +63,26 @@ export const maxValue = (max: number | string) => (value: string): ValidatorRetu
export const ok = (): undefined => undefined export const ok = (): undefined => undefined
export const mustBeEthereumAddress = memoize( export const mustBeAddressHash = memoize(
(address: string): ValidatorReturnType => { (address: string): ValidatorReturnType => {
const errorMessage = 'Must be a valid address'
const startsWith0x = address?.startsWith('0x') const startsWith0x = address?.startsWith('0x')
const isAddress = getWeb3().utils.isAddress(address) const isAddress = getWeb3().utils.isAddress(address)
const errorMessage = `Must be a valid address${
isFeatureEnabled(FEATURES.DOMAIN_LOOKUP) ? ', ENS or Unstoppable domain' : ''
}`
return startsWith0x && isAddress ? undefined : errorMessage return startsWith0x && isAddress ? undefined : errorMessage
}, },
) )
export const mustBeEthereumAddress = memoize(
(address: string): ValidatorReturnType => {
const errorMessage = 'Must be a valid address, ENS or Unstoppable domain'
const result = mustBeAddressHash(address)
if (result !== undefined && isFeatureEnabled(FEATURES.DOMAIN_LOOKUP)) {
return errorMessage
}
return result
},
)
export const mustBeEthereumContractAddress = memoize( export const mustBeEthereumContractAddress = memoize(
async (address: string): Promise<ValidatorReturnType> => { async (address: string): Promise<ValidatorReturnType> => {
const contractCode = await getWeb3().eth.getCode(address) const contractCode = await getWeb3().eth.getCode(address)

View File

@ -5,7 +5,7 @@ import Col from 'src/components/layout/Col'
import Field from 'src/components/forms/Field' import Field from 'src/components/forms/Field'
import TextField from 'src/components/forms/TextField' import TextField from 'src/components/forms/TextField'
import { composeValidators, mustBeEthereumAddress, required } from 'src/components/forms/validator' import { composeValidators, mustBeAddressHash, required } from 'src/components/forms/validator'
import { isArrayParameter } from 'src/routes/safe/components/Balances/SendModal/screens/ContractInteraction/utils' import { isArrayParameter } from 'src/routes/safe/components/Balances/SendModal/screens/ContractInteraction/utils'
import ArrayTypeInput from './ArrayTypeInput' import ArrayTypeInput from './ArrayTypeInput'
@ -41,7 +41,7 @@ export const InputComponent = ({ type, keyValue, placeholder }: Props): ReactEle
testId={keyValue} testId={keyValue}
text={placeholder} text={placeholder}
type="text" type="text"
validate={composeValidators(required, mustBeEthereumAddress)} validate={composeValidators(required, mustBeAddressHash)}
/> />
</Col> </Col>
) )