Refactored into namespaced packages.

This commit is contained in:
Jay
2025-10-31 19:12:21 -04:00
parent 223c9faec0
commit 67db088981
20 changed files with 150 additions and 120 deletions

37
events/id.go Normal file
View File

@@ -0,0 +1,37 @@
package events
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
)
// Serialize returns the canonical JSON array representation of the event.
// used for ID computation: [0, pubkey, created_at, kind, tags, content].
func (e *Event) Serialize() ([]byte, error) {
serialized := []interface{}{
0,
e.PubKey,
e.CreatedAt,
e.Kind,
e.Tags,
e.Content,
}
bytes, err := json.Marshal(serialized)
if err != nil {
return []byte{}, err
}
return bytes, nil
}
// GetID computes and returns the event ID as a lowercase, hex-encoded SHA-256 hash
// of the serialized event.
func (e *Event) GetID() (string, error) {
bytes, err := e.Serialize()
if err != nil {
return "", err
}
hash := sha256.Sum256(bytes)
return hex.EncodeToString(hash[:]), nil
}