serde ecnrypted tally as hex string
@@ -330,7 +330,49 @@ impl TallyResult {
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
-pub struct EncryptedTally(Vec<u8>);
+pub struct EncryptedTally(#[serde(with = "serde_bytes")] Vec<u8>);
+
+mod serde_hex_bytes {
+ use crate::interfaces::vote::EncryptedTally;
+ use serde::de::{Error, Visitor};
+ use serde::{Deserializer, Serialize, Serializer};
+
+ pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct ByteStringVisitor;
+ impl<'de> Visitor<'de> for ByteStringVisitor {
+ type Value = Vec<u8>;
+
+ fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+ formatter.write_str("Invalid hex encoded encrypted tally")
+ }
+
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ hex::decode(v).map_err(|e| E::custom(format!("{}", e)))
+ }
+
+ fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_str(&v)
+ }
+ }
+ deserializer.deserialize_string(ByteStringVisitor {})
+ }
+
+ pub fn serialize<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(&hex::encode(bytes))
+ }
+}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PrivateTallyState {
