summaryrefslogtreecommitdiffstats
path: root/internal/nostr/example_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/nostr/example_test.go')
-rw-r--r--internal/nostr/example_test.go85
1 files changed, 0 insertions, 85 deletions
diff --git a/internal/nostr/example_test.go b/internal/nostr/example_test.go
deleted file mode 100644
index 7031c65..0000000
--- a/internal/nostr/example_test.go
+++ /dev/null
@@ -1,85 +0,0 @@
1package nostr_test
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "northwest.io/muxstr/internal/nostr"
9)
10
11// Example_basic demonstrates basic usage of the nostr library.
12func Example_basic() {
13 // Generate a new key pair
14 key, err := nostr.GenerateKey()
15 if err != nil {
16 fmt.Printf("Failed to generate key: %v\n", err)
17 return
18 }
19
20 fmt.Printf("Public key (hex): %s...\n", key.Public()[:16])
21 fmt.Printf("Public key (npub): %s...\n", key.Npub()[:20])
22
23 // Create an event
24 event := &nostr.Event{
25 CreatedAt: time.Now().Unix(),
26 Kind: nostr.KindTextNote,
27 Tags: nostr.Tags{{"t", "test"}},
28 Content: "Hello from nostr-go!",
29 }
30
31 // Sign the event
32 if err := key.Sign(event); err != nil {
33 fmt.Printf("Failed to sign event: %v\n", err)
34 return
35 }
36
37 // Verify the signature
38 if event.Verify() {
39 fmt.Println("Event signature verified!")
40 }
41
42 // Create a filter to match our event
43 filter := nostr.Filter{
44 Kinds: []int{nostr.KindTextNote},
45 Authors: []string{key.Public()[:8]}, // Prefix matching
46 }
47
48 if filter.Matches(event) {
49 fmt.Println("Filter matches the event!")
50 }
51}
52
53// ExampleRelay demonstrates connecting to a relay (requires network).
54// This is a documentation example - run with: go test -v -run ExampleRelay
55func ExampleRelay() {
56 ctx := context.Background()
57
58 // Connect to a public relay
59 relay, err := nostr.Connect(ctx, "wss://relay.damus.io")
60 if err != nil {
61 fmt.Printf("Failed to connect: %v\n", err)
62 return
63 }
64 defer relay.Close()
65
66 fmt.Println("Connected to relay!")
67
68 ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
69 defer cancel()
70
71 // Fetch recent text notes (closes on EOSE)
72 since := time.Now().Add(-1 * time.Hour).Unix()
73 sub := relay.Fetch(ctx, nostr.Filter{
74 Kinds: []int{nostr.KindTextNote},
75 Since: &since,
76 Limit: 5,
77 })
78
79 eventCount := 0
80 for event := range sub.Events {
81 eventCount++
82 fmt.Printf("Received event from %s...\n", event.PubKey[:8])
83 }
84 fmt.Printf("Received %d events\n", eventCount)
85}