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