Skip to content

Commit 4542282

Browse files
committed
nostr: remove event verification from deserialization constructors
1 parent 1371314 commit 4542282

File tree

5 files changed

+41
-120
lines changed

5 files changed

+41
-120
lines changed

crates/nostr/src/event/builder.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,8 @@ mod tests {
876876
#[cfg(feature = "std")]
877877
use core::str::FromStr;
878878

879-
use bitcoin::secp256k1::Secp256k1;
880879
#[cfg(feature = "std")]
881-
use bitcoin::secp256k1::SecretKey;
880+
use bitcoin::secp256k1::{Secp256k1, SecretKey};
882881

883882
use super::*;
884883

@@ -935,14 +934,12 @@ mod tests {
935934

936935
#[test]
937936
fn test_zap_event_builder() {
938-
let secp = Secp256k1::new();
939-
940937
let bolt11 = String::from("lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0");
941938
let preimage = Some(String::from(
942939
"5d006d2cf1e73c7148e7519a4c68adc81642ce0e25a432b2434c99f97344c15f",
943940
));
944941
let zap_request_json = String::from("{\"pubkey\":\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\",\"content\":\"\",\"id\":\"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d\",\"created_at\":1674164539,\"sig\":\"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d\",\"kind\":9734,\"tags\":[[\"e\",\"3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8\"],[\"p\",\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\"],[\"relays\",\"wss://relay.damus.io\",\"wss://nostr-relay.wlvs.space\",\"wss://nostr.fmt.wiz.biz\",\"wss://relay.nostr.bg\",\"wss://nostr.oxtr.dev\",\"wss://nostr.v0l.io\",\"wss://brb.io\",\"wss://nostr.bitcoiner.social\",\"ws://monad.jb55.com:8080\",\"wss://relay.snort.social\"]]}");
945-
let zap_request_event: Event = Event::from_json_with_ctx(&secp, zap_request_json).unwrap();
942+
let zap_request_event: Event = Event::from_json(zap_request_json).unwrap();
946943
let event_builder = EventBuilder::new_zap_receipt(bolt11, preimage, zap_request_event);
947944

948945
assert_eq!(5, event_builder.tags.len());
@@ -959,12 +956,10 @@ mod tests {
959956

960957
#[test]
961958
fn test_zap_event_builder_without_preimage() {
962-
let secp = Secp256k1::new();
963-
964959
let bolt11 = String::from("lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0");
965960
let preimage = None;
966961
let zap_request_json = String::from("{\"pubkey\":\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\",\"content\":\"\",\"id\":\"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d\",\"created_at\":1674164539,\"sig\":\"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d\",\"kind\":9734,\"tags\":[[\"e\",\"3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8\"],[\"p\",\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\"],[\"relays\",\"wss://relay.damus.io\",\"wss://nostr-relay.wlvs.space\",\"wss://nostr.fmt.wiz.biz\",\"wss://relay.nostr.bg\",\"wss://nostr.oxtr.dev\",\"wss://nostr.v0l.io\",\"wss://brb.io\",\"wss://nostr.bitcoiner.social\",\"ws://monad.jb55.com:8080\",\"wss://relay.snort.social\"]]}");
967-
let zap_request_event = Event::from_json_with_ctx(&secp, zap_request_json).unwrap();
962+
let zap_request_event = Event::from_json(zap_request_json).unwrap();
968963
let event_builder = EventBuilder::new_zap_receipt(bolt11, preimage, zap_request_event);
969964

970965
assert_eq!(4, event_builder.tags.len());

crates/nostr/src/event/mod.rs

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,40 @@ pub struct Event {
115115
}
116116

117117
impl Event {
118-
/// Verify event
118+
/// Deserialize [`Event`] from [`Value`]
119+
///
120+
/// **This method NOT verify the signature!**
121+
pub fn from_value(value: Value) -> Result<Self, Error> {
122+
Ok(serde_json::from_value(value)?)
123+
}
124+
125+
/// Deserialize [`Event`] from JSON
126+
///
127+
/// **This method NOT verify the signature!**
128+
pub fn from_json<S>(json: S) -> Result<Self, Error>
129+
where
130+
S: Into<String>,
131+
{
132+
Ok(serde_json::from_str(&json.into())?)
133+
}
134+
135+
/// Serialize [`Event`] as JSON
136+
pub fn as_json(&self) -> String {
137+
serde_json::json!(self).to_string()
138+
}
139+
140+
/// Verify [`EventId`] and [`Signature`]
119141
#[cfg(feature = "std")]
120142
pub fn verify(&self) -> Result<(), Error> {
121143
self.verify_with_ctx(&SECP256K1)
122144
}
123145

124-
/// Verify event
146+
/// Verify [`EventId`] and [`Signature`]
125147
pub fn verify_with_ctx<C>(&self, secp: &Secp256k1<C>) -> Result<(), Error>
126148
where
127149
C: Verification,
128150
{
129-
let id = EventId::new(
151+
let id: EventId = EventId::new(
130152
&self.pubkey,
131153
self.created_at,
132154
&self.kind,
@@ -142,47 +164,6 @@ impl Event {
142164
}
143165
}
144166

145-
/// New event from [`Value`]
146-
#[cfg(feature = "std")]
147-
pub fn from_value(value: Value) -> Result<Self, Error> {
148-
Self::from_value_with_ctx(&SECP256K1, value)
149-
}
150-
151-
/// New event from [`Value`]
152-
pub fn from_value_with_ctx<C>(secp: &Secp256k1<C>, value: Value) -> Result<Self, Error>
153-
where
154-
C: Verification,
155-
{
156-
let event: Self = serde_json::from_value(value)?;
157-
event.verify_with_ctx(secp)?;
158-
Ok(event)
159-
}
160-
161-
/// New event from json string
162-
#[cfg(feature = "std")]
163-
pub fn from_json<S>(json: S) -> Result<Self, Error>
164-
where
165-
S: Into<String>,
166-
{
167-
Self::from_json_with_ctx(&SECP256K1, json)
168-
}
169-
170-
/// New event from json string
171-
pub fn from_json_with_ctx<C, S>(secp: &Secp256k1<C>, json: S) -> Result<Self, Error>
172-
where
173-
C: Verification,
174-
S: Into<String>,
175-
{
176-
let event: Self = serde_json::from_str(&json.into())?;
177-
event.verify_with_ctx(secp)?;
178-
Ok(event)
179-
}
180-
181-
/// Get event as json string
182-
pub fn as_json(&self) -> String {
183-
serde_json::json!(self).to_string()
184-
}
185-
186167
/// Returns `true` if the event has an expiration tag that is expired.
187168
/// If an event has no `Expiration` tag, then it will return `false`.
188169
#[cfg(feature = "std")]
@@ -283,11 +264,9 @@ mod tests {
283264

284265
#[test]
285266
fn test_tags_deser_without_recommended_relay() {
286-
let secp = Secp256k1::new();
287-
288267
//The TAG array has dynamic length because the third element(Recommended relay url) is optional
289268
let sample_event = r#"{"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","created_at":1640839235,"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","kind":4,"pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","sig":"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd","tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]]}"#;
290-
let ev_ser = Event::from_json_with_ctx(&secp, sample_event).unwrap();
269+
let ev_ser = Event::from_json(sample_event).unwrap();
291270
assert_eq!(ev_ser.as_json(), sample_event);
292271
}
293272

crates/nostr/src/event/tag.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,11 +1304,9 @@ mod tests {
13041304

13051305
#[test]
13061306
fn test_deserialize_tag_from_event() {
1307-
let secp = Secp256k1::new();
1308-
13091307
// Got this fresh off the wire
13101308
let event: &str = r#"{"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","created_at":1640839235,"kind":4,"tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]],"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","sig":"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd"}"#;
1311-
let event = Event::from_json_with_ctx(&secp, event).unwrap();
1309+
let event = Event::from_json(event).unwrap();
13121310
let tag = event.tags.first().unwrap();
13131311

13141312
assert_eq!(

crates/nostr/src/message/client.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ use alloc::boxed::Box;
88
use alloc::string::{String, ToString};
99
use alloc::vec::Vec;
1010

11-
use bitcoin::secp256k1::{Secp256k1, Verification};
1211
use negentropy::{Bytes, Negentropy};
13-
#[cfg(feature = "std")]
1412
use serde::{Deserialize, Deserializer};
1513
use serde::{Serialize, Serializer};
1614
use serde_json::{json, Value};
1715

1816
use super::{Filter, MessageHandleError, SubscriptionId};
1917
use crate::Event;
20-
#[cfg(feature = "std")]
21-
use crate::SECP256K1;
2218

2319
/// Messages sent by clients, received by relays
2420
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -80,7 +76,6 @@ impl Serialize for ClientMessage {
8076
}
8177
}
8278

83-
#[cfg(feature = "std")]
8479
impl<'de> Deserialize<'de> for ClientMessage {
8580
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
8681
where
@@ -217,19 +212,9 @@ impl ClientMessage {
217212
}
218213

219214
/// Deserialize from [`Value`]
220-
#[cfg(feature = "std")]
215+
///
216+
/// **This method NOT verify the event signature!**
221217
pub fn from_value(msg: Value) -> Result<Self, MessageHandleError> {
222-
Self::from_value_with_ctx(&SECP256K1, msg)
223-
}
224-
225-
/// Deserialize from [`Value`]
226-
pub fn from_value_with_ctx<C>(
227-
secp: &Secp256k1<C>,
228-
msg: Value,
229-
) -> Result<Self, MessageHandleError>
230-
where
231-
C: Verification,
232-
{
233218
let v = msg
234219
.as_array()
235220
.ok_or(MessageHandleError::InvalidMessageFormat)?;
@@ -246,7 +231,7 @@ impl ClientMessage {
246231
if v_len != 2 {
247232
return Err(MessageHandleError::InvalidMessageFormat);
248233
}
249-
let event = Event::from_json_with_ctx(secp, v[1].to_string())?;
234+
let event = Event::from_value(v[1].clone())?;
250235
return Ok(Self::new_event(event));
251236
}
252237

@@ -297,7 +282,7 @@ impl ClientMessage {
297282
if v_len != 2 {
298283
return Err(MessageHandleError::InvalidMessageFormat);
299284
}
300-
let event = Event::from_json_with_ctx(secp, v[1].to_string())?;
285+
let event = Event::from_value(v[1].clone())?;
301286
return Ok(Self::new_auth(event));
302287
}
303288

@@ -349,19 +334,11 @@ impl ClientMessage {
349334
}
350335

351336
/// Deserialize [`ClientMessage`] from JSON string
352-
#[cfg(feature = "std")]
337+
///
338+
/// **This method NOT verify the event signature!**
353339
pub fn from_json<S>(msg: S) -> Result<Self, MessageHandleError>
354340
where
355341
S: Into<String>,
356-
{
357-
Self::from_json_with_ctx(&SECP256K1, msg)
358-
}
359-
360-
/// Deserialize [`ClientMessage`] from JSON string
361-
pub fn from_json_with_ctx<C, S>(secp: &Secp256k1<C>, msg: S) -> Result<Self, MessageHandleError>
362-
where
363-
C: Verification,
364-
S: Into<String>,
365342
{
366343
let msg: &str = &msg.into();
367344
tracing::trace!("{}", msg);
@@ -371,7 +348,7 @@ impl ClientMessage {
371348
}
372349

373350
let value: Value = serde_json::from_str(msg)?;
374-
Self::from_value_with_ctx(secp, value)
351+
Self::from_value(value)
375352
}
376353
}
377354

@@ -423,8 +400,6 @@ mod tests {
423400

424401
#[test]
425402
fn test_negative_timestamp() {
426-
let secp = Secp256k1::new();
427-
428403
let req = json!([
429404
"REQ",
430405
"some_id",
@@ -441,7 +416,7 @@ mod tests {
441416
}
442417
]);
443418

444-
let msg = ClientMessage::from_value_with_ctx(&secp, req.clone()).unwrap();
419+
let msg = ClientMessage::from_value(req.clone()).unwrap();
445420

446421
assert_eq!(msg.as_value(), req)
447422
}

crates/nostr/src/message/relay.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ use alloc::boxed::Box;
88
use alloc::string::{String, ToString};
99
use core::fmt;
1010

11-
use bitcoin::secp256k1::{Secp256k1, Verification};
1211
use serde::{Deserialize, Deserializer};
1312
use serde::{Serialize, Serializer};
1413
use serde_json::{json, Value};
1514

1615
use super::MessageHandleError;
17-
#[cfg(feature = "std")]
18-
use crate::SECP256K1;
1916
use crate::{Event, EventId, SubscriptionId};
2017

2118
/// Negentropy error code
@@ -146,7 +143,6 @@ impl Serialize for RelayMessage {
146143
}
147144
}
148145

149-
#[cfg(feature = "std")]
150146
impl<'de> Deserialize<'de> for RelayMessage {
151147
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
152148
where
@@ -248,19 +244,7 @@ impl RelayMessage {
248244
}
249245

250246
/// Deserialize [`RelayMessage`] from [`Value`]
251-
#[cfg(feature = "std")]
252-
fn from_value(msg: Value) -> Result<Self, MessageHandleError> {
253-
Self::from_value_with_ctx(&SECP256K1, msg)
254-
}
255-
256-
/// Deserialize [`RelayMessage`] from [`Value`]
257-
pub fn from_value_with_ctx<C>(
258-
secp: &Secp256k1<C>,
259-
msg: Value,
260-
) -> Result<Self, MessageHandleError>
261-
where
262-
C: Verification,
263-
{
247+
pub fn from_value(msg: Value) -> Result<Self, MessageHandleError> {
264248
let v = msg
265249
.as_array()
266250
.ok_or(MessageHandleError::InvalidMessageFormat)?;
@@ -289,7 +273,7 @@ impl RelayMessage {
289273
}
290274

291275
let subscription_id: SubscriptionId = serde_json::from_value(v[1].clone())?;
292-
let event = Event::from_json_with_ctx(secp, v[2].to_string())?;
276+
let event = Event::from_value(v[2].clone())?;
293277

294278
return Ok(Self::new_event(subscription_id, event));
295279
}
@@ -381,19 +365,9 @@ impl RelayMessage {
381365
}
382366

383367
/// Deserialize [`RelayMessage`] as JSON string
384-
#[cfg(feature = "std")]
385368
pub fn from_json<S>(msg: S) -> Result<Self, MessageHandleError>
386369
where
387370
S: Into<String>,
388-
{
389-
Self::from_json_with_ctx(&SECP256K1, msg)
390-
}
391-
392-
/// Deserialize [`RelayMessage`] as JSON string
393-
pub fn from_json_with_ctx<C, S>(secp: &Secp256k1<C>, msg: S) -> Result<Self, MessageHandleError>
394-
where
395-
C: Verification,
396-
S: Into<String>,
397371
{
398372
let msg: &str = &msg.into();
399373
tracing::trace!("{}", msg);
@@ -403,7 +377,7 @@ impl RelayMessage {
403377
}
404378

405379
let value: Value = serde_json::from_str(msg)?;
406-
Self::from_value_with_ctx(secp, value)
380+
Self::from_value(value)
407381
}
408382
}
409383

0 commit comments

Comments
 (0)