From af30945803d440d1f803c814f4a37a1890494f1d Mon Sep 17 00:00:00 2001 From: bndw Date: Fri, 13 Feb 2026 17:35:33 -0800 Subject: docs: add design documentation and examples --- examples/basic/basic | Bin 0 -> 8724655 bytes examples/basic/main.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 examples/basic/basic create mode 100644 examples/basic/main.go (limited to 'examples/basic') diff --git a/examples/basic/basic b/examples/basic/basic new file mode 100755 index 0000000..4b25081 Binary files /dev/null and b/examples/basic/basic differ 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 @@ +package main + +import ( + "context" + "fmt" + "os" + "time" + + "northwest.io/nostr-grpc/internal/nostr" +) + +// Example_basic demonstrates basic usage of the nostr library. +func main() { + // Generate a new key pair + key, err := nostr.GenerateKey() + if err != nil { + fmt.Printf("Failed to generate key: %v\n", err) + os.Exit(1) + } + + 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{ + 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) + os.Exit(1) + } + + // 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!") + } + + fmt.Println("connecting to relay...") + 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, 25*time.Second) + defer cancel() + + filter := nostr.Filter{ + Kinds: []int{nostr.KindTextNote}, + Limit: 5, + } + for event := range relay.Fetch(ctx, filter).Events { + fmt.Printf("Received event from %s...\n", event) + } +} -- cgit v1.2.3