Skip to content

Commit

Permalink
Fix address validation for RSK network in custom token form (#3797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz authored Jan 29, 2021
1 parent e842e33 commit 69997d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HELP_ARTICLE } from 'config';
import translate from 'translations';
import { Token } from 'types/network';
import { HelpLink } from 'components/ui';
import { AddressField } from './AddressField';
import AddressField from './AddressField';
import { DecimalField } from './DecimalField';
import { SymbolField } from './SymbolField';
import { BalanceField } from './BalanceField';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import React from 'react';
import { Result } from 'mycrypto-nano-result';

import { translateRaw } from 'translations';
import { isValidETHAddress } from 'libs/validators';
import { getIsValidAddressFunction } from 'libs/validators';
import { Input } from 'components/ui';
import { IGenerateAddressLookup } from './AddCustomTokenForm';
import { connect } from 'react-redux';
import { AppState } from '../../../../features/reducers';
import { configSelectors } from '../../../../features/config';
import { NetworkConfig } from 'types/network';

interface OwnProps {
addressLookup: IGenerateAddressLookup;
onChange(address: Result<string>): void;
}

interface StateProps {
network: NetworkConfig;
}

enum ErrType {
INVALIDADDR = 'Not a valid address',
ADDRTAKEN = 'A token with this address already exists'
Expand All @@ -21,7 +29,7 @@ interface State {
userInput: string;
}

export class AddressField extends React.Component<OwnProps, State> {
class AddressField extends React.Component<OwnProps & StateProps, State> {
public state: State = {
address: Result.from({ res: '' }),
userInput: ''
Expand Down Expand Up @@ -49,11 +57,20 @@ export class AddressField extends React.Component<OwnProps, State> {
private handleFieldChange = (e: React.FormEvent<HTMLInputElement>) => {
const userInput = e.currentTarget.value;
const addrTaken = this.props.addressLookup[userInput];
const validAddr = isValidETHAddress(userInput);

const validAddrFn = getIsValidAddressFunction(this.props.network.chainId);

const validAddr = validAddrFn(userInput);
const err = addrTaken ? ErrType.ADDRTAKEN : !validAddr ? ErrType.INVALIDADDR : undefined;
const address: Result<string> = err ? Result.from({ err }) : Result.from({ res: userInput });

this.setState({ userInput, address });
this.props.onChange(address);
};
}

export default connect(
(state: AppState): StateProps => ({
network: configSelectors.getNetworkConfig(state)
})
)(AddressField);

0 comments on commit 69997d3

Please sign in to comment.