mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
nip19, subscription, publication code examples added to README.md
fixed typo in example/example.go
This commit is contained in:
parent
92ce379649
commit
f149ee8b4e
70
README.md
70
README.md
@ -11,12 +11,80 @@ A set of useful things for [Nostr Protocol](https://github.com/nostr-protocol/no
|
||||
|
||||
``` go
|
||||
sk, _ := nostr.GenerateKey()
|
||||
pk, _ := nostr.GetPublicKey(sk)
|
||||
nsec, _ = nip19.EncodePrivateKey(sk)
|
||||
npub, _ = nip19.EncodePublicKey(pk)
|
||||
|
||||
fmt.Println("sk:", sk)
|
||||
fmt.Println("pk:", nostr.GetPublicKey(sk))
|
||||
fmt.Println(nsec)
|
||||
fmt.Println(npub)
|
||||
```
|
||||
|
||||
### Subscriptions
|
||||
### Subscribing to a single relay
|
||||
|
||||
``` go
|
||||
relay, err := nostr.RelayConnect(context.Background(), "wss://nostr.zebedee.cloud")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
npub := "npub1422a7ws4yul24p0pf7cacn7cghqkutdnm35z075vy68ggqpqjcyswn8ekc"
|
||||
|
||||
var filters nostr.Filters
|
||||
if _, v, err := nip19.Decode(npub); err == nil {
|
||||
pub := v.(string)
|
||||
filters = []nostr.Filter{{
|
||||
Kinds: []int{1},
|
||||
Authors: []string{pub},
|
||||
Limit: 1,
|
||||
}}
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sub := relay.Subscribe(context.Background(), filters)
|
||||
|
||||
go func() {
|
||||
<-sub.EndOfStoredEvents
|
||||
// handle end of stored events (EOSE, see NIP-15)
|
||||
}()
|
||||
|
||||
for ev := range sub.Events {
|
||||
// handle returned event.
|
||||
// channel will stay open until sub.Unsub() is called
|
||||
}
|
||||
```
|
||||
|
||||
### Publishing to a single relay
|
||||
|
||||
``` go
|
||||
sk := nostr.GeneratePrivateKey()
|
||||
pub, _ := nostr.GetPublicKey(sk)
|
||||
|
||||
ev := nostr.Event{
|
||||
PubKey: pub,
|
||||
CreatedAt: time.Now(),
|
||||
Kind: 1,
|
||||
Tags: nil,
|
||||
Content: "Hello World!",
|
||||
}
|
||||
|
||||
// calling Sign sets the event ID field and the event Sig field
|
||||
ev.Sign(sk)
|
||||
|
||||
// publish the event to two relays
|
||||
for _, url := range []string{"wss://nostr.zebedee.cloud", "wss://nostr-pub.wellorder.net"} {
|
||||
relay, e := nostr.RelayConnect(context.Background(), url)
|
||||
if e != nil {
|
||||
fmt.Println(e)
|
||||
continue
|
||||
}
|
||||
fmt.Println("published to ", url, relay.Publish(context.Background(), ev))
|
||||
}
|
||||
```
|
||||
|
||||
### Example script
|
||||
|
||||
```
|
||||
go run example/example.go
|
||||
|
@ -37,7 +37,7 @@ func main() {
|
||||
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).
|
||||
// this filters for messages tagged with the user, mainly replies.
|
||||
t["p"] = []string{v.(string)}
|
||||
filters = []nostr.Filter{{
|
||||
Kinds: []int{1},
|
||||
|
Loading…
x
Reference in New Issue
Block a user