Skip to content

Commit 6ffebcb

Browse files
committed
nostr: move sig field from PartialEvent to MissingPartialEvent
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent db4b120 commit 6ffebcb

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* nostr: change `EventBuilder::award_badge` fingerprint ([Yuki Kishimoto])
3636
* nostr: add NIP-50 support to `Filter::match_event` method ([Yuki Kishimoto])
3737
* nostr: remove `Arc<T>` from `OnceCell<T>` in `Event` and `Tag` ([Yuki Kishimoto])
38+
* nostr: move `sig` field from `PartialEvent` to `MissingPartialEvent` ([Yuki Kishimoto])
3839
* pool: take mutex ownership instead of clone in `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
3940
* pool: remove IDs collection from `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
4041
* pool: better checks before perform queries or send messages to relays ([Yuki Kishimoto])

crates/nostr-relay-pool/src/relay/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ impl InternalRelay {
968968
}
969969

970970
// Deserialize missing event fields
971-
let missing: MissingPartialEvent = MissingPartialEvent::from_raw(event);
971+
let missing: MissingPartialEvent = MissingPartialEvent::from_raw(event)?;
972972

973973
// TODO: check if word/hashtag is blacklisted
974974

crates/nostr/src/event/partial.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use alloc::string::String;
88
use alloc::vec::Vec;
99
use core::fmt;
10+
use core::str::FromStr;
1011

12+
use bitcoin::secp256k1;
1113
use bitcoin::secp256k1::schnorr::Signature;
1214

1315
use super::raw::{self, RawEvent};
@@ -23,6 +25,8 @@ pub enum Error {
2325
RawEvent(raw::Error),
2426
/// Tag parse
2527
Tag(tag::Error),
28+
/// Secp256k1 error
29+
Secp256k1(secp256k1::Error),
2630
/// Invalid signature
2731
InvalidSignature,
2832
}
@@ -36,6 +40,7 @@ impl fmt::Display for Error {
3640
Self::Json(e) => write!(f, "Json: {e}"),
3741
Self::RawEvent(e) => write!(f, "Raw event: {e}"),
3842
Self::Tag(e) => write!(f, "Tag: {e}"),
43+
Self::Secp256k1(e) => write!(f, "{e}"),
3944
Self::InvalidSignature => write!(f, "Invalid signature"),
4045
}
4146
}
@@ -59,15 +64,19 @@ impl From<tag::Error> for Error {
5964
}
6065
}
6166

67+
impl From<secp256k1::Error> for Error {
68+
fn from(e: secp256k1::Error) -> Self {
69+
Self::Secp256k1(e)
70+
}
71+
}
72+
6273
/// Partial event
6374
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
6475
pub struct PartialEvent {
6576
/// ID
6677
pub id: EventId,
6778
/// Author
6879
pub pubkey: PublicKey,
69-
/// Signature
70-
pub sig: Signature,
7180
}
7281

7382
impl PartialEvent {
@@ -91,7 +100,7 @@ impl PartialEvent {
91100
missing.kind,
92101
tags,
93102
missing.content,
94-
self.sig,
103+
missing.sig,
95104
))
96105
}
97106
}
@@ -111,18 +120,21 @@ pub struct MissingPartialEvent {
111120
pub tags: Vec<Vec<String>>,
112121
/// Content
113122
pub content: String,
123+
/// Signature
124+
pub sig: Signature,
114125
}
115126

116127
impl MissingPartialEvent {
117128
/// Compose from [RawEvent]
118129
#[inline]
119-
pub fn from_raw(raw: RawEvent) -> Self {
120-
Self {
130+
pub fn from_raw(raw: RawEvent) -> Result<Self, Error> {
131+
Ok(Self {
121132
created_at: Timestamp::from(raw.created_at),
122133
kind: Kind::from(raw.kind),
123134
tags: raw.tags,
124135
content: raw.content,
125-
}
136+
sig: Signature::from_str(&raw.sig)?,
137+
})
126138
}
127139

128140
/// Extract identifier (`d` tag), if exists.

crates/nostr/src/event/raw.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use alloc::vec::Vec;
1010
use core::fmt;
1111
use core::str::FromStr;
1212

13+
use bitcoin::secp256k1;
1314
use bitcoin::secp256k1::schnorr::Signature;
14-
use bitcoin::secp256k1::{self};
1515

1616
use super::{id, tag};
1717
use crate::{key, Event, EventId, JsonUtil, Kind, PartialEvent, PublicKey, Tag, Timestamp};
@@ -122,11 +122,9 @@ impl TryFrom<&RawEvent> for PartialEvent {
122122
fn try_from(raw: &RawEvent) -> Result<Self, Self::Error> {
123123
let id: EventId = EventId::from_hex(&raw.id)?;
124124
let public_key: PublicKey = PublicKey::from_hex(&raw.pubkey)?;
125-
let sig: Signature = Signature::from_str(&raw.sig)?;
126125
Ok(Self {
127126
id,
128127
pubkey: public_key,
129-
sig,
130128
})
131129
}
132130
}

0 commit comments

Comments
 (0)