use crate::{
api::api_types::GraphKeyPair,
dsnp::{
dsnp_types::DsnpPublicKey,
encryption::{EncryptionBehavior, SealBox},
},
};
use dryoc::keypair::{PublicKey, SecretKey, StackKeyPair};
use dsnp_graph_config::{
errors::{DsnpGraphError, DsnpGraphResult},
DsnpVersion, GraphKeyType,
};
use log::Level;
use log_result_proc_macro::log_result_err;
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
pub enum DsnpVersionConfig {
Version1_0 { algorithm: SealBox },
}
#[derive(Clone, PartialEq, Debug)]
pub enum PublicKeyType {
Version1_0(PublicKey),
}
#[derive(Clone, PartialEq, Debug)]
pub enum KeyPairType {
Version1_0(StackKeyPair),
}
#[derive(Clone, PartialEq, Debug)]
pub enum SecretKeyType {
Version1_0(StackKeyPair),
}
impl DsnpVersionConfig {
pub fn new(version: DsnpVersion) -> Self {
match version {
DsnpVersion::Version1_0 => DsnpVersionConfig::Version1_0 { algorithm: SealBox },
}
}
pub fn get_algorithm(&self) -> Box<dyn EncryptionBehavior> {
match self {
DsnpVersionConfig::Version1_0 { algorithm } => Box::new(algorithm.clone()),
}
}
}
impl KeyPairType {
pub fn get_public_key_raw(&self) -> Vec<u8> {
match self {
KeyPairType::Version1_0(k) => k.public_key.to_vec(),
}
}
#[cfg(test)]
pub fn get_secret_key_raw(&self) -> Vec<u8> {
match self {
KeyPairType::Version1_0(k) => k.secret_key.to_vec(),
}
}
}
impl Into<PublicKeyType> for &'_ KeyPairType {
fn into(self) -> PublicKeyType {
match self {
KeyPairType::Version1_0(k) => PublicKeyType::Version1_0(k.public_key.clone()),
}
}
}
impl Into<SecretKeyType> for KeyPairType {
fn into(self) -> SecretKeyType {
match self {
KeyPairType::Version1_0(k) => SecretKeyType::Version1_0(k),
}
}
}
impl Into<DsnpVersionConfig> for &SecretKeyType {
fn into(self) -> DsnpVersionConfig {
match self {
SecretKeyType::Version1_0(_) => DsnpVersionConfig::new(DsnpVersion::Version1_0),
}
}
}
impl Into<DsnpVersionConfig> for &KeyPairType {
fn into(self) -> DsnpVersionConfig {
match self {
KeyPairType::Version1_0(_) => DsnpVersionConfig::new(DsnpVersion::Version1_0),
}
}
}
impl Into<DsnpVersionConfig> for &PublicKeyType {
fn into(self) -> DsnpVersionConfig {
match self {
PublicKeyType::Version1_0(_) => DsnpVersionConfig::new(DsnpVersion::Version1_0),
}
}
}
impl TryInto<PublicKeyType> for &'_ DsnpPublicKey {
type Error = DsnpGraphError;
#[log_result_err(Level::Info)]
fn try_into(self) -> DsnpGraphResult<PublicKeyType> {
let public_key =
PublicKey::try_from(&self.key[..]).map_err(|_| DsnpGraphError::InvalidPublicKey)?;
Ok(PublicKeyType::Version1_0(public_key))
}
}
impl Into<Vec<u8>> for PublicKeyType {
fn into(self) -> Vec<u8> {
match self {
PublicKeyType::Version1_0(k) => k.to_vec(),
}
}
}
impl TryInto<KeyPairType> for GraphKeyPair {
type Error = DsnpGraphError;
#[log_result_err(Level::Info)]
fn try_into(self) -> DsnpGraphResult<KeyPairType> {
match self.key_type {
GraphKeyType::X25519 => {
let secret_key = SecretKey::try_from(&self.secret_key[..])
.map_err(|_| DsnpGraphError::InvalidSecretKey)?;
let pair = StackKeyPair::from_secret_key(secret_key);
if pair.public_key.to_vec() != self.public_key {
return Err(DsnpGraphError::PublicKeyNotCompatibleWithSecretKey)
}
Ok(KeyPairType::Version1_0(pair))
},
}
}
}