78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
// Package envelope provides types and functions for working with Nostr protocol
|
|
// websocket messages. It defines the Envelope type representing a Nostr message
|
|
// and offers utilities for creating, parsing, and validating standardized message
|
|
// formats.
|
|
package envelope
|
|
|
|
// Envelope represents a Nostr websocket message.
|
|
type Envelope []byte
|
|
|
|
// GetLabel extracts the message label from an envelope.
|
|
// Returns the label as a byte slice or an error if the envelope is malformed.
|
|
func GetLabel(env Envelope) ([]byte, error) {
|
|
// begin walking byte slice
|
|
i, n := 0, len(env)
|
|
|
|
// skip whitespace before '['
|
|
for i < n && isSpace(env[i]) {
|
|
i++
|
|
}
|
|
|
|
// expect '['
|
|
if i >= n || env[i] != '[' {
|
|
return nil, ErrInvalidEnvelope
|
|
}
|
|
i++
|
|
|
|
// skip whitespace before '"'
|
|
for i < n && isSpace(env[i]) {
|
|
i++
|
|
}
|
|
|
|
// expect '"'
|
|
if i >= n || env[i] != '"' {
|
|
return nil, ErrInvalidEnvelope
|
|
}
|
|
i++
|
|
|
|
// scan label: [A-Z]+ terminated by '"'
|
|
start := i
|
|
for i < n && env[i] != '"' { // do until end or '"' is reached
|
|
if env[i] < 'A' || env[i] > 'Z' { // character is not [A-Z]
|
|
return nil, ErrInvalidEnvelope
|
|
}
|
|
i++
|
|
}
|
|
|
|
if i == n || i == start { // a label was not found in the previous loop
|
|
return nil, ErrInvalidEnvelope
|
|
}
|
|
|
|
return env[start:i], nil
|
|
}
|
|
|
|
func isSpace(b byte) bool {
|
|
return b == ' ' || b == '\t' || b == '\n' || b == '\r'
|
|
}
|
|
|
|
// GetStandardLabels returns a set of standard Nostr websocket message labels
|
|
func GetStandardLabels() map[string]struct{} {
|
|
return map[string]struct{}{
|
|
"EVENT": {},
|
|
"REQ": {},
|
|
"CLOSE": {},
|
|
"CLOSED": {},
|
|
"EOSE": {},
|
|
"NOTICE": {},
|
|
"OK": {},
|
|
"AUTH": {},
|
|
}
|
|
}
|
|
|
|
// IsStandardLabel checks if the given label is a standard Nostr websocket message label
|
|
func IsStandardLabel(label string) bool {
|
|
labels := GetStandardLabels()
|
|
_, ok := labels[label]
|
|
return ok
|
|
}
|