2022-01-02 08:44:18 -03:00
|
|
|
package nostr
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2025-03-07 21:45:47 -03:00
|
|
|
"strconv"
|
2023-03-11 10:53:17 -06:00
|
|
|
|
2023-04-16 15:56:50 -03:00
|
|
|
"github.com/mailru/easyjson"
|
2021-01-31 11:05:09 -03:00
|
|
|
)
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Event represents a Nostr event.
|
2022-02-09 20:23:10 -03:00
|
|
|
type Event struct {
|
2024-05-29 08:40:54 -03:00
|
|
|
ID string
|
|
|
|
PubKey string
|
|
|
|
CreatedAt Timestamp
|
|
|
|
Kind int
|
|
|
|
Tags Tags
|
|
|
|
Content string
|
|
|
|
Sig string
|
2022-02-09 20:23:10 -03:00
|
|
|
}
|
|
|
|
|
2023-04-11 11:02:35 -03:00
|
|
|
func (evt Event) String() string {
|
2023-04-16 15:56:50 -03:00
|
|
|
j, _ := easyjson.Marshal(evt)
|
2023-04-11 07:36:59 -03:00
|
|
|
return string(j)
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// GetID computes the event ID abd returns it as a hex string.
|
2022-02-09 13:40:26 -03:00
|
|
|
func (evt *Event) GetID() string {
|
|
|
|
h := sha256.Sum256(evt.Serialize())
|
|
|
|
return hex.EncodeToString(h[:])
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// CheckID checks if the implied ID matches the given ID more efficiently.
|
2024-09-14 10:55:16 -03:00
|
|
|
func (evt *Event) CheckID() bool {
|
|
|
|
ser := evt.Serialize()
|
|
|
|
h := sha256.Sum256(ser)
|
|
|
|
|
|
|
|
const hextable = "0123456789abcdef"
|
|
|
|
|
|
|
|
for i := 0; i < 32; i++ {
|
|
|
|
b := hextable[h[i]>>4]
|
|
|
|
if b != evt.ID[i*2] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
b = hextable[h[i]&0x0f]
|
|
|
|
if b != evt.ID[i*2+1] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Serialize outputs a byte array that can be hashed to produce the canonical event "id".
|
2021-01-31 11:05:09 -03:00
|
|
|
func (evt *Event) Serialize() []byte {
|
|
|
|
// the serialization process is just putting everything into a JSON array
|
2023-01-16 23:13:19 -05:00
|
|
|
// so the order is kept. See NIP-01
|
2025-02-04 17:52:05 -03:00
|
|
|
dst := make([]byte, 0, 100+len(evt.Content)+len(evt.Tags)*80)
|
2021-01-31 11:05:09 -03:00
|
|
|
|
2023-01-16 23:31:22 -05:00
|
|
|
// the header portion is easy to serialize
|
|
|
|
// [0,"pubkey",created_at,kind,[
|
2025-03-07 21:45:47 -03:00
|
|
|
dst = append(dst, "[0,\""...)
|
|
|
|
dst = append(dst, evt.PubKey...)
|
|
|
|
dst = append(dst, "\","...)
|
|
|
|
dst = append(dst, strconv.FormatInt(int64(evt.CreatedAt), 10)...)
|
|
|
|
dst = append(dst, ',')
|
|
|
|
dst = append(dst, strconv.Itoa(evt.Kind)...)
|
|
|
|
dst = append(dst, ',')
|
2023-01-18 11:06:59 -05:00
|
|
|
|
|
|
|
// tags
|
2025-03-10 02:33:29 -03:00
|
|
|
dst = append(dst, '[')
|
|
|
|
for i, tag := range evt.Tags {
|
|
|
|
if i > 0 {
|
|
|
|
dst = append(dst, ',')
|
|
|
|
}
|
|
|
|
// tag item
|
|
|
|
dst = append(dst, '[')
|
|
|
|
for i, s := range tag {
|
|
|
|
if i > 0 {
|
|
|
|
dst = append(dst, ',')
|
|
|
|
}
|
|
|
|
dst = escapeString(dst, s)
|
|
|
|
}
|
|
|
|
dst = append(dst, ']')
|
|
|
|
}
|
|
|
|
dst = append(dst, "],"...)
|
2021-01-31 11:05:09 -03:00
|
|
|
|
2023-01-16 23:31:22 -05:00
|
|
|
// content needs to be escaped in general as it is user generated.
|
2023-01-18 11:06:59 -05:00
|
|
|
dst = escapeString(dst, evt.Content)
|
|
|
|
dst = append(dst, ']')
|
2021-02-21 21:42:28 -06:00
|
|
|
|
2023-01-18 11:06:59 -05:00
|
|
|
return dst
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|