// Package envelope provides marshal/unmarshal for all nostr protocol envelope types. package envelope import ( "io" ) // Identify extracts the label from a nostr envelope JSON array. func Identify(b []byte) (t string, rem []byte, err error) { var openBrackets, openQuotes, afterQuotes bool var label []byte rem = b for ; len(rem) > 0; rem = rem[1:] { if !openBrackets && rem[0] == '[' { openBrackets = true } else if openBrackets { if !openQuotes && rem[0] == '"' { openQuotes = true } else if afterQuotes { if rem[0] == ',' { rem = rem[1:] return } } else if openQuotes { for i := range rem { if rem[i] == '"' { label = rem[:i] rem = rem[i:] t = string(label) afterQuotes = true break } } } } } return } // Marshaller is a function signature for marshalling envelope content. type Marshaller func(dst []byte) (b []byte) // Marshal wraps content in a JSON array envelope with the given label. func Marshal(dst []byte, label string, m Marshaller) (b []byte) { b = dst b = append(b, '[', '"') b = append(b, label...) b = append(b, '"', ',') b = m(b) b = append(b, ']') return } // SkipToTheEnd scans forward to the closing bracket of an envelope. func SkipToTheEnd(dst []byte) (rem []byte, err error) { if len(dst) == 0 { return } rem = dst for ; len(rem) > 0; rem = rem[1:] { if rem[0] == ']' { rem = rem[:0] return } } err = io.EOF return }