validators merge

This commit is contained in:
crptm 2017-06-27 03:27:26 +04:00
parent 240007fe43
commit 0ad93101e7
7 changed files with 32 additions and 49 deletions

View File

@ -1,6 +1,8 @@
// @flow
import React from 'react';
import { toDataUrl } from 'ethereum-blockies';
import { isValidAddress } from 'eth/validators';
import { isValidETHAddress } from 'libs/validators';
type Props = {
address: string
@ -8,7 +10,7 @@ type Props = {
export default function Identicon(props: Props) {
// FIXME breaks on failed checksums
const style = !isValidAddress(props.address)
const style = !isValidETHAddress(props.address)
? {}
: { backgroundImage: `url(${toDataUrl(props.address.toLowerCase())})` };
return <div className="addressIdenticon" style={style} title="Address Indenticon" />;

View File

@ -4,7 +4,7 @@ import { Identicon } from 'components/ui';
import { getEnsAddress } from 'selectors/ens';
import { connect } from 'react-redux';
import type { State } from 'reducers';
import { isValidENSorEtherAddress, isValidENSAddress } from 'eth/validators';
import { isValidENSorEtherAddress, isValidENSAddress } from 'libs/validators';
import { resolveEnsName } from 'actions/ens';
type PublicProps = {

View File

@ -1,7 +1,7 @@
// @flow
import React from 'react';
import translate from 'translations';
import { isValidHex } from 'eth/validators';
import { isValidHex } from 'libs/validators';
export default class DataField extends React.Component {
props: {

View File

@ -1,40 +1,32 @@
// @flow
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { DONATION_ADDRESSES_MAP } from 'config/data';
import Validator from 'libs/validator';
import {isValidBTCAddress, isValidETHAddress} from 'libs/validators';
import translate from 'translations';
export default class ReceivingAddress extends Component {
constructor(props) {
super(props);
this.validator = new Validator();
this.state = {
validAddress: false
};
}
static propTypes = {
destinationKind: PropTypes.string.isRequired,
receivingAddressSwap: PropTypes.func.isRequired,
receivingAddress: PropTypes.string
};
onChangeReceivingAddress = event => {
onChangeReceivingAddress = (event: SyntheticInputEvent) => {
const value = event.target.value;
this.props.receivingAddressSwap(value);
let validAddress;
// TODO - find better pattern here once currencies move beyond BTC, ETH, REP
if (this.props.destinationKind === 'BTC') {
validAddress = this.validator.isValidBTCAddress(value);
} else {
validAddress = this.validator.isValidETHAddress(value);
}
this.setState({ validAddress });
};
render() {
const { destinationKind, receivingAddress } = this.props;
const { validAddress } = this.state;
let validAddress;
// TODO - find better pattern here once currencies move beyond BTC, ETH, REP
if (this.props.destinationKind === 'BTC') {
validAddress = isValidBTCAddress(receivingAddress);
} else {
validAddress = isValidETHAddress(receivingAddress);
}
return (
<article className="swap-start">
<section className="swap-address block">

View File

@ -1,16 +0,0 @@
import WalletAddressValidator from 'wallet-address-validator';
import ethUtil from 'ethereumjs-util';
export default class Validator {
isValidETHAddress = function(address) {
if (address && address === '0x0000000000000000000000000000000000000000')
return false;
if (address) {
return ethUtil.isValidAddress(address);
}
return false;
};
isValidBTCAddress = function(address) {
return WalletAddressValidator.validate(address, 'BTC');
};
}

View File

@ -1,7 +1,20 @@
// @flow
import WalletAddressValidator from 'wallet-address-validator';
import { normalise } from './ens';
import { toChecksumAddress } from 'ethereumjs-util';
export function isValidETHAddress(address: string): boolean {
if (!address) {
return false;
}
if (address == '0x0000000000000000000000000000000000000000') return false;
return validateEtherAddress(address);
}
export function isValidBTCAddress(address: string): boolean {
return WalletAddressValidator.validate(address, 'BTC');
}
export function isValidHex(str: string): boolean {
if (typeof str !== 'string') {
return false;
@ -13,7 +26,7 @@ export function isValidHex(str: string): boolean {
}
export function isValidENSorEtherAddress(address: string): boolean {
return isValidAddress(address) || isValidENSAddress(address);
return isValidETHAddress(address) || isValidENSAddress(address);
}
export function isValidENSName(str: string) {
@ -44,6 +57,7 @@ function isChecksumAddress(address: string): boolean {
return address == toChecksumAddress(address);
}
// FIXME we probably want to do checksum checks sideways
function validateEtherAddress(address: string): boolean {
if (address.substring(0, 2) != '0x') return false;
else if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) return false;
@ -51,12 +65,3 @@ function validateEtherAddress(address: string): boolean {
return true;
else return isChecksumAddress(address);
}
// FIXME already in swap PR somewhere
export function isValidAddress(address: string): boolean {
if (!address) {
return false;
}
if (address == '0x0000000000000000000000000000000000000000') return false;
return validateEtherAddress(address);
}