From 9549c3624a301bc7f5eeb8f4ff60adf3bd7d59ea Mon Sep 17 00:00:00 2001 From: BitcoinCoderBob <90647227+BitcoinCoderBob@users.noreply.github.com> Date: Wed, 12 Oct 2022 16:24:30 -0400 Subject: [PATCH] nostr package, readme updates accordingly, matching example program (#14) --- README.md | 20 +++--- example/example.go | 91 ++++++++++++++++++++++++ connection.go => nostr/connection.go | 0 event.go => nostr/event.go | 0 event_aux.go => nostr/event_aux.go | 0 event_test.go => nostr/event_test.go | 0 filter.go => nostr/filter.go | 0 filter_aux.go => nostr/filter_aux.go | 0 filter_test.go => nostr/filter_test.go | 0 helpers.go => nostr/helpers.go | 0 keys.go => nostr/keys.go | 0 normalize.go => nostr/normalize.go | 0 relaypool.go => nostr/relaypool.go | 0 subscription.go => nostr/subscription.go | 0 14 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 example/example.go rename connection.go => nostr/connection.go (100%) rename event.go => nostr/event.go (100%) rename event_aux.go => nostr/event_aux.go (100%) rename event_test.go => nostr/event_test.go (100%) rename filter.go => nostr/filter.go (100%) rename filter_aux.go => nostr/filter_aux.go (100%) rename filter_test.go => nostr/filter_test.go (100%) rename helpers.go => nostr/helpers.go (100%) rename keys.go => nostr/keys.go (100%) rename normalize.go => nostr/normalize.go (100%) rename relaypool.go => nostr/relaypool.go (100%) rename subscription.go => nostr/subscription.go (100%) diff --git a/README.md b/README.md index d92fbba..1e18366 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,8 @@ A set of useful things for [Nostr Protocol](https://github.com/fiatjaf/nostr) im ```go pool := nostr.NewRelayPool() -pool.Add("wss://relay.nostr.com/", &nostr.RelayPoolPolicy{ - SimplePolicy: nostr.SimplePolicy{Read: true, Write: true}, -}) -pool.Add("wss://nostrrelay.example.com/", &nostr.RelayPoolPolicy{ - SimplePolicy: nostr.SimplePolicy{Read: true, Write: true}, -}) +pool.Add("wss://relay.nostr.com/", nostr.SimplePolicy{Read: true, Write: true}) +pool.Add("wss://nostrrelay.example.com/", nostr.SimplePolicy{Read: true, Write: true}) for notice := range pool.Notices { log.Printf("%s has sent a notice: '%s'\n", notice.Relay, notice.Message) @@ -28,10 +24,10 @@ for notice := range pool.Notices { ### Listening for events ```go -sub := pool.Sub(nostr.EventFilters{ +sub := pool.Sub(nostr.Filters{ { Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"}, - Kinds: []string{nostr.KindTextNote}, // or {1} + Kinds: []int{nostr.KindTextNote}, // or {1} }, }) @@ -53,7 +49,7 @@ secretKey := "3f06a81e0a0c2ad34ee9df2a30d87a810da9e3c3881f780755ace5e5e64d30a7" pool.SecretKey = &secretKey event, statuses, _ := pool.PublishEvent(&nostr.Event{ - CreatedAt: uint32(time.Now().Unix()), + CreatedAt: time.Now(), Kind: nostr.KindTextNote, Tags: make(nostr.Tags, 0), Content: "hello", @@ -83,3 +79,9 @@ sk, _ := nostr.GenerateKey() fmt.Println("sk:", sk) fmt.Println("pk:", nostr.GetPublicKey(sk)) ``` + +### Example Program + +``` +go run example/example.go +``` diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..6a2821f --- /dev/null +++ b/example/example.go @@ -0,0 +1,91 @@ +package main + +import ( + "fmt" + "time" + + "github.com/fiatjaf/go-nostr/nostr" +) + +// some nostr relay in the wild +var relayURL = "wss://nostr-relay.wlvs.space" + +func main() { + // create key pair + secretKey := nostr.GeneratePrivateKey() + publicKey, err := nostr.GetPublicKey(secretKey) + if err != nil { + fmt.Printf("error with GetPublicKey(): %s\n", err) + return + } + fmt.Printf("Our Pubkey: %s\n\n", publicKey) + fmt.Printf("Lets send and receive 4 events...\n\n") + + // subscribe to relay + pool := nostr.NewRelayPool() + pool.SecretKey = &secretKey + + // add a nostr relay to our pool + err = pool.Add(relayURL, nostr.SimplePolicy{Read: true, Write: true}) + if err != nil { + fmt.Printf("error calling Add(): %s\n", err.Error()) + } + + // subscribe to relays in our pool, with filtering + sub := pool.Sub(nostr.Filters{ + { + Authors: []string{publicKey}, + Kinds: []int{nostr.KindTextNote}, + }, + }) + + // listen for events from our subscriptions + go func() { + for event := range sub.UniqueEvents { + fmt.Printf("Received Event: %+v\n\n", event) + } + }() + + // create and publish events + go func() { + for { + content := fmt.Sprintf("henlo world at time: %s", time.Now().String()) + event, statuses, err := pool.PublishEvent(&nostr.Event{ + CreatedAt: time.Now(), + Kind: nostr.KindTextNote, + Tags: make(nostr.Tags, 0), + Content: content, + }) + if err != nil { + fmt.Printf("error calling PublishEvent(): %s\n", err.Error()) + } + + StatusProcess(event, statuses) + // sleep between publishing events + time.Sleep(time.Second * 5) + } + }() + + // after 20 seconds, unsubscribe from our pool and terminate program + time.Sleep(20 * time.Second) + fmt.Println("unsubscribing from nostr subscription") + sub.Unsub() + +} + +// handle events from out publish events +func StatusProcess(event *nostr.Event, statuses chan nostr.PublishStatus) { + for status := range statuses { + switch status.Status { + case nostr.PublishStatusSent: + fmt.Printf("Sent event with id %s to '%s'.\n", event.ID, status.Relay) + return + case nostr.PublishStatusFailed: + fmt.Printf("Failed to send event with id %s to '%s'.\n", event.ID, status.Relay) + return + case nostr.PublishStatusSucceeded: + fmt.Printf("Event with id %s seen on '%s'.\n", event.ID, status.Relay) + return + } + } +} diff --git a/connection.go b/nostr/connection.go similarity index 100% rename from connection.go rename to nostr/connection.go diff --git a/event.go b/nostr/event.go similarity index 100% rename from event.go rename to nostr/event.go diff --git a/event_aux.go b/nostr/event_aux.go similarity index 100% rename from event_aux.go rename to nostr/event_aux.go diff --git a/event_test.go b/nostr/event_test.go similarity index 100% rename from event_test.go rename to nostr/event_test.go diff --git a/filter.go b/nostr/filter.go similarity index 100% rename from filter.go rename to nostr/filter.go diff --git a/filter_aux.go b/nostr/filter_aux.go similarity index 100% rename from filter_aux.go rename to nostr/filter_aux.go diff --git a/filter_test.go b/nostr/filter_test.go similarity index 100% rename from filter_test.go rename to nostr/filter_test.go diff --git a/helpers.go b/nostr/helpers.go similarity index 100% rename from helpers.go rename to nostr/helpers.go diff --git a/keys.go b/nostr/keys.go similarity index 100% rename from keys.go rename to nostr/keys.go diff --git a/normalize.go b/nostr/normalize.go similarity index 100% rename from normalize.go rename to nostr/normalize.go diff --git a/relaypool.go b/nostr/relaypool.go similarity index 100% rename from relaypool.go rename to nostr/relaypool.go diff --git a/subscription.go b/nostr/subscription.go similarity index 100% rename from subscription.go rename to nostr/subscription.go