filter

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: Unlicense Imports: 19 Imported by: 0

Documentation

Overview

Package filter is a codec for nostr filters (queries) and includes tools for matching them to events, a canonical format scheme to enable compactly identifying subscription filters, and a simplified filter that leavse out the IDs and Search fields for use in the HTTP API.

Index

Constants

This section is empty.

Variables

View Source
var (

	// IDs is the JSON object key for IDs.
	IDs = []byte("ids")
	// Kinds is the JSON object key for Kinds.
	Kinds = []byte("kinds")
	// Authors is the JSON object key for Authors.
	Authors = []byte("authors")
	// Since is the JSON object key for Since.
	Since = []byte("since")
	// Until is the JSON object key for Until.
	Until = []byte("until")
	// Limit is the JSON object key for Limit.
	Limit = []byte("limit")
	// Search is the JSON object key for Search.
	Search = []byte("search")
)

Functions

This section is empty.

Types

type F

type F struct {
	Ids     *tag.T       `json:"ids,omitempty"`
	Kinds   *kinds.T     `json:"kinds,omitempty"`
	Authors *tag.T       `json:"authors,omitempty"`
	Tags    *tags.T      `json:"-,omitempty"`
	Since   *timestamp.T `json:"since,omitempty"`
	Until   *timestamp.T `json:"until,omitempty"`
	Search  []byte       `json:"search,omitempty"`
	Limit   *uint        `json:"limit,omitempty"`
}

F is the primary query form for requesting events from a nostr relay.

The ordering of fields of filters is not specified as in the protocol there is no requirement to generate a hash for fast recognition of identical filters. However, for internal use in a relay, by applying a consistent sort order, this library will produce an identical JSON from the same *set* of fields no matter what order they were provided.

This is to facilitate the deduplication of filters so an effective identical match is not performed on an identical filter.

func GenFilter

func GenFilter() (f *F, err error)

GenFilter is a testing tool to create random arbitrary filters for tests.

func New

func New() (f *F)

New creates a new, reasonably initialized filter that will be ready for most uses without further allocations.

func (*F) Clone

func (f *F) Clone() (clone *F)

Clone creates a new filter with all the same elements in them, because they are immutable, basically, except setting the Limit field as 1, because it is used in the subscription management code to act as a reference counter, and making a clone implicitly means 1 reference.

func (*F) Equal

func (f *F) Equal(b *F) bool

Equal checks a filter against another filter to see if they are the same filter.

func (*F) Fingerprint

func (f *F) Fingerprint() (fp uint64, err error)

Fingerprint returns an 8 byte truncated sha256 hash of the filter in the canonical form created by Marshal.

This hash is generated via the JSON encoded form of the filter, with the Limit field removed. This value should be set to zero after all results from a query of stored events, as per NIP-01.

func (*F) Marshal

func (f *F) Marshal(dst []byte) (b []byte)

Marshal a filter into raw JSON bytes, minified. The field ordering and sort of fields is canonicalized so that a hash can identify the same filter.

func (*F) Matches

func (f *F) Matches(ev *event.E) bool

Matches checks a filter against an event and determines if the event matches the filter.

func (*F) Serialize

func (f *F) Serialize() (b []byte)

Serialize a filter.F into raw minified JSON bytes.

func (*F) Sort

func (f *F) Sort()

Sort the fields of a filter so a fingerprint on a filter that has the same set of content produces the same fingerprint.

func (*F) Unmarshal

func (f *F) Unmarshal(b []byte) (r []byte, err error)

Unmarshal a filter from raw (minified) JSON bytes into the runtime format.

todo: this may tolerate whitespace, not certain currently.

type S

type S struct {
	Kinds   *kinds.T     `json:"kinds,omitempty"`
	Authors *tag.T       `json:"authors,omitempty"`
	Tags    *tags.T      `json:"-,omitempty"`
	Since   *timestamp.T `json:"since,omitempty"`
	Until   *timestamp.T `json:"until,omitempty"`
	Limit   *uint        `json:"limit,omitempty"`
}

S is a simplified filter that only covers the nip-01 REQ filter minus the separate and superseding Id list. The search field is from a different NIP, but it is a separate API for which reason it is also not here.

func NewSimple

func NewSimple() (f *S)

NewSimple creates a new, reasonably pre-allocated filter.S.

func (*S) Clone

func (f *S) Clone() (clone *S)

Clone creates a new filter with all the same elements in them, because they are immutable, basically, except setting the Limit field as 1, because it is used in the subscription management code to act as a reference counter, and making a clone implicitly means 1 reference.

func (*S) Equal

func (f *S) Equal(b *S) bool

Equal checks if two filters are the same filter.

func (*S) Fingerprint

func (f *S) Fingerprint() (fp uint64, err error)

Fingerprint returns an 8 byte truncated sha256 hash of the filter in the canonical form created by Marshal.

The purpose of this fingerprint is enabling the creation of a map of filters that can be searched by making a canonical form of a provided input filter that will match the same filter. It achieves this by making all fields sorted in lexicographical order and from this a single 8 byte truncated hash can be used to identify if a received filter is the same filter.

func (*S) Marshal

func (f *S) Marshal(dst []byte) (b []byte)

Marshal a filter.S in canonical form with sorted fields.

func (*S) Matches

func (f *S) Matches(ev *event.E) bool

Matches checks if a filter.S matches an event.

func (*S) Serialize

func (f *S) Serialize() (b []byte)

Serialize a filter directly to raw bytes.

func (*S) Sort

func (f *S) Sort()

Sort the fields of a filter so a fingerprint on a filter that has the same set of content produces the same fingerprint.

func (*S) Unmarshal

func (f *S) Unmarshal(b []byte) (r []byte, err error)

Unmarshal a filter from JSON (minified) form.

todo: maybe this tolerates whitespace?

Jump to

Keyboard shortcuts

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