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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
package nostr
import (
"encoding/json"
"fmt"
)
// Envelope represents a Nostr protocol message.
type Envelope interface {
Label() string
MarshalJSON() ([]byte, error)
}
// EventEnvelope wraps an event for the EVENT message.
// Used both client→relay (publish) and relay→client (subscription).
type EventEnvelope struct {
SubscriptionID string // Only for relay→client messages
Event *Event
}
func (e EventEnvelope) Label() string { return "EVENT" }
func (e EventEnvelope) MarshalJSON() ([]byte, error) {
if e.SubscriptionID != "" {
return json.Marshal([]interface{}{"EVENT", e.SubscriptionID, e.Event})
}
return json.Marshal([]interface{}{"EVENT", e.Event})
}
// ReqEnvelope represents a REQ message (client→relay).
type ReqEnvelope struct {
SubscriptionID string
Filters []Filter
}
func (e ReqEnvelope) Label() string { return "REQ" }
func (e ReqEnvelope) MarshalJSON() ([]byte, error) {
arr := make([]interface{}, 2+len(e.Filters))
arr[0] = "REQ"
arr[1] = e.SubscriptionID
for i, f := range e.Filters {
arr[2+i] = f
}
return json.Marshal(arr)
}
// CloseEnvelope represents a CLOSE message (client→relay).
type CloseEnvelope struct {
SubscriptionID string
}
func (e CloseEnvelope) Label() string { return "CLOSE" }
func (e CloseEnvelope) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{"CLOSE", e.SubscriptionID})
}
// OKEnvelope represents an OK message (relay→client).
type OKEnvelope struct {
EventID string
OK bool
Message string
}
func (e OKEnvelope) Label() string { return "OK" }
func (e OKEnvelope) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{"OK", e.EventID, e.OK, e.Message})
}
// EOSEEnvelope represents an EOSE (End of Stored Events) message (relay→client).
type EOSEEnvelope struct {
SubscriptionID string
}
func (e EOSEEnvelope) Label() string { return "EOSE" }
func (e EOSEEnvelope) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{"EOSE", e.SubscriptionID})
}
// ClosedEnvelope represents a CLOSED message (relay→client).
type ClosedEnvelope struct {
SubscriptionID string
Message string
}
func (e ClosedEnvelope) Label() string { return "CLOSED" }
func (e ClosedEnvelope) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{"CLOSED", e.SubscriptionID, e.Message})
}
// NoticeEnvelope represents a NOTICE message (relay→client).
type NoticeEnvelope struct {
Message string
}
func (e NoticeEnvelope) Label() string { return "NOTICE" }
func (e NoticeEnvelope) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{"NOTICE", e.Message})
}
// ParseEnvelope parses a raw JSON message into the appropriate envelope type.
func ParseEnvelope(data []byte) (Envelope, error) {
var arr []json.RawMessage
if err := json.Unmarshal(data, &arr); err != nil {
return nil, fmt.Errorf("invalid envelope: %w", err)
}
if len(arr) < 2 {
return nil, fmt.Errorf("envelope too short")
}
var label string
if err := json.Unmarshal(arr[0], &label); err != nil {
return nil, fmt.Errorf("invalid envelope label: %w", err)
}
switch label {
case "EVENT":
return parseEventEnvelope(arr)
case "REQ":
return parseReqEnvelope(arr)
case "CLOSE":
return parseCloseEnvelope(arr)
case "OK":
return parseOKEnvelope(arr)
case "EOSE":
return parseEOSEEnvelope(arr)
case "CLOSED":
return parseClosedEnvelope(arr)
case "NOTICE":
return parseNoticeEnvelope(arr)
default:
return nil, fmt.Errorf("unknown envelope type: %s", label)
}
}
func parseEventEnvelope(arr []json.RawMessage) (*EventEnvelope, error) {
env := &EventEnvelope{}
if len(arr) == 2 {
// Client→relay: ["EVENT", event]
var event Event
if err := json.Unmarshal(arr[1], &event); err != nil {
return nil, fmt.Errorf("invalid event: %w", err)
}
env.Event = &event
} else if len(arr) == 3 {
// Relay→client: ["EVENT", subscription_id, event]
if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil {
return nil, fmt.Errorf("invalid subscription ID: %w", err)
}
var event Event
if err := json.Unmarshal(arr[2], &event); err != nil {
return nil, fmt.Errorf("invalid event: %w", err)
}
env.Event = &event
} else {
return nil, fmt.Errorf("invalid EVENT envelope length: %d", len(arr))
}
return env, nil
}
func parseReqEnvelope(arr []json.RawMessage) (*ReqEnvelope, error) {
if len(arr) < 3 {
return nil, fmt.Errorf("REQ envelope must have at least 3 elements")
}
env := &ReqEnvelope{}
if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil {
return nil, fmt.Errorf("invalid subscription ID: %w", err)
}
for i := 2; i < len(arr); i++ {
var filter Filter
if err := json.Unmarshal(arr[i], &filter); err != nil {
return nil, fmt.Errorf("invalid filter at index %d: %w", i-2, err)
}
env.Filters = append(env.Filters, filter)
}
return env, nil
}
func parseCloseEnvelope(arr []json.RawMessage) (*CloseEnvelope, error) {
if len(arr) != 2 {
return nil, fmt.Errorf("CLOSE envelope must have exactly 2 elements")
}
env := &CloseEnvelope{}
if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil {
return nil, fmt.Errorf("invalid subscription ID: %w", err)
}
return env, nil
}
func parseOKEnvelope(arr []json.RawMessage) (*OKEnvelope, error) {
if len(arr) != 4 {
return nil, fmt.Errorf("OK envelope must have exactly 4 elements")
}
env := &OKEnvelope{}
if err := json.Unmarshal(arr[1], &env.EventID); err != nil {
return nil, fmt.Errorf("invalid event ID: %w", err)
}
if err := json.Unmarshal(arr[2], &env.OK); err != nil {
return nil, fmt.Errorf("invalid OK status: %w", err)
}
if err := json.Unmarshal(arr[3], &env.Message); err != nil {
return nil, fmt.Errorf("invalid message: %w", err)
}
return env, nil
}
func parseEOSEEnvelope(arr []json.RawMessage) (*EOSEEnvelope, error) {
if len(arr) != 2 {
return nil, fmt.Errorf("EOSE envelope must have exactly 2 elements")
}
env := &EOSEEnvelope{}
if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil {
return nil, fmt.Errorf("invalid subscription ID: %w", err)
}
return env, nil
}
func parseClosedEnvelope(arr []json.RawMessage) (*ClosedEnvelope, error) {
if len(arr) != 3 {
return nil, fmt.Errorf("CLOSED envelope must have exactly 3 elements")
}
env := &ClosedEnvelope{}
if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil {
return nil, fmt.Errorf("invalid subscription ID: %w", err)
}
if err := json.Unmarshal(arr[2], &env.Message); err != nil {
return nil, fmt.Errorf("invalid message: %w", err)
}
return env, nil
}
func parseNoticeEnvelope(arr []json.RawMessage) (*NoticeEnvelope, error) {
if len(arr) != 2 {
return nil, fmt.Errorf("NOTICE envelope must have exactly 2 elements")
}
env := &NoticeEnvelope{}
if err := json.Unmarshal(arr[1], &env.Message); err != nil {
return nil, fmt.Errorf("invalid message: %w", err)
}
return env, nil
}
|