Performant validation. Prevent redundant decoding. Remove unused errors.
This commit is contained in:
+12
-8
@@ -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...)
|
||||
|
||||
Reference in New Issue
Block a user