Skip to content

Commit 9ab939f

Browse files
authored
feat: add msg_id.rs (#36)
1 parent 77f5fef commit 9ab939f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

relay_rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ once_cell = "1.16"
2222
jsonwebtoken = "8.1"
2323
k256 = { version = "0.13", optional = true }
2424
sha3 = { version = "0.10", optional = true }
25+
sha2 = { version = "0.10.6" }

relay_rpc/src/rpc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use {
1111
std::{fmt::Debug, sync::Arc},
1212
};
1313

14+
pub mod msg_id;
1415
#[cfg(test)]
1516
mod tests;
1617
pub mod watch;

relay_rpc/src/rpc/msg_id.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use {
2+
crate::rpc,
3+
sha2::{Digest, Sha256},
4+
};
5+
6+
pub trait MsgId {
7+
fn msg_id(&self) -> String;
8+
}
9+
10+
impl MsgId for rpc::Publish {
11+
fn msg_id(&self) -> String {
12+
let msg_id = Sha256::new()
13+
.chain_update(self.message.as_ref().as_bytes())
14+
.finalize();
15+
format!("{msg_id:x}")
16+
}
17+
}
18+
19+
impl MsgId for rpc::Subscription {
20+
fn msg_id(&self) -> String {
21+
let msg_id = Sha256::new()
22+
.chain_update(self.data.message.as_ref().as_bytes())
23+
.finalize();
24+
format!("{msg_id:x}")
25+
}
26+
}

0 commit comments

Comments
 (0)