go-nostr/event.go

122 lines
3.0 KiB
Go
Raw 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
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
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[:])
}
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
}
// 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
pk, err := hex.DecodeString(evt.PubKey)
2021-01-31 11:05:09 -03:00
if err != nil {
return false, fmt.Errorf("event pubkey '%s' is invalid hex: %w", evt.PubKey, err)
2021-01-31 11:05:09 -03:00
}
pubkey, err := schnorr.ParsePubKey(pk)
if err != nil {
return false, fmt.Errorf("event has invalid pubkey '%s': %w", evt.PubKey, err)
}
// read signature
s, err := hex.DecodeString(evt.Sig)
2021-01-31 11:05:09 -03:00
if err != nil {
return false, fmt.Errorf("signature '%s' is invalid hex: %w", evt.Sig, err)
2021-01-31 11:05:09 -03:00
}
sig, err := schnorr.ParseSignature(s)
if err != nil {
return false, fmt.Errorf("failed to parse signature: %w", err)
2021-01-31 11:05:09 -03:00
}
// check signature
hash := sha256.Sum256(evt.Serialize())
return sig.Verify(hash[:], pubkey), nil
2021-01-31 11:05:09 -03:00
}
// Sign signs an event with a given privateKey.
func (evt *Event) Sign(privateKey string, signOpts ...schnorr.SignOption) error {
s, err := hex.DecodeString(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
}
if evt.Tags == nil {
evt.Tags = make(Tags, 0)
}
2023-04-11 15:33:13 -03:00
sk, pk := btcec.PrivKeyFromBytes(s)
pkBytes := pk.SerializeCompressed()
evt.PubKey = hex.EncodeToString(pkBytes[1:])
h := sha256.Sum256(evt.Serialize())
sig, err := schnorr.Sign(sk, h[:], signOpts...)
2021-01-31 11:05:09 -03:00
if err != nil {
return err
}
2021-02-07 07:56:55 -03:00
evt.ID = hex.EncodeToString(h[:])
evt.Sig = hex.EncodeToString(sig.Serialize())
2021-01-31 11:05:09 -03:00
return nil
}