refactoring, added documentation
This commit is contained in:
197
event.go
197
event.go
@@ -1,166 +1,63 @@
|
||||
// Roots is a purposefully minimal, core Nostr protocol library that provides
|
||||
// mathematically invariant primitives that define protocol compliance: event
|
||||
// structure, serialization, cryptographic signatures, and subscription
|
||||
// filters.
|
||||
package roots
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
)
|
||||
|
||||
// Tag represents a single tag within an event as an array of strings.
|
||||
// The first element identifies the tag name, the second contains the value,
|
||||
// and subsequent elements are optional.
|
||||
type Tag []string
|
||||
|
||||
// Event represents a Nostr protocol event, with its seven required fields.
|
||||
// All fields must be present for a valid event.
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
PubKey string `json:"pubkey"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
Kind int `json:"kind"`
|
||||
Tags [][]string `json:"tags"`
|
||||
Content string `json:"content"`
|
||||
Sig string `json:"sig"`
|
||||
ID string `json:"id"`
|
||||
PubKey string `json:"pubkey"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
Kind int `json:"kind"`
|
||||
Tags []Tag `json:"tags"`
|
||||
Content string `json:"content"`
|
||||
Sig string `json:"sig"`
|
||||
}
|
||||
|
||||
var (
|
||||
Hex64Pattern = regexp.MustCompile("^[a-f0-9]{64}$")
|
||||
// Hex64Pattern matches 64-character, lowercase, hexadecimal strings.
|
||||
// Used for validating event IDs and cryptographic keys.
|
||||
Hex64Pattern = regexp.MustCompile("^[a-f0-9]{64}$")
|
||||
|
||||
// Hex128Pattern matches 128-character, lowercase, hexadecimal strings.
|
||||
// Used for validating signatures.
|
||||
Hex128Pattern = regexp.MustCompile("^[a-f0-9]{128}$")
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMalformedPubKey = errors.New("pubkey must be 64 lowercase hex characters")
|
||||
ErrMalformedID = errors.New("id must be 64 hex characters")
|
||||
ErrMalformedSig = errors.New("signature must be 128 hex characters")
|
||||
ErrMalformedTag = errors.New("tags must contain at least two elements")
|
||||
ErrFailedIDComp = errors.New("failed to compute event id")
|
||||
ErrNoEventID = errors.New("event id is empty")
|
||||
ErrInvalidSig = errors.New("event signature is invalid")
|
||||
// ErrMalformedPubKey indicates a public key is not 64 lowercase hex characters.
|
||||
ErrMalformedPubKey = errors.New("public key must be 64 lowercase hex characters")
|
||||
|
||||
// ErrMalformedPrivKey indicates a private key is not 64 lowercase hex characters.
|
||||
ErrMalformedPrivKey = errors.New("private key must be 64 lowercase hex characters")
|
||||
|
||||
// ErrMalformedID indicates an event id is not 64 hex characters.
|
||||
ErrMalformedID = errors.New("event id must be 64 hex characters")
|
||||
|
||||
// ErrMalformedSig indicates an event signature is not 128 hex characters.
|
||||
ErrMalformedSig = errors.New("event signature must be 128 hex characters")
|
||||
|
||||
// ErrMalformedTag indicates an event tag contains fewer than two elements.
|
||||
ErrMalformedTag = errors.New("tags must contain at least two elements")
|
||||
|
||||
// ErrFailedIDComp indicates the event ID could not be computed during validation.
|
||||
ErrFailedIDComp = errors.New("failed to compute event id")
|
||||
|
||||
// ErrNoEventID indicates the event ID field is empty.
|
||||
ErrNoEventID = errors.New("event id is empty")
|
||||
|
||||
// ErrInvalidSig indicates the event signature failed cryptographic validation.
|
||||
ErrInvalidSig = errors.New("event signature is invalid")
|
||||
)
|
||||
|
||||
func (e *Event) Serialize() ([]byte, error) {
|
||||
serialized := []interface{}{
|
||||
0,
|
||||
e.PubKey,
|
||||
e.CreatedAt,
|
||||
e.Kind,
|
||||
e.Tags,
|
||||
e.Content,
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(serialized)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
func (e *Event) GetID() (string, error) {
|
||||
bytes, err := e.Serialize()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash := sha256.Sum256(bytes)
|
||||
return hex.EncodeToString(hash[:]), nil
|
||||
}
|
||||
|
||||
func SignEvent(eventID, privateKeyHex string) (string, error) {
|
||||
skBytes, err := hex.DecodeString(privateKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid private key hex: %w", err)
|
||||
}
|
||||
|
||||
idBytes, err := hex.DecodeString(eventID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid event id hex: %w", err)
|
||||
}
|
||||
|
||||
// discard public key return value
|
||||
sk, _ := btcec.PrivKeyFromBytes(skBytes)
|
||||
sig, err := schnorr.Sign(sk, idBytes)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("schnorr signature error: %w", err)
|
||||
}
|
||||
|
||||
return hex.EncodeToString(sig.Serialize()), nil
|
||||
}
|
||||
|
||||
func (e *Event) Validate() error {
|
||||
if err := e.ValidateStructure(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := e.ValidateID(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ValidateSignature(e.ID, e.Sig, e.PubKey)
|
||||
}
|
||||
|
||||
func (e *Event) ValidateStructure() error {
|
||||
if !Hex64Pattern.MatchString(e.PubKey) {
|
||||
return ErrMalformedPubKey
|
||||
}
|
||||
|
||||
if !Hex64Pattern.MatchString(e.ID) {
|
||||
return ErrMalformedID
|
||||
}
|
||||
|
||||
if !Hex128Pattern.MatchString(e.Sig) {
|
||||
return ErrMalformedSig
|
||||
}
|
||||
|
||||
for _, tag := range e.Tags {
|
||||
if len(tag) < 2 {
|
||||
return ErrMalformedTag
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Event) ValidateID() error {
|
||||
computedID, err := e.GetID()
|
||||
if err != nil {
|
||||
return ErrFailedIDComp
|
||||
}
|
||||
if e.ID == "" {
|
||||
return ErrNoEventID
|
||||
}
|
||||
if computedID != e.ID {
|
||||
return fmt.Errorf("event id %q does not match computed id %q", e.ID, computedID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateSignature(eventID, eventSig, publicKeyHex string) error {
|
||||
idBytes, err := hex.DecodeString(eventID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid event id hex: %w", err)
|
||||
}
|
||||
|
||||
sigBytes, err := hex.DecodeString(eventSig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid event signature hex: %w", err)
|
||||
}
|
||||
|
||||
pkBytes, err := hex.DecodeString(publicKeyHex)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid public key hex: %w", err)
|
||||
}
|
||||
|
||||
signature, err := schnorr.ParseSignature(sigBytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("malformed signature: %w", err)
|
||||
}
|
||||
|
||||
publicKey, err := schnorr.ParsePubKey(pkBytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("malformed public key: %w", err)
|
||||
}
|
||||
|
||||
if signature.Verify(idBytes, publicKey) {
|
||||
return nil
|
||||
} else {
|
||||
return ErrInvalidSig
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user