diff options
Diffstat (limited to 'envelope.go')
| -rw-r--r-- | envelope.go | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/envelope.go b/envelope.go new file mode 100644 index 0000000..d395efa --- /dev/null +++ b/envelope.go | |||
| @@ -0,0 +1,262 @@ | |||
| 1 | package nostr | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "fmt" | ||
| 6 | ) | ||
| 7 | |||
| 8 | // Envelope represents a Nostr protocol message. | ||
| 9 | type Envelope interface { | ||
| 10 | Label() string | ||
| 11 | MarshalJSON() ([]byte, error) | ||
| 12 | } | ||
| 13 | |||
| 14 | // EventEnvelope wraps an event for the EVENT message. | ||
| 15 | // Used both client→relay (publish) and relay→client (subscription). | ||
| 16 | type EventEnvelope struct { | ||
| 17 | SubscriptionID string // Only for relay→client messages | ||
| 18 | Event *Event | ||
| 19 | } | ||
| 20 | |||
| 21 | func (e EventEnvelope) Label() string { return "EVENT" } | ||
| 22 | |||
| 23 | func (e EventEnvelope) MarshalJSON() ([]byte, error) { | ||
| 24 | if e.SubscriptionID != "" { | ||
| 25 | return json.Marshal([]interface{}{"EVENT", e.SubscriptionID, e.Event}) | ||
| 26 | } | ||
| 27 | return json.Marshal([]interface{}{"EVENT", e.Event}) | ||
| 28 | } | ||
| 29 | |||
| 30 | // ReqEnvelope represents a REQ message (client→relay). | ||
| 31 | type ReqEnvelope struct { | ||
| 32 | SubscriptionID string | ||
| 33 | Filters []Filter | ||
| 34 | } | ||
| 35 | |||
| 36 | func (e ReqEnvelope) Label() string { return "REQ" } | ||
| 37 | |||
| 38 | func (e ReqEnvelope) MarshalJSON() ([]byte, error) { | ||
| 39 | arr := make([]interface{}, 2+len(e.Filters)) | ||
| 40 | arr[0] = "REQ" | ||
| 41 | arr[1] = e.SubscriptionID | ||
| 42 | for i, f := range e.Filters { | ||
| 43 | arr[2+i] = f | ||
| 44 | } | ||
| 45 | return json.Marshal(arr) | ||
| 46 | } | ||
| 47 | |||
| 48 | // CloseEnvelope represents a CLOSE message (client→relay). | ||
| 49 | type CloseEnvelope struct { | ||
| 50 | SubscriptionID string | ||
| 51 | } | ||
| 52 | |||
| 53 | func (e CloseEnvelope) Label() string { return "CLOSE" } | ||
| 54 | |||
| 55 | func (e CloseEnvelope) MarshalJSON() ([]byte, error) { | ||
| 56 | return json.Marshal([]interface{}{"CLOSE", e.SubscriptionID}) | ||
| 57 | } | ||
| 58 | |||
| 59 | // OKEnvelope represents an OK message (relay→client). | ||
| 60 | type OKEnvelope struct { | ||
| 61 | EventID string | ||
| 62 | OK bool | ||
| 63 | Message string | ||
| 64 | } | ||
| 65 | |||
| 66 | func (e OKEnvelope) Label() string { return "OK" } | ||
| 67 | |||
| 68 | func (e OKEnvelope) MarshalJSON() ([]byte, error) { | ||
| 69 | return json.Marshal([]interface{}{"OK", e.EventID, e.OK, e.Message}) | ||
| 70 | } | ||
| 71 | |||
| 72 | // EOSEEnvelope represents an EOSE (End of Stored Events) message (relay→client). | ||
| 73 | type EOSEEnvelope struct { | ||
| 74 | SubscriptionID string | ||
| 75 | } | ||
| 76 | |||
| 77 | func (e EOSEEnvelope) Label() string { return "EOSE" } | ||
| 78 | |||
| 79 | func (e EOSEEnvelope) MarshalJSON() ([]byte, error) { | ||
| 80 | return json.Marshal([]interface{}{"EOSE", e.SubscriptionID}) | ||
| 81 | } | ||
| 82 | |||
| 83 | // ClosedEnvelope represents a CLOSED message (relay→client). | ||
| 84 | type ClosedEnvelope struct { | ||
| 85 | SubscriptionID string | ||
| 86 | Message string | ||
| 87 | } | ||
| 88 | |||
| 89 | func (e ClosedEnvelope) Label() string { return "CLOSED" } | ||
| 90 | |||
| 91 | func (e ClosedEnvelope) MarshalJSON() ([]byte, error) { | ||
| 92 | return json.Marshal([]interface{}{"CLOSED", e.SubscriptionID, e.Message}) | ||
| 93 | } | ||
| 94 | |||
| 95 | // NoticeEnvelope represents a NOTICE message (relay→client). | ||
| 96 | type NoticeEnvelope struct { | ||
| 97 | Message string | ||
| 98 | } | ||
| 99 | |||
| 100 | func (e NoticeEnvelope) Label() string { return "NOTICE" } | ||
| 101 | |||
| 102 | func (e NoticeEnvelope) MarshalJSON() ([]byte, error) { | ||
| 103 | return json.Marshal([]interface{}{"NOTICE", e.Message}) | ||
| 104 | } | ||
| 105 | |||
| 106 | // ParseEnvelope parses a raw JSON message into the appropriate envelope type. | ||
| 107 | func ParseEnvelope(data []byte) (Envelope, error) { | ||
| 108 | var arr []json.RawMessage | ||
| 109 | if err := json.Unmarshal(data, &arr); err != nil { | ||
| 110 | return nil, fmt.Errorf("invalid envelope: %w", err) | ||
| 111 | } | ||
| 112 | |||
| 113 | if len(arr) < 2 { | ||
| 114 | return nil, fmt.Errorf("envelope too short") | ||
| 115 | } | ||
| 116 | |||
| 117 | var label string | ||
| 118 | if err := json.Unmarshal(arr[0], &label); err != nil { | ||
| 119 | return nil, fmt.Errorf("invalid envelope label: %w", err) | ||
| 120 | } | ||
| 121 | |||
| 122 | switch label { | ||
| 123 | case "EVENT": | ||
| 124 | return parseEventEnvelope(arr) | ||
| 125 | case "REQ": | ||
| 126 | return parseReqEnvelope(arr) | ||
| 127 | case "CLOSE": | ||
| 128 | return parseCloseEnvelope(arr) | ||
| 129 | case "OK": | ||
| 130 | return parseOKEnvelope(arr) | ||
| 131 | case "EOSE": | ||
| 132 | return parseEOSEEnvelope(arr) | ||
| 133 | case "CLOSED": | ||
| 134 | return parseClosedEnvelope(arr) | ||
| 135 | case "NOTICE": | ||
| 136 | return parseNoticeEnvelope(arr) | ||
| 137 | default: | ||
| 138 | return nil, fmt.Errorf("unknown envelope type: %s", label) | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | func parseEventEnvelope(arr []json.RawMessage) (*EventEnvelope, error) { | ||
| 143 | env := &EventEnvelope{} | ||
| 144 | |||
| 145 | if len(arr) == 2 { | ||
| 146 | // Client→relay: ["EVENT", event] | ||
| 147 | var event Event | ||
| 148 | if err := json.Unmarshal(arr[1], &event); err != nil { | ||
| 149 | return nil, fmt.Errorf("invalid event: %w", err) | ||
| 150 | } | ||
| 151 | env.Event = &event | ||
| 152 | } else if len(arr) == 3 { | ||
| 153 | // Relay→client: ["EVENT", subscription_id, event] | ||
| 154 | if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil { | ||
| 155 | return nil, fmt.Errorf("invalid subscription ID: %w", err) | ||
| 156 | } | ||
| 157 | var event Event | ||
| 158 | if err := json.Unmarshal(arr[2], &event); err != nil { | ||
| 159 | return nil, fmt.Errorf("invalid event: %w", err) | ||
| 160 | } | ||
| 161 | env.Event = &event | ||
| 162 | } else { | ||
| 163 | return nil, fmt.Errorf("invalid EVENT envelope length: %d", len(arr)) | ||
| 164 | } | ||
| 165 | |||
| 166 | return env, nil | ||
| 167 | } | ||
| 168 | |||
| 169 | func parseReqEnvelope(arr []json.RawMessage) (*ReqEnvelope, error) { | ||
| 170 | if len(arr) < 3 { | ||
| 171 | return nil, fmt.Errorf("REQ envelope must have at least 3 elements") | ||
| 172 | } | ||
| 173 | |||
| 174 | env := &ReqEnvelope{} | ||
| 175 | if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil { | ||
| 176 | return nil, fmt.Errorf("invalid subscription ID: %w", err) | ||
| 177 | } | ||
| 178 | |||
| 179 | for i := 2; i < len(arr); i++ { | ||
| 180 | var filter Filter | ||
| 181 | if err := json.Unmarshal(arr[i], &filter); err != nil { | ||
| 182 | return nil, fmt.Errorf("invalid filter at index %d: %w", i-2, err) | ||
| 183 | } | ||
| 184 | env.Filters = append(env.Filters, filter) | ||
| 185 | } | ||
| 186 | |||
| 187 | return env, nil | ||
| 188 | } | ||
| 189 | |||
| 190 | func parseCloseEnvelope(arr []json.RawMessage) (*CloseEnvelope, error) { | ||
| 191 | if len(arr) != 2 { | ||
| 192 | return nil, fmt.Errorf("CLOSE envelope must have exactly 2 elements") | ||
| 193 | } | ||
| 194 | |||
| 195 | env := &CloseEnvelope{} | ||
| 196 | if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil { | ||
| 197 | return nil, fmt.Errorf("invalid subscription ID: %w", err) | ||
| 198 | } | ||
| 199 | |||
| 200 | return env, nil | ||
| 201 | } | ||
| 202 | |||
| 203 | func parseOKEnvelope(arr []json.RawMessage) (*OKEnvelope, error) { | ||
| 204 | if len(arr) != 4 { | ||
| 205 | return nil, fmt.Errorf("OK envelope must have exactly 4 elements") | ||
| 206 | } | ||
| 207 | |||
| 208 | env := &OKEnvelope{} | ||
| 209 | if err := json.Unmarshal(arr[1], &env.EventID); err != nil { | ||
| 210 | return nil, fmt.Errorf("invalid event ID: %w", err) | ||
| 211 | } | ||
| 212 | if err := json.Unmarshal(arr[2], &env.OK); err != nil { | ||
| 213 | return nil, fmt.Errorf("invalid OK status: %w", err) | ||
| 214 | } | ||
| 215 | if err := json.Unmarshal(arr[3], &env.Message); err != nil { | ||
| 216 | return nil, fmt.Errorf("invalid message: %w", err) | ||
| 217 | } | ||
| 218 | |||
| 219 | return env, nil | ||
| 220 | } | ||
| 221 | |||
| 222 | func parseEOSEEnvelope(arr []json.RawMessage) (*EOSEEnvelope, error) { | ||
| 223 | if len(arr) != 2 { | ||
| 224 | return nil, fmt.Errorf("EOSE envelope must have exactly 2 elements") | ||
| 225 | } | ||
| 226 | |||
| 227 | env := &EOSEEnvelope{} | ||
| 228 | if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil { | ||
| 229 | return nil, fmt.Errorf("invalid subscription ID: %w", err) | ||
| 230 | } | ||
| 231 | |||
| 232 | return env, nil | ||
| 233 | } | ||
| 234 | |||
| 235 | func parseClosedEnvelope(arr []json.RawMessage) (*ClosedEnvelope, error) { | ||
| 236 | if len(arr) != 3 { | ||
| 237 | return nil, fmt.Errorf("CLOSED envelope must have exactly 3 elements") | ||
| 238 | } | ||
| 239 | |||
| 240 | env := &ClosedEnvelope{} | ||
| 241 | if err := json.Unmarshal(arr[1], &env.SubscriptionID); err != nil { | ||
| 242 | return nil, fmt.Errorf("invalid subscription ID: %w", err) | ||
| 243 | } | ||
| 244 | if err := json.Unmarshal(arr[2], &env.Message); err != nil { | ||
| 245 | return nil, fmt.Errorf("invalid message: %w", err) | ||
| 246 | } | ||
| 247 | |||
| 248 | return env, nil | ||
| 249 | } | ||
| 250 | |||
| 251 | func parseNoticeEnvelope(arr []json.RawMessage) (*NoticeEnvelope, error) { | ||
| 252 | if len(arr) != 2 { | ||
| 253 | return nil, fmt.Errorf("NOTICE envelope must have exactly 2 elements") | ||
| 254 | } | ||
| 255 | |||
| 256 | env := &NoticeEnvelope{} | ||
| 257 | if err := json.Unmarshal(arr[1], &env.Message); err != nil { | ||
| 258 | return nil, fmt.Errorf("invalid message: %w", err) | ||
| 259 | } | ||
| 260 | |||
| 261 | return env, nil | ||
| 262 | } | ||
