Fix IsValidID/IsValidSig doc comments; remove ValidateID, GetIDBytes, checkIDMatch

This commit is contained in:
Jay
2026-05-22 11:35:30 -04:00
parent 0ca44c7e20
commit 17789a7dbd
3 changed files with 12 additions and 44 deletions
+1 -6
View File
@@ -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 {
+11 -24
View File
@@ -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 {
-14
View File
@@ -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),