2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import copyToClipboard from 'copy-to-clipboard/index';
|
|
|
|
import ENS from 'ethjs-ens';
|
|
|
|
import networkMap from 'ethereum-ens-network-map';
|
|
|
|
import log from 'loglevel';
|
2021-05-17 21:00:59 +02:00
|
|
|
import { isHexString } from 'ethereumjs-util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { ellipsify } from '../../send.utils';
|
2021-05-17 21:00:59 +02:00
|
|
|
import { isValidDomainName } from '../../../../helpers/utils/util';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { MAINNET_NETWORK_ID } from '../../../../../shared/constants/network';
|
2021-05-17 21:00:59 +02:00
|
|
|
import {
|
|
|
|
isBurnAddress,
|
|
|
|
isValidHexAddress,
|
|
|
|
} from '../../../../../shared/modules/hexstring-utils';
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
// Local Constants
|
2021-02-04 19:15:23 +01:00
|
|
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
|
|
const ZERO_X_ERROR_ADDRESS = '0x';
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
export default class EnsInput extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
input: '',
|
|
|
|
toError: null,
|
2019-12-03 16:30:03 +01:00
|
|
|
ensResolution: undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { network, internalSearch } = this.props;
|
|
|
|
const networkHasEnsSupport = getNetworkEnsSupport(network);
|
|
|
|
this.setState({ ensResolution: ZERO_ADDRESS });
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-07 00:16:51 +01:00
|
|
|
if (networkHasEnsSupport && !internalSearch) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const provider = global.ethereumProvider;
|
|
|
|
this.ens = new ENS({ provider, network });
|
|
|
|
this.checkName = debounce(this.lookupEnsName, 200);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { input } = this.state;
|
|
|
|
const { network, value, internalSearch } = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let newValue;
|
2020-10-22 14:50:15 +02:00
|
|
|
|
2020-10-21 19:55:03 +02:00
|
|
|
// Set the value of our input based on QR code provided by parent
|
2021-02-04 19:15:23 +01:00
|
|
|
const newProvidedValue = input !== value && prevProps.value !== value;
|
2020-10-22 17:08:54 +02:00
|
|
|
if (newProvidedValue) {
|
2021-02-04 19:15:23 +01:00
|
|
|
newValue = value;
|
2020-10-21 18:35:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 21:56:44 +02:00
|
|
|
if (prevProps.network !== network) {
|
2021-03-03 01:20:37 +01:00
|
|
|
if (getNetworkEnsSupport(network)) {
|
|
|
|
const provider = global.ethereumProvider;
|
|
|
|
this.ens = new ENS({ provider, network });
|
|
|
|
this.checkName = debounce(this.lookupEnsName, 200);
|
|
|
|
if (!newProvidedValue) {
|
|
|
|
newValue = input;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ens is null on mount on a network that does not have ens support
|
|
|
|
// this is intended to prevent accidental lookup of domains across
|
|
|
|
// networks
|
|
|
|
this.ens = null;
|
|
|
|
this.checkName = null;
|
2020-10-22 17:08:54 +02:00
|
|
|
}
|
2020-10-22 14:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newValue !== undefined) {
|
2021-02-04 19:15:23 +01:00
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.resetInput();
|
2020-11-07 00:16:51 +01:00
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resetInput = () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
updateEnsResolution,
|
|
|
|
updateEnsResolutionError,
|
|
|
|
onReset,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
|
|
|
this.onChange({ target: { value: '' } });
|
|
|
|
onReset();
|
|
|
|
updateEnsResolution('');
|
|
|
|
updateEnsResolutionError('');
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-08-15 13:58:11 +02:00
|
|
|
lookupEnsName = (ensName) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { network } = this.props;
|
|
|
|
const recipient = ensName.trim();
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01: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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(this.context.t('noAddressForName'));
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
|
|
|
if (address === ZERO_X_ERROR_ADDRESS) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(this.context.t('ensRegistrationError'));
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.updateEnsResolution(address);
|
2019-07-31 21:56:44 +02:00
|
|
|
})
|
|
|
|
.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'),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(reason);
|
|
|
|
this.props.updateEnsResolutionError(reason.message);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
onPaste = (event) => {
|
|
|
|
event.clipboardData.items[0].getAsString((text) => {
|
2021-05-17 21:00:59 +02:00
|
|
|
if (
|
|
|
|
!isBurnAddress(text) &&
|
|
|
|
isValidHexAddress(text, { mixedCaseUseChecksum: true })
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.onPaste(text);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
|
|
|
const input = e.target.value;
|
|
|
|
const networkHasEnsSupport = getNetworkEnsSupport(network);
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ input }, () => onChange(input));
|
2020-11-07 00:16:51 +01:00
|
|
|
if (internalSearch) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-11-07 00:16:51 +01:00
|
|
|
}
|
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 &&
|
2021-05-17 21:00:59 +02:00
|
|
|
!(
|
|
|
|
isBurnAddress(input) === false &&
|
|
|
|
isValidHexAddress(input, { mixedCaseUseChecksum: true })
|
|
|
|
) &&
|
|
|
|
!isHexString(input)
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
updateEnsResolution('');
|
2020-11-03 00:41:28 +01:00
|
|
|
updateEnsResolutionError(
|
|
|
|
networkHasEnsSupport ? '' : 'Network does not support ENS',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return null;
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 02:20:01 +01:00
|
|
|
if (isValidDomainName(input)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.lookupEnsName(input);
|
2021-05-17 21:00:59 +02:00
|
|
|
} else if (
|
|
|
|
onValidAddressTyped &&
|
|
|
|
!isBurnAddress(input) &&
|
|
|
|
isValidHexAddress(input, { mixedCaseUseChecksum: true })
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
onValidAddressTyped(input);
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
updateEnsResolution('');
|
|
|
|
updateEnsResolutionError('');
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
|
|
|
const { className, selectedAddress } = this.props;
|
|
|
|
const { input } = this.state;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
if (selectedAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.renderSelected();
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.resetInput();
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.scanQrCode();
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderSelected() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
selectedAddress,
|
|
|
|
selectedName,
|
|
|
|
contact = {},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
|
|
|
const name = contact.name || selectedName;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
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>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
ensIcon(recipient) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { hoverText } = this.state;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
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>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
ensIconContents() {
|
2021-02-04 19:15:23 +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) {
|
2021-02-04 19:15:23 +01: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)',
|
|
|
|
}}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ensFailure) {
|
2021-04-26 16:31:20 +02:00
|
|
|
return <i className="fa fa-warning fa-lg warning" />;
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
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) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
copyToClipboard(ensResolution);
|
2019-07-31 21:56:44 +02:00
|
|
|
}}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2020-08-12 21:06:57 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function getNetworkEnsSupport(network) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Boolean(networkMap[network]);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|