use std::fmt::{self, Debug};
pub const PUBLIC_KEY_SIZE: usize = 32;
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct PublicKey(pub [u8; PUBLIC_KEY_SIZE]);
impl PublicKey {
pub fn new(bytes: [u8; PUBLIC_KEY_SIZE]) -> Self {
PublicKey(bytes)
}
pub fn from_bytes<B>(bytes: B) -> Option<Self>
where
B: AsRef<[u8]>,
{
if bytes.as_ref().len() == PUBLIC_KEY_SIZE {
let mut public_key = [0u8; PUBLIC_KEY_SIZE];
public_key.copy_from_slice(bytes.as_ref());
Some(PublicKey(public_key))
} else {
None
}
}
#[inline]
pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_SIZE] {
&self.0
}
#[inline]
pub fn into_bytes(self) -> [u8; PUBLIC_KEY_SIZE] {
self.0
}
}
impl AsRef<[u8]> for PublicKey {
#[inline]
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ed25519::PublicKey({:?})", self.as_ref())
}
}