2023-08-28 15:58:43 -03:00
2023-02-15 20:25:16 -03:00
2023-04-16 16:25:25 -03:00
2023-06-12 08:18:29 -03:00
2023-06-12 08:18:29 -03:00
2023-05-31 12:35:48 -03:00
2023-08-21 15:51:16 -03:00
2023-08-21 16:21:39 -03:00
2023-07-18 16:17:00 -03:00
2023-06-20 12:13:48 -03:00
2023-06-12 08:18:29 -03:00
2023-08-06 20:03:05 -03:00
2023-08-06 20:03:05 -03:00
2023-06-12 08:18:29 -03:00
2022-10-07 16:52:01 -07:00
2023-07-11 15:25:02 -03:00
2023-06-12 08:18:29 -03:00
2023-03-14 21:56:40 -03:00
2023-05-04 08:24:26 -03:00
2023-08-06 19:57:08 -03:00
2023-08-22 10:58:34 -03:00
2023-08-06 20:03:05 -03:00
2023-08-28 15:58:43 -03:00
2022-11-11 10:52:23 -03:00

go-nostr

A set of useful things for Nostr Protocol implementations.

GoDoc
test every commit

Install go-nostr:

go get github.com/nbd-wtf/go-nostr

Generating a key

package main

import (
    "fmt"

    "github.com/nbd-wtf/go-nostr"
    "github.com/nbd-wtf/go-nostr/nip19"
)

func main() {
    sk := nostr.GeneratePrivateKey()
    pk, _ := nostr.GetPublicKey(sk)
    nsec, _ := nip19.EncodePrivateKey(sk)
    npub, _ := nip19.EncodePublicKey(pk)

    fmt.Println("sk:", sk)
    fmt.Println("pk:", pk)
    fmt.Println(nsec)
    fmt.Println(npub)
}

Subscribing to a single relay

ctx := context.Background()
relay, err := nostr.RelayConnect(ctx, "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{nostr.KindTextNote},
		Authors: []string{pub},
		Limit:   1,
	}}
} else {
	panic(err)
}

ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()

sub, err := relay.Subscribe(ctx, filters)
if err != nil {
	panic(err)
}

for ev := range sub.Events {
	// handle returned event.
	// channel will stay open until the ctx is cancelled (in this case, context timeout)
	fmt.Println(ev.ID)
}

Publishing to two relays

sk := nostr.GeneratePrivateKey()
pub, _ := nostr.GetPublicKey(sk)

ev := nostr.Event{
	PubKey:    pub,
	CreatedAt: nostr.Now(),
	Kind:      nostr.KindTextNote,
	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
ctx := context.Background()
for _, url := range []string{"wss://nostr.zebedee.cloud", "wss://nostr-pub.wellorder.net"} {
	relay, err := nostr.RelayConnect(ctx, url)
	if err != nil {
		fmt.Println(err)
		continue
	}
	_, err = relay.Publish(ctx, ev)
	if err != nil {
		fmt.Println(err)
		continue
	}

	fmt.Printf("published to %s\n", url)
}

Authenticating with NIP-42

For this section, the user needs access to a relay implementing NIP-42. E.g., https://github.com/fiatjaf/relayer with a relay implementing the relayer.Auther interface.

func main() {
	url := "ws://localhost:7447"

	sk := nostr.GeneratePrivateKey()

	relay, err := nostr.RelayConnect(context.Background(), url,
        nostr.WithAuthHandler(func(ctx context.Context, authEvent *Event) (ok bool) {
            authEvent.Sign(sk)
        }),
    )
	if err != nil {
		panic(err)
	}
}

Example script

go run example/example.go
Description
Nostr library for Golang
Readme 5.5 MiB
Languages
C 83.7%
Go 15.5%
Assembly 0.7%
Just 0.1%