use ed25519_dalek::SigningKey;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use thiserror::Error;
#[derive(Clone, Copy, Debug, Error)]
#[error("Signing algorithm must be ed25519")]
pub struct InvalidSigningAlgoId;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[derive(Deserialize, Serialize)]
#[serde(try_from = "&str", into = "&'static str")]
pub struct Ed25519SigningAlgo;
impl From<Ed25519SigningAlgo> for &'static str {
fn from(_value: Ed25519SigningAlgo) -> &'static str {
"ed25519"
}
}
impl<'a> TryFrom<&'a str> for Ed25519SigningAlgo {
type Error = InvalidSigningAlgoId;
fn try_from(value: &'a str) -> Result<Self, InvalidSigningAlgoId> {
if value == "ed25519" {
Ok(Ed25519SigningAlgo)
} else {
Err(InvalidSigningAlgoId)
}
}
}
impl Display for Ed25519SigningAlgo {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter.write_str("ed25519")
}
}
#[derive(Clone)]
#[derive(Deserialize, Serialize)]
#[serde(tag = "what", rename = "keypair")]
pub struct Keypair {
pub algo: Ed25519SigningAlgo,
#[serde(with = "crate::serde::signing_key")]
pub private_key: SigningKey,
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<String>
}
#[derive(Clone)]
#[derive(Deserialize, Serialize)]
#[serde(tag = "what", rename = "public_key")]
pub struct PublicKey {
pub algo: Ed25519SigningAlgo,
#[serde(with = "crate::serde::public_key")]
pub public_key: [u8; 32],
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<String>
}