2019-07-31 21:56:44 +02:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2020-03-01 11:14:16 +01:00
|
|
|
import classnames from 'classnames'
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-01-29 18:36:03 +01:00
|
|
|
import { debounce } from 'lodash'
|
2019-07-31 21:56:44 +02:00
|
|
|
import copyToClipboard from 'copy-to-clipboard/index'
|
|
|
|
import ENS from 'ethjs-ens'
|
2020-01-31 14:26:50 +01:00
|
|
|
import networkMap from 'ethereum-ens-network-map'
|
2019-07-31 21:56:44 +02:00
|
|
|
import log from 'loglevel'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { ellipsify } from '../../send.utils'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
isValidDomainName,
|
|
|
|
isValidAddress,
|
|
|
|
isValidAddressHead,
|
|
|
|
} from '../../../../helpers/utils/util'
|
2021-01-20 17:13:14 +01:00
|
|
|
import { MAINNET_NETWORK_ID } from '../../../../../../shared/constants/network'
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
// Local Constants
|
|
|
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
|
|
|
const ZERO_X_ERROR_ADDRESS = '0x'
|
|
|
|
|
|
|
|
export default class EnsInput extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
network: PropTypes.string,
|
|
|
|
selectedAddress: PropTypes.string,
|
|
|
|
selectedName: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
updateEnsResolution: PropTypes.func,
|
|
|
|
scanQrCode: PropTypes.func,
|
|
|
|
updateEnsResolutionError: PropTypes.func,
|
|
|
|
onPaste: PropTypes.func,
|
|
|
|
onReset: PropTypes.func,
|
2019-08-03 01:00:50 +02:00
|
|
|
onValidAddressTyped: PropTypes.func,
|
2019-08-14 01:13:05 +02:00
|
|
|
contact: PropTypes.object,
|
2020-10-21 18:35:56 +02:00
|
|
|
value: PropTypes.string,
|
2020-11-07 00:16:51 +01:00
|
|
|
internalSearch: PropTypes.bool,
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
input: '',
|
|
|
|
toError: null,
|
2019-12-03 16:30:03 +01:00
|
|
|
ensResolution: undefined,
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2020-11-07 00:16:51 +01:00
|
|
|
const { network, internalSearch } = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
const networkHasEnsSupport = getNetworkEnsSupport(network)
|
|
|
|
this.setState({ ensResolution: ZERO_ADDRESS })
|
|
|
|
|
2020-11-07 00:16:51 +01:00
|
|
|
if (networkHasEnsSupport && !internalSearch) {
|
2019-07-31 21:56:44 +02:00
|
|
|
const provider = global.ethereumProvider
|
|
|
|
this.ens = new ENS({ provider, network })
|
|
|
|
this.checkName = debounce(this.lookupEnsName, 200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { input } = this.state
|
2020-11-07 00:16:51 +01:00
|
|
|
const { network, value, internalSearch } = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-10-22 14:50:15 +02:00
|
|
|
let newValue
|
|
|
|
|
2020-10-21 19:55:03 +02:00
|
|
|
// Set the value of our input based on QR code provided by parent
|
2020-10-22 17:08:54 +02:00
|
|
|
const newProvidedValue = input !== value && prevProps.value !== value
|
|
|
|
if (newProvidedValue) {
|
2020-10-22 14:50:15 +02:00
|
|
|
newValue = value
|
2020-10-21 18:35:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 21:56:44 +02:00
|
|
|
if (prevProps.network !== network) {
|
|
|
|
const provider = global.ethereumProvider
|
|
|
|
this.ens = new ENS({ provider, network })
|
2020-10-22 17:08:54 +02:00
|
|
|
if (!newProvidedValue) {
|
|
|
|
newValue = input
|
|
|
|
}
|
2020-10-22 14:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newValue !== undefined) {
|
|
|
|
this.onChange({ target: { value: newValue } })
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2020-11-07 00:16:51 +01:00
|
|
|
if (!internalSearch && prevProps.internalSearch) {
|
|
|
|
this.resetInput()
|
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resetInput = () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
updateEnsResolution,
|
|
|
|
updateEnsResolutionError,
|
|
|
|
onReset,
|
|
|
|
} = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
this.onChange({ target: { value: '' } })
|
|
|
|
onReset()
|
|
|
|
updateEnsResolution('')
|
|
|
|
updateEnsResolutionError('')
|
|
|
|
}
|
|
|
|
|
2020-08-15 13:58:11 +02:00
|
|
|
lookupEnsName = (ensName) => {
|
2020-08-27 00:56:14 +02:00
|
|
|
const { network } = this.props
|
2020-08-15 13:58:11 +02:00
|
|
|
const recipient = ensName.trim()
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
log.info(`ENS attempting to resolve name: ${recipient}`)
|
2020-11-03 00:41:28 +01:00
|
|
|
this.ens
|
|
|
|
.lookup(recipient)
|
2019-07-31 21:56:44 +02:00
|
|
|
.then((address) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address === ZERO_ADDRESS) {
|
|
|
|
throw new Error(this.context.t('noAddressForName'))
|
|
|
|
}
|
|
|
|
if (address === ZERO_X_ERROR_ADDRESS) {
|
|
|
|
throw new Error(this.context.t('ensRegistrationError'))
|
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
this.props.updateEnsResolution(address)
|
|
|
|
})
|
|
|
|
.catch((reason) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
isValidDomainName(recipient) &&
|
|
|
|
reason.message === 'ENS name not defined.'
|
|
|
|
) {
|
|
|
|
this.props.updateEnsResolutionError(
|
|
|
|
network === MAINNET_NETWORK_ID
|
|
|
|
? this.context.t('noAddressForName')
|
|
|
|
: this.context.t('ensNotFoundOnCurrentNetwork'),
|
|
|
|
)
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
|
|
|
log.error(reason)
|
|
|
|
this.props.updateEnsResolutionError(reason.message)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
onPaste = (event) => {
|
|
|
|
event.clipboardData.items[0].getAsString((text) => {
|
2019-07-31 21:56:44 +02:00
|
|
|
if (isValidAddress(text)) {
|
|
|
|
this.props.onPaste(text)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
onChange = (e) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
network,
|
|
|
|
onChange,
|
|
|
|
updateEnsResolution,
|
|
|
|
updateEnsResolutionError,
|
|
|
|
onValidAddressTyped,
|
2020-11-07 00:16:51 +01:00
|
|
|
internalSearch,
|
2020-11-03 00:41:28 +01:00
|
|
|
} = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
const input = e.target.value
|
|
|
|
const networkHasEnsSupport = getNetworkEnsSupport(network)
|
|
|
|
|
|
|
|
this.setState({ input }, () => onChange(input))
|
2020-11-07 00:16:51 +01:00
|
|
|
if (internalSearch) {
|
|
|
|
return null
|
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
// Empty ENS state if input is empty
|
|
|
|
// maybe scan ENS
|
2019-08-03 01:00:50 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
!networkHasEnsSupport &&
|
|
|
|
!isValidAddress(input) &&
|
|
|
|
!isValidAddressHead(input)
|
|
|
|
) {
|
2019-07-31 21:56:44 +02:00
|
|
|
updateEnsResolution('')
|
2020-11-03 00:41:28 +01:00
|
|
|
updateEnsResolutionError(
|
|
|
|
networkHasEnsSupport ? '' : 'Network does not support ENS',
|
|
|
|
)
|
2020-11-07 00:16:51 +01:00
|
|
|
return null
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 02:20:01 +01:00
|
|
|
if (isValidDomainName(input)) {
|
2019-07-31 21:56:44 +02:00
|
|
|
this.lookupEnsName(input)
|
2019-08-03 01:00:50 +02:00
|
|
|
} else if (onValidAddressTyped && isValidAddress(input)) {
|
|
|
|
onValidAddressTyped(input)
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
|
|
|
updateEnsResolution('')
|
|
|
|
updateEnsResolutionError('')
|
|
|
|
}
|
2020-11-07 00:16:51 +01:00
|
|
|
return null
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { t } = this.context
|
|
|
|
const { className, selectedAddress } = this.props
|
|
|
|
const { input } = this.state
|
|
|
|
|
|
|
|
if (selectedAddress) {
|
|
|
|
return this.renderSelected()
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-03-01 11:14:16 +01:00
|
|
|
<div className={classnames('ens-input', className)}>
|
2019-07-31 21:56:44 +02:00
|
|
|
<div
|
2020-03-01 11:14:16 +01:00
|
|
|
className={classnames('ens-input__wrapper', {
|
2019-07-31 21:56:44 +02:00
|
|
|
'ens-input__wrapper__status-icon--error': false,
|
|
|
|
'ens-input__wrapper__status-icon--valid': false,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div className="ens-input__wrapper__status-icon" />
|
|
|
|
<input
|
|
|
|
className="ens-input__wrapper__input"
|
|
|
|
type="text"
|
2019-09-03 19:47:54 +02:00
|
|
|
dir="auto"
|
2019-07-31 21:56:44 +02:00
|
|
|
placeholder={t('recipientAddressPlaceholder')}
|
|
|
|
onChange={this.onChange}
|
|
|
|
onPaste={this.onPaste}
|
|
|
|
value={selectedAddress || input}
|
|
|
|
autoFocus
|
2020-01-20 19:50:25 +01:00
|
|
|
data-testid="ens-input"
|
2019-07-31 21:56:44 +02:00
|
|
|
/>
|
2020-11-09 16:52:06 +01:00
|
|
|
<button
|
2020-03-01 11:14:16 +01:00
|
|
|
className={classnames('ens-input__wrapper__action-icon', {
|
2019-07-31 21:56:44 +02:00
|
|
|
'ens-input__wrapper__action-icon--erase': input,
|
|
|
|
'ens-input__wrapper__action-icon--qrcode': !input,
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
|
|
|
if (input) {
|
|
|
|
this.resetInput()
|
|
|
|
} else {
|
|
|
|
this.props.scanQrCode()
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderSelected() {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { t } = this.context
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
selectedAddress,
|
|
|
|
selectedName,
|
|
|
|
contact = {},
|
|
|
|
} = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
const name = contact.name || selectedName
|
|
|
|
|
|
|
|
return (
|
2020-03-01 11:14:16 +01:00
|
|
|
<div className={classnames('ens-input', className)}>
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="ens-input__wrapper ens-input__wrapper--valid">
|
2019-07-31 21:56:44 +02:00
|
|
|
<div className="ens-input__wrapper__status-icon ens-input__wrapper__status-icon--valid" />
|
|
|
|
<div
|
|
|
|
className="ens-input__wrapper__input ens-input__wrapper__input--selected"
|
|
|
|
placeholder={t('recipientAddress')}
|
|
|
|
onChange={this.onChange}
|
|
|
|
>
|
|
|
|
<div className="ens-input__selected-input__title">
|
|
|
|
{name || ellipsify(selectedAddress)}
|
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{name && (
|
|
|
|
<div className="ens-input__selected-input__subtitle">
|
|
|
|
{selectedAddress}
|
|
|
|
</div>
|
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="ens-input__wrapper__action-icon ens-input__wrapper__action-icon--erase"
|
|
|
|
onClick={this.resetInput}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
ensIcon(recipient) {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { hoverText } = this.state
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
|
|
|
className="#ensIcon"
|
|
|
|
title={hoverText}
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
top: '16px',
|
|
|
|
left: '-25px',
|
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.ensIconContents(recipient)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
ensIconContents() {
|
2019-12-03 16:30:03 +01:00
|
|
|
const { loadingEns, ensFailure, ensResolution, toError } = this.state
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2019-11-20 01:03:20 +01:00
|
|
|
if (toError) {
|
2020-08-12 21:06:57 +02:00
|
|
|
return null
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
if (loadingEns) {
|
|
|
|
return (
|
|
|
|
<img
|
|
|
|
src="images/loading.svg"
|
|
|
|
style={{
|
|
|
|
width: '30px',
|
|
|
|
height: '30px',
|
|
|
|
transform: 'translateY(-6px)',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ensFailure) {
|
|
|
|
return <i className="fa fa-warning fa-lg warning'" />
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (ensResolution && ensResolution !== ZERO_ADDRESS) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return (
|
|
|
|
<i
|
|
|
|
className="fa fa-check-circle fa-lg cursor-pointer"
|
|
|
|
style={{ color: 'green' }}
|
2020-02-15 21:34:12 +01:00
|
|
|
onClick={(event) => {
|
2019-07-31 21:56:44 +02:00
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
copyToClipboard(ensResolution)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2020-08-12 21:06:57 +02:00
|
|
|
|
|
|
|
return null
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function getNetworkEnsSupport(network) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return Boolean(networkMap[network])
|
|
|
|
}
|