From 0f66883dc7286b317faf941d1732a68882c38d2f Mon Sep 17 00:00:00 2001 From: Isaque Veras Date: Wed, 6 Sep 2023 21:00:05 -0300 Subject: [PATCH] feat(kind): using constants --- count_test.go | 2 +- event_test.go | 4 ++-- example/example.go | 4 ++-- filter_test.go | 18 +++++++++--------- nip13/nip13_test.go | 10 +++++----- nip19/nip19_test.go | 6 +++--- nip26/nip26_test.go | 2 +- nip42/nip42.go | 4 ++-- relay.go | 2 +- relay_test.go | 6 +++--- sdk/relays.go | 9 ++++++--- subscription_test.go | 6 +++--- 12 files changed, 38 insertions(+), 35 deletions(-) diff --git a/count_test.go b/count_test.go index 97bc418..6784e60 100644 --- a/count_test.go +++ b/count_test.go @@ -12,7 +12,7 @@ func TestCount(t *testing.T) { defer rl.Close() count, err := rl.Count(context.Background(), Filters{ - {Kinds: []int{3}, Tags: TagMap{"p": []string{"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}}}, + {Kinds: []int{KindContactList}, Tags: TagMap{"p": []string{"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}}}, }) if err != nil { t.Errorf("count request failed: %v", err) diff --git a/event_test.go b/event_test.go index 13676ef..273dcd1 100644 --- a/event_test.go +++ b/event_test.go @@ -44,7 +44,7 @@ func TestEventSerialization(t *testing.T) { { ID: "92570b321da503eac8014b23447301eb3d0bbdfbace0d11a4e4072e72bb7205d", PubKey: "e9142f724955c5854de36324dab0434f97b15ec6b33464d56ebe491e3f559d1b", - Kind: 4, + Kind: KindEncryptedDirectMessage, CreatedAt: Timestamp(1671028682), Tags: Tags{Tag{"p", "f8340b2bde651576b75af61aa26c80e13c65029f00f7f64004eece679bf7059f"}}, Content: "you say yes, I say no", @@ -90,7 +90,7 @@ func TestEventSerializationWithExtraFields(t *testing.T) { evt := Event{ ID: "92570b321da503eac8014b23447301eb3d0bbdfbace0d11a4e4072e72bb7205d", PubKey: "e9142f724955c5854de36324dab0434f97b15ec6b33464d56ebe491e3f559d1b", - Kind: 7, + Kind: KindReaction, CreatedAt: Timestamp(1671028682), Content: "there is an extra field here", Sig: "ed08d2dd5b0f7b6a3cdc74643d4adee3158ddede9cc848e8cd97630c097001acc2d052d2d3ec2b7ac4708b2314b797106d1b3c107322e61b5e5cc2116e099b79", diff --git a/example/example.go b/example/example.go index cfc6de4..55f0786 100644 --- a/example/example.go +++ b/example/example.go @@ -41,7 +41,7 @@ func main() { // this filters for messages tagged with the user, mainly replies. t["p"] = []string{v.(string)} filters = []nostr.Filter{{ - Kinds: []int{1}, + Kinds: []int{nostr.KindTextNote}, Tags: t, // limit = 3, get the three most recent notes Limit: 3, @@ -102,7 +102,7 @@ func main() { } ev.CreatedAt = nostr.Now() - ev.Kind = 1 + ev.Kind = nostr.KindTextNote var content string fmt.Fprintln(os.Stderr, "enter content of note, ending with an empty newline (ctrl+d):") for { diff --git a/filter_test.go b/filter_test.go index bb778d9..e0b0b9e 100644 --- a/filter_test.go +++ b/filter_test.go @@ -25,7 +25,7 @@ func TestFilterUnmarshal(t *testing.T) { func TestFilterMarshal(t *testing.T) { until := Timestamp(12345678) filterj, err := json.Marshal(Filter{ - Kinds: []int{1, 2, 4}, + Kinds: []int{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage}, Tags: TagMap{"fruit": {"banana", "mango"}}, Until: &until, }) @@ -53,15 +53,15 @@ func TestFilterMatchingLive(t *testing.T) { func TestFilterEquality(t *testing.T) { if !FilterEqual( - Filter{Kinds: []int{4, 5}}, - Filter{Kinds: []int{4, 5}}, + Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}}, + Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}}, ) { t.Error("kinds filters should be equal") } if !FilterEqual( - Filter{Kinds: []int{4, 5}, Tags: TagMap{"letter": {"a", "b"}}}, - Filter{Kinds: []int{4, 5}, Tags: TagMap{"letter": {"b", "a"}}}, + Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"a", "b"}}}, + Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"b", "a"}}}, ) { t.Error("kind+tags filters should be equal") } @@ -69,13 +69,13 @@ func TestFilterEquality(t *testing.T) { tm := Now() if !FilterEqual( Filter{ - Kinds: []int{4, 5}, + Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}}, Since: &tm, IDs: []string{"aaaa", "bbbb"}, }, Filter{ - Kinds: []int{5, 4}, + Kinds: []int{KindDeletion, KindEncryptedDirectMessage}, Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}}, Since: &tm, IDs: []string{"aaaa", "bbbb"}, @@ -85,8 +85,8 @@ func TestFilterEquality(t *testing.T) { } if FilterEqual( - Filter{Kinds: []int{1, 4, 5}}, - Filter{Kinds: []int{4, 5, 6}}, + Filter{Kinds: []int{KindTextNote, KindEncryptedDirectMessage, KindDeletion}}, + Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion, KindRepost}}, ) { t.Error("kinds filters shouldn't be equal") } diff --git a/nip13/nip13_test.go b/nip13/nip13_test.go index b1e4409..93d6d94 100644 --- a/nip13/nip13_test.go +++ b/nip13/nip13_test.go @@ -33,7 +33,7 @@ func TestCheck(t *testing.T) { func TestGenerateShort(t *testing.T) { event := &nostr.Event{ - Kind: 1, + Kind: nostr.KindTextNote, Content: "It's just me mining my own business", PubKey: "a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243", } @@ -53,7 +53,7 @@ func TestGenerateLong(t *testing.T) { t.Run(fmt.Sprintf("%dbits", difficulty), func(t *testing.T) { t.Parallel() event := &nostr.Event{ - Kind: 1, + Kind: nostr.KindTextNote, Content: "It's just me mining my own business", PubKey: "a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243", } @@ -89,7 +89,7 @@ func testNonceTag(t *testing.T, event *nostr.Event, commitment int) { func TestGenerateTimeout(t *testing.T) { event := &nostr.Event{ - Kind: 1, + Kind: nostr.KindTextNote, Content: "It's just me mining my own business", PubKey: "a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243", } @@ -117,7 +117,7 @@ func BenchmarkCheck(b *testing.B) { func BenchmarkGenerateOneIteration(b *testing.B) { for i := 0; i < b.N; i++ { event := &nostr.Event{ - Kind: 1, + Kind: nostr.KindTextNote, Content: "It's just me mining my own business", PubKey: "a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243", } @@ -136,7 +136,7 @@ func BenchmarkGenerate(b *testing.B) { b.Run(fmt.Sprintf("%dbits", difficulty), func(b *testing.B) { for i := 0; i < b.N; i++ { event := &nostr.Event{ - Kind: 1, + Kind: nostr.KindTextNote, Content: "It's just me mining my own business", PubKey: "a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243", } diff --git a/nip19/nip19_test.go b/nip19/nip19_test.go index 438f281..6e21360 100644 --- a/nip19/nip19_test.go +++ b/nip19/nip19_test.go @@ -112,7 +112,7 @@ func TestEncodeNprofile(t *testing.T) { func TestEncodeDecodeNaddr(t *testing.T) { naddr, err := EncodeEntity( "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", - 30023, + nostr.KindArticle, "banana", []string{ "wss://relay.nostr.example.mydomain.example.com", @@ -136,7 +136,7 @@ func TestEncodeDecodeNaddr(t *testing.T) { if ep.PublicKey != "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" { t.Error("returned wrong pubkey") } - if ep.Kind != 30023 { + if ep.Kind != nostr.KindArticle { t.Error("returned wrong kind") } if ep.Identifier != "banana" { @@ -159,7 +159,7 @@ func TestDecodeNaddrWithoutRelays(t *testing.T) { if ep.PublicKey != "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194" { t.Error("returned wrong pubkey") } - if ep.Kind != 30023 { + if ep.Kind != nostr.KindArticle { t.Error("returned wrong kind") } if ep.Identifier != "references" { diff --git a/nip26/nip26_test.go b/nip26/nip26_test.go index 653e8c3..f7102a3 100644 --- a/nip26/nip26_test.go +++ b/nip26/nip26_test.go @@ -19,7 +19,7 @@ func TestDelegateSign(t *testing.T) { ev := &nostr.Event{} ev.CreatedAt = nostr.Timestamp(1600000050) ev.Content = "hello world" - ev.Kind = 1 + ev.Kind = nostr.KindTextNote if err != nil { t.Error(err) } diff --git a/nip42/nip42.go b/nip42/nip42.go index a430874..2c05dbc 100644 --- a/nip42/nip42.go +++ b/nip42/nip42.go @@ -14,7 +14,7 @@ func CreateUnsignedAuthEvent(challenge, pubkey, relayURL string) nostr.Event { return nostr.Event{ PubKey: pubkey, CreatedAt: nostr.Now(), - Kind: 22242, + Kind: nostr.KindClientAuthentication, Tags: nostr.Tags{ nostr.Tag{"relay", relayURL}, nostr.Tag{"challenge", challenge}, @@ -35,7 +35,7 @@ func parseURL(input string) (*url.URL, error) { // ValidateAuthEvent checks whether event is a valid NIP-42 event for given challenge and relayURL. // The result of the validation is encoded in the ok bool. func ValidateAuthEvent(event *nostr.Event, challenge string, relayURL string) (pubkey string, ok bool) { - if event.Kind != 22242 { + if event.Kind != nostr.KindClientAuthentication { return "", false } diff --git a/relay.go b/relay.go index c8d27ef..6f383ad 100644 --- a/relay.go +++ b/relay.go @@ -93,7 +93,7 @@ func NewRelay(ctx context.Context, url string, opts ...RelayOption) *Relay { for challenge := range r.challenges { authEvent := Event{ CreatedAt: Now(), - Kind: 22242, + Kind: KindClientAuthentication, Tags: Tags{ Tag{"relay", url}, Tag{"challenge", challenge}, diff --git a/relay_test.go b/relay_test.go index 8a1ef62..51b2269 100644 --- a/relay_test.go +++ b/relay_test.go @@ -19,7 +19,7 @@ func TestPublish(t *testing.T) { // test note to be sent over websocket priv, pub := makeKeyPair(t) textNote := Event{ - Kind: 1, + Kind: KindTextNote, Content: "hello", CreatedAt: Timestamp(1672068534), // random fixed timestamp Tags: Tags{[]string{"foo", "bar"}}, @@ -67,7 +67,7 @@ func TestPublish(t *testing.T) { func TestPublishBlocked(t *testing.T) { // test note to be sent over websocket - textNote := Event{Kind: 1, Content: "hello"} + textNote := Event{Kind: KindTextNote, Content: "hello"} textNote.ID = textNote.GetID() // fake relay server @@ -93,7 +93,7 @@ func TestPublishBlocked(t *testing.T) { func TestPublishWriteFailed(t *testing.T) { // test note to be sent over websocket - textNote := Event{Kind: 1, Content: "hello"} + textNote := Event{Kind: KindTextNote, Content: "hello"} textNote.ID = textNote.GetID() // fake relay server diff --git a/sdk/relays.go b/sdk/relays.go index 79aea01..e00547f 100644 --- a/sdk/relays.go +++ b/sdk/relays.go @@ -27,7 +27,10 @@ func FetchRelaysForPubkey(ctx context.Context, pool *nostr.SimplePool, pubkey st ch := pool.SubManyEose(ctx, relays, nostr.Filters{ { - Kinds: []int{10002, 3}, + Kinds: []int{ + nostr.KindRelayListMetadata, + nostr.KindContactList, + }, Authors: []string{pubkey}, Limit: 2, }, @@ -37,9 +40,9 @@ func FetchRelaysForPubkey(ctx context.Context, pool *nostr.SimplePool, pubkey st i := 0 for event := range ch { switch event.Kind { - case 10002: + case nostr.KindRelayListMetadata: result = append(result, ParseRelaysFromKind10002(event)...) - case 3: + case nostr.KindContactList: result = append(result, ParseRelaysFromKind3(event)...) } diff --git a/subscription_test.go b/subscription_test.go index f267ce9..ca8760e 100644 --- a/subscription_test.go +++ b/subscription_test.go @@ -15,7 +15,7 @@ func TestSubscribe(t *testing.T) { rl := mustRelayConnect(RELAY) defer rl.Close() - sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Limit: 2}}) + sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 2}}) if err != nil { t.Errorf("subscription failed: %v", err) return @@ -56,7 +56,7 @@ func TestNestedSubscriptions(t *testing.T) { n := atomic.Uint32{} // fetch 2 replies to a note - sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{1}, Tags: TagMap{"e": []string{"0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"}}, Limit: 3}}) + sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Tags: TagMap{"e": []string{"0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"}}, Limit: 3}}) if err != nil { t.Errorf("subscription 1 failed: %v", err) return @@ -66,7 +66,7 @@ func TestNestedSubscriptions(t *testing.T) { select { case event := <-sub.Events: // now fetch author of this - sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{0}, Authors: []string{event.PubKey}, Limit: 1}}) + sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindSetMetadata}, Authors: []string{event.PubKey}, Limit: 1}}) if err != nil { t.Errorf("subscription 2 failed: %v", err) return