cat_gateway/utils/
ed25519.rs1use anyhow::{bail, Error};
4
5pub(crate) const HEX_ENCODED_LENGTH: usize = "0x".len() + (ed25519_dalek::PUBLIC_KEY_LENGTH * 2);
7
8pub(crate) fn verifying_key_from_vec(bytes: &[u8]) -> Result<ed25519_dalek::VerifyingKey, Error> {
14 let raw_key: [u8; ed25519_dalek::PUBLIC_KEY_LENGTH] = match bytes.try_into() {
15 Ok(raw) => raw,
16 Err(_) => {
17 bail!("Invalid ED25519 Public Key length");
18 },
19 };
20 let Ok(key) = ed25519_dalek::VerifyingKey::from_bytes(&raw_key) else {
21 bail!("Invalid ED25519 Public Key")
22 };
23 Ok(key)
24}
25
26pub(crate) fn verifying_key_from_hex(hex: &str) -> Result<ed25519_dalek::VerifyingKey, Error> {
28 if hex.len() != HEX_ENCODED_LENGTH {
29 anyhow::bail!("Invalid ED25519 Public Key Length");
30 }
31 if !hex.starts_with("0x") {
32 anyhow::bail!("Does not start with required hex string prefix");
33 }
34 #[allow(clippy::string_slice)] let raw_hex = &hex[2..];
36 let raw_binary = hex::decode(raw_hex)?;
37 verifying_key_from_vec(&raw_binary)
38}