Skip to content

Commit

Permalink
nostr: add MissingPartialEvent struct
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 16, 2023
1 parent db6b842 commit 56827df
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/nostr/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod unsigned;
pub use self::builder::EventBuilder;
pub use self::id::EventId;
pub use self::kind::Kind;
pub use self::partial::PartialEvent;
pub use self::partial::{MissingPartialEvent, PartialEvent};
pub use self::tag::{Marker, Tag, TagKind};
pub use self::unsigned::UnsignedEvent;
#[cfg(feature = "std")]
Expand Down
42 changes: 40 additions & 2 deletions crates/nostr/src/event/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

//! Partial Event for fast deserialization and signature verification
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

use bitcoin::secp256k1::schnorr::Signature;
use bitcoin::secp256k1::{self, Message, Secp256k1, Verification, XOnlyPublicKey};

#[cfg(feature = "std")]
use crate::SECP256K1;
use crate::{EventId, JsonUtil};
use crate::{Event, EventId, JsonUtil, Kind, Tag, Timestamp};

/// [`PartialEvent`] error
#[derive(Debug)]
Expand Down Expand Up @@ -51,7 +53,7 @@ impl From<secp256k1::Error> for Error {
/// Partial event
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct PartialEvent {
/// Id
/// ID
pub id: EventId,
/// Author
pub pubkey: XOnlyPublicKey,
Expand All @@ -76,8 +78,44 @@ impl PartialEvent {
secp.verify_schnorr(&self.sig, &message, &self.pubkey)
.map_err(|_| Error::InvalidSignature)
}

/// Merge [`MissingPartialEvent`] and compose [`Event`]
pub fn merge(&self, missing: MissingPartialEvent) -> Event {
Event {
id: self.id,
pubkey: self.pubkey,
created_at: missing.created_at,
kind: missing.kind,
tags: missing.tags,
content: missing.content,
sig: self.sig,
#[cfg(feature = "nip03")]
ots: missing.ots,
}
}
}

impl JsonUtil for PartialEvent {
type Err = Error;
}

/// Missing Partial event fields
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct MissingPartialEvent {
/// Timestamp (seconds)
pub created_at: Timestamp,
/// Kind
pub kind: Kind,
/// Vector of [`Tag`]
pub tags: Vec<Tag>,
/// Content
pub content: String,
/// OpenTimestamps Attestations
#[cfg(feature = "nip03")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ots: Option<String>,
}

impl JsonUtil for MissingPartialEvent {
type Err = Error;
}
4 changes: 3 additions & 1 deletion crates/nostr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub use self::event::tag::{
ExternalIdentity, HttpMethod, Identity, ImageDimensions, Marker, RelayMetadata, Report, Tag,
TagKind,
};
pub use self::event::{Event, EventBuilder, EventId, Kind, PartialEvent, UnsignedEvent};
pub use self::event::{
Event, EventBuilder, EventId, Kind, MissingPartialEvent, PartialEvent, UnsignedEvent,
};
pub use self::key::Keys;
pub use self::message::{
Alphabet, ClientMessage, Filter, RawRelayMessage, RelayMessage, SubscriptionId,
Expand Down

0 comments on commit 56827df

Please sign in to comment.