-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage.go
More file actions
281 lines (202 loc) · 5.44 KB
/
message.go
File metadata and controls
281 lines (202 loc) · 5.44 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package nostr
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
)
// TODO: Split this into DecodeMessageFromRelay and DecodeMessageFromClient.
func DecodeMessage(msg []byte) Message {
// Extract message label (EVENT, REQ, CLOSE, EOSE, NOTICE) from byte slice.
firstComma := bytes.Index(msg, []byte{','})
if firstComma == -1 {
return nil
}
label := msg[0:firstComma]
var v Message
switch {
case bytes.Contains(label, []byte("EVENT")):
v = &MessageEvent{}
case bytes.Contains(label, []byte("REQ")):
v = &MessageReq{}
case bytes.Contains(label, []byte("OK")):
v = &MessageOk{}
case bytes.Contains(label, []byte("EOSE")):
v = &MessageEose{}
default:
log.Fatalln("cannot decode message")
}
if err := v.UnmarshalJSON(msg); err != nil {
return nil
}
return v
}
type MessageType string
type Message interface {
// Return the message type.
Type() MessageType
// Implement json.Unmarshaler interface
UnmarshalJSON([]byte) error
// Implement json.Marshaler interface
MarshalJSON() ([]byte, error)
}
// NIP-20 - ["OK", <event_id>, <true|false>, <message>]
type MessageOk struct {
EventId string
Ok bool
Message string
}
func (s MessageOk) GetEventId() string {
return strings.Trim(s.EventId, "\"")
}
func (s MessageOk) Type() MessageType {
return MessageType("OK")
}
func (s *MessageOk) UnmarshalJSON(data []byte) error {
var tmp []json.RawMessage
err := json.Unmarshal(data, &tmp)
if err != nil {
log.Fatalln("unable to unmarshal result msg from relay")
}
s.EventId = string(tmp[1])
s.Ok = false
if string(tmp[2]) == "true" {
s.Ok = true
}
s.Message = string(tmp[3])
return nil
}
func (s MessageOk) MarshalJSON() ([]byte, error) {
msg := append([]byte(nil), []byte(`["OK",`)...)
if len(s.EventId) != 0 {
msg = append(msg, []byte(s.EventId+`,`)...)
}
if s.Ok {
msg = append(msg, []byte("true")...)
} else {
msg = append(msg, []byte("false")...)
}
msg = append(msg, []byte(s.Message+`]`)...)
return msg, nil
}
// NIP-01 - ["EVENT", <subscription_id>, <event JSON as defined above>]
type MessageEvent struct {
SubscriptionId string
Event
}
// Jesus christ, the Subscription map didnt want to map unless trimmed. Mother fucker. Fuck trimming string always. jesus christ
func (s MessageEvent) GetSubId() string {
return strings.Trim(s.SubscriptionId, "\"")
}
func (s MessageEvent) Type() MessageType {
return MessageType("EVENT")
}
func (s *MessageEvent) UnmarshalJSON(data []byte) error {
var tmp []json.RawMessage
err := json.Unmarshal(data, &tmp)
if err != nil {
log.Fatalln("unable to unmarshal EVENT msg")
}
// Is len(2) then it is an event from client-to-relay
// Otherwise its an event from relay-to-client
switch len(tmp) {
case 2:
return json.Unmarshal(tmp[1], &s.Event)
case 3:
s.SubscriptionId = string(tmp[1])
return json.Unmarshal(tmp[2], &s.Event)
default:
return fmt.Errorf("failed to decode EVENT message")
}
}
func (s MessageEvent) MarshalJSON() ([]byte, error) {
msg := append([]byte(nil), []byte(`["EVENT",`)...)
if len(s.SubscriptionId) != 0 {
msg = append(msg, []byte(s.SubscriptionId+`,`)...)
}
// Marshal the signed event to a slice of bytes ready for transmission.
bytes, err := json.Marshal(s.Event)
if err != nil {
log.Fatalln("unable to marchal incoming event")
}
msg = append(msg, bytes...)
msg = append(msg, []byte(`]`)...)
return msg, nil
}
// NIP-01 - ["REQ", <subscription_id>, <filters JSON>...]
type MessageReq struct {
SubscriptionId string
Filters
}
func (s MessageReq) GetSubId() string {
return strings.Trim(s.SubscriptionId, "\"")
}
func (s MessageReq) Type() MessageType {
return MessageType("REQ")
}
func (s *MessageReq) UnmarshalJSON(data []byte) error {
var tmp []json.RawMessage
err := json.Unmarshal(data, &tmp)
if err != nil {
log.Fatalln("unable to unmarshal REQ msg")
}
s.SubscriptionId = string(tmp[1])
return json.Unmarshal(tmp[2], &s.Filters)
}
func (s MessageReq) MarshalJSON() ([]byte, error) {
msg := []byte(nil)
// Open message array.
msg = append(msg, []byte(`[`)...)
// Add message label
msg = append(msg, []byte(`"REQ",`)...)
// Add subscription ID between string braces.
msg = append(msg, []byte(`"`+s.SubscriptionId+`",`)...)
for i, v := range s.Filters {
// Encode the individual filter.
bytes, err := json.Marshal(v)
if err != nil {
log.Fatal(err)
}
// Add filter data to json list.
msg = append(msg, bytes...)
// Add delimiter to next item, except the last one
if i != len(s.Filters)-1 {
msg = append(msg, []byte(`,`)...)
}
}
// Close the entire message.
msg = append(msg, []byte(`]`)...)
return msg, nil
}
// NIP-01 - ["EOSE", <subscription_id>]
type MessageEose struct {
SubscriptionId string
}
func (s MessageEose) GetSubId() string {
return strings.Trim(s.SubscriptionId, "\"")
}
func (s MessageEose) Type() MessageType {
return MessageType("EOSE")
}
func (s *MessageEose) UnmarshalJSON(data []byte) error {
var tmp []json.RawMessage
err := json.Unmarshal(data, &tmp)
if err != nil {
log.Fatalln("unable to unmarshal REQ msg")
}
s.SubscriptionId = string(tmp[1])
return nil
}
func (s MessageEose) MarshalJSON() ([]byte, error) {
msg := []byte(nil)
// Open message array.
msg = append(msg, []byte(`[`)...)
// Add message label
msg = append(msg, []byte(`"REQ",`)...)
// Add subscription ID between string braces.
msg = append(msg, []byte(`"`+s.SubscriptionId+`"`)...)
// Close the entire message.
msg = append(msg, []byte(`]`)...)
return msg, nil
}