From 92ce379649030ab52d9b0a93320d9e17da47a6f0 Mon Sep 17 00:00:00 2001 From: Dylan Cant Date: Fri, 13 Jan 2023 18:50:35 -0500 Subject: [PATCH] added example/example.go with subscription and publication code example --- README.md | 6 +++ example/example.go | 132 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 example/example.go diff --git a/README.md b/README.md index b2f6488..f16e44c 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,9 @@ sk, _ := nostr.GenerateKey() fmt.Println("sk:", sk) fmt.Println("pk:", nostr.GetPublicKey(sk)) ``` + +### Subscriptions + +``` +go run example/example.go +``` diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..aa416a1 --- /dev/null +++ b/example/example.go @@ -0,0 +1,132 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "os" + "strings" + "time" + + "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip19" +) + +func main() { + ctx := context.Background() + + // connect to relay + url := "wss://nostr.zebedee.cloud" + relay, err := nostr.RelayConnect(ctx, url) + if err != nil { + panic(err) + } + + reader := os.Stdin + var npub string + var b [64]byte + fmt.Fprintf(os.Stderr, "using %s\n----\nexample subscription for three most recent notes mentioning user\npaste npub key: ", url) + if n, err := reader.Read(b[:]); err == nil { + npub = strings.TrimSpace(fmt.Sprintf("%s", b[:n])) + } else { + panic(err) + } + + // create filters + var filters nostr.Filters + if _, v, err := nip19.Decode(npub); err == nil { + t := make(map[string][]string) + // making a "p" tag for the above public key. + // this filters for messages tagged with the user "npub1sg6plz..." (mainly replies to this user). + t["p"] = []string{v.(string)} + filters = []nostr.Filter{{ + Kinds: []int{1}, + Tags: t, + // limit = 3, get the three most recent notes + Limit: 3, + }} + } else { + panic("not a valid npub!") + } + + // create a subscription and submit to relay + // results will be returned on the sub.Events channel + sub := relay.Subscribe(ctx, filters) + + // we will append the returned events to this slice + evs := make([]nostr.Event, 0) + + go func() { + <-sub.EndOfStoredEvents + sub.Unsub() + }() + for ev := range sub.Events { + evs = append(evs, ev) + } + + filename := "example_output.json" + if f, err := os.Create(filename); err == nil { + fmt.Fprintf(os.Stderr, "returned events saved to %s\n", filename) + // encode the returned events in a file + enc := json.NewEncoder(f) + enc.SetIndent("", " ") + enc.Encode(evs) + f.Close() + } else { + panic(err) + } + + fmt.Fprintf(os.Stderr, "----\nexample publication of note.\npaste nsec key (leave empty to autogenerate): ") + var nsec string + if n, err := reader.Read(b[:]); err == nil { + nsec = strings.TrimSpace(fmt.Sprintf("%s", b[:n])) + } else { + panic(err) + } + + var sk string + ev := nostr.Event{} + if _, s, e := nip19.Decode(nsec); e == nil { + sk = s.(string) + } else { + sk = nostr.GeneratePrivateKey() + } + if pub, e := nostr.GetPublicKey(sk); e == nil { + ev.PubKey = pub + if npub, e := nip19.EncodePublicKey(pub); e == nil { + fmt.Fprintln(os.Stderr, "using:", npub) + } + } else { + panic(e) + } + + ev.CreatedAt = time.Now() + ev.Kind = 1 + var content string + fmt.Fprintln(os.Stderr, "enter content of note, ending with an empty newline:") + for { + if n, err := reader.Read(b[:]); err == nil { + new_line := strings.TrimSpace(fmt.Sprintf("%s", b[:n])) + if new_line == "" { + break + } else if content == "" { + content = new_line + } else { + content = fmt.Sprintf("%s\n%s", content, new_line) + } + } else { + panic(err) + } + } + ev.Content = content + ev.Sign(sk) + for _, url := range []string{"wss://nostr.zebedee.cloud"} { + ctx := context.WithValue(context.Background(), "url", url) + relay, e := nostr.RelayConnect(ctx, url) + if e != nil { + fmt.Println(e) + continue + } + fmt.Println("posting to: ", url, relay.Publish(ctx, ev)) + } +}