Class: Nostr::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/nostr/event.rb

Overview

The only object type that exists in Nostr is an event. Events are immutable.

Direct Known Subclasses

Nostr::Events::EncryptedDirectMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pubkey:, kind:, content:, created_at: Time.now.to_i, tags: [], id: nil, sig: nil) ⇒ Event

Instantiates a new Event

)

the same as the “id” field

Examples:

Instantiating a new event

Nostr::Event.new(
 id: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
 pubkey: '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e',
 created_at: 1230981305,
 kind: 1,
 tags: [],
 content: 'Your feedback is appreciated, now pay $8',
 sig: '123ac2923b792ce730b3da34f16155470ab13c8f97f9c53eaeb334f1fb3a5dc9a7f643
       937c6d6e9855477638f5655c5d89c9aa5501ea9b578a66aced4f1cd7b3'

Parameters:

  • id (String|nil) (defaults to: nil)

    32-bytes sha256 of the the serialized event data.

  • sig (String|nil) (defaults to: nil)

    64-bytes signature of the sha256 hash of the serialized event data, which is

  • pubkey (String)

    32-bytes hex-encoded public key of the event creator.

  • created_at (Integer) (defaults to: Time.now.to_i)

    Date of the creation of the vent. A UNIX timestamp, in seconds.

  • kind (Integer)

    The kind of the event. An integer from 0 to 3.

  • tags (Array<Array>) (defaults to: [])

    An array of tags. Each tag is an array of strings.

  • content (String)

    Arbitrary string.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/nostr/event.rb', line 122

def initialize(
  pubkey:,
  kind:,
  content:,
  created_at: Time.now.to_i,
  tags: [],
  id: nil,
  sig: nil
)

  @id = id
  @sig = sig
  @pubkey = pubkey
  @created_at = created_at
  @kind = kind
  @tags = tags
  @content = content
end

Instance Attribute Details

#contentString (readonly)

An arbitrary string

Examples:

event.content # => 'Your feedback is appreciated, now pay $8'

Returns:

  • (String)


62
63
64
# File 'lib/nostr/event.rb', line 62

def content
  @content
end

#created_atInteger (readonly)

Date of the creation of the vent. A UNIX timestamp, in seconds

Examples:

event.created_at # => 1230981305

Returns:

  • (Integer)


26
27
28
# File 'lib/nostr/event.rb', line 26

def created_at
  @created_at
end

#idString|nil

32-bytes sha256 of the the serialized event data. To obtain the event.id, we sha256 the serialized event. The serialization is done over the UTF-8 JSON-serialized string (with no white space or line breaks)

Examples:

Getting the event id

event.id # => 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460'

Setting the event id

event.id = 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460'
event.id # => 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460'

Returns:

  • (String|nil)


79
80
81
# File 'lib/nostr/event.rb', line 79

def id
  @id
end

#kindInteger (readonly)

The kind of the event. An integer from 0 to 3

Examples:

event.kind # => 1

Returns:

  • (Integer)


37
38
39
# File 'lib/nostr/event.rb', line 37

def kind
  @kind
end

#pubkeyString (readonly)

32-bytes hex-encoded public key of the event creator

Examples:

event.pubkey # => '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e'

Returns:

  • (String)


15
16
17
# File 'lib/nostr/event.rb', line 15

def pubkey
  @pubkey
end

#sigString|nil

64-bytes signature of the sha256 hash of the serialized event data, which is the same as the “id” field

Examples:

Getting the event signature

event.sig # => ''

Setting the event signature

event.sig = '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39'
event.sig # => '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39'

Returns:

  • (String|nil)


95
96
97
# File 'lib/nostr/event.rb', line 95

def sig
  @sig
end

#tagsArray<Array> (readonly)

An array of tags. Each tag is an array of strings

Examples:

Tags referencing an event

event.tags #=> [["e", "event_id", "relay URL"]]

Tags referencing a key

event.tags #=> [["p", "event_id", "relay URL"]]

Returns:

  • (Array<Array>)


51
52
53
# File 'lib/nostr/event.rb', line 51

def tags
  @tags
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two events. Returns true if all attributes are equal and false otherwise

Examples:

event1 == event2 # => true

Returns:

  • (Boolean)

    True if all attributes are equal and false otherwise



235
236
237
# File 'lib/nostr/event.rb', line 235

def ==(other)
  to_h == other.to_h
end

#add_event_reference(event_id) ⇒ Array<String>

Adds a reference to an event id as an ‘e’ tag

Examples:

Adding a reference to a pubkey

event_id = '189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408'
event.add_event_reference(event_id)

Parameters:

  • event_id (String)

    32-bytes hex-encoded event id.

Returns:

  • (Array<String>)

    The event’s updated list of tags



153
# File 'lib/nostr/event.rb', line 153

def add_event_reference(event_id) = tags.push(['e', event_id])

#add_pubkey_reference(pubkey) ⇒ Array<String>

Adds a reference to a pubkey as a ‘p’ tag

Examples:

Adding a reference to a pubkey

pubkey = '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e'
event.add_pubkey_reference(pubkey)

Parameters:

  • pubkey (String)

    32-bytes hex-encoded public key.

Returns:

  • (Array<String>)

    The event’s updated list of tags



167
# File 'lib/nostr/event.rb', line 167

def add_pubkey_reference(pubkey) = tags.push(['p', pubkey])

#serializeArray

Serializes the event, to obtain a SHA256 digest of it

Examples:

Converting the event to a digest

event.serialize

Returns:

  • (Array)

    The event as an array.



194
195
196
197
198
199
200
201
202
203
# File 'lib/nostr/event.rb', line 194

def serialize
  [
    0,
    pubkey,
    created_at,
    kind,
    tags,
    content
  ]
end

#sign(private_key) ⇒ Event

Signs an event with the user’s private key

Examples:

Signing an event

event.sign(private_key)

Parameters:

  • private_key (String)

    32-bytes hex-encoded private key.

Returns:

  • (Event)

    A signed event.



180
181
182
183
# File 'lib/nostr/event.rb', line 180

def sign(private_key)
  crypto = Crypto.new
  crypto.sign_event(self, private_key)
end

#to_hHash

Converts the event to a hash

Examples:

Converting the event to a hash

event.to_h

Returns:

  • (Hash)

    The event as a hash.



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/nostr/event.rb', line 214

def to_h
  {
    id:,
    pubkey:,
    created_at:,
    kind:,
    tags:,
    content:,
    sig:
  }
end