add constructor functions with options. update tests.
This commit is contained in:
+63
-1
@@ -3,6 +3,10 @@
|
||||
// serialization, cryptographic signatures, and subscription filters.
|
||||
package events
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Tag represents a single tag within an event as an array of strings.
|
||||
// The first element identifies the tag name, the second contains the value,
|
||||
// and subsequent elements are optional.
|
||||
@@ -13,9 +17,67 @@ type Tag []string
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
PubKey string `json:"pubkey"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Kind int `json:"kind"`
|
||||
Tags []Tag `json:"tags"`
|
||||
Content string `json:"content"`
|
||||
Sig string `json:"sig"`
|
||||
}
|
||||
|
||||
func NewEvent(opts ...EventOption) Event {
|
||||
e := Event{Tags: make([]Tag, 0)}
|
||||
for _, opt := range opts {
|
||||
opt(&e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
type EventOption func(*Event)
|
||||
|
||||
func WithID(id string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
func WithPubKey(pk string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.PubKey = pk
|
||||
}
|
||||
}
|
||||
|
||||
func WithCreatedAt(t int64) EventOption {
|
||||
return func(e *Event) {
|
||||
e.CreatedAt = t
|
||||
}
|
||||
}
|
||||
|
||||
func WithCreatedAtTime(t time.Time) EventOption {
|
||||
return func(e *Event) {
|
||||
e.CreatedAt = t.Unix()
|
||||
}
|
||||
}
|
||||
|
||||
func WithKind(k int) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Kind = k
|
||||
}
|
||||
}
|
||||
|
||||
func WithTag(t Tag) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Tags = append(e.Tags, t)
|
||||
}
|
||||
}
|
||||
|
||||
func WithContent(c string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Content = c
|
||||
}
|
||||
}
|
||||
|
||||
func WithSig(s string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Sig = s
|
||||
}
|
||||
}
|
||||
|
||||
+12
-14
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestUnmarshalEventJSON(t *testing.T) {
|
||||
event := Event{}
|
||||
event := NewEvent()
|
||||
json.Unmarshal(testEventJSONBytes, &event)
|
||||
if err := Validate(event); err != nil {
|
||||
t.Error("unmarshalled event is invalid")
|
||||
@@ -22,19 +22,17 @@ func TestMarshalEventJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEventJSONRoundTrip(t *testing.T) {
|
||||
event := Event{
|
||||
ID: "86e856d0527dd08527498cd8afd8a7d296bde37e4757a8921f034f0b344df3ad",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{
|
||||
{"a", "value"},
|
||||
{"b", "value", "optional"},
|
||||
{"name", "value", "optional", "optional"},
|
||||
},
|
||||
Content: testEvent.Content,
|
||||
Sig: "c05fe02a9c082ff56aad2b16b5347498a21665f02f050ba086dbe6bd593c8cd448505d2831d1c0340acc1793eaf89b7c0cb21bb696c71da6b8d6b857702bb557",
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID("86e856d0527dd08527498cd8afd8a7d296bde37e4757a8921f034f0b344df3ad"),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a", "value"}),
|
||||
WithTag(Tag{"b", "value", "optional"}),
|
||||
WithTag(Tag{"name", "value", "optional", "optional"}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig("c05fe02a9c082ff56aad2b16b5347498a21665f02f050ba086dbe6bd593c8cd448505d2831d1c0340acc1793eaf89b7c0cb21bb696c71da6b8d6b857702bb557"),
|
||||
)
|
||||
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 := Validate(event); err != nil {
|
||||
|
||||
@@ -8,15 +8,14 @@ import (
|
||||
const testSK = "f43a0435f69529f310bbd1d6263d2fbf0977f54bfe2310cc37ae5904b83bb167"
|
||||
const testPK = "cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef"
|
||||
|
||||
var testEvent = Event{
|
||||
ID: "c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
|
||||
PubKey: testPK,
|
||||
CreatedAt: 1760740551,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "hello world",
|
||||
Sig: "83b71e15649c9e9da362c175f988c36404cabf357a976d869102a74451cfb8af486f6088b5631033b4927bd46cad7a0d90d7f624aefc0ac260364aa65c36071a",
|
||||
}
|
||||
var testEvent = NewEvent(
|
||||
WithID("c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad"),
|
||||
WithPubKey(testPK),
|
||||
WithCreatedAt(1760740551),
|
||||
WithKind(1),
|
||||
WithContent("hello world"),
|
||||
WithSig("83b71e15649c9e9da362c175f988c36404cabf357a976d869102a74451cfb8af486f6088b5631033b4927bd46cad7a0d90d7f624aefc0ac260364aa65c36071a"),
|
||||
)
|
||||
|
||||
var testEventJSON = `{"id":"c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad","pubkey":"cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef","created_at":1760740551,"kind":1,"tags":[],"content":"hello world","sig":"83b71e15649c9e9da362c175f988c36404cabf357a976d869102a74451cfb8af486f6088b5631033b4927bd46cad7a0d90d7f624aefc0ac260364aa65c36071a"}`
|
||||
var testEventJSONBytes = []byte(testEventJSON)
|
||||
|
||||
+90
-110
@@ -14,181 +14,161 @@ type IDTestCase struct {
|
||||
var idTestCases = []IDTestCase{
|
||||
{
|
||||
name: "minimal event",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
),
|
||||
expected: "13a55672a600398894592f4cb338652d4936caffe5d3718d11597582bb030c39",
|
||||
},
|
||||
|
||||
{
|
||||
name: "alphanumeric content",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "hello world",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithContent("hello world"),
|
||||
),
|
||||
expected: "c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
|
||||
},
|
||||
|
||||
{
|
||||
name: "unicode content",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "hello world 😀",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithContent("hello world 😀"),
|
||||
),
|
||||
expected: "e42083fafbf9a39f97914fd9a27cedb38c429ac3ca8814288414eaad1f472fe8",
|
||||
},
|
||||
|
||||
{
|
||||
name: "escaped content",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "\"You say yes.\"\\n\\t\"I say no.\"",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithContent("\"You say yes.\"\\n\\t\"I say no.\""),
|
||||
),
|
||||
expected: "343de133996a766bf00561945b6f2b2717d4905275976ca75c1d7096b7d1900c",
|
||||
},
|
||||
|
||||
{
|
||||
name: "json content",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "{\"field\": [\"value\",\"value\"],\"numeral\": 123}",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithContent("{\"field\": [\"value\",\"value\"],\"numeral\": 123}"),
|
||||
),
|
||||
expected: "c6140190453ee947efb790e70541a9d37c41604d1f29e4185da4325621ed5270",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty tag",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{
|
||||
{"a", ""},
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithTag(Tag{"a", ""}),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "7d3e394c75916362436f11c603b1a89b40b50817550cfe522a90d769655007a4",
|
||||
},
|
||||
|
||||
{
|
||||
name: "single tag",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{
|
||||
{"a", "value"},
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithTag(Tag{"a", "value"}),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "7db394e274fb893edbd9f4aa9ff189d4f3264bf1a29cef8f614e83ebf6fa19fe",
|
||||
},
|
||||
|
||||
{
|
||||
name: "optional tag values",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{
|
||||
{"a", "value", "optional"},
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithTag(Tag{"a", "value", "optional"}),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "656b47884200959e0c03054292c453cfc4beea00b592d92c0f557bff765e9d34",
|
||||
},
|
||||
|
||||
{
|
||||
name: "multiple tags",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{
|
||||
{"a", "value", "optional"},
|
||||
{"b", "another"},
|
||||
{"c", "data"},
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithTag(Tag{"a", "value", "optional"}),
|
||||
WithTag(Tag{"b", "another"}),
|
||||
WithTag(Tag{"c", "data"}),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "f7c27f2eacda7ece5123a4f82db56145ba59f7c9e6c5eeb88552763664506b06",
|
||||
},
|
||||
|
||||
{
|
||||
name: "unicode tag",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 1,
|
||||
Tags: []Tag{
|
||||
{"a", "😀"},
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(1),
|
||||
WithTag(Tag{"a", "😀"}),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "fd2798d165d9bf46acbe817735dc8cedacd4c42dfd9380792487d4902539e986",
|
||||
},
|
||||
|
||||
{
|
||||
name: "zero timestamp",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: 0,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(0),
|
||||
WithKind(1),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "9ca742f2e2eea72ad6e0277a6287e2bb16a3e47d64b8468bc98474e266cf0ec2",
|
||||
},
|
||||
|
||||
{
|
||||
name: "negative timestamp",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: -1760740551,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(-1760740551),
|
||||
WithKind(1),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "4740b027040bb4d0ee8e885f567a80277097da70cddd143d8a6dadf97f6faaa3",
|
||||
},
|
||||
|
||||
{
|
||||
name: "max int64 timestamp",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: 9223372036854775807,
|
||||
Kind: 1,
|
||||
Tags: []Tag{},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(9223372036854775807),
|
||||
WithKind(1),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "b28cdd44496acb49e36c25859f0f819122829a12dc57c07612d5f44cb121d2a7",
|
||||
},
|
||||
|
||||
{
|
||||
name: "different kind",
|
||||
event: Event{
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: 20021,
|
||||
Tags: []Tag{},
|
||||
Content: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(20021),
|
||||
WithContent(""),
|
||||
),
|
||||
expected: "995c4894c264e6b9558cb94b7b34008768d53801b99960b47298d4e3e23fadd3",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package events
|
||||
|
||||
func intPtr(i int) *int {
|
||||
return &i
|
||||
}
|
||||
+133
-140
@@ -14,169 +14,161 @@ type ValidateEventTestCase struct {
|
||||
var structureTestCases = []ValidateEventTestCase{
|
||||
{
|
||||
name: "empty pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(""),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "short pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "abc123",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("abc123"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "long pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48adabc",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48adabc"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "non-hex pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "zyx-!2e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("zyx-!2e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "uppercase pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "C7A702E6158744CA03508BBB4C90F9DBB0D6E88FEFBFAA511D5AB24B4E3C48AD",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("C7A702E6158744CA03508BBB4C90F9DBB0D6E88FEFBFAA511D5AB24B4E3C48AD"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty id",
|
||||
event: Event{
|
||||
ID: "",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(""),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "id must be 64 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "short id",
|
||||
event: Event{
|
||||
ID: "abc123",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID("abc123"),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "id must be 64 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty signature",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(""),
|
||||
),
|
||||
expectedError: "signature must be 128 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "short signature",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: "abc123",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig("abc123"),
|
||||
),
|
||||
expectedError: "signature must be 128 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty tag",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{{}},
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "tags must contain at least two elements",
|
||||
},
|
||||
|
||||
{
|
||||
name: "single element tag",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{{"a"}},
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a"}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "tags must contain at least two elements",
|
||||
},
|
||||
|
||||
{
|
||||
name: "one good tag, one single element tag",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{{"a", "value"}, {"b"}},
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a", "value"}),
|
||||
WithTag(Tag{"b"}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "tags must contain at least two elements",
|
||||
},
|
||||
}
|
||||
@@ -191,37 +183,36 @@ func TestValidateEventStructure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateEventIDFailure(t *testing.T) {
|
||||
event := Event{
|
||||
ID: "7f661c2a3c1ed67dc959d6cd968d743d5e6e334313df44724bca939e2aa42c9e",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
}
|
||||
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 := Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
Sig: testEvent.Sig,
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithSig(testEvent.Sig),
|
||||
)
|
||||
err := ValidateSignature(event)
|
||||
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateInvalidSignature(t *testing.T) {
|
||||
event := Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
Sig: "9e43cbcf7e828a21c53fa35371ee79bffbfd7a3063ae46fc05ec623dd3186667c57e3d006488015e19247df35eb41c61013e051aa87860e23fa5ffbd44120482",
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithSig("9e43cbcf7e828a21c53fa35371ee79bffbfd7a3063ae46fc05ec623dd3186667c57e3d006488015e19247df35eb41c61013e051aa87860e23fa5ffbd44120482"),
|
||||
)
|
||||
err := ValidateSignature(event)
|
||||
|
||||
assert.ErrorContains(t, err, "event signature is invalid")
|
||||
@@ -280,7 +271,11 @@ var validateSignatureTestCases = []ValidateSignatureTestCase{
|
||||
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}
|
||||
event := NewEvent(
|
||||
WithID(tc.id),
|
||||
WithPubKey(tc.pubkey),
|
||||
WithSig(tc.sig),
|
||||
)
|
||||
err := ValidateSignature(event)
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
})
|
||||
@@ -288,18 +283,16 @@ func TestValidateSignatureInvalidEventSignature(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateEvent(t *testing.T) {
|
||||
event := Event{
|
||||
ID: "c9a0f84fcaa889654da8992105eb122eb210c8cbd58210609a5ef7e170b51400",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{
|
||||
{"a", "value"},
|
||||
{"b", "value", "optional"},
|
||||
},
|
||||
Content: "valid event",
|
||||
Sig: "668a715f1eb983172acf230d17bd283daedb2598adf8de4290bcc7eb0b802fdb60669d1e7d1104ac70393f4dbccd07e8abf897152af6ce6c0a75499874e27f14",
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID("c9a0f84fcaa889654da8992105eb122eb210c8cbd58210609a5ef7e170b51400"),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a", "value"}),
|
||||
WithTag(Tag{"b", "value", "optional"}),
|
||||
WithContent("valid event"),
|
||||
WithSig("668a715f1eb983172acf230d17bd283daedb2598adf8de4290bcc7eb0b802fdb60669d1e7d1104ac70393f4dbccd07e8abf897152af6ce6c0a75499874e27f14"),
|
||||
)
|
||||
|
||||
err := Validate(event)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user