nostr_tools.calc_event_id

nostr_tools.calc_event_id(pubkey, created_at, kind, tags, content)[source]

Calculate the event ID for a Nostr event according to NIP-01 specification.

The event ID is calculated as the SHA-256 hash of the UTF-8 encoded JSON serialization of the event data array: [0, pubkey, created_at, kind, tags, content]. The JSON is serialized with minimal formatting (no spaces, compact separators).

Parameters:
  • pubkey (str) – Public key in lowercase hex format (64 characters).

  • created_at (int) – Unix timestamp (seconds since epoch) of event creation.

  • kind (int) – Event kind number (0-65535).

  • tags (list[list[str]]) – List of event tags. Each tag is a list of strings.

  • content (str) – Event content as a string.

Returns:

Event ID as lowercase hexadecimal string (64 characters).

This is the SHA-256 hash of the serialized event data.

Return type:

str

Examples

Calculate event ID:

>>> pubkey = "abc123..." # 64-char hex
>>> created_at = 1234567890
>>> kind = 1
>>> tags = [["p", "def456..."], ["e", "789abc..."]]
>>> content = "Hello Nostr!"
>>> event_id = calc_event_id(pubkey, created_at, kind, tags, content)
>>> len(event_id)
64

Use in event creation:

>>> event_id = calc_event_id(
...     public_key, timestamp, kind, tags, content
... )
>>> signature = sig_event_id(event_id, private_key)
>>> event = {
...     "id": event_id,
...     "pubkey": public_key,
...     "created_at": timestamp,
...     "kind": kind,
...     "tags": tags,
...     "content": content,
...     "sig": signature
... }

Verify event ID matches:

>>> recalculated_id = calc_event_id(
...     event['pubkey'], event['created_at'],
...     event['kind'], event['tags'], event['content']
... )
>>> assert recalculated_id == event['id']