isValidEthereumAddress function

bool isValidEthereumAddress(
  1. String address
)

Returns whether a given Ethereum address is valid.

Implementation

bool isValidEthereumAddress(String address) {
  if (!isValidFormat(address)) {
    return false;
  }

  final addr = strip0x(address);
  // if all lowercase or all uppercase, as in checksum is not present
  if (RegExp(r"^[0-9a-f]{40}$").hasMatch(addr) ||
      RegExp(r"^[0-9A-F]{40}$").hasMatch(addr)) {
    return true;
  }

  String checksumAddress;
  try {
    checksumAddress = checksumEthereumAddress(address);
  } catch (err) {
    return false;
  }

  return addr == checksumAddress.substring(2);
}