1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package nostr
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
)
// Event represents a Nostr event as defined in NIP-01.
type Event struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt int64 `json:"created_at"`
Kind int `json:"kind"`
Tags Tags `json:"tags"`
Content string `json:"content"`
Sig string `json:"sig"`
}
// Serialize returns the canonical JSON serialization of the event for ID computation.
// Format: [0, "pubkey", created_at, kind, tags, "content"]
func (e *Event) Serialize() []byte {
// Use json.Marshal for proper escaping of content and tags
arr := []interface{}{
0,
e.PubKey,
e.CreatedAt,
e.Kind,
e.Tags,
e.Content,
}
data, _ := json.Marshal(arr)
return data
}
// ComputeID calculates the SHA256 hash of the serialized event.
// Returns the 64-character hex-encoded ID.
func (e *Event) ComputeID() string {
serialized := e.Serialize()
hash := sha256.Sum256(serialized)
return hex.EncodeToString(hash[:])
}
// SetID computes and sets the event ID.
func (e *Event) SetID() {
e.ID = e.ComputeID()
}
// CheckID verifies that the event ID matches the computed ID.
func (e *Event) CheckID() bool {
return e.ID == e.ComputeID()
}
// MarshalJSON implements json.Marshaler with empty tags as [] instead of null.
func (e Event) MarshalJSON() ([]byte, error) {
type eventAlias Event
ea := eventAlias(e)
if ea.Tags == nil {
ea.Tags = Tags{}
}
return json.Marshal(ea)
}
// String returns a JSON representation of the event for debugging.
func (e *Event) String() string {
data, err := json.MarshalIndent(e, "", " ")
if err != nil {
return fmt.Sprintf("<Event error: %v>", err)
}
return string(data)
}
|