NetworkAddress.ipv4 constructor

NetworkAddress.ipv4(
  1. String ipv4,
  2. int port, {
  3. int services = ServiceFlags.nodeNetwork,
})

Create IPv4 address (mapped to IPv6)

Implementation

factory NetworkAddress.ipv4(String ipv4, int port, {int services = ServiceFlags.nodeNetwork}) {
  final parts = ipv4.split('.');
  if (parts.length != 4) {
    throw ArgumentError('Invalid IPv4 address: $ipv4');
  }

  // Validate each octet
  for (final part in parts) {
    final octet = int.tryParse(part);
    if (octet == null || octet < 0 || octet > 255) {
      throw FormatException('Invalid IPv4 octet: $part');
    }
  }

  // IPv4-mapped IPv6 address: ::ffff:a.b.c.d
  final ip = <int>[
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, // IPv4-mapped prefix
    ...parts.map((p) => int.parse(p)), // IPv4 bytes
  ];

  return NetworkAddress(
    services: services,
    ip: ip,
    port: port,
  );
}