Refactored methods into pure functions.

This commit is contained in:
Jay
2025-10-31 19:48:56 -04:00
parent 67db088981
commit 4df91938ef
9 changed files with 42 additions and 42 deletions

View File

@@ -9,21 +9,21 @@ import (
// Validate performs a complete event validation: structure, ID computation,
// and signature verification. Returns the first error encountered.
func (e *Event) Validate() error {
if err := e.ValidateStructure(); err != nil {
func Validate(e Event) error {
if err := ValidateStructure(e); err != nil {
return err
}
if err := e.ValidateID(); err != nil {
if err := ValidateID(e); err != nil {
return err
}
return e.ValidateSignature()
return ValidateSignature(e)
}
// ValidateStructure checks that all event fields conform to the protocol
// specification: hex lengths, tag structure, and field formats.
func (e *Event) ValidateStructure() error {
func ValidateStructure(e Event) error {
if !Hex64Pattern.MatchString(e.PubKey) {
return errors.MalformedPubKey
}
@@ -46,8 +46,8 @@ func (e *Event) ValidateStructure() error {
}
// ValidateID recomputes the event ID and verifies it matches the stored ID field.
func (e *Event) ValidateID() error {
computedID, err := e.GetID()
func ValidateID(e Event) error {
computedID, err := GetID(e)
if err != nil {
return errors.FailedIDComp
}
@@ -62,7 +62,7 @@ func (e *Event) ValidateID() error {
// ValidateSignature verifies the event signature is cryptographically valid
// for the event ID and public key using Schnorr verification.
func (e *Event) ValidateSignature() error {
func ValidateSignature(e Event) error {
idBytes, err := hex.DecodeString(e.ID)
if err != nil {
return fmt.Errorf("invalid event id hex: %w", err)