mithril_common/crypto_helper/types/
wrappers.rs

1use 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
10/// Wrapper of [MithrilStm:StmVerificationKeyPoP](type@StmVerificationKeyPoP) to add serialization
11/// utilities.
12pub type ProtocolSignerVerificationKey = ProtocolKey<StmVerificationKeyPoP>;
13
14/// Wrapper of [KES:Sum6KesSig](https://github.com/input-output-hk/kes/blob/master/src/kes.rs) to add
15/// serialization utilities.
16pub type ProtocolSignerVerificationKeySignature = ProtocolKey<Sum6KesSig>;
17
18/// Wrapper of [MithrilStm:StmSig](type@StmSig) to add serialization utilities.
19pub type ProtocolSingleSignature = ProtocolKey<StmSig>;
20
21/// Wrapper of [MithrilStm:StmAggrSig](struct@StmAggrSig) to add serialization utilities.
22pub type ProtocolMultiSignature = ProtocolKey<StmAggrSig<D>>;
23
24/// Wrapper of [OpCert] to add serialization utilities.
25pub type ProtocolOpCert = ProtocolKey<OpCert>;
26
27/// Wrapper of [MithrilStm:StmAggrVerificationKey](struct@StmAggrVerificationKey).
28pub type ProtocolAggregateVerificationKey = ProtocolKey<StmAggrVerificationKey<D>>;
29
30/// Wrapper of [MKProof] to add serialization utilities.
31pub type ProtocolMkProof = ProtocolKey<MKMapProof<BlockRange>>;
32
33impl ProtocolKey<ed25519_dalek::Signature> {
34    /// Create an instance from a bytes hex representation
35    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    /// Create an instance from a bytes representation
45    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    /// Create a bytes hash representation of the key
56    pub fn to_bytes_hex(&self) -> String {
57        Self::key_to_bytes_hex(&self.key)
58    }
59
60    /// Create a bytes hash representation of the given key
61    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);