//! EIP-191 challenge signing payloads.
//!
//! This module implements construction of Ethereum EIP-191 (`personal_sign`)
//! signing digests for server-issued authentication challenges.
//!
//! ## When This Is Used
//!
//! - **WebSocket authentication**: The server sends a challenge message that must
//!   be signed to prove ownership of the wallet address.
//! - **REST authentication**: The `rest_auth` module uses this
//!   internally when generating authentication credentials.
//!
//! ## EIP-191 Format
//!
//! The signed message follows the EIP-191 "personal_sign" format:
//! ```text
//! \x19Ethereum Signed Message:\n{length}{message}
//! ```
//!
//! This is the same format used by MetaMask's `personal_sign` and similar wallet
//! signing methods.
//!
//! ## Note
//!
//! For WebSocket authentication, you typically don't call this directly - use
//! [`WsSession::authenticate_and_subscribe`](crate::ws::client::WsSession::authenticate_and_subscribe)
//! which handles the challenge-response flow automatically.

use alloy::primitives::{B256, keccak256};

/// Compute the EIP-191 signing digest for a message.
///
/// Returns the 32-byte keccak256 hash of the EIP-191 prefixed message,
/// ready for signing with ECDSA over secp256k1.
///
/// # Arguments
/// * `message` - The raw message string to sign
///
/// # Returns
/// The hash of `\x19Ethereum Signed Message:\n{len}{message}`
pub fn signing_digest(message: &str) -> B256 {
    let message_bytes = message.as_bytes();
    let prefix = format!("\x19Ethereum Signed Message:\n{}", message_bytes.len());

    let mut payload = Vec::with_capacity(prefix.len() + message_bytes.len());
    payload.extend_from_slice(prefix.as_bytes());
    payload.extend_from_slice(message_bytes);

    keccak256(payload)
}
