use super::Signature;
use super::SigningKey;
use crate::error::{Error, ErrorKind};
use signature::Verifier;
#[derive(Clone, Debug)]
pub struct VerifyingKey(pub(super) ed25519_dalek::VerifyingKey);
impl VerifyingKey {
pub const BYTE_SIZE: usize = 32;
pub fn as_bytes(&self) -> &[u8; Self::BYTE_SIZE] {
self.0.as_bytes()
}
}
impl From<&SigningKey> for VerifyingKey {
fn from(signing_key: &SigningKey) -> VerifyingKey {
signing_key.verifying_key()
}
}
impl From<VerifyingKey> for cometbft::PublicKey {
fn from(verifying_key: VerifyingKey) -> cometbft::PublicKey {
cometbft::PublicKey::from_raw_ed25519(verifying_key.as_bytes())
.expect("invalid Ed25519 key")
}
}
impl From<VerifyingKey> for cometbft_p2p::PublicKey {
#[inline]
fn from(verifying_key: VerifyingKey) -> cometbft_p2p::PublicKey {
Self::from(&verifying_key)
}
}
impl From<&VerifyingKey> for cometbft_p2p::PublicKey {
fn from(verifying_key: &VerifyingKey) -> cometbft_p2p::PublicKey {
verifying_key.0.into()
}
}
impl Verifier<Signature> for VerifyingKey {
fn verify(&self, msg: &[u8], sig: &Signature) -> signature::Result<()> {
self.0.verify(msg, sig)
}
}
impl TryFrom<&[u8]> for VerifyingKey {
type Error = Error;
fn try_from(slice: &[u8]) -> Result<Self, Error> {
slice
.try_into()
.map(Self)
.map_err(|_| ErrorKind::InvalidKey.into())
}
}