serialization tests for events and filters.

This commit is contained in:
fiatjaf
2022-02-09 13:40:26 -03:00
parent 5266482b2a
commit 41955a0601
4 changed files with 149 additions and 5 deletions

View File

@ -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