mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-10-10 21:04:10 +02:00
Changing (evt Event) MarshalJSON to avoid string escaping bug
This commit is contained in:
@@ -106,7 +106,7 @@ func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON() returns the JSON byte encoding of the event, as in NIP-01.
|
// 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 := make([]byte, 0)
|
||||||
dst = append(dst, '{')
|
dst = append(dst, '{')
|
||||||
dst = append(dst, []byte(fmt.Sprintf("\"id\":\"%s\",\"pubkey\":\"%s\",\"created_at\":%d,\"kind\":%d,\"tags\":",
|
dst = append(dst, []byte(fmt.Sprintf("\"id\":\"%s\",\"pubkey\":\"%s\",\"created_at\":%d,\"kind\":%d,\"tags\":",
|
||||||
|
10
relay.go
10
relay.go
@@ -229,15 +229,7 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
|
|||||||
defer r.okCallbacks.Delete(event.ID)
|
defer r.okCallbacks.Delete(event.ID)
|
||||||
|
|
||||||
// publish event
|
// publish event
|
||||||
message := []byte("[\"EVENT\",")
|
if err := r.Connection.WriteJSON([]interface{}{"EVENT", event}); err != nil {
|
||||||
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 {
|
|
||||||
return status
|
return status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
8
tags.go
8
tags.go
@@ -12,15 +12,10 @@ 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)
|
||||||
@@ -160,7 +155,8 @@ func (tag Tag) marshalTo(dst []byte) []byte {
|
|||||||
return dst
|
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 {
|
func (tags Tags) marshalTo(dst []byte) []byte {
|
||||||
dst = append(dst, '[')
|
dst = append(dst, '[')
|
||||||
for i, tag := range tags {
|
for i, tag := range tags {
|
||||||
|
Reference in New Issue
Block a user