mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-05-02 23:00:14 +02:00
remove outdated docs. must add new ones later.
This commit is contained in:
parent
0397395261
commit
c64078e5cc
70
README.md
70
README.md
@ -7,70 +7,6 @@ A set of useful things for [Nostr Protocol](https://github.com/nostr-protocol/no
|
|||||||
|
|
||||||
<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>
|
<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>
|
||||||
|
|
||||||
|
|
||||||
### Subscribing to a set of relays
|
|
||||||
|
|
||||||
```go
|
|
||||||
pool := nostr.NewRelayPool()
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Listening for events
|
|
||||||
|
|
||||||
```go
|
|
||||||
subId, events, unsubscribe := pool.Sub(nostr.Filters{
|
|
||||||
{
|
|
||||||
Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"},
|
|
||||||
Kinds: []int{nostr.KindTextNote}, // or {1}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for event := range nostr.Unique(events) {
|
|
||||||
log.Print(event)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
unsubscribe()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Publishing an event
|
|
||||||
|
|
||||||
```go
|
|
||||||
secretKey := "3f06a81e0a0c2ad34ee9df2a30d87a810da9e3c3881f780755ace5e5e64d30a7"
|
|
||||||
|
|
||||||
pool.SecretKey = &secretKey
|
|
||||||
|
|
||||||
event, statuses, _ := pool.PublishEvent(&nostr.Event{
|
|
||||||
CreatedAt: time.Now(),
|
|
||||||
Kind: nostr.KindTextNote,
|
|
||||||
Tags: make(nostr.Tags, 0),
|
|
||||||
Content: "hello",
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Print(event.PubKey)
|
|
||||||
log.Print(event.ID)
|
|
||||||
log.Print(event.Sig)
|
|
||||||
|
|
||||||
for status := range statuses {
|
|
||||||
switch status.Status {
|
|
||||||
case nostr.PublishStatusSent:
|
|
||||||
fmt.Printf("Sent event %s to '%s'.\n", event.ID, status.Relay)
|
|
||||||
case nostr.PublishStatusFailed:
|
|
||||||
fmt.Printf("Failed to send event %s to '%s'.\n", event.ID, status.Relay)
|
|
||||||
case nostr.PublishStatusSucceeded:
|
|
||||||
fmt.Printf("Event seen %s on '%s'.\n", event.ID, status.Relay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Generating a key
|
### Generating a key
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
@ -79,9 +15,3 @@ sk, _ := nostr.GenerateKey()
|
|||||||
fmt.Println("sk:", sk)
|
fmt.Println("sk:", sk)
|
||||||
fmt.Println("pk:", nostr.GetPublicKey(sk))
|
fmt.Println("pk:", nostr.GetPublicKey(sk))
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example Program
|
|
||||||
|
|
||||||
```
|
|
||||||
go run example/example.go
|
|
||||||
```
|
|
@ -1,92 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
|
||||||
)
|
|
||||||
|
|
||||||
// some nostr relay in the wild
|
|
||||||
var relayURL = "wss://nostr.688.org"
|
|
||||||
|
|
||||||
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
|
|
||||||
errchan := pool.Add(relayURL, nostr.SimplePolicy{Read: true, Write: true})
|
|
||||||
go func() {
|
|
||||||
for err := range errchan {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// subscribe to relays in our pool, with filtering
|
|
||||||
_, events, unsubscribe := pool.Sub(nostr.Filters{
|
|
||||||
{
|
|
||||||
Authors: []string{publicKey},
|
|
||||||
Kinds: []int{nostr.KindTextNote},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// listen for events from our subscriptions
|
|
||||||
go func() {
|
|
||||||
for event := range nostr.Unique(events) {
|
|
||||||
fmt.Printf("Received Event: %+v\n\n", event)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// create and publish events
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
content := fmt.Sprintf("Hello 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")
|
|
||||||
unsubscribe()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user