Kind
As a core component of nostr objects, kinds are used to signal to clients how to parse the data contained within an event.
A kind
is represented by an integer between 0
and 65535
the most well known of which is the Kind 1
,
or text note
which contains plaintext data to be displayed.
Other commonly used kinds include kind 0
(user metadata) and Kind 3
(following/contact lists).
For more details and to see the full range of proposed/adopted Kinds please refer to the Nostr NIPs documentation.
Kind by Integer and Enum
TODO
Working with kinds is facilitated by the Kind
and KindEnum
classes.
If you are familiar already with the specific integer value for a given Kind it is as simple as calling and instance of the class Kind()
and passing the specific number for the Kind you wish to create.
In the example below we've used the common 0
/1
/3
Kinds (user metadata, text note and following list, respectively) as an illustration of this.
Once we've created the Kind
object we can use the as_enum()
method to present the Kind object as an easy to read KindEnum
object.
print(" Kind from integer:")
kind = Kind(1)
print(f" - Kind 1: {kind.as_enum()}")
kind = Kind(0)
print(f" - Kind 0: {kind.as_enum()}")
kind = Kind(3)
print(f" - Kind 3: {kind.as_enum()}")
Alternatively, if you are less familiar with the specific integer values for a Kind we can use the individual Kind classes, in conjunction with the KindEnum
class, to generate the objects.
Below we see the TEXT_NOTE()
, METADATA()
and CONTACT_LIST()
enums being passed to an instance of the Kind
class via the from_enum()
method.
In order to present these as their integer values we can use the as_u16()
or as_u64()
methods.
print(" Kind from enum:")
kind = Kind.from_enum(cast(KindEnum, KindEnum.TEXT_NOTE()))
print(f" - Kind TEXT_NOTE: {kind.as_u16()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.METADATA()))
print(f" - Kind METADATA: {kind.as_u16()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.CONTACT_LIST()))
print(f" - Kind CONTRACT_LIST: {kind.as_u16()}")
TODO
TODO
TODO
TODO
Events and Kinds
TODO
Although it's possible to construct EventBuilder
objects by passing the Kind
class as the first argument (see Event section for examples),
one of the simplest ways of constructing Event
objects is by using the purpose built methods available to the EventBuilder
class.
For example, the text_note()
method can be used to quickly and efficiently create Kind 1 events, the metadata()
and contact_list()
methods can be used in much the same way.
print(" Kind methods EventBuilder:")
event = EventBuilder.text_note("This is a note", []).sign_with_keys(keys)
print(f" - Kind text_note(): {event.kind().as_u16()} - {event.kind().as_enum()}")
event = EventBuilder.metadata(Metadata()).sign_with_keys(keys)
print(f" - Kind metadata(): {event.kind().as_u16()} - {event.kind().as_enum()}")
event = EventBuilder.contact_list([]).sign_with_keys(keys)
print(f" - Kind contact_list(): {event.kind().as_u16()} - {event.kind().as_enum()}")
Occasionally you may want more generic usage of kinds, like if you wanted to create your own custom (or experimental) event type,
or if you want to leverage one of the commonly defined event types (i.e. replaceable, ephemeral, regular etc.).
To do this we can use the Kind
class along with the from_enum()
method much as we did in previous examples,
but we can leverage enums representing these types of events e.g. CUSTOM()
or REPLACEABLE()
and pass them the specific Kind integer for the new type of event we're creating.
A good example of this may be events termed as "Parameterized Replaceable Lists".
In the Nostr NIP-01 documentation we see a recommended range for these lists as 30000 <= n < 40000
,
however at the time of writing, only kinds 30000
, 30002
, 30003
, 30004
, 30005
, 30015
, 30030
and 30063
are currently well defined.
Therefore, if we wanted to extend this to say create a new list event of our favourite memes, Kind 30420
,
then we could do this using the PARAMETERIZED_REPLACEABLE(30420)
enum to define the type of event as in the example below.
print(" Kinds Representing types of Events:")
kind = Kind.from_enum(cast(KindEnum, KindEnum.CUSTOM(1337)))
print(f" - Custom Event Kind: {kind.as_u16()} - {kind.as_enum()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.REPLACEABLE(10420)))
print(f" - Replacable Event Kind: {kind.as_u16()} - {kind.as_enum()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.PARAMETERIZED_REPLACEABLE(30420)))
print(f" - Parameterised Replaceable Event Kind: {kind.as_u16()} - {kind.as_enum()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.EPHEMERAL(21420)))
print(f" - Ephemeral Event Kind: {kind.as_u16()} - {kind.as_enum()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.REGULAR(420)))
print(f" - Regular Event Kind: {kind.as_u16()} - {kind.as_enum()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.JOB_REQUEST(123)))
print(f" - Job Request Event Kind: {kind.as_u16()} - {kind.as_enum()}")
kind = Kind.from_enum(cast(KindEnum, KindEnum.JOB_RESULT(321)))
print(f" - Job Result Event Kind: {kind.as_u16()} - {kind.as_enum()}")
TODO
TODO
TODO
TODO