binary: fix encoding, was using uint8 for tags length, which was obviously not working.

This commit is contained in:
fiatjaf 2023-12-10 21:13:34 -03:00
parent 7ecbc0a7cf
commit c55b509653
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 19 additions and 7 deletions

View File

@ -23,8 +23,10 @@ func UnmarshalBinary(data []byte, evt *Event) (err error) {
evt.Content = string(data[136 : 136+contentLength])
curr := 136 + contentLength
ntags := int(data[curr])
evt.Tags = make(nostr.Tags, ntags)
nTags := binary.BigEndian.Uint16(data[curr : curr+2])
curr++
evt.Tags = make(nostr.Tags, nTags)
for t := range evt.Tags {
curr = curr + 1
@ -57,7 +59,10 @@ func MarshalBinary(evt *Event) []byte {
copy(buf[136:], content)
curr := 136 + len(content)
buf[curr] = uint8(len(evt.Tags))
binary.BigEndian.PutUint16(buf[curr:curr+2], uint16(len(evt.Tags)))
curr++
for _, tag := range evt.Tags {
curr++
buf[curr] = uint8(len(tag))

File diff suppressed because one or more lines are too long

View File

@ -24,11 +24,13 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) {
evt.Content = string(data[136 : 136+contentLength])
curr := 136 + contentLength
ntags := int(data[curr])
evt.Tags = make(nostr.Tags, ntags)
nTags := binary.BigEndian.Uint16(data[curr : curr+2])
curr++
evt.Tags = make(nostr.Tags, nTags)
for t := range evt.Tags {
curr = curr + 1
curr++
nItems := int(data[curr])
tag := make(nostr.Tag, nItems)
for i := range tag {
@ -60,7 +62,10 @@ func Marshal(evt *nostr.Event) ([]byte, error) {
copy(buf[136:], content)
curr := 136 + len(content)
buf[curr] = uint8(len(evt.Tags))
binary.BigEndian.PutUint16(buf[curr:curr+2], uint16(len(evt.Tags)))
curr++
for _, tag := range evt.Tags {
curr++
buf[curr] = uint8(len(tag))