# Functions

## `arnacon_authenticate(ens, x_data, x_sign)`

Authenticates a user by verifying ENS domain ownership through cryptographic signature validation. This function implements the core authentication logic by resolving ENS ownership, recovering the signer's address from the provided signature, and validating that the signer owns the specified ENS domain.

Authentication Flow:

1. Resolve ENS domain ownership (handling both traditional and wrapped domains)

2. Extract and validate timestamp from X-Data header

3. Recover Ethereum address from ECDSA signature

4. Compare recovered address with ENS domain owner

Parameters:

- user_identifier (string) - ENS domain name (e.g., "user.cellact.global") or plain Ethereum address (e.g., "0x1234...abcd")

- x_data (string) - X-Data header value in format "UUID:TIMESTAMP" (can be empty to extract from headers)

- x_sign (string) - X-Sign header value containing Ethereum signature (can be empty to extract from headers)

Return Type: integer

Return codes:

- 1 - Authentication successful

- -1 - Authentication failed (invalid signature, ENS ownership mismatch, timeout, or error)

This function can be used from REQUEST_ROUTE.

### arnacon_authenticate usage

    ...
    # Basic authentication with explicit parameters
    if (arnacon_authenticate("$fU", "$hdr(X-Data)", "$hdr(X-Sign)")) {
        xlog("L_INFO", "User $fU authenticated successfully\n");
        # Allow registration/call
    } else {
        xlog("L_ERR", "Authentication failed for $fU\n");
        sl_send_reply("403", "Forbidden");
        exit;
    }
    ...

    ...
    # Automatic header extraction (pass empty strings)
    if (arnacon_authenticate("$fU", "", "")) {
        xlog("L_INFO", "ENS domain $fU authenticated\n");
        # Proceed with request processing
    } else {
        sl_send_reply("403", "Forbidden");
        exit;
    }
    ...

    ...
    # Complete authentication flow with error handling
    route[AUTH] {
        # Check if user exists first
        if (!arnacon_user_exists("$fU")) {
            xlog("L_ERR", "User $fU does not exist\n");
            sl_send_reply("404", "User Not Found");
            exit;
        }

        # Authenticate with headers
        if (arnacon_authenticate("$fU", "$hdr(X-Data)", "$hdr(X-Sign)")) {
            xlog("L_INFO", "User $fU authenticated successfully\n");
            return;
        } else {
            xlog("L_ERR", "Authentication failed for $fU\n");
            sl_send_reply("403", "Forbidden");
            exit;
        }
    }
    ...

## `arnacon_user_exists(ens)`

Checks if an ENS domain has an owner, effectively verifying if the user exists in the ENS system. This function performs ENS resolution to determine if the domain is registered and has a valid owner address (not the zero address).

Use Case: This function is useful for pre-authentication checks to provide better error messages to clients and avoid unnecessary authentication attempts for non-existent ENS domains.

ENS Resolution: The function handles both traditional ENS ownership through the ENS Registry and wrapped domain ownership through the ENS Name Wrapper contract, automatically detecting the appropriate resolution method.

Parameters:

- ens (string) - ENS domain name to check

Return Type: integer

Return codes:

- 1 - User exists (ENS has a valid owner)

- -1 - User does not exist (ENS unregistered, zero owner, or error)

This function can be used from REQUEST_ROUTE.

### arnacon_user_exists usage

    ...
    # Basic user existence check
    if (arnacon_user_exists("$fU")) {
        xlog("L_INFO", "User $fU exists\n");
        # Proceed with authentication
    } else {
        xlog("L_ERR", "User $fU does not exist\n");
        sl_send_reply("404", "User Not Found");
        exit;
    }
    ...

    ...
    # Pre-authentication validation
    route[PREAUTH] {
        if (!arnacon_user_exists("$fU")) {
            xlog("L_WARN", "Authentication attempt for non-existent user: $fU\n");
            sl_send_reply("404", "User Not Found");
            exit;
        }
        xlog("L_INFO", "User $fU exists, proceeding with authentication\n");
    }
    ...

    ...
    # Registration flow with user existence check
    if (is_method("REGISTER")) {
        if (!arnacon_user_exists("$fU")) {
            sl_send_reply("404", "ENS domain not registered");
            exit;
        }
        # Continue with authentication
        route(AUTH);
    }
    ...