Skip to content

Commit

Permalink
binary: fix encoding, was using uint8 for tags length, which was obvi…
Browse files Browse the repository at this point in the history
…ously not working.
  • Loading branch information
fiatjaf committed Dec 11, 2023
1 parent 7ecbc0a commit c55b509
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 8 additions & 3 deletions binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions binary/binary_test.go

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions binary/hybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit c55b509

Please sign in to comment.