def to_addresses_field(hostname: str, port: int) -> bytes:
        """Encodes a hostname/port pair into a BOLT-7 'addresses' field."""
        if (NodeInfo.invalid_announcement_hostname(hostname)
                or port is None or port <= 0 or port > 65535):
            return b''
        port_bytes = port.to_bytes(2, 'big')
        if is_ip_address(hostname):  # ipv4 or ipv6
            ip_addr = ipaddress.ip_address(hostname)
            if ip_addr.version == 4:
                return b'\x01' + ip_addr.packed + port_bytes
            elif ip_addr.version == 6:
                return b'\x02' + ip_addr.packed + port_bytes
            return b''
        elif hostname.endswith('.onion'):  # Tor onion v3
            onion_addr: bytes = base64.b32decode(hostname[:-6], casefold=True)
            return b'\x04' + onion_addr + port_bytes
        else:
            try:
                hostname_ascii: bytes = hostname.encode('ascii')
            except UnicodeEncodeError:
                # encoding single characters to punycode (according to spec) doesn't make sense
                # as you can't differentiate them from regular ascii? encoding the whole string to punycode
                # doesn't work either as the receiver would interpret it as regular ascii.
                # hostname_ascii: bytes = hostname.encode('punycode')
                return b''
            if len(hostname_ascii) + 3 > 258:  # + 1 byte for length and 2 for port
                return b''  # too long
            return b'\x05' + len(hostname_ascii).to_bytes(1, "big") + hostname_ascii + port_bytes
Encodes a hostname/port pair into a BOLT-7 'addresses' field.
electrum/channel_db.py