Skip to content

Commit

Permalink
messagehash directly derive from Deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Jan 9, 2025
1 parent 566fae9 commit 6541dc3
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions waku-bindings/src/general/messagehash.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::general::waku_decode::WakuDecode;
use hex::FromHex;
use serde::{Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::fmt;
use std::fmt::Write;
use std::hash::Hash;
use std::str::FromStr;

/// Waku message hash, hex encoded sha256 digest of the message
#[derive(Debug, Serialize, PartialEq, Eq, Clone, Hash)]
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Hash)]
pub struct MessageHash([u8; 32]);

impl MessageHash {
Expand Down Expand Up @@ -38,23 +38,6 @@ impl FromStr for MessageHash {
}
}

impl<'de> Deserialize<'de> for MessageHash {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize the input as a vector of u8
let vec: Vec<u8> = Deserialize::deserialize(deserializer)?;

// Ensure the vector has exactly 32 elements
let array: [u8; 32] = vec
.try_into()
.map_err(|_| serde::de::Error::custom("Expected an array of length 32"))?;

Ok(MessageHash(array))
}
}

impl WakuDecode for MessageHash {
fn decode(input: &str) -> Result<Self, String> {
MessageHash::from_str(input)
Expand Down

0 comments on commit 6541dc3

Please sign in to comment.