fastn_net/
get_endpoint.rs

1/// Creates an Iroh endpoint configured for fastn networking.
2///
3/// This function creates an Iroh endpoint with:
4/// - Local network discovery enabled
5/// - N0 discovery (DHT-based) enabled  
6/// - ALPN set to `/fastn/identity/0.1`
7/// - The provided secret key for identity
8///
9/// # Errors
10///
11/// Returns an error if the endpoint fails to bind to the network.
12pub async fn get_endpoint(secret_key: fastn_id52::SecretKey) -> eyre::Result<iroh::Endpoint> {
13    // Convert fastn_id52::SecretKey to iroh::SecretKey
14    let iroh_secret_key = iroh::SecretKey::from_bytes(&secret_key.to_bytes());
15
16    match iroh::Endpoint::builder()
17        .discovery_n0()
18        .discovery_local_network()
19        .alpns(vec![crate::APNS_IDENTITY.into()])
20        .secret_key(iroh_secret_key)
21        .bind()
22        .await
23    {
24        Ok(ep) => Ok(ep),
25        Err(e) => {
26            // https://github.com/n0-computer/iroh/issues/2741
27            // this is why you MUST NOT use anyhow::Error etc. in library code.
28            Err(eyre::anyhow!("failed to bind to iroh network2: {e:?}"))
29        }
30    }
31}