Skip to main content

hbkr_rs/
basic.rs

1//! hbkr Basic type
2
3use std::str::FromStr;
4
5use crate::{
6    basicpre::BasicPrefix, derivation::DerivationCode, errors::KrError, key_manage::Publickey,
7};
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10pub enum Basic {
11    ED25519,
12    PASTA,
13}
14
15impl Basic {
16    pub fn derive(&self, public_key: Publickey) -> BasicPrefix {
17        BasicPrefix::new(*self, public_key)
18    }
19}
20
21impl DerivationCode for Basic {
22    fn to_str(&self) -> String {
23        match self {
24            Self::ED25519 => "D",
25            Self::PASTA => "1AAE",
26        }
27        .into()
28    }
29
30    fn code_len(&self) -> usize {
31        match self {
32            Self::ED25519 => 1,
33            Self::PASTA => 4,
34        }
35    }
36
37    fn derivative_b64_len(&self) -> usize {
38        match self {
39            Self::ED25519 | Self::PASTA => 43,
40        }
41    }
42}
43
44impl FromStr for Basic {
45    type Err = KrError;
46
47    fn from_str(s: &str) -> Result<Self, Self::Err> {
48        match s
49            .get(..1)
50            .ok_or_else(|| KrError::DeserializeError("Empty prefix".into()))?
51        {
52            "D" => Ok(Self::ED25519),
53            "1" => match &s[1..4] {
54                "AAE" => Ok(Self::PASTA),
55                _ => Err(KrError::DeserializeError("Unknown signature code".into())),
56            },
57            _ => Err(KrError::DeserializeError("Unknown prefix code".into())),
58        }
59    }
60}