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 ¶
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 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 ¶
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) Fingerprint ¶
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 ¶
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 ¶
Matches checks a filter against an event and determines if the event matches the filter.
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 ¶
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) Fingerprint ¶
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.