Skip to main content

tempo_consensus/
network_identity.rs

1use alloy_primitives::{FixedBytes, hex};
2use commonware_codec::ReadExt as _;
3use commonware_cryptography::bls12381::primitives::variant::{MinSig, Variant};
4use std::str::FromStr;
5
6#[derive(Debug, Clone, Copy)]
7pub(crate) struct NetworkIdentity(pub <MinSig as Variant>::Public);
8
9impl NetworkIdentity {
10    pub(crate) fn public_key(&self) -> <MinSig as Variant>::Public {
11        self.0
12    }
13}
14
15#[derive(Debug, thiserror::Error)]
16pub(crate) enum ParseNetworkIdentityError {
17    #[error("invalid hex string: {0}")]
18    Hex(#[from] hex::FromHexError),
19    #[error("must be a valid BLS public key: {0}")]
20    InvalidPublicKey(#[from] commonware_codec::Error),
21}
22
23impl FromStr for NetworkIdentity {
24    type Err = ParseNetworkIdentityError;
25
26    fn from_str(s: &str) -> Result<Self, Self::Err> {
27        let bytes = s.parse::<FixedBytes<96>>()?;
28
29        let mut bytes = bytes.as_slice();
30        let key = <MinSig as Variant>::Public::read(&mut bytes)?;
31
32        Ok(Self(key))
33    }
34}