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()
.
It is somewhat trivial to perform the reverse action given that this has been generalised across, hex/bech32 or nostr uri formats. This is achived by calling the parse()
method and passing this the event id string matching one of these formats. The exception to this rule is for bytes where the from_bytes()
method is to be used.
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)}")
# 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)}")
# 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)}")
# 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)}")
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: publicKey
, createdAt
, kind
, tags
and content
.
console.log(" Build Event ID:");
let event_id = new EventId(keys.publicKey, Timestamp.now(), new Kind(1), [], "");
console.log(` - ${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 toHex()
, toBech32()
, toNostrUri()
or asBytes()
.
It is somewhat trivial to perform the reverse action given that this has been generalised across, hex/bech32 or nostr uri formats. This is achived by calling the parse()
method and passing this the event id string matching one of these formats. The exception to this rule is for bytes where the fromBytes()
method is to be used.
For more information/examples on the formatting of Nostr objects please refer to NIP-19 and NIP-21.
// To Hex and then Parse
console.log(" Event ID (hex):");
let event_id_hex = event_id.toHex();
console.log(` - Hex: ${event_id_hex}`);
console.log(` - Parse: ${EventId.parse(event_id_hex)}`);
// To Bech32 and then Parse
console.log(" Event ID (bech32):");
let event_id_bech32 = event_id.toBech32();
console.log(` - Bech32: ${event_id_bech32}`);
console.log(` - Parse: ${EventId.parse(event_id_bech32)}`);
// To Nostr URI and then Parse
console.log(" Event ID (nostr uri):");
let event_id_nostr_uri = event_id.toNostrUri();
console.log(` - Nostr URI: ${event_id_nostr_uri}`);
console.log(` - Parse: ${EventId.parse(event_id_nostr_uri)}`);
// As Bytes and then Parse
console.log(" Event ID (bytes):");
let event_id_bytes = event_id.asBytes();
console.log(` - Bytes: ${event_id_bytes}`);
console.log(` - From Bytes: ${EventId.fromSlice(event_id_bytes)}`);
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()}")
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 verify()
method for both Signature & ID or the verifyId()
method for the ID alone.
// Event ID from Event & Verfiy
console.log(" Event ID from Event:");
let event = EventBuilder.textNote("This is a note").signWithKeys(keys);
console.log(` - Event ID: ${event.id.toBech32()}`);
console.log(` - Verify the ID & Signature: ${event.verify()}`);
console.log(` - Verify the ID Only: ${event.verifyId()}`);
TODO
TODO
TODO