Merge pull request #43 from barkyq/event_MarshalJSON

This commit is contained in:
fiatjaf 2023-01-20 16:27:16 -03:00 committed by GitHub
commit 3d58f81ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 99 deletions

View File

@ -4,10 +4,9 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"time"
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/btcsuite/btcd/btcec/v2/schnorr"
"time"
) )
type Event struct { type Event struct {
@ -45,79 +44,32 @@ func (evt *Event) GetID() string {
return hex.EncodeToString(h[:]) return hex.EncodeToString(h[:])
} }
// Escaping strings for JSON encoding according to RFC4627.
// Also encloses result in quotation marks "".
func quoteEscapeString(dst []byte, s string) []byte {
dst = append(dst, '"')
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case c == '"':
// quotation mark
dst = append(dst, []byte{'\\', '"'}...)
case c == '\\':
// reverse solidus
dst = append(dst, []byte{'\\', '\\'}...)
case c >= 0x20:
// default, rest below are control chars
dst = append(dst, c)
case c < 0x09:
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', '0' + c}...)
case c == 0x09:
dst = append(dst, []byte{'\\', 't'}...)
case c == 0x0a:
dst = append(dst, []byte{'\\', 'n'}...)
case c == 0x0d:
dst = append(dst, []byte{'\\', 'r'}...)
case c < 0x10:
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', 0x57 + c}...)
case c < 0x1a:
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x20 + c}...)
case c < 0x20:
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x47 + c}...)
}
}
dst = append(dst, '"')
return dst
}
// Serialize outputs a byte array that can be hashed/signed to identify/authenticate. // Serialize outputs a byte array that can be hashed/signed to identify/authenticate.
// JSON encoding as defined in RFC4627. // JSON encoding as defined in RFC4627.
func (evt *Event) Serialize() []byte { func (evt *Event) Serialize() []byte {
// the serialization process is just putting everything into a JSON array // the serialization process is just putting everything into a JSON array
// so the order is kept. See NIP-01 // so the order is kept. See NIP-01
ser := make([]byte, 0) dst := make([]byte, 0)
// the header portion is easy to serialize // the header portion is easy to serialize
// [0,"pubkey",created_at,kind,[ // [0,"pubkey",created_at,kind,[
ser = append(ser, []byte( dst = append(dst, []byte(
fmt.Sprintf( fmt.Sprintf(
"[0,\"%s\",%d,%d,[", "[0,\"%s\",%d,%d,",
evt.PubKey, evt.PubKey,
evt.CreatedAt.Unix(), evt.CreatedAt.Unix(),
evt.Kind, evt.Kind,
))...) ))...)
// tags need to be escaped in general.
for i, tag := range evt.Tags { // tags
if i > 0 { dst = evt.Tags.marshalTo(dst)
ser = append(ser, ',') dst = append(dst, ',')
}
ser = append(ser, '[')
for i, s := range tag {
if i > 0 {
ser = append(ser, ',')
}
ser = quoteEscapeString(ser, s)
}
ser = append(ser, ']')
}
ser = append(ser, []byte{']', ','}...)
// content needs to be escaped in general as it is user generated. // content needs to be escaped in general as it is user generated.
ser = quoteEscapeString(ser, evt.Content) dst = escapeString(dst, evt.Content)
ser = append(ser, ']') dst = append(dst, ']')
return ser return dst
} }
// CheckSignature checks if the signature is valid for the id // CheckSignature checks if the signature is valid for the id

View File

@ -1,11 +1,11 @@
package nostr package nostr
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
"github.com/valyala/fastjson" "github.com/valyala/fastjson"
"time"
) )
func (evt *Event) UnmarshalJSON(payload []byte) error { func (evt *Event) UnmarshalJSON(payload []byte) error {
@ -74,35 +74,10 @@ func (evt *Event) UnmarshalJSON(payload []byte) error {
evt.extra[key] = anyValue evt.extra[key] = anyValue
} }
}) })
if visiterr != nil { return visiterr
return visiterr
}
return nil
}
func (evt Event) MarshalJSON() ([]byte, error) {
var arena fastjson.Arena
o := arena.NewObject()
o.Set("id", arena.NewString(evt.ID))
o.Set("pubkey", arena.NewString(evt.PubKey))
o.Set("created_at", arena.NewNumberInt(int(evt.CreatedAt.Unix())))
o.Set("kind", arena.NewNumberInt(evt.Kind))
o.Set("tags", tagsToFastjsonArray(&arena, evt.Tags))
o.Set("content", arena.NewString(evt.Content))
o.Set("sig", arena.NewString(evt.Sig))
for k, v := range evt.extra {
b, _ := json.Marshal(v)
if val, err := fastjson.ParseBytes(b); err == nil {
o.Set(k, val)
}
}
return o.MarshalTo(nil), nil
} }
// unmarshaling helper
func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) { func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) {
arr, err := v.Array() arr, err := v.Array()
if err != nil { if err != nil {
@ -130,14 +105,37 @@ func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) {
return sll, nil return sll, nil
} }
func tagsToFastjsonArray(arena *fastjson.Arena, tags Tags) *fastjson.Value { // MarshalJSON() returns the JSON byte encoding of the event, as in NIP-01.
jtags := arena.NewArray() func (evt Event) MarshalJSON() ([]byte, error) {
for i, v := range tags { dst := make([]byte, 0)
arr := arena.NewArray() dst = append(dst, '{')
for j, subv := range v { dst = append(dst, []byte(fmt.Sprintf("\"id\":\"%s\",\"pubkey\":\"%s\",\"created_at\":%d,\"kind\":%d,\"tags\":",
arr.SetArrayItem(j, arena.NewString(subv)) evt.ID,
evt.PubKey,
evt.CreatedAt.Unix(),
evt.Kind,
))...)
dst = evt.Tags.marshalTo(dst)
dst = append(dst, []byte(",\"content\":")...)
dst = escapeString(dst, evt.Content)
dst = append(dst, []byte(fmt.Sprintf(",\"sig\":\"%s\"",
evt.Sig,
))...)
// slower marshaling of "any" interface type
if evt.extra != nil {
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
for k, v := range evt.extra {
if e := enc.Encode(v); e == nil {
dst = append(dst, ',')
dst = escapeString(dst, k)
dst = append(dst, ':')
dst = append(dst, buf.Bytes()[:buf.Len()-1]...)
}
buf.Reset()
} }
jtags.SetArrayItem(i, arr)
} }
return jtags dst = append(dst, '}')
return dst, nil
} }

View File

@ -35,3 +35,43 @@ func ContainsPrefixOf(haystack []string, needle string) bool {
} }
return false return false
} }
// Escaping strings for JSON encoding according to RFC8259.
// Also encloses result in quotation marks "".
func escapeString(dst []byte, s string) []byte {
dst = append(dst, '"')
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case c == '"':
// quotation mark
dst = append(dst, []byte{'\\', '"'}...)
case c == '\\':
// reverse solidus
dst = append(dst, []byte{'\\', '\\'}...)
case c >= 0x20:
// default, rest below are control chars
dst = append(dst, c)
case c == 0x08:
dst = append(dst, []byte{'\\', 'b'}...)
case c < 0x09:
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', '0' + c}...)
case c == 0x09:
dst = append(dst, []byte{'\\', 't'}...)
case c == 0x0a:
dst = append(dst, []byte{'\\', 'n'}...)
case c == 0x0c:
dst = append(dst, []byte{'\\', 'f'}...)
case c == 0x0d:
dst = append(dst, []byte{'\\', 'r'}...)
case c < 0x10:
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', 0x57 + c}...)
case c < 0x1a:
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x20 + c}...)
case c < 0x20:
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x47 + c}...)
}
}
dst = append(dst, '"')
return dst
}

33
tags.go
View File

@ -12,11 +12,11 @@ type Tag []string
// StartsWith checks if a tag contains a prefix. // StartsWith checks if a tag contains a prefix.
// for example, // for example,
// ["p", "abcdef...", "wss://relay.com"] // ["p", "abcdef...", "wss://relay.com"]
// would match against // would match against
// ["p", "abcdef..."] // ["p", "abcdef..."]
// or even // or even
// ["p", "abcdef...", "wss://"] // ["p", "abcdef...", "wss://"]
func (tag Tag) StartsWith(prefix []string) bool { func (tag Tag) StartsWith(prefix []string) bool {
prefixLen := len(prefix) prefixLen := len(prefix)
@ -141,3 +141,30 @@ func (tags Tags) ContainsAny(tagName string, values []string) bool {
return false return false
} }
// Marshal Tag. Used for Serialization so string escaping should be as in RFC8259.
func (tag Tag) marshalTo(dst []byte) []byte {
dst = append(dst, '[')
for i, s := range tag {
if i > 0 {
dst = append(dst, ',')
}
dst = escapeString(dst, s)
}
dst = append(dst, ']')
return dst
}
// MarshalTo appends the JSON encoded byte of Tags as [][]string to dst.
// String escaping is as described in RFC8259.
func (tags Tags) marshalTo(dst []byte) []byte {
dst = append(dst, '[')
for i, tag := range tags {
if i > 0 {
dst = append(dst, ',')
}
dst = tag.marshalTo(dst)
}
dst = append(dst, ']')
return dst
}