Skip to content

Commit 4274095

Browse files
committed
fix: restore GenericTag fallback for DM decrypt
1 parent 199764f commit 4274095

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

nostr-java-api/src/main/java/nostr/api/NIP04.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.NoSuchElementException;
1919
import java.util.Objects;
20+
import java.util.Optional;
2021

2122
/**
2223
* NIP-04: Encrypted Direct Messages.
@@ -344,6 +345,7 @@ public static String decrypt(@NonNull Identity rcptId, @NonNull GenericEvent eve
344345
PubKeyTag pTag =
345346
Filterable.getTypeSpecificTags(PubKeyTag.class, event).stream()
346347
.findFirst()
348+
.or(() -> findGenericPubKeyTag(event))
347349
.orElseThrow(() -> new NoSuchElementException("No matching p-tag found."));
348350

349351
boolean rcptFlag = amITheRecipient(rcptId, event);
@@ -364,6 +366,26 @@ public static String decrypt(@NonNull Identity rcptId, @NonNull GenericEvent eve
364366
return cipher.decrypt(event.getContent());
365367
}
366368

369+
private static Optional<PubKeyTag> findGenericPubKeyTag(GenericEvent event) {
370+
return event.getTags().stream()
371+
.filter(tag -> "p".equalsIgnoreCase(tag.getCode()))
372+
.map(NIP04::toPubKeyTag)
373+
.findFirst();
374+
}
375+
376+
private static PubKeyTag toPubKeyTag(BaseTag tag) {
377+
if (tag instanceof PubKeyTag pubKeyTag) {
378+
return pubKeyTag;
379+
}
380+
381+
if (tag instanceof GenericTag genericTag) {
382+
return PubKeyTag.updateFields(genericTag);
383+
}
384+
385+
throw new IllegalArgumentException(
386+
"Unsupported tag type for p-tag conversion: " + tag.getClass().getName());
387+
}
388+
367389
private static boolean amITheRecipient(@NonNull Identity recipient, @NonNull GenericEvent event) {
368390
// Use helper to fetch the p-tag without manual casts
369391
PubKeyTag pTag =

0 commit comments

Comments
 (0)