diff --git a/README.md b/README.md index fe5cd2e..532d42e 100644 --- a/README.md +++ b/README.md @@ -578,14 +578,14 @@ func main() { tags := tag.NewS(eTag, pTag, tTag) // Access tag elements - fmt.Printf("Key: %s\n", eTag.Key()) // "e" - fmt.Printf("Value: %x\n", eTag.Value()) // event-id bytes - fmt.Printf("Relay: %s\n", eTag.Relay()) // relay URL + fmt.Printf("Key: %s\n", eTag.Key()) // "e" + fmt.Printf("Value: %s\n", eTag.ValueHex()) // event-id as hex (handles binary storage) + fmt.Printf("Relay: %s\n", eTag.Relay()) // relay URL - // Find tags + // Find tags - use ValueHex() for e/p tags (may be binary-encoded internally) pTags := tags.GetAll([]byte("p")) for _, pt := range pTags { - fmt.Printf("P tag: %x\n", pt.Value()) + fmt.Printf("P tag: %s\n", pt.ValueHex()) // Always returns hex regardless of storage } // Filter tags diff --git a/encoders/tag/tags.go b/encoders/tag/tags.go index 973663b..d7241e4 100644 --- a/encoders/tag/tags.go +++ b/encoders/tag/tags.go @@ -46,7 +46,8 @@ func (s *S) Append(t ...*T) { } // ContainsAny returns true if any of the values given in `values` matches any -// of the tag elements. +// of the tag elements. For e/p tags with binary-encoded values, this handles +// comparison correctly by using ValueHex() to ensure consistent comparison. func (s *S) ContainsAny(tagName []byte, values [][]byte) bool { if s == nil { return false @@ -54,6 +55,9 @@ func (s *S) ContainsAny(tagName []byte, values [][]byte) bool { if len(tagName) < 1 { return false } + // Check if this is a binary-optimized tag type (e or p) + isBinaryTag := len(tagName) == 1 && (tagName[0] == 'e' || tagName[0] == 'p') + for _, v := range *s { if v.Len() < 2 { continue @@ -61,8 +65,18 @@ func (s *S) ContainsAny(tagName []byte, values [][]byte) bool { if !utils.FastEqual(v.Key(), tagName) { continue } + + // For e/p tags, use ValueHex() to get consistent hex representation + // regardless of whether the value is stored in binary or hex format + var tagValue []byte + if isBinaryTag { + tagValue = v.ValueHex() + } else { + tagValue = v.Value() + } + for _, candidate := range values { - if bytes.HasPrefix(v.Value(), candidate) { + if bytes.HasPrefix(tagValue, candidate) { return true } }