tmkms 0.15.0

Tendermint Key Management System: provides isolated, optionally HSM-backed signing key management for Tendermint applications including validators, oracles, IBC relayers, and other transaction signing applications
Documentation
use super::Signature;
use super::SigningKey;
use crate::error::{Error, ErrorKind};
use signature::Verifier;

/// Ed25519 verification key.
#[derive(Clone, Debug)]
pub struct VerifyingKey(pub(super) ed25519_dalek::VerifyingKey);

impl VerifyingKey {
    /// Size of an encoded Ed25519 verifying key in bytes.
    pub const BYTE_SIZE: usize = 32;

    /// Borrow the serialized verification key as bytes.
    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())
    }
}