summaryrefslogtreecommitdiffstats
path: root/examples/basic/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'examples/basic/main.go')
-rw-r--r--examples/basic/main.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/examples/basic/main.go b/examples/basic/main.go
new file mode 100644
index 0000000..4f2d10d
--- /dev/null
+++ b/examples/basic/main.go
@@ -0,0 +1,78 @@
1package main
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "time"
8
9 "northwest.io/nostr-grpc/internal/nostr"
10)
11
12// Example_basic demonstrates basic usage of the nostr library.
13func main() {
14 // Generate a new key pair
15 key, err := nostr.GenerateKey()
16 if err != nil {
17 fmt.Printf("Failed to generate key: %v\n", err)
18 os.Exit(1)
19 }
20
21 fmt.Printf("Public key (hex): %s...\n", key.Public()[:16])
22 fmt.Printf("Public key (npub): %s...\n", key.Npub()[:20])
23
24 // Create an event
25 event := &nostr.Event{
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 os.Exit(1)
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 fmt.Println("connecting to relay...")
53 ExampleRelay()
54}
55
56func ExampleRelay() {
57 ctx := context.Background()
58
59 // Connect to a public relay
60 relay, err := nostr.Connect(ctx, "wss://relay.damus.io")
61 if err != nil {
62 fmt.Printf("Failed to connect: %v\n", err)
63 return
64 }
65 defer relay.Close()
66 fmt.Println("Connected to relay!")
67
68 ctx, cancel := context.WithTimeout(ctx, 25*time.Second)
69 defer cancel()
70
71 filter := nostr.Filter{
72 Kinds: []int{nostr.KindTextNote},
73 Limit: 5,
74 }
75 for event := range relay.Fetch(ctx, filter).Events {
76 fmt.Printf("Received event from %s...\n", event)
77 }
78}