toChecksumHex function

String toChecksumHex(
  1. EthereumAddress address
)

Returns the EIP-55 checksummed hex address for address.

Handles both web3dart 2.x (hexEip55) and 3.x (eip55With0x) APIs without static references to version-specific getters (uses dynamic).

Implementation

String toChecksumHex(EthereumAddress address) {
  final dynamic d = address;
  // Direct checksummed getters if available.
  try {
    final v = d.hexEip55;
    if (v is String) return v;
  } catch (_) {}
  try {
    final v = d.eip55With0x;
    if (v is String) return v;
  } catch (_) {}
  try {
    final v = d.eip55Without0x;
    if (v is String) return '0x$v';
  } catch (_) {}

  // Derive from non-checksummed hex.
  String? no0x;
  try {
    final v = d.hexNo0x;
    if (v is String) no0x = v;
  } catch (_) {}
  if (no0x == null) {
    try {
      final v = d.without0x;
      if (v is String) no0x = v;
    } catch (_) {}
  }
  if (no0x == null) {
    try {
      final v = d.hex;
      if (v is String) no0x = v.startsWith('0x') ? v.substring(2) : v;
    } catch (_) {}
  }
  if (no0x == null) {
    try {
      final v = d.with0x;
      if (v is String) no0x = v.startsWith('0x') ? v.substring(2) : v;
    } catch (_) {}
  }
  if (no0x == null) {
    try {
      final bytes = d.addressBytes as Uint8List?;
      if (bytes != null) {
        no0x = bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
      }
    } catch (_) {}
  }
  if (no0x == null) {
    try {
      final bytes = d.value as Uint8List?;
      if (bytes != null) {
        no0x = bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
      }
    } catch (_) {}
  }
  if (no0x != null) {
    final checksummed = toChecksumAddress(no0x.toLowerCase());
    return '0x$checksummed';
  }
  // Fallback – should not happen.
  return address.toString();
}