fix tag comparison to correctly handle binary and hex

This commit is contained in:
2025-11-26 14:15:03 +00:00
parent 81eebdcd1e
commit 40e67fc331
2 changed files with 21 additions and 7 deletions

View File

@@ -579,13 +579,13 @@ func main() {
// Access tag elements
fmt.Printf("Key: %s\n", eTag.Key()) // "e"
fmt.Printf("Value: %x\n", eTag.Value()) // event-id bytes
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

View File

@@ -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
}
}