From c1e72e0af1ba51e4a70f25c50c8ffeef8edac20c Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 13 Mar 2026 00:30:46 -0300 Subject: [PATCH] convert tags to appropriate hex pubkeys, ids etc, even from nip05, but check the tag name first for better (hardcoded) context. --- event.go | 15 +++++++++------ helpers.go | 20 ++++++++++++++++++-- req.go | 12 ++++++++---- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/event.go b/event.go index fc4e037..e304616 100644 --- a/event.go +++ b/event.go @@ -212,8 +212,12 @@ example: if found { // tags may also contain extra elements separated with a ";" tagValues := strings.Split(tagValue, ";") + val := tagValues[0] + if len(tagName) == 1 { + val = decodeTagValue(val, rune(tagName[0])) + } if len(tagValues) >= 1 { - tagValues[0] = decodeTagValue(tagValues[0]) + tagValues[0] = val } tag = append(tag, tagValues...) } @@ -221,21 +225,20 @@ example: } for _, etag := range c.StringSlice("e") { - decodedEtag := decodeTagValue(etag) + decodedEtag := decodeTagValue(etag, 'e') if tags.FindWithValue("e", decodedEtag) == nil { tags = append(tags, nostr.Tag{"e", decodedEtag}) } } for _, ptag := range c.StringSlice("p") { - decodedPtag := decodeTagValue(ptag) + decodedPtag := decodeTagValue(ptag, 'p') if tags.FindWithValue("p", decodedPtag) == nil { tags = append(tags, nostr.Tag{"p", decodedPtag}) } } for _, dtag := range c.StringSlice("d") { - decodedDtag := decodeTagValue(dtag) - if tags.FindWithValue("d", decodedDtag) == nil { - tags = append(tags, nostr.Tag{"d", decodedDtag}) + if tags.FindWithValue("d", dtag) == nil { + tags = append(tags, nostr.Tag{"d", dtag}) } } if len(tags) > 0 { diff --git a/helpers.go b/helpers.go index 181484d..cee604f 100644 --- a/helpers.go +++ b/helpers.go @@ -18,6 +18,7 @@ import ( "strings" "sync" "time" + "unicode" "fiatjaf.com/nostr" "fiatjaf.com/nostr/nip05" @@ -528,8 +529,23 @@ func parseEventID(value string) (nostr.ID, error) { return nostr.ID{}, fmt.Errorf("invalid event id (\"%s\"): expected hex, note, or nevent", value) } -func decodeTagValue(value string) string { - if strings.HasPrefix(value, "npub1") || strings.HasPrefix(value, "nevent1") || strings.HasPrefix(value, "note1") || strings.HasPrefix(value, "nprofile1") || strings.HasPrefix(value, "naddr1") { +func decodeTagValue(value string, letter rune) string { + letter = unicode.ToLower(letter) + + if letter == 'p' { + if nip05.IsValidIdentifier(value) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) + pp, err := nip05.QueryIdentifier(ctx, value) + cancel() + if err == nil { + return pp.PublicKey.Hex() + } + } + } + + if (letter == 'p' && (strings.HasPrefix(value, "npub1") || strings.HasPrefix(value, "nprofile1"))) || + ((letter == 'a' || letter == 'q') && strings.HasPrefix(value, "naddr1")) || + ((letter == 'e' || letter == 'q') && (strings.HasPrefix(value, "nevent1") || strings.HasPrefix(value, "note1"))) { if ptr, err := nip19.ToPointer(value); err == nil { return ptr.AsTagReference() } diff --git a/req.go b/req.go index 55be5f6..998a726 100644 --- a/req.go +++ b/req.go @@ -468,19 +468,23 @@ func applyFlagsToFilter(c *cli.Command, filter *nostr.Filter) error { for _, tagFlag := range c.StringSlice("tag") { spl := strings.SplitN(tagFlag, "=", 2) if len(spl) == 2 { - tags = append(tags, []string{spl[0], decodeTagValue(spl[1])}) + val := spl[1] + if len(spl) == 1 { + val = decodeTagValue(val, []rune(spl[0])[0]) + } + tags = append(tags, []string{spl[0], val}) } else { return fmt.Errorf("invalid --tag '%s'", tagFlag) } } for _, etag := range c.StringSlice("e") { - tags = append(tags, []string{"e", decodeTagValue(etag)}) + tags = append(tags, []string{"e", decodeTagValue(etag, 'e')}) } for _, ptag := range c.StringSlice("p") { - tags = append(tags, []string{"p", decodeTagValue(ptag)}) + tags = append(tags, []string{"p", decodeTagValue(ptag, 'p')}) } for _, dtag := range c.StringSlice("d") { - tags = append(tags, []string{"d", decodeTagValue(dtag)}) + tags = append(tags, []string{"d", dtag}) } if len(tags) > 0 && filter.Tags == nil {