mithril_common/crypto_helper/types/
wrappers.rs1use anyhow::Context;
2use hex::{FromHex, ToHex};
3use kes_summed_ed25519::kes::Sum6KesSig;
4use mithril_stm::{StmAggrSig, StmAggrVerificationKey, StmSig, StmVerificationKeyPoP};
5
6use crate::crypto_helper::{MKMapProof, MKProof, OpCert, ProtocolKey, ProtocolKeyCodec, D};
7use crate::entities::BlockRange;
8use crate::StdResult;
9
10pub type ProtocolSignerVerificationKey = ProtocolKey<StmVerificationKeyPoP>;
13
14pub type ProtocolSignerVerificationKeySignature = ProtocolKey<Sum6KesSig>;
17
18pub type ProtocolSingleSignature = ProtocolKey<StmSig>;
20
21pub type ProtocolMultiSignature = ProtocolKey<StmAggrSig<D>>;
23
24pub type ProtocolOpCert = ProtocolKey<OpCert>;
26
27pub type ProtocolAggregateVerificationKey = ProtocolKey<StmAggrVerificationKey<D>>;
29
30pub type ProtocolMkProof = ProtocolKey<MKMapProof<BlockRange>>;
32
33impl ProtocolKey<ed25519_dalek::Signature> {
34 pub fn from_bytes_hex(hex_string: &str) -> StdResult<Self> {
36 let hex_bytes = Vec::from_hex(hex_string).with_context(|| {
37 "Could not deserialize a ED25519 signature from bytes hex string:\
38 could not convert the encoded string to bytes."
39 })?;
40
41 Self::from_bytes(&hex_bytes)
42 }
43
44 pub fn from_bytes(bytes: &[u8]) -> StdResult<Self> {
46 let key = ed25519_dalek::Signature::from_slice(bytes).with_context(|| {
47 "Could not deserialize a ED25519 signature from bytes hex string:\
48 invalid bytes"
49 .to_string()
50 })?;
51
52 Ok(Self { key })
53 }
54
55 pub fn to_bytes_hex(&self) -> String {
57 Self::key_to_bytes_hex(&self.key)
58 }
59
60 pub fn key_to_bytes_hex(key: &ed25519_dalek::Signature) -> String {
62 key.to_bytes().encode_hex::<String>()
63 }
64}
65
66impl ProtocolKeyCodec<ed25519_dalek::Signature> for ed25519_dalek::Signature {
67 fn decode_key(encoded: &str) -> StdResult<ProtocolKey<ed25519_dalek::Signature>> {
68 ProtocolKey::<ed25519_dalek::Signature>::from_bytes_hex(encoded)
69 }
70
71 fn encode_key(key: &ed25519_dalek::Signature) -> StdResult<String> {
72 Ok(ProtocolKey::<ed25519_dalek::Signature>::key_to_bytes_hex(
73 key,
74 ))
75 }
76}
77
78impl_codec_and_type_conversions_for_protocol_key!(
79 json_hex_codec => StmVerificationKeyPoP, Sum6KesSig, StmSig, StmAggrSig<D>, OpCert,
80 ed25519_dalek::VerifyingKey, ed25519_dalek::SigningKey, StmAggrVerificationKey<D>,
81 MKProof
82);
83impl_codec_and_type_conversions_for_protocol_key!(no_default_codec => ed25519_dalek::Signature);