mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-13 10:21:02 +02:00
added lenght check
This commit is contained in:
parent
c1d9c094ee
commit
25838a024e
10
event.go
10
event.go
@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/mailru/easyjson"
|
"github.com/mailru/easyjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const hextable = "0123456789abcdef"
|
||||||
|
|
||||||
// Event represents a Nostr event.
|
// Event represents a Nostr event.
|
||||||
type Event struct {
|
type Event struct {
|
||||||
ID string
|
ID string
|
||||||
@ -24,7 +26,7 @@ func (evt Event) String() string {
|
|||||||
return string(j)
|
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 {
|
func (evt *Event) GetID() string {
|
||||||
h := sha256.Sum256(evt.Serialize())
|
h := sha256.Sum256(evt.Serialize())
|
||||||
return hex.EncodeToString(h[:])
|
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.
|
// CheckID checks if the implied ID matches the given ID more efficiently.
|
||||||
func (evt *Event) CheckID() bool {
|
func (evt *Event) CheckID() bool {
|
||||||
|
if len(evt.ID) != 64 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
ser := evt.Serialize()
|
ser := evt.Serialize()
|
||||||
h := sha256.Sum256(ser)
|
h := sha256.Sum256(ser)
|
||||||
|
|
||||||
const hextable = "0123456789abcdef"
|
|
||||||
|
|
||||||
for i := 0; i < 32; i++ {
|
for i := 0; i < 32; i++ {
|
||||||
b := hextable[h[i]>>4]
|
b := hextable[h[i]>>4]
|
||||||
if b != evt.ID[i*2] {
|
if b != evt.ID[i*2] {
|
||||||
|
@ -87,6 +87,9 @@ func TestIDCheck(t *testing.T) {
|
|||||||
Content: fmt.Sprintf("hello %d", i),
|
Content: fmt.Sprintf("hello %d", i),
|
||||||
Tags: Tags{},
|
Tags: Tags{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require.False(t, evt.CheckID())
|
||||||
|
|
||||||
evt.Sign(GeneratePrivateKey())
|
evt.Sign(GeneratePrivateKey())
|
||||||
require.True(t, evt.CheckID())
|
require.True(t, evt.CheckID())
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user