Skip to content

Commit

Permalink
nostr: move sig field from PartialEvent to MissingPartialEvent
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Aug 8, 2024
1 parent db4b120 commit 6ffebcb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* nostr: change `EventBuilder::award_badge` fingerprint ([Yuki Kishimoto])
* nostr: add NIP-50 support to `Filter::match_event` method ([Yuki Kishimoto])
* nostr: remove `Arc<T>` from `OnceCell<T>` in `Event` and `Tag` ([Yuki Kishimoto])
* nostr: move `sig` field from `PartialEvent` to `MissingPartialEvent` ([Yuki Kishimoto])
* pool: take mutex ownership instead of clone in `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
* pool: remove IDs collection from `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
* pool: better checks before perform queries or send messages to relays ([Yuki Kishimoto])
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-relay-pool/src/relay/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ impl InternalRelay {
}

// Deserialize missing event fields
let missing: MissingPartialEvent = MissingPartialEvent::from_raw(event);
let missing: MissingPartialEvent = MissingPartialEvent::from_raw(event)?;

// TODO: check if word/hashtag is blacklisted

Expand Down
24 changes: 18 additions & 6 deletions crates/nostr/src/event/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use core::str::FromStr;

use bitcoin::secp256k1;
use bitcoin::secp256k1::schnorr::Signature;

use super::raw::{self, RawEvent};
Expand All @@ -23,6 +25,8 @@ pub enum Error {
RawEvent(raw::Error),
/// Tag parse
Tag(tag::Error),
/// Secp256k1 error
Secp256k1(secp256k1::Error),
/// Invalid signature
InvalidSignature,
}
Expand All @@ -36,6 +40,7 @@ impl fmt::Display for Error {
Self::Json(e) => write!(f, "Json: {e}"),
Self::RawEvent(e) => write!(f, "Raw event: {e}"),
Self::Tag(e) => write!(f, "Tag: {e}"),
Self::Secp256k1(e) => write!(f, "{e}"),
Self::InvalidSignature => write!(f, "Invalid signature"),
}
}
Expand All @@ -59,15 +64,19 @@ impl From<tag::Error> for Error {
}
}

impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
Self::Secp256k1(e)
}
}

/// Partial event
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct PartialEvent {
/// ID
pub id: EventId,
/// Author
pub pubkey: PublicKey,
/// Signature
pub sig: Signature,
}

impl PartialEvent {
Expand All @@ -91,7 +100,7 @@ impl PartialEvent {
missing.kind,
tags,
missing.content,
self.sig,
missing.sig,
))
}
}
Expand All @@ -111,18 +120,21 @@ pub struct MissingPartialEvent {
pub tags: Vec<Vec<String>>,
/// Content
pub content: String,
/// Signature
pub sig: Signature,
}

impl MissingPartialEvent {
/// Compose from [RawEvent]
#[inline]
pub fn from_raw(raw: RawEvent) -> Self {
Self {
pub fn from_raw(raw: RawEvent) -> Result<Self, Error> {
Ok(Self {
created_at: Timestamp::from(raw.created_at),
kind: Kind::from(raw.kind),
tags: raw.tags,
content: raw.content,
}
sig: Signature::from_str(&raw.sig)?,
})
}

/// Extract identifier (`d` tag), if exists.
Expand Down
4 changes: 1 addition & 3 deletions crates/nostr/src/event/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use alloc::vec::Vec;
use core::fmt;
use core::str::FromStr;

use bitcoin::secp256k1;
use bitcoin::secp256k1::schnorr::Signature;
use bitcoin::secp256k1::{self};

use super::{id, tag};
use crate::{key, Event, EventId, JsonUtil, Kind, PartialEvent, PublicKey, Tag, Timestamp};
Expand Down Expand Up @@ -122,11 +122,9 @@ impl TryFrom<&RawEvent> for PartialEvent {
fn try_from(raw: &RawEvent) -> Result<Self, Self::Error> {
let id: EventId = EventId::from_hex(&raw.id)?;
let public_key: PublicKey = PublicKey::from_hex(&raw.pubkey)?;
let sig: Signature = Signature::from_str(&raw.sig)?;
Ok(Self {
id,
pubkey: public_key,
sig,
})
}
}

0 comments on commit 6ffebcb

Please sign in to comment.