Skip to content
Merged
Prev Previous commit
Next Next commit
refactor: address additional Qodana issues (duplicate expressions, re…
…dundant casts, imports)
  • Loading branch information
erict875 committed Oct 6, 2025
commit d69c0c52a88d0600d41f51856e186ed4b34e1412
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![codecov](https://codecov.io/gh/tcheeric/nostr-java/branch/main/graph/badge.svg)](https://codecov.io/gh/tcheeric/nostr-java)
[![GitHub release](https://img.shields.io/github/v/release/tcheeric/nostr-java)](https://github.com/tcheeric/nostr-java/releases)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Qodana](https://github.com/tcheeric/nostr-java/actions/workflows/qodana_code_quality.yml/badge.svg)](https://github.com/tcheeric/nostr-java/actions/workflows/qodana_code_quality.yml)

`nostr-java` is a Java SDK for the [Nostr](https://github.com/nostr-protocol/nips) protocol. It provides utilities for creating, signing and publishing Nostr events to relays.

Expand Down
7 changes: 3 additions & 4 deletions nostr-java-api/src/main/java/nostr/api/NIP04.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import nostr.encryption.MessageCipher04;
import nostr.event.BaseTag;
import nostr.event.impl.GenericEvent;
import nostr.event.filter.Filterable;
import nostr.event.tag.GenericTag;
import nostr.event.tag.PubKeyTag;
import nostr.id.Identity;
Expand Down Expand Up @@ -131,12 +132,10 @@ public static String decrypt(@NonNull Identity rcptId, @NonNull GenericEvent eve
throw new IllegalArgumentException("Event is not an encrypted direct message");
}

var recipient =
event.getTags().stream()
.filter(t -> t.getCode().equalsIgnoreCase("p"))
PubKeyTag pTag =
Filterable.getTypeSpecificTags(PubKeyTag.class, event).stream()
.findFirst()
.orElseThrow(() -> new NoSuchElementException("No matching p-tag found."));
var pTag = (PubKeyTag) recipient;

boolean rcptFlag = amITheRecipient(rcptId, event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,13 @@ private static byte[] convertBits(byte[] data, int fromWidth, int toWidth, boole
result.add((byte) ((acc >> bits) & ((1 << toWidth) - 1)));
}
}
int mask = (1 << toWidth) - 1;
if (pad) {
if (bits > 0) {
result.add((byte) ((acc << (toWidth - bits)) & ((1 << toWidth) - 1)));
int partial = (acc << (toWidth - bits)) & mask;
result.add((byte) partial);
}
} else if (bits == fromWidth || ((acc << (toWidth - bits)) & ((1 << toWidth) - 1)) != 0) {
} else if (bits == fromWidth || ((acc << (toWidth - bits)) & mask) != 0) {
return null;
}
byte[] output = new byte[result.size()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,23 @@ public ChannelMessageEvent(PublicKey pubKey, List<BaseTag> baseTags, String cont
}

public String getChannelCreateEventId() {
return getTags().stream()
.filter(tag -> "e".equals(tag.getCode()))
.map(tag -> (EventTag) tag)
return nostr.event.filter.Filterable.getTypeSpecificTags(EventTag.class, this).stream()
.filter(tag -> tag.getMarker() == Marker.ROOT)
.map(EventTag::getIdEvent)
.findFirst()
.orElseThrow();
}

public String getChannelMessageReplyEventId() {
return getTags().stream()
.filter(tag -> "e".equals(tag.getCode()))
.map(tag -> (EventTag) tag)
return nostr.event.filter.Filterable.getTypeSpecificTags(EventTag.class, this).stream()
.filter(tag -> tag.getMarker() == Marker.REPLY)
.map(EventTag::getIdEvent)
.findFirst()
.orElse(null);
}

public Relay getRootRecommendedRelay() {
return getTags().stream()
.filter(tag -> "e".equals(tag.getCode()))
.map(tag -> (EventTag) tag)
return nostr.event.filter.Filterable.getTypeSpecificTags(EventTag.class, this).stream()
.filter(tag -> tag.getMarker() == Marker.ROOT)
.map(EventTag::getRecommendedRelayUrl)
.map(Relay::new)
Expand All @@ -55,9 +49,7 @@ public Relay getRootRecommendedRelay() {
}

public Relay getReplyRecommendedRelay(@NonNull String eventId) {
return getTags().stream()
.filter(tag -> "e".equals(tag.getCode()))
.map(tag -> (EventTag) tag)
return nostr.event.filter.Filterable.getTypeSpecificTags(EventTag.class, this).stream()
.filter(tag -> tag.getMarker() == Marker.REPLY && tag.getIdEvent().equals(eventId))
.map(EventTag::getRecommendedRelayUrl)
.map(Relay::new)
Expand All @@ -70,9 +62,7 @@ public void validate() {

// Check 'e' root - tag
EventTag rootTag =
getTags().stream()
.filter(tag -> "e".equals(tag.getCode()))
.map(tag -> (EventTag) tag)
nostr.event.filter.Filterable.getTypeSpecificTags(EventTag.class, this).stream()
.filter(tag -> tag.getMarker() == Marker.ROOT)
.findFirst()
.orElseThrow(() -> new AssertionError("Missing or invalid `e` root tag."));
Expand Down