diff --git a/nostr-java-api/src/main/java/nostr/api/NIP04.java b/nostr-java-api/src/main/java/nostr/api/NIP04.java index 6611516b..5e01ca08 100644 --- a/nostr-java-api/src/main/java/nostr/api/NIP04.java +++ b/nostr-java-api/src/main/java/nostr/api/NIP04.java @@ -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"); @@ -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; } diff --git a/nostr-java-api/src/test/java/nostr/api/unit/NIP04Test.java b/nostr-java-api/src/test/java/nostr/api/unit/NIP04Test.java index 14fcbfa0..076396e5 100644 --- a/nostr-java-api/src/test/java/nostr/api/unit/NIP04Test.java +++ b/nostr-java-api/src/test/java/nostr/api/unit/NIP04Test.java @@ -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; @@ -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). * @@ -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