Nostr.Event (Nostr Lib v0.2.0)

View Source

Nostr Event

Summary

Types

t()

Nostr event

Functions

Compute event ID from Nostr.Event struct

Create new Nostr event struct

Parse raw event map to Nostr.Event struct and validate ID and signature

Parse raw event map to specific event struct (also validates ID and signature)

Serialize event from Nostr.Event struct

Sign the event with seckey

Types

t()

@type t() :: %Nostr.Event{
  content: binary(),
  created_at: DateTime.t(),
  id: <<_::32, _::_*8>>,
  kind: non_neg_integer(),
  pubkey: <<_::32, _::_*8>>,
  sig: <<_::64, _::_*8>>,
  tags: [Nostr.Tag.t()]
}

Nostr event

Functions

compute_id(event)

@spec compute_id(event :: t()) :: binary()

Compute event ID from Nostr.Event struct

create(kind, opts \\ [])

@spec create(non_neg_integer(), Keyword.t()) :: t()

Create new Nostr event struct

Requires event kind and optionally any other event field:

  • pubkey - default is nil (derived later during event signing)
  • tags - default is [] (needs to be list of Nostr.Tag structs)
  • created_at - default is DateTime.utc_now/0
  • content - default is ""
  • id ignored (computed later during event signing)
  • sig ignored, if you want to signed event use Nostr.Event.sign/2

Example

iex> Nostr.Event.create(1, content: "My note", created_at: ~U[2023-06-09 11:07:59.031962Z])
%Nostr.Event{
  kind: 1,
  pubkey: nil,
  tags: [],
  created_at: ~U[2023-06-09 11:07:59.031962Z],
  content: "My note"
}

iex> tags = [Nostr.Tag.create(:e, "event-id")]
iex> Nostr.Event.create(1, created_at: ~U[2023-06-09 11:07:59.031962Z], tags: tags)
%Nostr.Event{
  kind: 1,
  pubkey: nil,
  tags: [%Nostr.Tag{type: :e, data: "event-id", info: []}],
  created_at: ~U[2023-06-09 11:07:59.031962Z],
  content: ""
}

parse(event)

@spec parse(event :: map()) :: nil | t()

Parse raw event map to Nostr.Event struct and validate ID and signature

parse_specific(event)

@spec parse_specific(event :: map()) :: struct()

Parse raw event map to specific event struct (also validates ID and signature)

serialize(event)

@spec serialize(event :: t()) :: String.t()

Serialize event from Nostr.Event struct

sign(event, seckey)

@spec sign(t() | %{event: t()}, binary()) :: t() | %{event: t()}

Sign the event with seckey

It auto-populates pubkey from seckey if event doesn't contain one or check it matches the seckey if there is an pubkey

It auto-populates event ID or check the ID is correct before signing