use std::str::FromStr;
use crate::{
basicpre::BasicPrefix, derivation::DerivationCode, errors::KrError, key_manage::Publickey,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Basic {
ED25519,
PASTA,
}
impl Basic {
pub fn derive(&self, public_key: Publickey) -> BasicPrefix {
BasicPrefix::new(*self, public_key)
}
}
impl DerivationCode for Basic {
fn to_str(&self) -> String {
match self {
Self::ED25519 => "D",
Self::PASTA => "1AAE",
}
.into()
}
fn code_len(&self) -> usize {
match self {
Self::ED25519 => 1,
Self::PASTA => 4,
}
}
fn derivative_b64_len(&self) -> usize {
match self {
Self::ED25519 | Self::PASTA => 43,
}
}
}
impl FromStr for Basic {
type Err = KrError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s
.get(..1)
.ok_or_else(|| KrError::DeserializeError("Empty prefix".into()))?
{
"D" => Ok(Self::ED25519),
"1" => match &s[1..4] {
"AAE" => Ok(Self::PASTA),
_ => Err(KrError::DeserializeError("Unknown signature code".into())),
},
_ => Err(KrError::DeserializeError("Unknown prefix code".into())),
}
}
}