1717import java .util .List ;
1818import java .util .NoSuchElementException ;
1919import 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