nws/exit/nostr.go

70 lines
1.5 KiB
Go
Raw Normal View History

package exit
import (
"context"
"log/slog"
2024-08-01 22:37:01 +00:00
"strconv"
"time"
2024-08-02 15:54:18 +00:00
"github.com/asmogo/nws/protocol"
"github.com/nbd-wtf/go-nostr"
)
func (e *Exit) announceExitNode(ctx context.Context) error {
if !e.config.Public {
return nil
}
2024-08-01 22:37:01 +00:00
go func() {
for {
event := nostr.Event{
PubKey: e.publicKey,
CreatedAt: nostr.Now(),
2024-08-02 15:54:18 +00:00
Kind: protocol.KindAnnouncementEvent,
2024-08-01 22:42:26 +00:00
Tags: nostr.Tags{
nostr.Tag{"expiration", strconv.FormatInt(time.Now().Add(time.Second*10).Unix(), 20)},
},
2024-08-01 22:37:01 +00:00
}
err := event.Sign(e.config.NostrPrivateKey)
if err != nil {
slog.Error("could not sign event", "error", err)
continue
}
// publish the event
for _, relay := range e.relays {
err = relay.Publish(ctx, event)
if err != nil {
slog.Error("could not publish event", "error", err)
// do not return here, try to publish the event to other relays
}
}
2024-08-01 22:42:26 +00:00
time.Sleep(time.Second * 10)
}
2024-08-01 22:37:01 +00:00
}()
return nil
}
func (e *Exit) DeleteEvent(ctx context.Context, ev *nostr.Event) error {
for _, responseRelay := range e.config.NostrRelays {
var relay *nostr.Relay
relay, err := e.pool.EnsureRelay(responseRelay)
if err != nil {
return err
}
event := nostr.Event{
CreatedAt: nostr.Now(),
PubKey: e.publicKey,
Kind: nostr.KindDeletion,
Tags: nostr.Tags{
nostr.Tag{"e", ev.ID},
},
}
err = event.Sign(e.config.NostrPrivateKey)
err = relay.Publish(ctx, event)
if err != nil {
return err
}
}
return nil
}