mirror of
https://github.com/fiatjaf/nak.git
synced 2026-10-05 15:18:23 +00:00
convert tags to appropriate hex pubkeys, ids etc, even from nip05, but check the tag name first for better (hardcoded) context.
This commit is contained in:
@@ -212,8 +212,12 @@ example:
|
|||||||
if found {
|
if found {
|
||||||
// tags may also contain extra elements separated with a ";"
|
// tags may also contain extra elements separated with a ";"
|
||||||
tagValues := strings.Split(tagValue, ";")
|
tagValues := strings.Split(tagValue, ";")
|
||||||
|
val := tagValues[0]
|
||||||
|
if len(tagName) == 1 {
|
||||||
|
val = decodeTagValue(val, rune(tagName[0]))
|
||||||
|
}
|
||||||
if len(tagValues) >= 1 {
|
if len(tagValues) >= 1 {
|
||||||
tagValues[0] = decodeTagValue(tagValues[0])
|
tagValues[0] = val
|
||||||
}
|
}
|
||||||
tag = append(tag, tagValues...)
|
tag = append(tag, tagValues...)
|
||||||
}
|
}
|
||||||
@@ -221,21 +225,20 @@ example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, etag := range c.StringSlice("e") {
|
for _, etag := range c.StringSlice("e") {
|
||||||
decodedEtag := decodeTagValue(etag)
|
decodedEtag := decodeTagValue(etag, 'e')
|
||||||
if tags.FindWithValue("e", decodedEtag) == nil {
|
if tags.FindWithValue("e", decodedEtag) == nil {
|
||||||
tags = append(tags, nostr.Tag{"e", decodedEtag})
|
tags = append(tags, nostr.Tag{"e", decodedEtag})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, ptag := range c.StringSlice("p") {
|
for _, ptag := range c.StringSlice("p") {
|
||||||
decodedPtag := decodeTagValue(ptag)
|
decodedPtag := decodeTagValue(ptag, 'p')
|
||||||
if tags.FindWithValue("p", decodedPtag) == nil {
|
if tags.FindWithValue("p", decodedPtag) == nil {
|
||||||
tags = append(tags, nostr.Tag{"p", decodedPtag})
|
tags = append(tags, nostr.Tag{"p", decodedPtag})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, dtag := range c.StringSlice("d") {
|
for _, dtag := range c.StringSlice("d") {
|
||||||
decodedDtag := decodeTagValue(dtag)
|
if tags.FindWithValue("d", dtag) == nil {
|
||||||
if tags.FindWithValue("d", decodedDtag) == nil {
|
tags = append(tags, nostr.Tag{"d", dtag})
|
||||||
tags = append(tags, nostr.Tag{"d", decodedDtag})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(tags) > 0 {
|
if len(tags) > 0 {
|
||||||
|
|||||||
+18
-2
@@ -18,6 +18,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
"fiatjaf.com/nostr/nip05"
|
"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)
|
return nostr.ID{}, fmt.Errorf("invalid event id (\"%s\"): expected hex, note, or nevent", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeTagValue(value string) string {
|
func decodeTagValue(value string, letter rune) string {
|
||||||
if strings.HasPrefix(value, "npub1") || strings.HasPrefix(value, "nevent1") || strings.HasPrefix(value, "note1") || strings.HasPrefix(value, "nprofile1") || strings.HasPrefix(value, "naddr1") {
|
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 {
|
if ptr, err := nip19.ToPointer(value); err == nil {
|
||||||
return ptr.AsTagReference()
|
return ptr.AsTagReference()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -468,19 +468,23 @@ func applyFlagsToFilter(c *cli.Command, filter *nostr.Filter) error {
|
|||||||
for _, tagFlag := range c.StringSlice("tag") {
|
for _, tagFlag := range c.StringSlice("tag") {
|
||||||
spl := strings.SplitN(tagFlag, "=", 2)
|
spl := strings.SplitN(tagFlag, "=", 2)
|
||||||
if len(spl) == 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 {
|
} else {
|
||||||
return fmt.Errorf("invalid --tag '%s'", tagFlag)
|
return fmt.Errorf("invalid --tag '%s'", tagFlag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, etag := range c.StringSlice("e") {
|
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") {
|
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") {
|
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 {
|
if len(tags) > 0 && filter.Tags == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user