def is_public_key_valid(key):
    """
    Check public key is valid.

    :param key: public key in HEX or bytes string format.
    :return: boolean.
    """
    if isinstance(key, str):
        key = bytes.fromhex(key)
    if len(key) < 33:
        return False
    elif key[0] == 4 and len(key) != 65:
        return False
    elif key[0] == 2 or key[0] == 3:
        if len(key) != 33:
            return False
    return True
