From 581ceecbf046f99b39885c74e2780a5320e5b15e Mon Sep 17 00:00:00 2001 From: bndw Date: Fri, 13 Feb 2026 17:35:32 -0800 Subject: feat: add Nostr protocol implementation (internal/nostr, internal/websocket) --- internal/nostr/example_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 internal/nostr/example_test.go (limited to 'internal/nostr/example_test.go') 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 @@ +package nostr_test + +import ( + "context" + "fmt" + "time" + + "northwest.io/nostr-grpc/internal/nostr" +) + +// Example_basic demonstrates basic usage of the nostr library. +func Example_basic() { + // Generate a new key pair + key, err := nostr.GenerateKey() + if err != nil { + fmt.Printf("Failed to generate key: %v\n", err) + return + } + + fmt.Printf("Public key (hex): %s...\n", key.Public()[:16]) + fmt.Printf("Public key (npub): %s...\n", key.Npub()[:20]) + + // Create an event + event := &nostr.Event{ + CreatedAt: time.Now().Unix(), + Kind: nostr.KindTextNote, + Tags: nostr.Tags{{"t", "test"}}, + Content: "Hello from nostr-go!", + } + + // Sign the event + if err := key.Sign(event); err != nil { + fmt.Printf("Failed to sign event: %v\n", err) + return + } + + // Verify the signature + if event.Verify() { + fmt.Println("Event signature verified!") + } + + // Create a filter to match our event + filter := nostr.Filter{ + Kinds: []int{nostr.KindTextNote}, + Authors: []string{key.Public()[:8]}, // Prefix matching + } + + if filter.Matches(event) { + fmt.Println("Filter matches the event!") + } +} + +// ExampleRelay demonstrates connecting to a relay (requires network). +// This is a documentation example - run with: go test -v -run ExampleRelay +func ExampleRelay() { + ctx := context.Background() + + // Connect to a public relay + relay, err := nostr.Connect(ctx, "wss://relay.damus.io") + if err != nil { + fmt.Printf("Failed to connect: %v\n", err) + return + } + defer relay.Close() + + fmt.Println("Connected to relay!") + + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + // Fetch recent text notes (closes on EOSE) + since := time.Now().Add(-1 * time.Hour).Unix() + sub := relay.Fetch(ctx, nostr.Filter{ + Kinds: []int{nostr.KindTextNote}, + Since: &since, + Limit: 5, + }) + + eventCount := 0 + for event := range sub.Events { + eventCount++ + fmt.Printf("Received event from %s...\n", event.PubKey[:8]) + } + fmt.Printf("Received %d events\n", eventCount) +} -- cgit v1.2.3