thinWaistForm function

ThinWaist? thinWaistForm(
  1. MultiAddr a
)

Creates a thin waist form of a multiaddress.

A thin waist address is an address that contains an IP and a TCP/UDP port.

Implementation

ThinWaist? thinWaistForm(MultiAddr a) {
  int i = 0;
  final components = a.components;

  if (components.length < 2) {
    _log.fine('Not a thinwaist address: $a (too few components)');
    return null;
  }

  // Check first component is IP
  final (protocol1, _ ) = components[0];
  if (protocol1.code != Protocols.ip4 &&
      protocol1.code != Protocols.ip6) {
    _log.fine('Not a thinwaist address: $a (first component not IP)');
    return null;
  }

  final (protocol2, _ ) = components[1];
  // Check second component is TCP or UDP
  if (protocol2.code != Protocols.tcp &&
      protocol2.code != Protocols.udp) {
    _log.fine('Not a thinwaist address: $a (second component not TCP/UDP)');
    return null;
  }

  // Split the address into thin waist and rest
  // Create a multiaddr with just the first two components
  final twStr = "/${protocol1.name}/${components[0].$2}/${protocol2.name}/${components[1].$2}";
  final tw = MultiAddr(twStr);

  final restComponents = components.sublist(2);
  final rest = restComponents.isEmpty
      ? MultiAddr("")
      : MultiAddr(restComponents.map((c) => "/${c.$1.name}/${c.$2}").join(""));

  return ThinWaist(addr: a, tw: tw, rest: rest);
}