mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <wpfleger@block.xyz> Signed-off-by: Will Pfleger <wpfleger@squareup.com> Signed-off-by: Will Pfleger <wpfleger96@gmail.com> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub16v54tttfqacx9ycvc3k0ut0npj564ahcuajzy6qjvh57ntmsf4uq4806j2 <d32955ad69077062930cc46cfe2df30ca9aaf6f8e76422681265e9e9af704d78@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Will Pfleger <wpfleger96@gmail.com>
75 lines
2.1 KiB
Rust
75 lines
2.1 KiB
Rust
//! Relay-side event wrapper.
|
|
//!
|
|
//! [`StoredEvent`] wraps a [`nostr::Event`] with relay-assigned metadata
|
|
//! (receive time, channel scope, verification status).
|
|
|
|
use chrono::{DateTime, Utc};
|
|
use uuid::Uuid;
|
|
|
|
/// A Nostr event with relay-assigned metadata.
|
|
#[derive(Debug, Clone)]
|
|
pub struct StoredEvent {
|
|
/// The underlying Nostr event.
|
|
pub event: nostr::Event,
|
|
/// Wall-clock time the relay received this event.
|
|
pub received_at: DateTime<Utc>,
|
|
/// Channel scope; `None` for global/DM events.
|
|
pub channel_id: Option<Uuid>,
|
|
verified: bool,
|
|
}
|
|
|
|
impl StoredEvent {
|
|
/// Creates a new `StoredEvent` with `received_at` set to now and `verified = false`.
|
|
pub fn new(event: nostr::Event, channel_id: Option<Uuid>) -> Self {
|
|
Self {
|
|
event,
|
|
received_at: Utc::now(),
|
|
channel_id,
|
|
verified: false,
|
|
}
|
|
}
|
|
|
|
/// Returns whether this event's signature has been verified.
|
|
pub fn is_verified(&self) -> bool {
|
|
self.verified
|
|
}
|
|
|
|
/// Creates a `StoredEvent` with an explicit `received_at` timestamp and verification status.
|
|
pub fn with_received_at(
|
|
event: nostr::Event,
|
|
received_at: DateTime<Utc>,
|
|
channel_id: Option<Uuid>,
|
|
verified: bool,
|
|
) -> Self {
|
|
Self {
|
|
event,
|
|
received_at,
|
|
channel_id,
|
|
verified,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use nostr::{EventBuilder, JsonUtil, Keys, Kind};
|
|
|
|
fn make_event() -> nostr::Event {
|
|
let keys = Keys::generate();
|
|
EventBuilder::new(Kind::TextNote, "hello buzz")
|
|
.tags([])
|
|
.sign_with_keys(&keys)
|
|
.expect("sign")
|
|
}
|
|
|
|
#[test]
|
|
fn tampered_signature_fails_verify() {
|
|
let event = make_event();
|
|
let mut json: serde_json::Value = serde_json::from_str(&event.as_json()).expect("parse");
|
|
json["sig"] = serde_json::Value::String("0".repeat(128));
|
|
let tampered = nostr::Event::from_json(json.to_string()).expect("parse");
|
|
assert!(tampered.verify_id());
|
|
assert!(!tampered.verify_signature());
|
|
}
|
|
}
|