getChecksumAddress function

String getChecksumAddress(
  1. String address
)

Implementation

String getChecksumAddress(String address) {
  if (!isHexString(address, length: 20)) {
    throw 'invalid hex value';
  }
  address = address.toLowerCase();

  final chars = address.substring(2).split("");

  Uint8List expanded = Uint8List(40);
  for (var i = 0; i < 40; i++) {
    expanded[i] = chars[i].codeUnitAt(0);
  }

  final hashed = keccak256(expanded);
  for (var i = 0; i < 40; i += 2) {
    if ((hashed[i >> 1] >> 4) >= 8) {
      chars[i] = chars[i].toUpperCase();
    }
    if ((hashed[i >> 1] & 0x0f) >= 8) {
      chars[i + 1] = chars[i + 1].toUpperCase();
    }
  }

  return '0x' + chars.join('');
}