mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-03 20:17:04 +02:00
serialization tests for events and filters.
This commit is contained in:
11
event.go
11
event.go
@ -38,14 +38,13 @@ type Event struct {
|
||||
type Time time.Time
|
||||
|
||||
func (tm *Time) UnmarshalJSON(payload []byte) error {
|
||||
var unix int64
|
||||
err := json.Unmarshal(payload, &unix)
|
||||
unix, err := strconv.ParseInt(string(payload), 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("time must be a unix timestamp as an integer, not '%s': %w",
|
||||
string(payload), err)
|
||||
}
|
||||
t := Time(time.Unix(unix, 0))
|
||||
tm = &t
|
||||
*tm = t
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -53,6 +52,12 @@ func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return []byte(strconv.FormatInt(time.Time(t).Unix(), 10)), nil
|
||||
}
|
||||
|
||||
// GetID serializes and returns the event ID as a string
|
||||
func (evt *Event) GetID() string {
|
||||
h := sha256.Sum256(evt.Serialize())
|
||||
return hex.EncodeToString(h[:])
|
||||
}
|
||||
|
||||
// Serialize outputs a byte array that can be hashed/signed to identify/authenticate
|
||||
func (evt *Event) Serialize() []byte {
|
||||
// the serialization process is just putting everything into a JSON array
|
||||
|
Reference in New Issue
Block a user