nostr

package
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2024 License: CC0-1.0, MIT Imports: 27 Imported by: 0

README

nostr

Tools for structuring, processing and encoding things for the nostr protocol.

This will eventually cover everything found in https://github.com/nbd-wtf/go-nostr except rewritten in fully idiomatic Go and restructured for better readability and organisation for a more friendly developer experience.

Documentation

Index

Constants

View Source
const MAX_LOCKS = 50

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func NewConnection

func NewConnection(ctx context.Context, url string, requestHeader http.Header) (*Connection, error)

func (*Connection) Close

func (c *Connection) Close() error

func (*Connection) ReadMessage

func (c *Connection) ReadMessage(ctx context.Context, buf io.Writer) error

func (*Connection) WriteMessage

func (c *Connection) WriteMessage(data []byte) error

type EventMessage

type EventMessage struct {
	Event nip1.Event
	Relay string
}

type IncomingEvent

type IncomingEvent struct {
	*nip1.Event
	Relay *Relay
}

type Relay

type Relay struct {
	URL           string
	RequestHeader http.Header // e.g. for origin header
	Connection    *Connection
	Subscriptions *xsync.MapOf[string, *Subscription]
	Err           error

	// custom things that aren't often used
	//
	AssumeValid bool // this will skip verifying signatures for events received from this relay
	// contains filtered or unexported fields
}

func NewRelay

func NewRelay(ctx context.Context, url string, opts ...RelayOption) (r *Relay)

NewRelay returns a new relay. The relay connection will be closed when the context is canceled.

func RelayConnect

func RelayConnect(ctx context.Context, url string,
	opts ...RelayOption) (r *Relay, e error)

RelayConnect returns a relay object connected to url. Once successfully connected, cancelling ctx has no effect. To close the connection, call r.Close().

func (*Relay) Auth

func (r *Relay) Auth(ctx context.Context, event *nip1.Event) (s Status, e error)

Auth sends an "AUTH" command client -> relay as in NIP-42. Status can be: success, failed, or sent (no response from relay before ctx times out).

func (*Relay) Close

func (r *Relay) Close() (e error)

func (*Relay) Connect

func (r *Relay) Connect(ctx context.Context) (e error)

Connect tries to establish a websocket connection to r.URL. If the context expires before the connection is complete, an error is returned. Once successfully connected, context expiration has no effect: call r.Close to close the connection.

The underlying relay connection will use a background context. If you want to pass a custom context to the underlying relay connection, use NewRelay() and then Relay.Connect().

func (*Relay) Context

func (r *Relay) Context() context.Context

Context retrieves the context that is associated with this relay connection.

func (*Relay) Count

func (r *Relay) Count(ctx context.Context, filters nip1.Filters,
	opts ...SubscriptionOption) (c int64, e error)

func (*Relay) IsConnected

func (r *Relay) IsConnected() bool

IsConnected returns true if the connection to this relay seems to be active.

func (*Relay) PrepareSubscription

func (r *Relay) PrepareSubscription(ctx context.Context, filters nip1.Filters,
	opts ...SubscriptionOption) (s *Subscription)

PrepareSubscription creates a subscription, but doesn't fire it.

Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point. Failure to do that will result in a huge number of halted goroutines being created.

func (*Relay) Publish

func (r *Relay) Publish(ctx context.Context, event *nip1.Event) (s Status, e error)

Publish sends an "EVENT" command to the relay r as in NIP-01. Status can be: success, failed, or sent (no response from relay before ctx times out).

func (*Relay) QuerySync

func (r *Relay) QuerySync(ctx context.Context, filter *nip1.Filter,
	opts ...SubscriptionOption) (evs []*nip1.Event, e error)

func (*Relay) String

func (r *Relay) String() string

String just returns the relay URL.

func (*Relay) Subscribe

func (r *Relay) Subscribe(ctx context.Context, filters nip1.Filters,
	opts ...SubscriptionOption) (s *Subscription, e error)

Subscribe sends a "REQ" command to the relay r as in NIP-01. Events are returned through the channel sub.Events. The subscription is closed when context ctx is cancelled ("CLOSE" in NIP-01).

Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point. Failure to do that will result in a huge number of halted goroutines being created.

func (*Relay) Write

func (r *Relay) Write(msg []byte) <-chan error

Write queues a message to be sent to the relay.

type RelayOption

type RelayOption interface {
	IsRelayOption()
}

RelayOption is the type of the argument passed for that. Some examples of this are WithNoticeHandler and WithAuthHandler.

type SimplePool

type SimplePool struct {
	Relays  map[string]*Relay
	Context context.Context
	// contains filtered or unexported fields
}

func NewSimplePool

func NewSimplePool(ctx context.Context) *SimplePool

func (*SimplePool) EnsureRelay

func (pool *SimplePool) EnsureRelay(url string) (*Relay, error)

func (*SimplePool) QuerySingle

func (pool *SimplePool) QuerySingle(ctx context.Context, urls []string, filter *nip1.Filter) *IncomingEvent

QuerySingle returns the first event returned by the first relay, cancels everything else.

func (*SimplePool) SubMany

func (pool *SimplePool) SubMany(ctx context.Context, urls []string, filters nip1.Filters, unique bool) chan IncomingEvent

SubMany opens a subscription with the given filters to multiple relays the subscriptions only end when the context is canceled

func (*SimplePool) SubManyEose

func (pool *SimplePool) SubManyEose(ctx context.Context, urls []string, filters nip1.Filters) chan IncomingEvent

SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE

func (*SimplePool) SubManyEoseNonUnique

func (pool *SimplePool) SubManyEoseNonUnique(ctx context.Context, urls []string, filters nip1.Filters) chan IncomingEvent

SubManyEoseNonUnique is like SubManyEose, but returns duplicate events if they come from different relays

func (*SimplePool) SubManyNonUnique

func (pool *SimplePool) SubManyNonUnique(ctx context.Context, urls []string, filters nip1.Filters, unique bool) chan IncomingEvent

SubManyNonUnique is like SubMany, but returns duplicate events if they come from different relays

type Status

type Status int
const (
	PublishStatusSent      Status = 0
	PublishStatusFailed    Status = -1
	PublishStatusSucceeded Status = 1
)

func (Status) String

func (s Status) String() string

type Subscription

type Subscription struct {
	Relay   *Relay
	Filters nip1.Filters

	// the Events channel emits all EVENTs that come in a Subscription
	// will be closed when the subscription ends
	Events chan *nip1.Event

	// the EndOfStoredEvents channel gets closed when an EOSE comes for that subscription
	EndOfStoredEvents chan struct{}
	// Context will be .Done() when the subscription ends
	Context context.Context
	// contains filtered or unexported fields
}

func (*Subscription) Close

func (sub *Subscription) Close()

Close just sends a CLOSE message. You probably want Unsub() instead.

func (*Subscription) Fire

func (sub *Subscription) Fire() (e error)

Fire sends the "REQ" command to the relay.

func (*Subscription) GetID

func (sub *Subscription) GetID() string

GetID return the Nostr subscription ID as given to the Relay it is a concatenation of the label and a serial number.

func (*Subscription) Sub

func (sub *Subscription) Sub(_ context.Context, filters nip1.Filters)

Sub sets sub.Filters and then calls sub.Fire(ctx). The subscription will be closed if the context expires.

func (*Subscription) Unsub

func (sub *Subscription) Unsub()

Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01. Unsub() also closes the channel sub.Events and makes a new one.

type SubscriptionOption

type SubscriptionOption interface {
	IsSubscriptionOption()
}

SubscriptionOption is the type of the argument passed for that. Some examples are WithLabel.

type WithAuthHandler

type WithAuthHandler func(ctx context.Context, authEvent *nip1.Event) (ok bool)

WithAuthHandler takes an auth event and expects it to be signed. when not given, AUTH messages from relays are ignored.

func (WithAuthHandler) IsRelayOption

func (_ WithAuthHandler) IsRelayOption()

type WithLabel

type WithLabel string

WithLabel puts a label on the subscription (it is prepended to the automatic id) that is sent to relays.

func (WithLabel) IsSubscriptionOption

func (_ WithLabel) IsSubscriptionOption()

type WithNoticeHandler

type WithNoticeHandler func(notice string)

WithNoticeHandler just takes notices and is expected to do something with them. when not given, defaults to logging the notices.

func (WithNoticeHandler) IsRelayOption

func (_ WithNoticeHandler) IsRelayOption()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL