"""Local node identity helpers."""

from __future__ import annotations

import hashlib
import secrets

from rc.config import NodeConfig


def generate_node_secret() -> str:
    """Create a high-entropy bearer secret for one node."""
    return secrets.token_urlsafe(32)


def hash_node_secret(secret: str) -> str:
    """Hash a node secret for storage in Supabase."""
    return hashlib.sha256(secret.encode("utf-8")).hexdigest()


def node_auth_payload(config: NodeConfig) -> dict[str, str]:
    """Return the common RPC auth payload for this node."""
    return {
        "p_node_id": config.node_id,
        "p_node_secret_hash": hash_node_secret(config.node_secret),
    }
