refactoring, added documentation

This commit is contained in:
Jay
2025-10-23 16:16:27 -04:00
parent e0c669351c
commit 205aafcfe5
13 changed files with 342 additions and 228 deletions

37
id.go Normal file
View File

@@ -0,0 +1,37 @@
package roots
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
}