tendermint/crypto/ed25519/
verification_key.rs

1use crate::Error;
2
3#[derive(Copy, Clone, Eq, PartialEq)]
4pub struct VerificationKey([u8; 32]);
5
6impl core::fmt::Display for VerificationKey {
7    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
8        for byte in &self.0 {
9            write!(f, "{byte:02x}")?;
10        }
11        Ok(())
12    }
13}
14
15impl core::fmt::Debug for VerificationKey {
16    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17        <Self as core::fmt::Display>::fmt(self, f)
18    }
19}
20
21impl VerificationKey {
22    #[allow(dead_code)]
23    pub(super) fn new(bytes: [u8; 32]) -> Self {
24        Self(bytes)
25    }
26
27    pub fn as_bytes(&self) -> &[u8] {
28        &self.0
29    }
30}
31
32impl TryFrom<&'_ [u8]> for VerificationKey {
33    type Error = Error;
34
35    fn try_from(slice: &'_ [u8]) -> Result<Self, Self::Error> {
36        if slice.len() != 32 {
37            return Err(Error::invalid_key("invalid ed25519 key length".into()));
38        }
39        let mut bytes = [0u8; 32];
40        bytes[..].copy_from_slice(slice);
41        Ok(Self(bytes))
42    }
43}
44
45#[cfg(feature = "rust-crypto")]
46impl TryFrom<VerificationKey> for ed25519_consensus::VerificationKey {
47    type Error = Error;
48
49    fn try_from(src: VerificationKey) -> Result<Self, Error> {
50        ed25519_consensus::VerificationKey::try_from(src.0)
51            .map_err(|_| Error::invalid_key("malformed Ed25519 public key".into()))
52    }
53}
54
55#[cfg(feature = "rust-crypto")]
56impl From<ed25519_consensus::VerificationKey> for VerificationKey {
57    fn from(vk: ed25519_consensus::VerificationKey) -> Self {
58        Self::new(vk.to_bytes())
59    }
60}