Skip to content

Commit 2c5d401

Browse files
committed
nostr: add UnsignedEvent::id method
Pull-Request: #868 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 677cb7f commit 2c5d401

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
### Added
4242

43+
- nostr: add `UnsignedEvent::id` method ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/868)
4344
- blossom: add new crate with Blossom support ([Daniel D’Aquino] at https://github.com/rust-nostr/nostr/pull/838)
4445
- mls-storage: add new crate with traits and types for mls storage implementations ([JeffG] at https://github.com/rust-nostr/nostr/pull/836)
4546
- mls-memory-storage: add an in-memory implementation for MLS ([JeffG] at https://github.com/rust-nostr/nostr/pull/839)

crates/nostr/src/event/unsigned.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ impl UnsignedEvent {
6868
}
6969
}
7070

71+
/// Get the unsigned event ID
72+
///
73+
/// If the [`EventId`] is not set, will be computed and saved in the struct.
74+
pub fn id(&mut self) -> EventId {
75+
// Ensure that the ID is set
76+
self.ensure_id();
77+
78+
// This can't fail, because we already ensured that the ID is set
79+
self.id.unwrap()
80+
}
81+
7182
#[inline]
7283
fn compute_id(&self) -> EventId {
7384
EventId::new(
@@ -212,6 +223,18 @@ impl From<Event> for UnsignedEvent {
212223
mod tests {
213224
use super::*;
214225

226+
#[test]
227+
fn test_get_id() {
228+
let json = r#"{"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","created_at":1640839235,"kind":4,"pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]]}"#;
229+
let mut unsigned = UnsignedEvent::from_json(json).unwrap();
230+
let expected_id: EventId =
231+
EventId::from_hex("2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45")
232+
.unwrap();
233+
234+
assert!(unsigned.id.is_none());
235+
assert_eq!(unsigned.id(), expected_id);
236+
}
237+
215238
#[test]
216239
fn test_deserialize_unsigned_event_with_id() {
217240
let json = r#"{"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","created_at":1640839235,"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","kind":4,"pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]]}"#;

0 commit comments

Comments
 (0)