mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-02 14:03:12 +00:00
messagehash.rs only contains a String as the hex representation of msg hash
This commit is contained in:
parent
29421b4e46
commit
a20f5fd8c9
4
examples/Cargo.lock
generated
4
examples/Cargo.lock
generated
@ -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",
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user