You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.9 KiB
53 lines
1.9 KiB
import {
|
|
isHexString,
|
|
isValidAddress,
|
|
isValidChecksumAddress,
|
|
addHexPrefix,
|
|
} from 'ethereumjs-util';
|
|
|
|
export const BURN_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
|
|
export function isBurnAddress(address) {
|
|
return address === BURN_ADDRESS;
|
|
}
|
|
|
|
/**
|
|
* Validates that the input is a hex address. This utility method is a thin
|
|
* wrapper around ethereumjs-util.isValidAddress, with the exception that it
|
|
* does not throw an error when provided values that are not hex strings. In
|
|
* addition, and by default, this method will return true for hex strings that
|
|
* meet the length requirement of a hex address, but are not prefixed with `0x`
|
|
* Finally, if the mixedCaseUseChecksum flag is true and a mixed case string is
|
|
* provided this method will validate it has the proper checksum formatting.
|
|
* @param {string} possibleAddress - Input parameter to check against
|
|
* @param {Object} [options] - options bag
|
|
* @param {boolean} [options.allowNonPrefixed] - If true will first ensure '0x'
|
|
* is prepended to the string
|
|
* @param {boolean} [options.mixedCaseUseChecksum] - If true will treat mixed
|
|
* case addresses as checksum addresses and validate that proper checksum
|
|
* format is used
|
|
* @returns {boolean} whether or not the input is a valid hex address
|
|
*/
|
|
export function isValidHexAddress(
|
|
possibleAddress,
|
|
{ allowNonPrefixed = true, mixedCaseUseChecksum = false } = {},
|
|
) {
|
|
const addressToCheck = allowNonPrefixed
|
|
? addHexPrefix(possibleAddress)
|
|
: possibleAddress;
|
|
if (!isHexString(addressToCheck)) {
|
|
return false;
|
|
}
|
|
|
|
if (mixedCaseUseChecksum) {
|
|
const prefixRemoved = addressToCheck.slice(2);
|
|
const lower = prefixRemoved.toLowerCase();
|
|
const upper = prefixRemoved.toUpperCase();
|
|
const allOneCase = prefixRemoved === lower || prefixRemoved === upper;
|
|
if (!allOneCase) {
|
|
return isValidChecksumAddress(addressToCheck);
|
|
}
|
|
}
|
|
|
|
return isValidAddress(addressToCheck);
|
|
}
|
|
|