ftnet_utils/
get_endpoint.rs

1pub enum Key {
2    ID(String),
3    ID52(String),
4    SecretKey(iroh::SecretKey),
5}
6
7pub async fn get_endpoint(key: Key) -> eyre::Result<iroh::Endpoint> {
8    use eyre::WrapErr;
9
10    let secret_key = match key {
11        Key::ID(id) => ftnet_utils::get_secret(id.as_str())
12            .wrap_err_with(|| format!("failed to get secret key from keychain for {id}"))?,
13        Key::SecretKey(key) => key,
14        Key::ID52(v) => {
15            let public_key = ftnet_utils::utils::id52_to_public_key(&v)?;
16            ftnet_utils::get_secret(public_key.to_string().as_str())?
17        }
18    };
19
20    match iroh::Endpoint::builder()
21        .discovery_n0()
22        .discovery_local_network()
23        .alpns(vec![ftnet_utils::APNS_IDENTITY.into()])
24        .secret_key(secret_key)
25        .bind()
26        .await
27    {
28        Ok(ep) => Ok(ep),
29        Err(e) => {
30            // https://github.com/n0-computer/iroh/issues/2741
31            // this is why you MUST NOT use anyhow::Error etc. in library code.
32            Err(eyre::anyhow!("failed to bind to iroh network2: {e:?}"))
33        }
34    }
35}