ftnet_utils/
utils.rs

1use eyre::WrapErr;
2
3pub fn id52_to_public_key(id: &str) -> eyre::Result<iroh::PublicKey> {
4    let bytes = data_encoding::BASE32_DNSSEC.decode(id.as_bytes())?;
5    if bytes.len() != 32 {
6        return Err(eyre::anyhow!(
7            "read: id has invalid length: {}",
8            bytes.len()
9        ));
10    }
11
12    let bytes: [u8; 32] = bytes.try_into().unwrap(); // unwrap ok as already asserted
13
14    iroh::PublicKey::from_bytes(&bytes).wrap_err_with(|| "failed to parse id to public key")
15}
16
17pub fn public_key_to_id52(key: &iroh::PublicKey) -> String {
18    data_encoding::BASE32_DNSSEC.encode(key.as_bytes())
19}
20
21pub type FrameReader =
22    tokio_util::codec::FramedRead<iroh::endpoint::RecvStream, tokio_util::codec::LinesCodec>;
23
24pub fn frame_reader(recv: iroh::endpoint::RecvStream) -> FrameReader {
25    FrameReader::new(recv, tokio_util::codec::LinesCodec::new())
26}