getObserver function

String? getObserver(
  1. MultiAddr ma
)

getObserver returns the observer for the multiaddress For an IPv4 multiaddress the observer is the IP address For an IPv6 multiaddress the observer is the first /56 prefix of the IP address

Implementation

String? getObserver(MultiAddr ma) {
  try {
    InternetAddress? ip = ma.toIP();
    if (ip == null) {
      return null;
    }

    if (ip.type == InternetAddressType.IPv4) {
      return ip.address;
    } else {
      // For IPv6, use the /56 prefix as the observer
      // This is a simplification - in a real implementation we would need to mask the IP
      // with a /56 CIDR mask, but for now we'll just use the first 7 bytes (56 bits)
      final bytes = ip.rawAddress;
      if (bytes.length >= 7) {
        return bytes.sublist(0, 7).toString();
      }
      return ip.address;
    }
  } catch (e) {
    _log.fine('Error getting observer for $ma: $e');
    return null;
  }
}