use anyhow::Context;
use hex::{FromHex, ToHex};
use kes_summed_ed25519::kes::Sum6KesSig;
use mithril_stm::stm::{StmAggrSig, StmAggrVerificationKey, StmSig, StmVerificationKeyPoP};
use crate::crypto_helper::{MKMapProof, MKProof, OpCert, ProtocolKey, ProtocolKeyCodec, D};
use crate::entities::BlockRange;
use crate::StdResult;
pub type ProtocolSignerVerificationKey = ProtocolKey<StmVerificationKeyPoP>;
pub type ProtocolSignerVerificationKeySignature = ProtocolKey<Sum6KesSig>;
pub type ProtocolSingleSignature = ProtocolKey<StmSig>;
pub type ProtocolMultiSignature = ProtocolKey<StmAggrSig<D>>;
pub type ProtocolGenesisSignature = ProtocolKey<ed25519_dalek::Signature>;
pub type ProtocolOpCert = ProtocolKey<OpCert>;
pub type ProtocolGenesisVerificationKey = ProtocolKey<ed25519_dalek::VerifyingKey>;
pub type ProtocolGenesisSecretKey = ProtocolKey<ed25519_dalek::SigningKey>;
pub type ProtocolAggregateVerificationKey = ProtocolKey<StmAggrVerificationKey<D>>;
pub type ProtocolMkProof = ProtocolKey<MKMapProof<BlockRange>>;
impl ProtocolGenesisSignature {
pub fn from_bytes_hex(hex_string: &str) -> StdResult<Self> {
let hex_bytes = Vec::from_hex(hex_string).with_context(|| {
"Could not deserialize a ProtocolGenesisSignature from bytes hex string:\
could not convert the encoded string to bytes."
})?;
Self::from_bytes(&hex_bytes)
}
pub fn from_bytes(bytes: &[u8]) -> StdResult<Self> {
let key = ed25519_dalek::Signature::from_slice(bytes).with_context(|| {
"Could not deserialize a ProtocolGenesisSignature from bytes hex string:\
invalid bytes"
.to_string()
})?;
Ok(Self { key })
}
pub fn to_bytes_hex(&self) -> String {
Self::key_to_bytes_hex(&self.key)
}
pub fn key_to_bytes_hex(key: &ed25519_dalek::Signature) -> String {
key.to_bytes().encode_hex::<String>()
}
}
impl ProtocolKeyCodec<ed25519_dalek::Signature> for ed25519_dalek::Signature {
fn decode_key(encoded: &str) -> StdResult<ProtocolKey<ed25519_dalek::Signature>> {
ProtocolGenesisSignature::from_bytes_hex(encoded)
}
fn encode_key(key: &ed25519_dalek::Signature) -> StdResult<String> {
Ok(ProtocolGenesisSignature::key_to_bytes_hex(key))
}
}
impl_codec_and_type_conversions_for_protocol_key!(
json_hex_codec => StmVerificationKeyPoP, Sum6KesSig, StmSig, StmAggrSig<D>, OpCert,
ed25519_dalek::VerifyingKey, ed25519_dalek::SigningKey, StmAggrVerificationKey<D>,
MKProof
);
impl_codec_and_type_conversions_for_protocol_key!(no_default_codec => ed25519_dalek::Signature);