diff options
Diffstat (limited to 'example_test.go')
| -rw-r--r-- | example_test.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..90dae0f --- /dev/null +++ b/example_test.go | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | package nostr_test | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "fmt" | ||
| 6 | "time" | ||
| 7 | |||
| 8 | "northwest.io/nostr" | ||
| 9 | ) | ||
| 10 | |||
| 11 | // Example_basic demonstrates basic usage of the nostr library. | ||
| 12 | func 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 | ||
| 55 | func ExampleRelay() { | ||
| 56 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| 57 | defer cancel() | ||
| 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 | |||
| 67 | fmt.Println("Connected to relay!") | ||
| 68 | |||
| 69 | // Subscribe to recent text notes | ||
| 70 | since := time.Now().Add(-1 * time.Hour).Unix() | ||
| 71 | sub, err := relay.Subscribe(ctx, "my-sub", nostr.Filter{ | ||
| 72 | Kinds: []int{nostr.KindTextNote}, | ||
| 73 | Since: &since, | ||
| 74 | Limit: 5, | ||
| 75 | }) | ||
| 76 | if err != nil { | ||
| 77 | fmt.Printf("Failed to subscribe: %v\n", err) | ||
| 78 | return | ||
| 79 | } | ||
| 80 | |||
| 81 | // Listen for events in the background | ||
| 82 | go relay.Listen(ctx) | ||
| 83 | |||
| 84 | // Collect events until EOSE | ||
| 85 | eventCount := 0 | ||
| 86 | for { | ||
| 87 | select { | ||
| 88 | case event := <-sub.Events: | ||
| 89 | eventCount++ | ||
| 90 | fmt.Printf("Received event from %s...\n", event.PubKey[:8]) | ||
| 91 | case <-sub.EOSE: | ||
| 92 | fmt.Printf("Received %d events before EOSE\n", eventCount) | ||
| 93 | sub.Close(ctx) | ||
| 94 | return | ||
| 95 | case <-ctx.Done(): | ||
| 96 | fmt.Println("Timeout") | ||
| 97 | return | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
