tendermint/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    #[allow(dead_code)]
11    pub(super) fn new(bytes: [u8; 32]) -> Self {
12        Self(bytes)
13    }
14
15    pub fn as_bytes(&self) -> &[u8] {
16        &self.0
17    }
18
19    #[cfg(feature = "rust-crypto")]
20    pub fn verification_key(&self) -> VerificationKey {
21        let privkey = ed25519_consensus::SigningKey::from(self.0);
22        let pubkey = privkey.verification_key();
23        let pubkey_bytes = pubkey.to_bytes();
24        VerificationKey::new(pubkey_bytes)
25    }
26}
27
28impl TryFrom<&'_ [u8]> for SigningKey {
29    type Error = Error;
30
31    fn try_from(slice: &'_ [u8]) -> Result<Self, Self::Error> {
32        if slice.len() != 32 {
33            return Err(Error::invalid_key("invalid ed25519 key length".into()));
34        }
35        let mut bytes = [0u8; 32];
36        bytes[..].copy_from_slice(slice);
37        Ok(Self(bytes))
38    }
39}
40
41#[cfg(feature = "rust-crypto")]
42impl TryFrom<SigningKey> for ed25519_consensus::SigningKey {
43    type Error = Error;
44
45    fn try_from(src: SigningKey) -> Result<Self, Error> {
46        Ok(ed25519_consensus::SigningKey::from(src.0))
47    }
48}
49
50#[cfg(feature = "rust-crypto")]
51impl From<ed25519_consensus::SigningKey> for SigningKey {
52    fn from(sk: ed25519_consensus::SigningKey) -> Self {
53        Self::new(sk.to_bytes())
54    }
55}