2022-01-02 08:44:18 -03:00
|
|
|
package nostr
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2022-02-08 16:27:33 -03:00
|
|
|
"time"
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
"github.com/fiatjaf/bip340"
|
2022-02-09 20:23:10 -03:00
|
|
|
"github.com/valyala/fastjson"
|
2021-01-31 11:05:09 -03:00
|
|
|
)
|
|
|
|
|
2022-02-09 20:23:10 -03:00
|
|
|
type Event struct {
|
|
|
|
ID string
|
|
|
|
PubKey string
|
|
|
|
CreatedAt time.Time
|
|
|
|
Kind int
|
|
|
|
Tags Tags
|
|
|
|
Content string
|
|
|
|
Sig string
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:05:09 -03:00
|
|
|
const (
|
2021-11-28 16:36:42 -03:00
|
|
|
KindSetMetadata int = 0
|
|
|
|
KindTextNote int = 1
|
|
|
|
KindRecommendServer int = 2
|
|
|
|
KindContactList int = 3
|
|
|
|
KindEncryptedDirectMessage int = 4
|
2022-01-01 14:14:59 -03:00
|
|
|
KindDeletion int = 5
|
2021-01-31 11:05:09 -03:00
|
|
|
)
|
|
|
|
|
2022-02-09 13:40:26 -03:00
|
|
|
// GetID serializes and returns the event ID as a string
|
|
|
|
func (evt *Event) GetID() string {
|
|
|
|
h := sha256.Sum256(evt.Serialize())
|
|
|
|
return hex.EncodeToString(h[:])
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:05:09 -03:00
|
|
|
// 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
|
|
|
|
// so the order is kept
|
2022-02-09 20:23:10 -03:00
|
|
|
var arena fastjson.Arena
|
|
|
|
|
|
|
|
arr := arena.NewArray()
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
// version: 0
|
2022-02-09 20:23:10 -03:00
|
|
|
arr.SetArrayItem(0, arena.NewNumberInt(0))
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
// pubkey
|
2022-02-09 20:23:10 -03:00
|
|
|
arr.SetArrayItem(1, arena.NewString(evt.PubKey))
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
// created_at
|
2022-02-09 20:23:10 -03:00
|
|
|
arr.SetArrayItem(2, arena.NewNumberInt(int(evt.CreatedAt.Unix())))
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
// kind
|
2022-02-09 20:23:10 -03:00
|
|
|
arr.SetArrayItem(3, arena.NewNumberInt(evt.Kind))
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
// tags
|
2022-02-09 20:23:10 -03:00
|
|
|
arr.SetArrayItem(4, tagsToFastjsonArray(&arena, evt.Tags))
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
// content
|
2022-02-09 20:23:10 -03:00
|
|
|
arr.SetArrayItem(5, arena.NewString(evt.Content))
|
2021-02-21 21:42:28 -06:00
|
|
|
|
2022-02-09 20:23:10 -03:00
|
|
|
return arr.MarshalTo(nil)
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckSignature checks if the signature is valid for the id
|
|
|
|
// (which is a hash of the serialized event content).
|
|
|
|
// returns an error if the signature itself is invalid.
|
|
|
|
func (evt Event) CheckSignature() (bool, error) {
|
|
|
|
// read and check pubkey
|
2021-12-16 20:47:53 -03:00
|
|
|
pubkey, err := bip340.ParsePublicKey(evt.PubKey)
|
2021-01-31 11:05:09 -03:00
|
|
|
if err != nil {
|
2021-12-16 20:47:53 -03:00
|
|
|
return false, fmt.Errorf("Event has invalid pubkey '%s': %w", evt.PubKey, err)
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
|
|
|
|
2021-12-16 20:47:53 -03:00
|
|
|
s, err := hex.DecodeString(evt.Sig)
|
2021-01-31 11:05:09 -03:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("signature is invalid hex: %w", err)
|
|
|
|
}
|
2021-12-16 20:47:53 -03:00
|
|
|
if len(s) != 64 {
|
|
|
|
return false, fmt.Errorf("signature must be 64 bytes, not %d", len(s))
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
|
|
|
|
2021-12-16 20:47:53 -03:00
|
|
|
var sig [64]byte
|
|
|
|
copy(sig[:], s)
|
2021-01-31 11:05:09 -03:00
|
|
|
|
2021-12-16 20:47:53 -03:00
|
|
|
hash := sha256.Sum256(evt.Serialize())
|
|
|
|
return bip340.Verify(pubkey, hash, sig)
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs an event with a given privateKey
|
2021-02-07 07:56:55 -03:00
|
|
|
func (evt *Event) Sign(privateKey string) error {
|
2021-01-31 11:05:09 -03:00
|
|
|
h := sha256.Sum256(evt.Serialize())
|
|
|
|
|
2021-12-16 20:47:53 -03:00
|
|
|
s, err := bip340.ParsePrivateKey(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Sign called with invalid private key '%s': %w", privateKey, err)
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
aux := make([]byte, 32)
|
|
|
|
rand.Read(aux)
|
|
|
|
sig, err := bip340.Sign(s, h, aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-07 07:56:55 -03:00
|
|
|
evt.ID = hex.EncodeToString(h[:])
|
2021-01-31 11:05:09 -03:00
|
|
|
evt.Sig = hex.EncodeToString(sig[:])
|
|
|
|
return nil
|
|
|
|
}
|