go-nostr/event.go

106 lines
2.4 KiB
Go
Raw Permalink Normal View History

2022-01-02 08:44:18 -03:00
package nostr
2021-01-31 11:05:09 -03:00
import (
"crypto/sha256"
"encoding/hex"
"fmt"
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
)
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
// anything here will be mashed together with the main event object when serializing
extra map[string]any
}
// Event Stringer interface, just returns the raw JSON as a string.
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)
}
// GetID serializes and returns the event ID as a string.
func (evt *Event) GetID() string {
h := sha256.Sum256(evt.Serialize())
return hex.EncodeToString(h[:])
}
// CheckID checks if the implied ID matches the given ID
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
}
2023-01-16 23:13:19 -05:00
// Serialize outputs a byte array that can be hashed/signed to identify/authenticate.
// JSON encoding as defined in RFC4627.
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
dst := make([]byte, 0)
2021-01-31 11:05:09 -03:00
// the header portion is easy to serialize
// [0,"pubkey",created_at,kind,[
dst = append(dst, []byte(
fmt.Sprintf(
"[0,\"%s\",%d,%d,",
evt.PubKey,
2023-04-16 15:56:50 -03:00
evt.CreatedAt,
evt.Kind,
))...)
// tags
dst = evt.Tags.marshalTo(dst)
dst = append(dst, ',')
2021-01-31 11:05:09 -03:00
// content needs to be escaped in general as it is user generated.
dst = escapeString(dst, evt.Content)
dst = append(dst, ']')
2021-02-21 21:42:28 -06:00
return dst
2021-01-31 11:05:09 -03:00
}
2024-09-19 14:12:41 +03:30
// IsRegular checks if the given kind is in Regular range.
func (evt *Event) IsRegular() bool {
2024-09-19 11:38:22 -03:00
return evt.Kind < 10000 && evt.Kind != 0 && evt.Kind != 3
2024-09-19 14:12:41 +03:30
}
// IsReplaceable checks if the given kind is in Replaceable range.
func (evt *Event) IsReplaceable() bool {
2024-09-19 11:38:22 -03:00
return evt.Kind == 0 || evt.Kind == 3 ||
(10000 <= evt.Kind && evt.Kind < 20000)
2024-09-19 14:12:41 +03:30
}
// IsEphemeral checks if the given kind is in Ephemeral range.
func (evt *Event) IsEphemeral() bool {
2024-09-19 11:38:22 -03:00
return 20000 <= evt.Kind && evt.Kind < 30000
2024-09-19 14:12:41 +03:30
}
2024-09-19 11:38:22 -03:00
// IsAddressable checks if the given kind is in Addressable range.
func (evt *Event) IsAddressable() bool {
return 30000 <= evt.Kind && evt.Kind < 40000
2024-09-19 14:12:41 +03:30
}