Skip to content

Commit 7089339

Browse files
committed
relay-builder: add support for NIP-70 protected events
Require the client to auth for publishing NIP-70 protected events. Pull-Request: #875 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 02108f7 commit 7089339

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- mls: add new crate for implementing MLS messaging ([JeffG] at https://github.com/rust-nostr/nostr/pull/843)
5050
- pool: add relay monitor ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/851)
5151
- sdk: add `Options::pool` ([Yuki Kishimoto])
52+
- relay-builder: add support for NIP-70 protected events ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/875)
5253

5354
### Fixed
5455

crates/nostr-relay-builder/src/local/inner.rs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -334,34 +334,56 @@ impl InnerLocalRelay {
334334
}
335335
}
336336

337-
// Check NIP42
338-
if let Some(nip42) = &self.nip42 {
339-
// TODO: check if public key allowed
337+
// Check if it's configured to require NIP42 authentication for writing
338+
let require_nip42_auth: bool = match &self.nip42 {
339+
Some(nip42) => nip42.mode.is_write(),
340+
None => false,
341+
};
340342

341-
// Check mode and if it's authenticated
342-
if nip42.mode.is_write() && !session.nip42.is_authenticated() {
343-
// Generate and send AUTH challenge
344-
send_msg(
345-
ws_tx,
346-
RelayMessage::Auth {
347-
challenge: Cow::Owned(session.nip42.generate_challenge()),
348-
},
349-
)
350-
.await?;
343+
// Check if it's a protected event
344+
let is_protected: bool = event.is_protected();
351345

352-
// Return error
353-
return send_msg(
346+
// Check if authentication is required
347+
if (require_nip42_auth || is_protected) && !session.nip42.is_authenticated() {
348+
// Generate and send AUTH challenge
349+
send_msg(
350+
ws_tx,
351+
RelayMessage::Auth {
352+
challenge: Cow::Owned(session.nip42.generate_challenge()),
353+
},
354+
).await?;
355+
356+
// Return error
357+
return send_msg(
358+
ws_tx,
359+
RelayMessage::Ok {
360+
event_id: event.id,
361+
status: false,
362+
message: Cow::Owned(format!(
363+
"{}: you must auth",
364+
MachineReadablePrefix::AuthRequired
365+
)),
366+
},
367+
).await;
368+
}
369+
370+
if is_protected {
371+
if let Some(authenticated_public_key) = &session.nip42.public_key {
372+
// Block if the event author not matches the authenticated public key
373+
if event.pubkey != *authenticated_public_key {
374+
return send_msg(
354375
ws_tx,
355376
RelayMessage::Ok {
356377
event_id: event.id,
357378
status: false,
358379
message: Cow::Owned(format!(
359-
"{}: you must auth",
360-
MachineReadablePrefix::AuthRequired
380+
"{}: this event may only be published by its author",
381+
MachineReadablePrefix::Blocked
361382
)),
362383
},
363384
)
364-
.await;
385+
.await;
386+
}
365387
}
366388
}
367389

0 commit comments

Comments
 (0)