celestia_core/crypto/ed25519/
signing_key.rs

1#[cfg(feature = "rust-crypto")]
2use super::VerificationKey;
3
4use crate::Error;
5
6#[derive(Clone, Debug)]
7pub struct SigningKey([u8; 32]);
8
9impl SigningKey {
10    pub fn as_bytes(&self) -> &[u8] {
11        &self.0
12    }
13
14    #[cfg(feature = "rust-crypto")]
15    pub fn verification_key(&self) -> VerificationKey {
16        let privkey = ed25519_consensus::SigningKey::from(self.0);
17        let pubkey = privkey.verification_key();
18        let pubkey_bytes = pubkey.to_bytes();
19        VerificationKey::new(pubkey_bytes)
20    }
21}
22
23impl TryFrom<&'_ [u8]> for SigningKey {
24    type Error = Error;
25
26    fn try_from(slice: &'_ [u8]) -> Result<Self, Self::Error> {
27        if slice.len() != 32 {
28            return Err(Error::invalid_key("invalid ed25519 key length".into()));
29        }
30        let mut bytes = [0u8; 32];
31        bytes[..].copy_from_slice(slice);
32        Ok(Self(bytes))
33    }
34}
35
36#[cfg(feature = "rust-crypto")]
37impl TryFrom<SigningKey> for ed25519_consensus::SigningKey {
38    type Error = Error;
39
40    fn try_from(src: SigningKey) -> Result<Self, Error> {
41        Ok(ed25519_consensus::SigningKey::from(src.0))
42    }
43}