toChecksum method

String toChecksum(
  1. Uint8List keccak256(
    1. Uint8List
    )
)

Returns the address as a checksummed hex string (EIP-55).

Note: This requires keccak256 hashing. If not available, use hex for lowercase representation.

Implementation

String toChecksum(Uint8List Function(Uint8List) keccak256) {
  final lowercaseHex = HexUtils.strip0x(hex);
  final hash = keccak256(Uint8List.fromList(lowercaseHex.codeUnits));
  final hashHex = HexUtils.strip0x(HexUtils.encode(hash));

  final result = StringBuffer('0x');
  for (var i = 0; i < lowercaseHex.length; i++) {
    final char = lowercaseHex[i];
    final hashChar = hashHex[i];

    // If the hash character is >= 8, uppercase the address character
    if (int.parse(hashChar, radix: 16) >= 8) {
      result.write(char.toUpperCase());
    } else {
      result.write(char);
    }
  }

  return result.toString();
}