messagehash.rs only contains a String as the hex representation of msg hash

This commit is contained in:
Ivan Folgueira Bande 2025-01-22 20:56:08 +01:00
parent 29421b4e46
commit a20f5fd8c9
No known key found for this signature in database
GPG Key ID: 3C117481F89E24A7
2 changed files with 5 additions and 27 deletions

4
examples/Cargo.lock generated
View File

@ -4945,7 +4945,7 @@ checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7"
[[package]]
name = "waku-bindings"
version = "0.5.0"
version = "1.0.0"
dependencies = [
"aes-gcm",
"base64 0.21.7",
@ -4972,7 +4972,7 @@ dependencies = [
[[package]]
name = "waku-sys"
version = "0.5.0"
version = "1.0.0"
dependencies = [
"bindgen",
"cc",

View File

@ -1,40 +1,18 @@
use crate::general::waku_decode::WakuDecode;
use hex::FromHex;
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, Deserialize, Serialize, PartialEq, Eq, Clone, Hash)]
pub struct MessageHash([u8; 32]);
impl MessageHash {
fn to_hex_string(&self) -> String {
self.0.iter().fold(String::new(), |mut output, b| {
let _ = write!(output, "{b:02X}");
output
})
}
}
pub struct MessageHash(String);
impl FromStr for MessageHash {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let s = s.strip_prefix("0x").unwrap_or(s);
// Decode the hexadecimal string to a Vec<u8>
// We expect a string format like: d38220de82fbcf2df865b680692fce98c36600fdd1d954b8a71e916dc4222b8e
let bytes = Vec::from_hex(s).map_err(|e| format!("Hex decode error MessageHash: {}", e))?;
// Ensure the length is exactly 32 bytes
let res = bytes
.try_into()
.map_err(|_| "Hex string must represent exactly 32 bytes".to_string())?;
Ok(MessageHash(res))
Ok(MessageHash(s.to_string()))
}
}
@ -47,6 +25,6 @@ impl WakuDecode for MessageHash {
// Implement the Display trait
impl fmt::Display for MessageHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex_string())
write!(f, "{}", self.0)
}
}