Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions nostr-java-api/src/main/java/nostr/api/NIP04.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static String decrypt(@NonNull Identity rcptId, @NonNull GenericEvent eve
.or(() -> findGenericPubKeyTag(event))
.orElseThrow(() -> new NoSuchElementException("No matching p-tag found."));

boolean rcptFlag = amITheRecipient(rcptId, event);
boolean rcptFlag = amITheRecipient(rcptId, event, pTag);

if (!rcptFlag) { // I am the message sender
log.debug("Decrypting own sent message");
Expand Down Expand Up @@ -386,14 +386,11 @@ private static PubKeyTag toPubKeyTag(BaseTag tag) {
"Unsupported tag type for p-tag conversion: " + tag.getClass().getName());
}

private static boolean amITheRecipient(@NonNull Identity recipient, @NonNull GenericEvent event) {
// Use helper to fetch the p-tag without manual casts
PubKeyTag pTag =
Filterable.getTypeSpecificTags(PubKeyTag.class, event).stream()
.findFirst()
.orElseThrow(() -> new NoSuchElementException("No matching p-tag found."));

if (Objects.equals(recipient.getPublicKey(), pTag.getPublicKey())) {
private static boolean amITheRecipient(
@NonNull Identity recipient,
@NonNull GenericEvent event,
@NonNull PubKeyTag resolvedPubKeyTag) {
if (Objects.equals(recipient.getPublicKey(), resolvedPubKeyTag.getPublicKey())) {
return true;
}

Expand Down
28 changes: 28 additions & 0 deletions nostr-java-api/src/test/java/nostr/api/unit/NIP04Test.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package nostr.api.unit;

import nostr.api.NIP04;
import nostr.base.ElementAttribute;
import nostr.base.Kind;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.GenericTag;
import nostr.event.tag.PubKeyTag;
import nostr.id.Identity;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -14,6 +16,8 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

/**
* Unit tests for NIP-04 (Encrypted Direct Messages).
*
Expand Down Expand Up @@ -152,6 +156,30 @@ public void testEncryptSpecialCharacters() {
assertEquals(content, decrypted);
}

@Test
// Ensures decrypt can resolve generic p-tags when determining the recipient.
public void testDecryptWithGenericPubKeyTagFallback() {
String content = "Generic tag ciphertext";

String encrypted = NIP04.encrypt(sender, content, recipient.getPublicKey());

GenericTag genericPTag =
new GenericTag(
"p",
List.of(new ElementAttribute("param0", recipient.getPublicKey().toString())));

GenericEvent event =
GenericEvent.builder()
.pubKey(sender.getPublicKey())
.kind(Kind.ENCRYPTED_DIRECT_MESSAGE)
.tags(List.of(genericPTag))
.content(encrypted)
.build();

String decrypted = NIP04.decrypt(recipient, event);
assertEquals(content, decrypted);
}

@Test
public void testDecryptInvalidEventKindThrowsException() {
// Create a non-DM event
Expand Down