Changing (evt Event) MarshalJSON to avoid string escaping bug

This commit is contained in:
Dylan Cant 2023-01-18 14:50:44 -05:00
parent b9d04f1496
commit afcfa20763
3 changed files with 4 additions and 16 deletions

View File

@ -106,7 +106,7 @@ func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) {
}
// MarshalJSON() returns the JSON byte encoding of the event, as in NIP-01.
func (evt *Event) MarshalJSON() ([]byte, error) {
func (evt Event) MarshalJSON() ([]byte, error) {
dst := make([]byte, 0)
dst = append(dst, '{')
dst = append(dst, []byte(fmt.Sprintf("\"id\":\"%s\",\"pubkey\":\"%s\",\"created_at\":%d,\"kind\":%d,\"tags\":",

View File

@ -229,15 +229,7 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
defer r.okCallbacks.Delete(event.ID)
// publish event
message := []byte("[\"EVENT\",")
if m, e := event.MarshalJSON(); e == nil {
message = append(message, m...)
message = append(message, ']')
} else {
return status
}
if err := r.Connection.WriteMessage(websocket.TextMessage, message); err != nil {
if err := r.Connection.WriteJSON([]interface{}{"EVENT", event}); err != nil {
return status
}

View File

@ -12,15 +12,10 @@ type Tag []string
// StartsWith checks if a tag contains a prefix.
// for example,
//
// ["p", "abcdef...", "wss://relay.com"]
//
// would match against
//
// ["p", "abcdef..."]
//
// or even
//
// ["p", "abcdef...", "wss://"]
func (tag Tag) StartsWith(prefix []string) bool {
prefixLen := len(prefix)
@ -160,7 +155,8 @@ func (tag Tag) marshalTo(dst []byte) []byte {
return dst
}
// Marshal Tags. Used for Serialization so string escaping should be as in RFC8259.
// 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 {