alex 435579dc75 publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:

    ["OK", event-id, false, "blocked"]

this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:

    func (r Relay) Publish(event Event) chan Status {
      // uses a **copy** of statusChans here:
      r.statusChans.Store(event.ID, statusChan)
      ...
    }

the bugfix is a one character change:

    func (r *Relay) Publish(event Event) chan Status

but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:

    // ok, which is a command status from earlier, is shadowed here:
    if statusChan, ok := r.statusChans.Load(eventId); ok {
      statusChan <- ...
    }

as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 14:44:40 -03:00
2022-12-23 10:12:22 +08:00
2022-12-23 11:35:24 -03:00
2022-11-26 19:31:49 -03:00
2022-01-06 21:57:04 -03:00
2022-07-11 15:51:24 -03:00
2022-12-19 15:10:23 -03:00
2022-11-26 09:25:31 -03:00
2022-12-18 06:37:54 -03:00
2022-12-18 06:37:54 -03:00
2022-10-07 16:52:01 -07:00
2022-12-11 16:35:04 -03:00
2022-11-19 07:19:10 -03:00
2022-12-23 10:12:22 +08:00
2022-11-11 10:52:23 -03:00
2022-11-18 14:18:37 -03:00

go-nostr

A set of useful things for Nostr Protocol implementations.

GoDoc

Subscribing to a set of relays

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

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

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

sk, _ := nostr.GenerateKey()

fmt.Println("sk:", sk)
fmt.Println("pk:", nostr.GetPublicKey(sk))

Example Program

go run example/example.go
Description
Nostr library for Golang
Readme 5.6 MiB
Languages
C 83.3%
Go 15.9%
Assembly 0.7%
Just 0.1%