/
nson.go
247 lines (208 loc) · 7.41 KB
/
nson.go
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
package nson
import (
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"strings"
"unsafe"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/event"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/eventid"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/kind"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/tag"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/tags"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/timestamp"
)
/*
nson size
kind chars
content chars
number of tags (let's say it's two)
number of items on the first tag (let's say it's three)
number of chars on the first item
number of chars on the second item
number of chars on the third item
number of items on the second tag (let's say it's two)
number of chars on the first item
number of chars on the second item
"nson":"xxkkccccttnn111122223333nn11112222"
*/
const (
IdStart = 7
IdEnd = 7 + 64
PubkeyStart = 83
PubkeyEnd = 83 + 64
SigStart = 156
SigEnd = 156 + 128
CreatedAtStart = 299
CreatedAtEnd = 299 + 10
StringStart = 318 // the actual json string for the "nson" field
ValuesStart = 318 + 2 // skipping the first byte which delimits the nson size
MarkerStart = 309 // this is used just to determine if an event is nson or not
MarkerEnd = 317 // it's just the `,"nson":` (including ,": garbage to reduce false positives) part
)
var ErrNotNSON = fmt.Errorf("not nson")
func UnmarshalBytes(data []byte, evt *event.T) (err error) {
return Unmarshal(unsafe.String(unsafe.SliceData(data), len(data)), evt)
}
// Unmarshal turns a NSON string into a event.T struct.
func Unmarshal(data string, evt *event.T) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("failed to decode nson: %v", r)
}
}()
// check if it's nson
if data[MarkerStart:MarkerEnd] != ",\"nson\":" {
return ErrNotNSON
}
// nson values
nsonSize, nsonDescriptors := parseDescriptors(data)
// static fields
evt.ID = eventid.T(data[IdStart:IdEnd])
evt.PubKey = data[PubkeyStart:PubkeyEnd]
evt.Sig = data[SigStart:SigEnd]
ts, _ := strconv.ParseUint(data[CreatedAtStart:CreatedAtEnd], 10, 64)
evt.CreatedAt = timestamp.T(ts)
// dynamic fields
// kind
kindChars := int(nsonDescriptors[0])
kindStart := ValuesStart + nsonSize + 9 // len(`","kind":`)
ki, _ := strconv.Atoi(data[kindStart : kindStart+kindChars])
evt.Kind = kind.T(ki)
// content
contentChars := int(binary.BigEndian.Uint16(nsonDescriptors[1:3]))
contentStart := kindStart + kindChars + 12 // len(`,"content":"`)
evt.Content, _ = strconv.Unquote(data[contentStart-1 : contentStart+contentChars+1])
// tags
nTags := int(nsonDescriptors[3])
evt.Tags = make(tags.T, nTags)
tagsStart := contentStart + contentChars + 9 // len(`","tags":`)
nsonIndex := 3
tagsIndex := tagsStart
for t := 0; t < nTags; t++ {
nsonIndex++
tagsIndex += 1 // len(`[`) or len(`,`)
nItems := int(nsonDescriptors[nsonIndex])
tg := make(tag.T, nItems)
for n := 0; n < nItems; n++ {
nsonIndex++
itemStart := tagsIndex + 2 // len(`["`) or len(`,"`)
itemChars := int(binary.BigEndian.Uint16(nsonDescriptors[nsonIndex:]))
nsonIndex++
tg[n], _ = strconv.Unquote(data[itemStart-1 : itemStart+itemChars+1])
tagsIndex = itemStart + itemChars + 1 // len(`"`)
}
tagsIndex += 1 // len(`]`)
evt.Tags[t] = tg
}
return err
}
func MarshalBytes(evt *event.T) ([]byte, error) {
v, err := Marshal(evt)
return unsafe.Slice(unsafe.StringData(v), len(v)), err
}
func Marshal(evt *event.T) (string, error) {
// start building the nson descriptors (without the first byte that represents the nson size)
nsonBuf := make([]byte, 256)
// build the tags
nTags := len(evt.Tags)
nsonBuf[3] = uint8(nTags)
nsonIndex := 3 // start here
tagBuilder := strings.Builder{}
tagBuilder.Grow(1000) // a guess
tagBuilder.WriteString(`[`)
for t, tag := range evt.Tags {
nItems := len(tag)
nsonIndex++
nsonBuf[nsonIndex] = uint8(nItems)
tagBuilder.WriteString(`[`)
for i, item := range tag {
v := strconv.Quote(item)
nsonIndex++
binary.BigEndian.PutUint16(nsonBuf[nsonIndex:], uint16(len(v)-2))
nsonIndex++
tagBuilder.WriteString(v)
if nItems > i+1 {
tagBuilder.WriteString(`,`)
}
}
tagBuilder.WriteString(`]`)
if nTags > t+1 {
tagBuilder.WriteString(`,`)
}
}
tagBuilder.WriteString(`]}`)
nsonBuf = nsonBuf[0 : nsonIndex+1]
ki := strconv.Itoa(int(evt.Kind))
kindChars := len(ki)
nsonBuf[0] = uint8(kindChars)
content := strconv.Quote(evt.Content)
contentChars := len(content) - 2
binary.BigEndian.PutUint16(nsonBuf[1:3], uint16(contentChars))
// actually build the json
base := strings.Builder{}
base.Grow(ValuesStart + // everything up to "nson":
2 + len(nsonBuf)*2 + // nson
9 + kindChars + // kind and its label
12 + contentChars + // content and its label
9 + tagBuilder.Len() + // tags and its label
2, // the end
)
base.WriteString(`{"id":"` + evt.ID.String() + `","pubkey":"` + evt.PubKey + `","sig":"` + evt.Sig +
`","created_at":` + strconv.FormatInt(int64(evt.CreatedAt), 10) + `,"nson":"`)
nsonSizeBytes := len(nsonBuf)
if nsonSizeBytes > 255 {
return "", fmt.Errorf("can't encode to nson, there are too many tags or tag items")
}
base.WriteString(hex.EncodeToString([]byte{uint8(nsonSizeBytes)})) // nson size (bytes)
base.WriteString(hex.EncodeToString(nsonBuf)) // nson descriptors
base.WriteString(`","kind":` + ki + `,"content":` + content + `,"tags":`)
base.WriteString(tagBuilder.String() /* includes the end */)
return base.String(), nil
}
func parseDescriptors(data string) (int, []byte) {
nsonSizeBytes, _ := hex.DecodeString(data[StringStart:ValuesStart])
// number of bytes is given, we x2 because the string is in hex
size := int(nsonSizeBytes[0]) * 2
values, _ := hex.DecodeString(data[ValuesStart : ValuesStart+size])
return size, values
}
// An Event is basically a wrapper over the string that makes it easy to get
// each event property (except tags).
type Event struct {
data string
descriptorsSize int
descriptors []byte
}
func New(nsonText string) Event {
return Event{data: nsonText}
}
func (ne *Event) parseDescriptors() {
if ne.descriptors == nil {
ne.descriptorsSize, ne.descriptors = parseDescriptors(ne.data)
}
}
func (ne *Event) GetID() string { return ne.data[IdStart:IdEnd] }
func (ne *Event) GetPubkey() string { return ne.data[PubkeyStart:PubkeyEnd] }
func (ne *Event) GetSig() string { return ne.data[SigStart:SigEnd] }
func (ne *Event) GetCreatedAt() timestamp.T {
ts, _ := strconv.ParseUint(ne.data[CreatedAtStart:CreatedAtEnd], 10, 64)
return timestamp.T(ts)
}
func (ne *Event) GetKind() int {
ne.parseDescriptors()
kindChars := int(ne.descriptors[0])
kindStart := ValuesStart + ne.descriptorsSize + 9 // len(`","kind":`)
kind, _ := strconv.Atoi(ne.data[kindStart : kindStart+kindChars])
return kind
}
func (ne *Event) GetContent() string {
ne.parseDescriptors()
kindChars := int(ne.descriptors[0])
kindStart := ValuesStart + ne.descriptorsSize + 9 // len(`","kind":`)
contentChars := int(binary.BigEndian.Uint16(ne.descriptors[1:3]))
contentStart := kindStart + kindChars + 12 // len(`,"content":"`)
content, _ := strconv.Unquote(`"` + ne.data[contentStart:contentStart+contentChars] + `"`)
return content
}