multiversx_sdk/crypto/
public_key.rsuse std::fmt::Display;
use super::private_key::PrivateKey;
use anyhow::Result;
use multiversx_chain_core::types::Address;
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
pub const PUBLIC_KEY_LENGTH: usize = 32;
#[derive(Copy, Clone, Debug)]
pub struct PublicKey([u8; PUBLIC_KEY_LENGTH]);
impl PublicKey {
pub fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] {
self.0
}
pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LENGTH] {
&self.0
}
pub fn to_address(&self) -> Address {
self.0.into()
}
pub fn from_hex_str(pk: &str) -> Result<Self> {
let bytes = hex::decode(pk)?;
let mut bits: [u8; 32] = [0u8; 32];
bits.copy_from_slice(&bytes[32..]);
Ok(Self(bits))
}
}
impl From<&PrivateKey> for PublicKey {
fn from(private_key: &PrivateKey) -> PublicKey {
let bytes = private_key.to_bytes();
let mut bits: [u8; 32] = [0u8; 32];
bits.copy_from_slice(&bytes[32..]);
PublicKey(bits)
}
}
impl Display for PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
hex::encode(self.0).fmt(f)
}
}
impl Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}
impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(Self::from_hex_str(s.as_str()).unwrap())
}
}