1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::Error;

#[derive(Copy, Clone, Eq, PartialEq)]
pub struct VerificationKey([u8; 32]);

impl core::fmt::Display for VerificationKey {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        for byte in &self.0 {
            write!(f, "{byte:02x}")?;
        }
        Ok(())
    }
}

impl core::fmt::Debug for VerificationKey {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        <Self as core::fmt::Display>::fmt(self, f)
    }
}

impl VerificationKey {
    #[allow(dead_code)]
    pub(super) fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

impl TryFrom<&'_ [u8]> for VerificationKey {
    type Error = Error;

    fn try_from(slice: &'_ [u8]) -> Result<Self, Self::Error> {
        if slice.len() != 32 {
            return Err(Error::invalid_key("invalid ed25519 key length".into()));
        }
        let mut bytes = [0u8; 32];
        bytes[..].copy_from_slice(slice);
        Ok(Self(bytes))
    }
}

#[cfg(feature = "rust-crypto")]
impl TryFrom<VerificationKey> for ed25519_consensus::VerificationKey {
    type Error = Error;

    fn try_from(src: VerificationKey) -> Result<Self, Error> {
        ed25519_consensus::VerificationKey::try_from(src.0)
            .map_err(|_| Error::invalid_key("malformed Ed25519 public key".into()))
    }
}