Refactored methods into pure functions.
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
||||
func TestUnmarshalEventJSON(t *testing.T) {
|
||||
event := Event{}
|
||||
json.Unmarshal(testEventJSONBytes, &event)
|
||||
if err := event.Validate(); err != nil {
|
||||
if err := Validate(event); err != nil {
|
||||
t.Error("unmarshalled event is invalid")
|
||||
}
|
||||
expectEqualEvents(t, event, testEvent)
|
||||
@@ -37,7 +37,7 @@ func TestEventJSONRoundTrip(t *testing.T) {
|
||||
}
|
||||
expectedJSON := `{"id":"86e856d0527dd08527498cd8afd8a7d296bde37e4757a8921f034f0b344df3ad","pubkey":"cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef","created_at":1760740551,"kind":1,"tags":[["a","value"],["b","value","optional"],["name","value","optional","optional"]],"content":"hello world","sig":"c05fe02a9c082ff56aad2b16b5347498a21665f02f050ba086dbe6bd593c8cd448505d2831d1c0340acc1793eaf89b7c0cb21bb696c71da6b8d6b857702bb557"}`
|
||||
|
||||
if err := event.Validate(); err != nil {
|
||||
if err := Validate(event); err != nil {
|
||||
t.Error("test event is invalid")
|
||||
}
|
||||
eventJSON, err := json.Marshal(event)
|
||||
@@ -47,7 +47,7 @@ func TestEventJSONRoundTrip(t *testing.T) {
|
||||
unmarshalledEvent := Event{}
|
||||
json.Unmarshal(eventJSON, &unmarshalledEvent)
|
||||
|
||||
if err := unmarshalledEvent.Validate(); err != nil {
|
||||
if err := Validate(unmarshalledEvent); err != nil {
|
||||
t.Error("unmarshalled event is invalid")
|
||||
}
|
||||
expectEqualEvents(t, unmarshalledEvent, event)
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
// Serialize returns the canonical JSON array representation of the event.
|
||||
// used for ID computation: [0, pubkey, created_at, kind, tags, content].
|
||||
func (e *Event) Serialize() ([]byte, error) {
|
||||
func Serialize(e Event) ([]byte, error) {
|
||||
serialized := []interface{}{
|
||||
0,
|
||||
e.PubKey,
|
||||
@@ -27,8 +27,8 @@ func (e *Event) Serialize() ([]byte, error) {
|
||||
|
||||
// GetID computes and returns the event ID as a lowercase, hex-encoded SHA-256 hash
|
||||
// of the serialized event.
|
||||
func (e *Event) GetID() (string, error) {
|
||||
bytes, err := e.Serialize()
|
||||
func GetID(e Event) (string, error) {
|
||||
bytes, err := Serialize(e)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ var idTestCases = []IDTestCase{
|
||||
func TestEventGetId(t *testing.T) {
|
||||
for _, tc := range idTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actual, err := tc.event.GetID()
|
||||
actual, err := GetID(tc.event)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expected, actual)
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -184,7 +184,7 @@ var structureTestCases = []ValidateEventTestCase{
|
||||
func TestValidateEventStructure(t *testing.T) {
|
||||
for _, tc := range structureTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.event.ValidateStructure()
|
||||
err := ValidateStructure(tc.event)
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
})
|
||||
}
|
||||
@@ -201,7 +201,7 @@ func TestValidateEventIDFailure(t *testing.T) {
|
||||
Sig: testEvent.Sig,
|
||||
}
|
||||
|
||||
err := event.ValidateID()
|
||||
err := ValidateID(event)
|
||||
assert.ErrorContains(t, err, "does not match computed id")
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ func TestValidateSignature(t *testing.T) {
|
||||
PubKey: testEvent.PubKey,
|
||||
Sig: testEvent.Sig,
|
||||
}
|
||||
err := event.ValidateSignature()
|
||||
err := ValidateSignature(event)
|
||||
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
@@ -222,7 +222,7 @@ func TestValidateInvalidSignature(t *testing.T) {
|
||||
PubKey: testEvent.PubKey,
|
||||
Sig: "9e43cbcf7e828a21c53fa35371ee79bffbfd7a3063ae46fc05ec623dd3186667c57e3d006488015e19247df35eb41c61013e051aa87860e23fa5ffbd44120482",
|
||||
}
|
||||
err := event.ValidateSignature()
|
||||
err := ValidateSignature(event)
|
||||
|
||||
assert.ErrorContains(t, err, "event signature is invalid")
|
||||
}
|
||||
@@ -281,7 +281,7 @@ func TestValidateSignatureInvalidEventSignature(t *testing.T) {
|
||||
for _, tc := range validateSignatureTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
event := Event{ID: tc.id, PubKey: tc.pubkey, Sig: tc.sig}
|
||||
err := event.ValidateSignature()
|
||||
err := ValidateSignature(event)
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
})
|
||||
}
|
||||
@@ -301,6 +301,6 @@ func TestValidateEvent(t *testing.T) {
|
||||
Sig: "668a715f1eb983172acf230d17bd283daedb2598adf8de4290bcc7eb0b802fdb60669d1e7d1104ac70393f4dbccd07e8abf897152af6ce6c0a75499874e27f14",
|
||||
}
|
||||
|
||||
err := event.Validate()
|
||||
err := Validate(event)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user