1 package envelopes
2 3 // Identify handles determining what kind of codec.Envelope is, by the Label,
4 // the first step in identifying the structure of the message. This first step
5 // is not sufficient because the same labels are used on several codec.Envelope
6 // types in the nostr specification. The rest of the context is in whether this
7 // is a client or a relay receiving it.
8 func Identify(b []byte) (t string, rem []byte, err error) {
9 var openBrackets, openQuotes, afterQuotes bool
10 var label []byte
11 rem = b
12 for ; len(rem) > 0; rem = rem[1:] {
13 if !openBrackets && rem[0] == '[' {
14 openBrackets = true
15 } else if openBrackets {
16 if !openQuotes && rem[0] == '"' {
17 openQuotes = true
18 } else if afterQuotes {
19 // return the remainder after the comma
20 if rem[0] == ',' {
21 rem = rem[1:]
22 return
23 }
24 } else if openQuotes {
25 for i := range rem {
26 if rem[i] == '"' {
27 label = rem[:i]
28 rem = rem[i:]
29 t = string(label)
30 afterQuotes = true
31 break
32 }
33 }
34 }
35 }
36 }
37 return
38 }
39