1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/ui/app/pages/send/send-content/add-recipient/add-recipient.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

import ethUtil from 'ethereumjs-util';
import contractMap from '@metamask/contract-metadata';
import { isConfusing } from 'unicode-confusables';
import {
REQUIRED_ERROR,
INVALID_RECIPIENT_ADDRESS_ERROR,
KNOWN_RECIPIENT_ADDRESS_ERROR,
INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR,
CONFUSING_ENS_ERROR,
CONTRACT_ADDRESS_ERROR,
} from '../../send.constants';
2020-11-03 00:41:28 +01:00
import {
isValidAddress,
checkExistingAddresses,
isValidDomainName,
isOriginContractAddress,
isDefaultMetaMaskChain,
} from '../../../../helpers/utils/util';
export function getToErrorObject(to, sendTokenAddress, chainId) {
let toError = null;
2018-04-07 00:29:51 +02:00
if (!to) {
toError = REQUIRED_ERROR;
} else if (!isValidAddress(to)) {
toError = isDefaultMetaMaskChain(chainId)
2020-11-03 00:41:28 +01:00
? INVALID_RECIPIENT_ADDRESS_ERROR
: INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR;
} else if (isOriginContractAddress(to, sendTokenAddress)) {
toError = CONTRACT_ADDRESS_ERROR;
2018-04-07 00:29:51 +02: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) {
let toWarning = null;
2020-11-03 00:41:28 +01:00
if (
sendToken &&
(ethUtil.toChecksumAddress(to) in contractMap ||
checkExistingAddresses(to, tokens))
) {
toWarning = KNOWN_RECIPIENT_ADDRESS_ERROR;
} else if (isValidDomainName(to) && isConfusing(to)) {
toWarning = CONFUSING_ENS_ERROR;
2019-01-23 19:09:56 +01:00
}
return { to: toWarning };
2019-01-23 19:09:56 +01:00
}