evsifter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 6 Imported by: 0

README

strfry-evsifter

A tiny framework for writing strfry's event sifter (write policy) plugin in Go.

Installation

go get github.com/jiftechnify/strfry-evsifter

Example

The same logic as the example implemented using this framework.

package main


import (
	"log"

	evsifter "github.com/jiftechnify/strfry-evsifter"
)

var whiteList = map[string]struct{}{
    "003ba9b2c5bd8afeed41a4ce362a8b7fc3ab59c25b6a1359cae9093f296dac01": {},
}

// event-sifting function
func acceptWhiteListed(input *evsifter.Input) (*evsifter.Result, error) {
	if _, ok := whiteList[input.Event.PubKey]; ok {
        return input.Accept()
	}

    // you can emit arbitrary logs by log.Print() family
    log.Println("blocking event!")
    return input.Reject("blocked: not on white-list")
}

func main() {
    // initialize a evsifter.Runner and set an event-sifting function
    var s evsifter.Runner
    s.SiftWithFunc(acceptWhiteListed)

    // start the event sifter routine
    s.Run()
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action string

Action represents a type of action by event sifter, in other words, how to process an event.

const (
	ActionAccept       Action = "accept"
	ActionReject       Action = "reject"
	ActionShadowReject Action = "shadowReject"
)

type Input

type Input struct {
	// A type of input. As of strfry 0.9.6, it is always "new".
	Type string `json:"type"`

	// An event data sent by a client or imported from file / another relay.
	Event *nostr.Event `json:"event"`

	// Unix timestamp (in second) of when the event was received by the relay.
	ReceivedAt uint64 `json:"receivedAt"`

	// The source type, or where the event came from.
	SourceType SourceType `json:"sourceType"`

	// Information about event source. If SourceType is...
	//
	//   - SourceTypeIP4 or SourceTypeIP6, it's a string representaion of client's IP address.
	//   - SourceTypeStream or SourceTypeSync, it's a URL of a source relay.
	//   - SourceTypeImport, it's an empty string.
	SourceInfo string `json:"sourceInfo"`
}

Input is a data structure of event sifter's input.

func (*Input) Accept

func (i *Input) Accept() (*Result, error)

Accept accepts the event in the input.

func (*Input) Reject

func (i *Input) Reject(msg string) (*Result, error)

Reject rejects the event in the input with a rejection message to the client.

As per NIP-01, the message should be prefixed with a machine-readable word followed by ":", e.g. "blocked: you are not allowed to write events"

func (*Input) ShadowReject

func (i *Input) ShadowReject() (*Result, error)

ShadowReject silently rejects the event in the input, that is, makes it look accepted to the client, but actually reject it.

type Result

type Result struct {
	// The ID of the target event, taken from the ID field of Input.
	ID string `json:"id"`

	// An action to take on the target event.
	Action Action `json:"action"`

	// A message to be sent to a client (included in an OK message) if event is rejected.
	Msg string `json:"msg"`
}

Result is a data structure of event sifter's output. It can be generated from methods of an Input.

type Runner

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

Runner implements the main routine of a event sifter as Run() method. The zero value for Runner is a valid Runner that accepts all events.

func (*Runner) Run

func (r *Runner) Run()

Run executes the main routine of a event sifter.

func (*Runner) SiftWith

func (r *Runner) SiftWith(s Sifter)

SiftWith sets a Sifter implementation to the Runner.

func (*Runner) SiftWithFunc

func (r *Runner) SiftWithFunc(sf func(input *Input) (*Result, error))

SiftWithFunc sets an event sifting function as a Sifter to the Runner.

type Sifter

type Sifter interface {
	Sift(input *Input) (*Result, error)
}

A Sifter decides whether accept or reject an event based on Input, the event data itself with context information.

Sift should return either Result with an action to take on the event, or error if it couldn't process the input. If error is returned from Sift, the event is rejected by default.

type SifterFunc

type SifterFunc func(input *Input) (*Result, error)

SifterFunc is an adapter to allow the use of functions which takes a sifter Input and returns a sifter Result as a Sifter.

func (SifterFunc) Sift

func (s SifterFunc) Sift(input *Input) (*Result, error)

type SourceType

type SourceType string

SourceType represents a source type of a Nostr event, in other words, where an event came from.

const (
	// SourceTypeIP4 shows that an event was sent from a client which has an IPv4 address.
	SourceTypeIP4 SourceType = "IP4"

	// SourceTypeIP6 shows that an event was sent from a client which has an IPv6 address.
	SourceTypeIP6 SourceType = "IP6"

	// SourceTypeImport shows that an event was imported via "strfry import" command.
	SourceTypeImport SourceType = "Import"

	// SourceTypeStream shows that an event was imported from another relay via "strfry stream" or "strfry router" command.
	SourceTypeStream SourceType = "Stream"

	// SourceTypeSync shows that an event was imported from another relay via "strfry sync" command.
	SourceTypeSync SourceType = "Sync"
)

Jump to

Keyboard shortcuts

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