-
Notifications
You must be signed in to change notification settings - Fork 28
Verify events before dispatch #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tcheeric
merged 2 commits into
develop
from
codex/verify-events-before-sending-to-relays
Aug 1, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.NonNull; | ||
| import nostr.api.service.NoteService; | ||
| import nostr.base.IEvent; | ||
| import nostr.base.ISignable; | ||
| import nostr.client.springwebsocket.SpringWebSocketClient; | ||
|
|
@@ -12,6 +13,7 @@ | |
| import nostr.event.message.ReqMessage; | ||
| import nostr.id.Identity; | ||
| import nostr.util.NostrUtil; | ||
| import nostr.api.service.impl.DefaultNoteService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
|
|
@@ -27,12 +29,23 @@ public class NostrSpringWebSocketClient implements NostrIF { | |
| @Getter | ||
| private Identity sender; | ||
|
|
||
| private NoteService noteService = new DefaultNoteService(); | ||
|
|
||
| private static volatile NostrSpringWebSocketClient INSTANCE; | ||
|
|
||
| public NostrSpringWebSocketClient(String relayName, String relayUri) { | ||
| setRelays(Map.of(relayName, relayUri)); | ||
| } | ||
|
|
||
| public NostrSpringWebSocketClient(@NonNull NoteService noteService) { | ||
| this.noteService = noteService; | ||
| } | ||
|
|
||
| public NostrSpringWebSocketClient(@NonNull Identity sender, @NonNull NoteService noteService) { | ||
| this.sender = sender; | ||
| this.noteService = noteService; | ||
| } | ||
|
|
||
| public static NostrIF getInstance() { | ||
| if (INSTANCE == null) { | ||
| synchronized (NostrSpringWebSocketClient.class) { | ||
|
|
@@ -78,8 +91,13 @@ public NostrIF setRelays(@NonNull Map<String, String> relays) { | |
|
|
||
| @Override | ||
| public List<String> sendEvent(@NonNull IEvent event) { | ||
| return clientMap.values().stream().map(client -> | ||
| client.sendEvent(event)).flatMap(List::stream).distinct().toList(); | ||
| if (event instanceof GenericEvent genericEvent) { | ||
| if (!verify(genericEvent)) { | ||
|
||
| throw new IllegalStateException("Event verification failed"); | ||
| } | ||
| } | ||
|
|
||
| return noteService.send(event, clientMap); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
nostr-java-api/src/main/java/nostr/api/service/NoteService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package nostr.api.service; | ||
|
|
||
| import lombok.NonNull; | ||
| import nostr.api.WebSocketClientHandler; | ||
| import nostr.base.IEvent; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public interface NoteService { | ||
| List<String> send(@NonNull IEvent event, @NonNull Map<String, WebSocketClientHandler> clients); | ||
| } |
23 changes: 23 additions & 0 deletions
23
nostr-java-api/src/main/java/nostr/api/service/impl/DefaultNoteService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package nostr.api.service.impl; | ||
|
|
||
| import lombok.NonNull; | ||
| import nostr.api.service.NoteService; | ||
| import nostr.api.WebSocketClientHandler; | ||
| import nostr.base.IEvent; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Default implementation that dispatches notes through all WebSocket clients. | ||
| */ | ||
| public class DefaultNoteService implements NoteService { | ||
| @Override | ||
| public List<String> send(@NonNull IEvent event, @NonNull Map<String, WebSocketClientHandler> clients) { | ||
| return clients.values().stream() | ||
| .map(client -> client.sendEvent(event)) | ||
| .flatMap(List::stream) | ||
| .distinct() | ||
| .toList(); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...ava-api/src/test/java/nostr/api/unit/NostrSpringWebSocketClientEventVerificationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package nostr.api.unit; | ||
|
|
||
| import nostr.api.NostrSpringWebSocketClient; | ||
| import nostr.api.service.NoteService; | ||
| import org.mockito.Mockito; | ||
| import nostr.config.Constants; | ||
| import nostr.event.impl.GenericEvent; | ||
| import nostr.id.Identity; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class NostrSpringWebSocketClientEventVerificationTest { | ||
|
|
||
| @Test | ||
| void sendEventThrowsWhenUnsigned() { | ||
| GenericEvent event = new GenericEvent(); | ||
| event.setPubKey(Identity.generateRandomIdentity().getPublicKey()); | ||
| event.setKind(Constants.Kind.SHORT_TEXT_NOTE); | ||
| event.setContent("test"); | ||
|
|
||
| NoteService service = Mockito.mock(NoteService.class); | ||
| Mockito.when(service.send(Mockito.any(), Mockito.any())).thenReturn(List.of()); | ||
| NostrSpringWebSocketClient client = new NostrSpringWebSocketClient(service); | ||
| assertThrows(IllegalStateException.class, () -> client.sendEvent(event)); | ||
| } | ||
|
|
||
| @Test | ||
| void sendEventReturnsEmptyListWhenSigned() { | ||
| Identity identity = Identity.generateRandomIdentity(); | ||
| GenericEvent event = new GenericEvent(identity.getPublicKey(), Constants.Kind.SHORT_TEXT_NOTE); | ||
| event.setContent("signed"); | ||
| identity.sign(event); | ||
|
|
||
| NoteService service = Mockito.mock(NoteService.class); | ||
| Mockito.when(service.send(Mockito.any(), Mockito.any())).thenReturn(List.of()); | ||
| NostrSpringWebSocketClient client = new NostrSpringWebSocketClient(service); | ||
| List<String> responses = client.sendEvent(event); | ||
| assertTrue(responses.isEmpty()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The field is initialized with a default implementation but can be overridden by constructors. Consider using final modifier and proper constructor chaining to ensure consistent initialization.