nostr

package module
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: Unlicense Imports: 18 Imported by: 0

README

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.RelayPoolPolicy{
	SimplePolicy: nostr.SimplePolicy{Read: true, Write: true},
})
pool.Add("wss://nostrrelay.example.com/", &nostr.RelayPoolPolicy{
	SimplePolicy: 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
sub := pool.Sub(nostr.EventFilters{
	{
		Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"},
		Kinds:   []string{nostr.KindTextNote}, // or {1}
	},
})

go func() {
	for event := range sub.UniqueEvents {
		log.Print(event)
	}
}()

time.Sleep(5 * time.Second)
sub.Unsub()
Publishing an event
secretKey := "3f06a81e0a0c2ad34ee9df2a30d87a810da9e3c3881f780755ace5e5e64d30a7"

pool.SecretKey = &secretKey

event, statuses, _ := pool.PublishEvent(&nostr.Event{
	CreatedAt: uint32(time.Now().Unix()),
	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))

Documentation

Index

Constants

View Source
const (
	KindSetMetadata            int = 0
	KindTextNote               int = 1
	KindRecommendServer        int = 2
	KindContactList            int = 3
	KindEncryptedDirectMessage int = 4
	KindDeletion               int = 5
)

Variables

This section is empty.

Functions

func FilterEqual added in v0.4.2

func FilterEqual(a Filter, b Filter) bool

func GeneratePrivateKey added in v0.5.0

func GeneratePrivateKey() string

func GetPublicKey added in v0.5.0

func GetPublicKey(sk string) (string, error)

func NormalizeURL

func NormalizeURL(u string) string

Types

type Connection added in v0.5.1

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

func NewConnection added in v0.5.1

func NewConnection(socket *websocket.Conn) *Connection

func (*Connection) Close added in v0.5.1

func (c *Connection) Close() error

func (*Connection) WriteJSON added in v0.5.1

func (c *Connection) WriteJSON(v interface{}) error

func (*Connection) WriteMessage added in v0.5.1

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

type Event

type Event struct {
	ID        string
	PubKey    string
	CreatedAt time.Time
	Kind      int
	Tags      Tags
	Content   string
	Sig       string
}

func (Event) CheckSignature

func (evt Event) CheckSignature() (bool, error)

CheckSignature checks if the signature is valid for the id (which is a hash of the serialized event content). returns an error if the signature itself is invalid.

func (*Event) GetID added in v0.6.1

func (evt *Event) GetID() string

GetID serializes and returns the event ID as a string

func (Event) MarshalJSON added in v0.6.1

func (evt Event) MarshalJSON() ([]byte, error)

func (*Event) Serialize

func (evt *Event) Serialize() []byte

Serialize outputs a byte array that can be hashed/signed to identify/authenticate

func (*Event) Sign

func (evt *Event) Sign(privateKey string) error

Sign signs an event with a given privateKey

func (*Event) UnmarshalJSON added in v0.6.1

func (evt *Event) UnmarshalJSON(payload []byte) error

type EventMessage

type EventMessage struct {
	Event Event
	Relay string
}

type Filter added in v0.6.1

type Filter struct {
	IDs     StringList
	Kinds   IntList
	Authors StringList
	Tags    TagMap
	Since   *time.Time
	Until   *time.Time
	Limit   int
}

func (Filter) MarshalJSON added in v0.6.1

func (f Filter) MarshalJSON() ([]byte, error)

func (Filter) Matches added in v0.6.1

func (ef Filter) Matches(event *Event) bool

func (*Filter) UnmarshalJSON added in v0.6.1

func (f *Filter) UnmarshalJSON(payload []byte) error

type Filters added in v0.6.1

type Filters []Filter

func (Filters) Match added in v0.6.1

func (eff Filters) Match(event *Event) bool

type IntList

type IntList []int

func (IntList) Contains

func (haystack IntList) Contains(needle int) bool

func (IntList) Equals

func (as IntList) Equals(bs IntList) bool

type NoticeMessage

type NoticeMessage struct {
	Message string
	Relay   string
}

type PublishStatus

type PublishStatus struct {
	Relay  string
	Status Status
}

type RelayPool

type RelayPool struct {
	SecretKey *string

	Relays s.MapOf[string, RelayPoolPolicy]

	Notices chan *NoticeMessage
	// contains filtered or unexported fields
}

func NewRelayPool

func NewRelayPool() *RelayPool

New creates a new RelayPool with no relays in it

func (*RelayPool) Add

func (r *RelayPool) Add(url string, policy RelayPoolPolicy) error

Add adds a new relay to the pool, if policy is nil, it will be a simple read+write policy.

func (*RelayPool) PublishEvent

func (r *RelayPool) PublishEvent(evt *Event) (*Event, chan PublishStatus, error)

func (*RelayPool) Remove

func (r *RelayPool) Remove(url string)

Remove removes a relay from the pool.

func (*RelayPool) Sub

func (r *RelayPool) Sub(filters Filters) *Subscription

type RelayPoolPolicy

type RelayPoolPolicy interface {
	ShouldRead(Filters) bool
	ShouldWrite(*Event) bool
}

type SimplePolicy

type SimplePolicy struct {
	Read  bool
	Write bool
}

func (SimplePolicy) ShouldRead added in v0.4.1

func (s SimplePolicy) ShouldRead(_ Filters) bool

func (SimplePolicy) ShouldWrite added in v0.4.1

func (s SimplePolicy) ShouldWrite(_ *Event) bool

type Status added in v0.7.1

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

func (Status) String added in v0.7.1

func (s Status) String() string

type StringList

type StringList []string

func (StringList) Contains

func (haystack StringList) Contains(needle string) bool

func (StringList) ContainsPrefixOf added in v0.6.1

func (haystack StringList) ContainsPrefixOf(needle string) bool

func (StringList) Equals

func (as StringList) Equals(bs StringList) bool

type Subscription

type Subscription struct {
	Events chan EventMessage

	UniqueEvents chan Event
	// contains filtered or unexported fields
}

func (*Subscription) Sub

func (subscription *Subscription) Sub(filters Filters)

func (Subscription) Unsub

func (subscription Subscription) Unsub()

type TagMap added in v0.6.1

type TagMap map[string]StringList

type Tags

type Tags []StringList

func (Tags) ContainsAny

func (tags Tags) ContainsAny(tagName string, values StringList) bool

func (*Tags) Scan

func (t *Tags) Scan(src interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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