Skip to content

Commit 5bb7ee4

Browse files
committed
refac: add the example that publishs event to relayer
1 parent 3ac0fc9 commit 5bb7ee4

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

event_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ func BenchmarkIDCheck(b *testing.B) {
107107
evt.Sign(GeneratePrivateKey())
108108

109109
b.Run("naïve", func(b *testing.B) {
110-
for b.Loop() {
110+
for i := 0; i < b.N; i++ {
111111
_ = evt.GetID() == evt.ID
112112
}
113113
})
114114

115115
b.Run("big brain", func(b *testing.B) {
116-
for b.Loop() {
116+
for i := 0; i < b.N; i++ {
117117
_ = evt.CheckID()
118118
}
119119
})

example/publish/publish.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/nbd-wtf/go-nostr"
8+
)
9+
10+
func main() {
11+
sk := nostr.GeneratePrivateKey()
12+
pub, _ := nostr.GetPublicKey(sk)
13+
ev := nostr.Event{
14+
PubKey: pub,
15+
CreatedAt: nostr.Now(),
16+
Kind: nostr.KindTextNote,
17+
Tags: nil,
18+
Content: "Hello Causality Graph!",
19+
}
20+
21+
// calling Sign sets the event ID field and the event Sig field
22+
ev.Sign(sk)
23+
24+
// publish the event to self relays
25+
ctx := context.Background()
26+
for _, url := range []string{"ws://161.97.129.166:10547"} {
27+
relay, err := nostr.RelayConnect(ctx, url)
28+
if err != nil {
29+
fmt.Println(err)
30+
continue
31+
}
32+
if err := relay.Publish(ctx, ev); err != nil {
33+
fmt.Println(err)
34+
continue
35+
}
36+
37+
fmt.Printf("published to %s\n", url)
38+
}
39+
}

0 commit comments

Comments
 (0)