added lenght check

This commit is contained in:
pippellia-btc 2025-03-20 12:35:40 +01:00 committed by fiatjaf_
parent c1d9c094ee
commit 25838a024e
2 changed files with 10 additions and 3 deletions

View File

@ -8,6 +8,8 @@ import (
"github.com/mailru/easyjson"
)
const hextable = "0123456789abcdef"
// Event represents a Nostr event.
type Event struct {
ID string
@ -24,7 +26,7 @@ func (evt Event) String() string {
return string(j)
}
// GetID computes the event ID abd returns it as a hex string.
// GetID computes the event ID and returns it as a hex string.
func (evt *Event) GetID() string {
h := sha256.Sum256(evt.Serialize())
return hex.EncodeToString(h[:])
@ -32,11 +34,13 @@ func (evt *Event) GetID() string {
// CheckID checks if the implied ID matches the given ID more efficiently.
func (evt *Event) CheckID() bool {
if len(evt.ID) != 64 {
return false
}
ser := evt.Serialize()
h := sha256.Sum256(ser)
const hextable = "0123456789abcdef"
for i := 0; i < 32; i++ {
b := hextable[h[i]>>4]
if b != evt.ID[i*2] {

View File

@ -87,6 +87,9 @@ func TestIDCheck(t *testing.T) {
Content: fmt.Sprintf("hello %d", i),
Tags: Tags{},
}
require.False(t, evt.CheckID())
evt.Sign(GeneratePrivateKey())
require.True(t, evt.CheckID())