summaryrefslogtreecommitdiffstats
path: root/internal/nostr/example_test.go
diff options
context:
space:
mode:
authorbndw <ben@bdw.to>2026-02-13 17:35:32 -0800
committerbndw <ben@bdw.to>2026-02-13 17:35:32 -0800
commit581ceecbf046f99b39885c74e2780a5320e5b15e (patch)
treec82dcaddb4f555d5051684221881e36f7fe3f718 /internal/nostr/example_test.go
parent06b9b13274825f797523935494a1b5225f0e0862 (diff)
feat: add Nostr protocol implementation (internal/nostr, internal/websocket)
Diffstat (limited to 'internal/nostr/example_test.go')
-rw-r--r--internal/nostr/example_test.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/internal/nostr/example_test.go b/internal/nostr/example_test.go
new file mode 100644
index 0000000..80acd21
--- /dev/null
+++ b/internal/nostr/example_test.go
@@ -0,0 +1,85 @@
1package nostr_test
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "northwest.io/nostr-grpc/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}