mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-18 22:03:02 +01:00
117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package nostr
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
|
"github.com/valyala/fastjson"
|
|
)
|
|
|
|
type Event struct {
|
|
ID string
|
|
PubKey string
|
|
CreatedAt time.Time
|
|
Kind int
|
|
Tags Tags
|
|
Content string
|
|
Sig string
|
|
}
|
|
|
|
const (
|
|
KindSetMetadata int = 0
|
|
KindTextNote int = 1
|
|
KindRecommendServer int = 2
|
|
KindContactList int = 3
|
|
KindEncryptedDirectMessage int = 4
|
|
KindDeletion int = 5
|
|
)
|
|
|
|
// 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
|
|
// so the order is kept
|
|
var arena fastjson.Arena
|
|
|
|
arr := arena.NewArray()
|
|
|
|
// version: 0
|
|
arr.SetArrayItem(0, arena.NewNumberInt(0))
|
|
|
|
// pubkey
|
|
arr.SetArrayItem(1, arena.NewString(evt.PubKey))
|
|
|
|
// created_at
|
|
arr.SetArrayItem(2, arena.NewNumberInt(int(evt.CreatedAt.Unix())))
|
|
|
|
// kind
|
|
arr.SetArrayItem(3, arena.NewNumberInt(evt.Kind))
|
|
|
|
// tags
|
|
arr.SetArrayItem(4, tagsToFastjsonArray(&arena, evt.Tags))
|
|
|
|
// content
|
|
arr.SetArrayItem(5, arena.NewString(evt.Content))
|
|
|
|
return arr.MarshalTo(nil)
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return false, fmt.Errorf("event pubkey '%s' is invalid hex: %w", evt.PubKey, err)
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return false, fmt.Errorf("signature '%s' is invalid hex: %w", evt.Sig, err)
|
|
}
|
|
sig, err := schnorr.ParseSignature(s)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to parse signature: %w", err)
|
|
}
|
|
|
|
// check signature
|
|
hash := sha256.Sum256(evt.Serialize())
|
|
return sig.Verify(hash[:], pubkey), nil
|
|
}
|
|
|
|
// Sign signs an event with a given privateKey
|
|
func (evt *Event) Sign(privateKey string) error {
|
|
h := sha256.Sum256(evt.Serialize())
|
|
|
|
s, err := hex.DecodeString(privateKey)
|
|
if err != nil {
|
|
return fmt.Errorf("Sign called with invalid private key '%s': %w", privateKey, err)
|
|
}
|
|
sk, _ := btcec.PrivKeyFromBytes(s)
|
|
|
|
sig, err := schnorr.Sign(sk, h[:])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
evt.ID = hex.EncodeToString(h[:])
|
|
evt.Sig = hex.EncodeToString(sig.Serialize())
|
|
return nil
|
|
}
|