diff --git a/common/components/ui/Identicon.jsx b/common/components/ui/Identicon.jsx index 2422bedd..7f9dac56 100644 --- a/common/components/ui/Identicon.jsx +++ b/common/components/ui/Identicon.jsx @@ -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
; diff --git a/common/containers/Tabs/SendTransaction/components/AddressField.jsx b/common/containers/Tabs/SendTransaction/components/AddressField.jsx index c42ca04f..50aace23 100644 --- a/common/containers/Tabs/SendTransaction/components/AddressField.jsx +++ b/common/containers/Tabs/SendTransaction/components/AddressField.jsx @@ -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 = { diff --git a/common/containers/Tabs/SendTransaction/components/DataField.jsx b/common/containers/Tabs/SendTransaction/components/DataField.jsx index 1cc35fc3..371f5b04 100644 --- a/common/containers/Tabs/SendTransaction/components/DataField.jsx +++ b/common/containers/Tabs/SendTransaction/components/DataField.jsx @@ -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: { diff --git a/common/containers/Tabs/Swap/components/receivingAddress.js b/common/containers/Tabs/Swap/components/receivingAddress.js index c2bc37a8..1932e026 100644 --- a/common/containers/Tabs/Swap/components/receivingAddress.js +++ b/common/containers/Tabs/Swap/components/receivingAddress.js @@ -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 (
diff --git a/common/eth/ens.js b/common/libs/ens.js similarity index 100% rename from common/eth/ens.js rename to common/libs/ens.js diff --git a/common/libs/validator.js b/common/libs/validator.js deleted file mode 100644 index c930f47a..00000000 --- a/common/libs/validator.js +++ /dev/null @@ -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'); - }; -} diff --git a/common/eth/validators.js b/common/libs/validators.js similarity index 81% rename from common/eth/validators.js rename to common/libs/validators.js index bff7f035..75e2f7e9 100644 --- a/common/eth/validators.js +++ b/common/libs/validators.js @@ -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); -}