2022-10-07 18:51:36 -07:00
|
|
|
<a href="https://nbd.wtf"><img align="right" height="196" src="https://user-images.githubusercontent.com/1653275/194609043-0add674b-dd40-41ed-986c-ab4a2e053092.png" /></a>
|
|
|
|
|
2021-12-16 20:47:53 -03:00
|
|
|
go-nostr
|
|
|
|
========
|
|
|
|
|
2022-11-04 08:24:32 -03:00
|
|
|
A set of useful things for [Nostr Protocol](https://github.com/nostr-protocol/nostr) implementations.
|
2021-12-16 20:47:53 -03:00
|
|
|
|
2022-11-04 08:24:32 -03:00
|
|
|
<a href="https://godoc.org/github.com/nbd-wtf/go-nostr"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
|
2021-12-16 20:47:53 -03:00
|
|
|
|
|
|
|
|
|
|
|
### Subscribing to a set of relays
|
|
|
|
|
|
|
|
```go
|
2022-01-02 08:44:18 -03:00
|
|
|
pool := nostr.NewRelayPool()
|
2021-12-16 20:47:53 -03:00
|
|
|
|
2022-10-12 16:24:30 -04:00
|
|
|
pool.Add("wss://relay.nostr.com/", nostr.SimplePolicy{Read: true, Write: true})
|
|
|
|
pool.Add("wss://nostrrelay.example.com/", nostr.SimplePolicy{Read: true, Write: true})
|
2021-12-16 20:47:53 -03:00
|
|
|
|
|
|
|
for notice := range pool.Notices {
|
|
|
|
log.Printf("%s has sent a notice: '%s'\n", notice.Relay, notice.Message)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Listening for events
|
|
|
|
|
|
|
|
```go
|
2022-11-13 19:33:48 -03:00
|
|
|
subId, allEvents, uniqueEvents := pool.Sub(nostr.Filters{
|
2021-12-16 20:47:53 -03:00
|
|
|
{
|
|
|
|
Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"},
|
2022-10-12 16:24:30 -04:00
|
|
|
Kinds: []int{nostr.KindTextNote}, // or {1}
|
2021-12-16 20:47:53 -03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
2022-11-13 19:33:48 -03:00
|
|
|
for event := range uniqueEvents {
|
2021-12-16 20:47:53 -03:00
|
|
|
log.Print(event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
sub.Unsub()
|
|
|
|
```
|
|
|
|
|
|
|
|
### Publishing an event
|
|
|
|
|
|
|
|
```go
|
|
|
|
secretKey := "3f06a81e0a0c2ad34ee9df2a30d87a810da9e3c3881f780755ace5e5e64d30a7"
|
|
|
|
|
|
|
|
pool.SecretKey = &secretKey
|
|
|
|
|
2022-01-02 08:44:18 -03:00
|
|
|
event, statuses, _ := pool.PublishEvent(&nostr.Event{
|
2022-10-12 16:24:30 -04:00
|
|
|
CreatedAt: time.Now(),
|
2022-01-02 08:44:18 -03:00
|
|
|
Kind: nostr.KindTextNote,
|
|
|
|
Tags: make(nostr.Tags, 0),
|
2021-12-16 20:47:53 -03:00
|
|
|
Content: "hello",
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Print(event.PubKey)
|
|
|
|
log.Print(event.ID)
|
|
|
|
log.Print(event.Sig)
|
|
|
|
|
|
|
|
for status := range statuses {
|
|
|
|
switch status.Status {
|
2022-01-02 08:44:18 -03:00
|
|
|
case nostr.PublishStatusSent:
|
2021-12-16 20:47:53 -03:00
|
|
|
fmt.Printf("Sent event %s to '%s'.\n", event.ID, status.Relay)
|
2022-01-02 08:44:18 -03:00
|
|
|
case nostr.PublishStatusFailed:
|
2021-12-16 20:47:53 -03:00
|
|
|
fmt.Printf("Failed to send event %s to '%s'.\n", event.ID, status.Relay)
|
2022-01-02 08:44:18 -03:00
|
|
|
case nostr.PublishStatusSucceeded:
|
2021-12-16 20:47:53 -03:00
|
|
|
fmt.Printf("Event seen %s on '%s'.\n", event.ID, status.Relay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2022-01-05 10:16:36 -04:00
|
|
|
|
|
|
|
### Generating a key
|
|
|
|
|
|
|
|
``` go
|
|
|
|
sk, _ := nostr.GenerateKey()
|
|
|
|
|
2022-01-23 15:23:53 -03:00
|
|
|
fmt.Println("sk:", sk)
|
|
|
|
fmt.Println("pk:", nostr.GetPublicKey(sk))
|
2022-01-05 10:16:36 -04:00
|
|
|
```
|
2022-10-12 16:24:30 -04:00
|
|
|
|
|
|
|
### Example Program
|
|
|
|
|
|
|
|
```
|
|
|
|
go run example/example.go
|
|
|
|
```
|