From 17789a7dbd75deba059dde1b08c16281219738cb Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 22 May 2026 11:35:30 -0400 Subject: [PATCH] Fix IsValidID/IsValidSig doc comments; remove ValidateID, GetIDBytes, checkIDMatch --- events/id.go | 7 +------ events/validate.go | 35 +++++++++++------------------------ events/validate_test.go | 14 -------------- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/events/id.go b/events/id.go index 3f8399e..9bb7eec 100644 --- a/events/id.go +++ b/events/id.go @@ -9,15 +9,10 @@ import ( // GetID computes and returns the event ID as a lowercase, hex-encoded SHA-256 hash // of the serialized event. func GetID(e Event) string { - hash := GetIDBytes(e) + hash := sha256.Sum256(Serialize(e)) return hex.EncodeToString(hash[:]) } -// GetIDBytes computes and returns the event ID as a raw SHA256 digest -func GetIDBytes(e Event) [32]byte { - return sha256.Sum256(Serialize(e)) -} - // Serialize returns the canonical JSON array representation of the event. // used for ID computation: [0, pubkey, created_at, kind, tags, content]. func Serialize(e Event) []byte { diff --git a/events/validate.go b/events/validate.go index 19d339a..17dfd05 100644 --- a/events/validate.go +++ b/events/validate.go @@ -2,6 +2,7 @@ package events import ( "bytes" + "crypto/sha256" "encoding/hex" "fmt" "git.wisehodl.dev/jay/go-roots/errors" @@ -15,9 +16,15 @@ func Validate(e Event) error { return err } - idBytes, err := checkIDMatch(e) + idHash := sha256.Sum256(Serialize(e)) + idBytes, err := hex.DecodeString(e.ID) if err != nil { - return err + return errors.MalformedID + } + if !bytes.Equal(idBytes, idHash[:]) { + return fmt.Errorf( + "event id %q does not match computed id %q", + e.ID, hex.EncodeToString(idHash[:])) } return validateSignatureBytes(idBytes, e.Sig, e.PubKey) @@ -47,12 +54,6 @@ func ValidateStructure(e Event) error { return nil } -// ValidateID recomputes the event ID and verifies it matches the stored ID field. -func ValidateID(e Event) error { - _, err := checkIDMatch(e) - return err -} - // ValidateSignature verifies the event signature is cryptographically valid // for the event ID and public key using Schnorr verification. func ValidateSignature(e Event) error { @@ -70,32 +71,18 @@ func IsValidKey(value string) bool { return isLowerHex(value, 64) } -// IsValidKey verifies that an event id is properly formatted. +// IsValidID verifies that an event id is properly formatted. func IsValidID(value string) bool { return isLowerHex(value, 64) } -// IsValidKey verifies that an event signature is properly formatted. +// IsValidSig verifies that an event signature is properly formatted. func IsValidSig(value string) bool { return isLowerHex(value, 128) } // Helpers -func checkIDMatch(e Event) ([]byte, error) { - idHash := GetIDBytes(e) - idBytes, err := hex.DecodeString(e.ID) - if err != nil { - return nil, errors.MalformedID - } - if !bytes.Equal(idBytes, idHash[:]) { - return nil, fmt.Errorf( - "event id %q does not match computed id %q", - e.ID, hex.EncodeToString(idHash[:])) - } - return idBytes, nil -} - func validateSignatureBytes(idBytes []byte, sigHex, pkHex string) error { sigBytes, err := hex.DecodeString(sigHex) if err != nil { diff --git a/events/validate_test.go b/events/validate_test.go index 984123a..3a19613 100644 --- a/events/validate_test.go +++ b/events/validate_test.go @@ -182,20 +182,6 @@ func TestValidateEventStructure(t *testing.T) { } } -func TestValidateEventIDFailure(t *testing.T) { - event := NewEvent( - WithID("7f661c2a3c1ed67dc959d6cd968d743d5e6e334313df44724bca939e2aa42c9e"), - WithPubKey(testEvent.PubKey), - WithCreatedAt(testEvent.CreatedAt), - WithKind(testEvent.Kind), - WithContent(testEvent.Content), - WithSig(testEvent.Sig), - ) - - err := ValidateID(event) - assert.ErrorContains(t, err, "does not match computed id") -} - func TestValidateSignature(t *testing.T) { event := NewEvent( WithID(testEvent.ID),