Event ID
An event ID is defined per the Nostr NIP-01 documentation as the 32-bytes lowercase hex-encoded sha256 of the serialised event data
.
It's fundamentally a unique identifier for an event generated from the hash of the content of a Nostr event object (excluding the signature).
Creation, Formatting and Parsing
TODO
The EventId
class can be called in order to construct event ids, although this is not necessary when building Event
objects as it will be done automatically at that time.
Upon instantiation the following content are passed to the class instance to generate the event ID: public_key
, created_at
, kind
, tags
and content
.
print(" Build Event ID:")
event_id = EventId(keys.public_key(), Timestamp.now(), Kind(1), [], "content")
print(f" - {event_id}")
Once we have an event id object we are able to format and parse this using a few simple methods.
To present as a hex, bech32, nostr uri or as bytes we need only call the relevant methods to_hex()
, to_bech32()
, to_nostr_uri()
or to_bytes()
.
Similarly, we can parse these different representations of the event ID by using the opposite 'from' methods: from_hex()
, from_bech32()
, from_nostr_uri()
or from_bytes()
.
In the event that we want to generalise and simplify this process, across hex/bech32 or nostr uri formats, we can instead simply call parse()
method and pass this the event id string matching one of these formats.
For more information/examples on the formatting of Nostr objects please refer to NIP-19 and NIP-21.
# To Hex and then Parse
print(" Event ID (hex):")
event_id_hex = event_id.to_hex()
print(f" - Hex: {event_id_hex}")
print(f" - Parse: {EventId.parse(event_id_hex)}")
print(f" - From Hex: {EventId.from_hex(event_id_hex)}")
# To Bech32 and then Parse
print(" Event ID (bech32):")
event_id_bech32 = event_id.to_bech32()
print(f" - Bech32: {event_id_bech32}")
print(f" - Parse: {EventId.parse(event_id_bech32)}")
print(f" - From Bech32: {EventId.from_bech32(event_id_bech32)}")
# To Nostr URI and then Parse
print(" Event ID (nostr uri):")
event_id_nostr_uri = event_id.to_nostr_uri()
print(f" - Nostr URI: {event_id_nostr_uri}")
print(f" - Parse: {EventId.parse(event_id_nostr_uri)}")
print(f" - From Nostr URI: {EventId.from_nostr_uri(event_id_nostr_uri)}")
# As Bytes and then Parse
print(" Event ID (bytes):")
event_id_bytes = event_id.as_bytes()
print(f" - Bytes: {event_id_bytes}")
print(f" - From Bytes: {EventId.from_bytes(event_id_bytes)}")
TODO
TODO
TODO
TODO
Access and Verify
TODO
In addition to directly creating/manipulating event ID objects we can also easily access these directly from events, by calling the id()
method on and instance of the Event
class, or, verify that the event id (and signature) for an event is valid, by using the verify()
method.
# Event ID from Event & Verfiy
print(" Event ID from Event & Verify:")
event = EventBuilder.text_note("This is a note", []).sign_with_keys(keys)
print(f" - Event ID: {event.id()}")
print(f" - Verify the ID & Signature: {event.verify()}")
TODO
TODO
TODO
TODO