2020-08-18 21:18:25 +02:00
|
|
|
import ethUtil from 'ethereumjs-util'
|
2020-12-01 23:55:01 +01:00
|
|
|
import contractMap from '@metamask/contract-metadata'
|
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,
|
2020-01-09 04:34:58 +01:00
|
|
|
} from '../../send.constants'
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
isValidAddress,
|
|
|
|
isEthNetwork,
|
|
|
|
checkExistingAddresses,
|
|
|
|
} from '../../../../helpers/utils/util'
|
2019-01-22 20:05:59 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getToErrorObject(to, hasHexData = false, network) {
|
2020-03-19 18:32:38 +01:00
|
|
|
let toError = null
|
2018-04-07 00:29:51 +02:00
|
|
|
if (!to) {
|
2018-10-01 14:50:33 +02:00
|
|
|
if (!hasHexData) {
|
|
|
|
toError = REQUIRED_ERROR
|
|
|
|
}
|
2020-10-13 15:48:22 +02:00
|
|
|
} else if (!isValidAddress(to) && !toError) {
|
2020-11-03 00:41:28 +01:00
|
|
|
toError = isEthNetwork(network)
|
|
|
|
? INVALID_RECIPIENT_ADDRESS_ERROR
|
|
|
|
: INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
2019-05-08 19:34:56 +02:00
|
|
|
|
2018-04-07 00:29:51 +02:00
|
|
|
return { to: toError }
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getToWarningObject(to, tokens = [], sendToken = null) {
|
2020-03-19 18:32:38 +01:00
|
|
|
let toWarning = null
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
sendToken &&
|
|
|
|
(ethUtil.toChecksumAddress(to) in contractMap ||
|
|
|
|
checkExistingAddresses(to, tokens))
|
|
|
|
) {
|
2019-01-23 19:09:56 +01:00
|
|
|
toWarning = KNOWN_RECIPIENT_ADDRESS_ERROR
|
|
|
|
}
|
|
|
|
return { to: toWarning }
|
|
|
|
}
|