2021-02-04 19:15:23 +01:00
|
|
|
import ethUtil from 'ethereumjs-util';
|
|
|
|
import contractMap from '@metamask/contract-metadata';
|
2021-02-27 06:26:04 +01:00
|
|
|
import { isConfusing } from 'unicode-confusables';
|
2020-01-09 04:34:58 +01:00
|
|
|
import {
|
2018-04-30 18:30:01 +02:00
|
|
|
REQUIRED_ERROR,
|
|
|
|
INVALID_RECIPIENT_ADDRESS_ERROR,
|
2019-01-22 20:05:59 +01:00
|
|
|
KNOWN_RECIPIENT_ADDRESS_ERROR,
|
2019-02-13 21:30:46 +01:00
|
|
|
INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR,
|
2021-02-27 06:26:04 +01:00
|
|
|
CONFUSING_ENS_ERROR,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../send.constants';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
isValidAddress,
|
|
|
|
isEthNetwork,
|
|
|
|
checkExistingAddresses,
|
2021-02-27 06:26:04 +01:00
|
|
|
isValidDomainName,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../../helpers/utils/util';
|
2019-01-22 20:05:59 +01:00
|
|
|
|
2021-03-02 16:43:53 +01:00
|
|
|
export function getToErrorObject(to, network) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let toError = null;
|
2018-04-07 00:29:51 +02:00
|
|
|
if (!to) {
|
2021-03-02 16:43:53 +01:00
|
|
|
toError = REQUIRED_ERROR;
|
|
|
|
} else if (!isValidAddress(to)) {
|
2020-11-03 00:41:28 +01:00
|
|
|
toError = isEthNetwork(network)
|
|
|
|
? INVALID_RECIPIENT_ADDRESS_ERROR
|
2021-02-04 19:15:23 +01:00
|
|
|
: INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR;
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
2019-05-08 19:34:56 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return { to: toError };
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getToWarningObject(to, tokens = [], sendToken = null) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let toWarning = null;
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
sendToken &&
|
|
|
|
(ethUtil.toChecksumAddress(to) in contractMap ||
|
|
|
|
checkExistingAddresses(to, tokens))
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
toWarning = KNOWN_RECIPIENT_ADDRESS_ERROR;
|
2021-02-27 06:26:04 +01:00
|
|
|
} else if (isValidDomainName(to) && isConfusing(to)) {
|
|
|
|
toWarning = CONFUSING_ENS_ERROR;
|
2019-01-23 19:09:56 +01:00
|
|
|
}
|
2021-02-27 06:26:04 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return { to: toWarning };
|
2019-01-23 19:09:56 +01:00
|
|
|
}
|