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
# 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



261
262
263
# File 'lib/nostr/event.rb', line 261

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



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

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 (PublicKey)

    32-bytes hex-encoded public key.

Returns:

  • (Array<String>)

    The event’s updated list of tags



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

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

#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.



220
221
222
223
224
225
226
227
228
229
# File 'lib/nostr/event.rb', line 220

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 (PrivateKey)

    32-bytes hex-encoded private key.

Returns:

  • (Event)

    A signed event.



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

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.



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/nostr/event.rb', line 240

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

#verify_signatureBoolean

Verifies if the signature of the event is valid. A valid signature means that the event was signed by the owner

Examples:

Verifying the signature of an event

event = Nostr::Event.new(
  id: '90b75b78daf883ae57fbcc414d43faa028560b3211ee58e4ea82bf395bb82042',
  pubkey: Nostr::PublicKey.new('2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'),
  created_at: 1667422587,
  kind: Nostr::EventKind::TEXT_NOTE,
  content: 'Your feedback is appreciated, now pay $8',
  sig: '32f18adebe942e19b171c1c7d2fb27ce794dfea4155e289dca7952b43ed1ec39' \
    '1d3dc198ba2761bc6d40c737a6eaf4edcc8963acabd3bfcebd04f16637025bdc'
)

event.verify_signature # => true

Returns:

  • (Boolean)

    Whether the signature is valid or not.



203
204
205
206
207
208
209
# File 'lib/nostr/event.rb', line 203

def verify_signature
  crypto = Crypto.new

  return false if id.nil? || pubkey.nil? || sig.nil?

  crypto.valid_sig?(id, pubkey, sig)
end