ftnet_utils/
lib.rs

1extern crate self as ftnet_utils;
2
3pub mod proxy;
4
5pub mod connection;
6pub mod get_endpoint;
7pub mod http;
8mod http_connection;
9pub mod http_peer_proxy;
10pub mod protocol;
11mod utils;
12
13pub use connection::{IDMap, PeerConnections};
14pub use get_endpoint::get_endpoint;
15pub use http::ProxyResult;
16pub use http_connection::{ConnectionManager, ConnectionPool, ConnectionPools};
17pub use protocol::{APNS_IDENTITY, Protocol};
18pub use utils::{FrameReader, frame_reader, id52_to_public_key, public_key_to_id52};
19
20use eyre::WrapErr;
21
22// TODO: convert it to use id52 (we will store id52 in keyring)
23fn keyring_entry(id: &str) -> eyre::Result<keyring::Entry> {
24    keyring::Entry::new("FTNet", id)
25        .wrap_err_with(|| format!("failed to create keyring Entry for {id}"))
26}
27
28// TODO: convert it to use id52 (we will store id52 in keyring)
29pub fn save_secret(secret_key: &iroh::SecretKey) -> eyre::Result<()> {
30    let public = secret_key.public().to_string();
31    Ok(keyring_entry(public.as_str())?.set_secret(&secret_key.to_bytes())?)
32}
33
34// TODO: convert it to use id52 (we will store id52 in keyring)
35pub fn get_secret(id: &str) -> eyre::Result<iroh::SecretKey> {
36    let entry = keyring_entry(id)?;
37    let secret = entry
38        .get_secret()
39        .wrap_err_with(|| format!("keyring: secret not found for {id}"))?;
40
41    if secret.len() != 32 {
42        return Err(eyre::anyhow!(
43            "keyring: secret has invalid length: {}",
44            secret.len()
45        ));
46    }
47
48    let bytes: [u8; 32] = secret.try_into().unwrap(); // unwrap ok as already asserted
49    Ok(iroh::SecretKey::from_bytes(&bytes))
50}
51
52pub fn create_public_key() -> eyre::Result<iroh::PublicKey> {
53    let mut rng = rand::rngs::OsRng;
54    let secret_key = iroh::SecretKey::generate(&mut rng);
55    // we do not want to keep secret key in memory, only in keychain
56    save_secret(&secret_key).wrap_err_with(|| "failed to store secret key to keychain")?;
57    Ok(secret_key.public())
58}
59
60pub fn create_secret_key() -> iroh::SecretKey {
61    let mut rng = rand::rngs::OsRng;
62    iroh::SecretKey::generate(&mut rng)
63}