use crate::{
algorithm::ED25519_ALG_ID,
encoding::Encodable,
error::{Error, ErrorKind},
};
use anomaly::fail;
use std::convert::TryInto;
mod ed25519;
pub use self::ed25519::Ed25519PublicKey;
pub enum PublicKey {
Ed25519(Ed25519PublicKey),
}
impl PublicKey {
pub fn new(alg: &str, bytes: &[u8]) -> Result<Self, Error> {
let result = match alg {
ED25519_ALG_ID => PublicKey::Ed25519(bytes.try_into()?),
_ => fail!(ErrorKind::AlgorithmInvalid, "{}", alg),
};
Ok(result)
}
pub fn ed25519_key(&self) -> Option<&Ed25519PublicKey> {
match self {
PublicKey::Ed25519(ref key) => Some(key),
}
}
pub fn is_ed25519_key(&self) -> bool {
self.ed25519_key().is_some()
}
}
impl Encodable for PublicKey {
fn to_uri_string(&self) -> String {
match self {
PublicKey::Ed25519(ref key) => key.to_uri_string(),
}
}
fn to_dasherized_string(&self) -> String {
match self {
PublicKey::Ed25519(ref key) => key.to_dasherized_string(),
}
}
}