Performant validation. Prevent redundant decoding. Remove unused errors.

This commit is contained in:
Jay
2026-04-20 22:59:48 -04:00
parent 62aeef4eaf
commit 747781f5bf
4 changed files with 57 additions and 42 deletions
+12 -8
View File
@@ -6,6 +6,18 @@ import (
"strconv"
)
// GetID computes and returns the event ID as a lowercase, hex-encoded SHA-256 hash
// of the serialized event.
func GetID(e Event) string {
hash := GetIDBytes(e)
return hex.EncodeToString(hash[:])
}
// GetIDBytes computes and returns the event ID as a raw SHA256 digest
func GetIDBytes(e Event) [32]byte {
return sha256.Sum256(Serialize(e))
}
// Serialize returns the canonical JSON array representation of the event.
// used for ID computation: [0, pubkey, created_at, kind, tags, content].
func Serialize(e Event) []byte {
@@ -13,14 +25,6 @@ func Serialize(e Event) []byte {
return appendSerialized(buf, e)
}
// GetID computes and returns the event ID as a lowercase, hex-encoded SHA-256 hash
// of the serialized event.
func GetID(e Event) string {
bytes := Serialize(e)
hash := sha256.Sum256(bytes)
return hex.EncodeToString(hash[:])
}
func appendSerialized(dst []byte, e Event) []byte {
dst = append(dst, "[0,\""...)
dst = append(dst, e.PubKey...)