checkBase58 function

bool checkBase58(
  1. String address, {
  2. int length = 25,
})

Check base58 address with checksum, such as btc.

Implementation

bool checkBase58(String address, {int length = 25}) {
  Uint8List decoded = base58.decode(address);
  if (decoded.length != length) {
    return false;
  }
  Uint8List data = decoded.sublist(0, decoded.length - 4);
  Uint8List checksum = decoded.sublist(decoded.length - 4);
  Uint8List midData = Uint8List.fromList(sha256.convert(data).bytes);
  Uint8List calculatedChecksum =
      Uint8List.fromList(sha256.convert(midData).bytes.sublist(0, 4));
  for (int i = 0; i < 4; i++) {
    if (checksum[i] != calculatedChecksum[i]) {
      return false;
    }
  }
  return true;
}