checkTonAddress static method

bool checkTonAddress(
  1. dynamic addressString
)

Implementation

static bool checkTonAddress(addressString) {
  if (addressString.contains('-') || addressString.contains('_')) {
    addressString = addressString.replaceAll('-', '+').replaceAll('_', '/');
  }
  if (addressString.contains(':')) {
    List<String> arr = addressString.split(':');
    if (arr.length != 2) return false;
    int wc = int.tryParse(arr[0]) ?? -2;
    if (wc != 0 && wc != -1) return false;
    String hex = arr[1];
    if (hex.length != 64) return false;
  }

  if (addressString.length != 48) return false;

  final data = base64.decode(addressString);
  if (data.length != 36) return false;

  final addr = data.sublist(0, 34);
  final crc = data.sublist(34, 36);
  final calcedCrc = crc16(addr);

  if (!(calcedCrc[0] == crc[0] && calcedCrc[1] == crc[1])) return false;

  int tag = addr[0];

  const bounceableTag = 0x11;
  const nonBounceableTag = 0x51;
  const testFlag = 0x80;

  if ((tag & testFlag) != 0) tag = tag ^ testFlag;
  if (tag != bounceableTag && tag != nonBounceableTag) return false;

  int wc;
  if (addr[1] == 0xff) {
    wc = -1;
  } else {
    wc = addr[1];
  }

  if (wc != 0 && wc != -1) return false;

  return true;
}