use crate::utils;
use serde::{Deserialize, Serialize};
use std::{
fmt::{self, Debug, Formatter},
hash::{Hash, Hasher},
};
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct SignatureShare {
pub index: usize,
pub share: threshold_crypto::SignatureShare,
}
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum Signature {
Ed25519(ed25519_dalek::Signature),
Bls(threshold_crypto::Signature),
BlsShare(SignatureShare),
}
impl Signature {
pub fn into_bls(self) -> Option<threshold_crypto::Signature> {
match self {
Self::Bls(sig) => Some(sig),
_ => None,
}
}
pub fn into_ed(self) -> Option<ed25519_dalek::Signature> {
match self {
Self::Ed25519(sig) => Some(sig),
_ => None,
}
}
}
impl From<threshold_crypto::Signature> for Signature {
fn from(sig: threshold_crypto::Signature) -> Self {
Self::Bls(sig)
}
}
impl From<ed25519_dalek::Signature> for Signature {
fn from(sig: ed25519_dalek::Signature) -> Self {
Self::Ed25519(sig)
}
}
impl From<SignatureShare> for Signature {
fn from(sig: SignatureShare) -> Self {
Self::BlsShare(sig)
}
}
impl From<(usize, threshold_crypto::SignatureShare)> for Signature {
fn from(sig: (usize, threshold_crypto::SignatureShare)) -> Self {
let (index, share) = sig;
Self::BlsShare(SignatureShare { index, share })
}
}
#[allow(clippy::derive_hash_xor_eq)]
impl Hash for Signature {
fn hash<H: Hasher>(&self, state: &mut H) {
utils::serialise(&self).hash(state)
}
}
impl Debug for Signature {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "Signature::")?;
match self {
Self::Ed25519(_) => write!(formatter, "Ed25519(..)"),
Self::Bls(_) => write!(formatter, "Bls(..)"),
Self::BlsShare(_) => write!(formatter, "BlsShare(..)"),
}
}
}