Class: Nostr::Event
- Inherits:
-
Object
- Object
- Nostr::Event
- Defined in:
- lib/nostr/event.rb
Overview
The only object type that exists in Nostr is an event. Events are immutable.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
An arbitrary string.
-
#created_at ⇒ Integer
readonly
Date of the creation of the vent.
-
#id ⇒ String|nil
32-bytes sha256 of the the serialized event data.
-
#kind ⇒ Integer
readonly
The kind of the event.
-
#pubkey ⇒ String
readonly
32-bytes hex-encoded public key of the event creator.
-
#sig ⇒ String|nil
64-bytes signature of the sha256 hash of the serialized event data, which is the same as the “id” field.
-
#tags ⇒ Array<Array>
readonly
An array of tags.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two events.
-
#add_event_reference(event_id) ⇒ Array<String>
Adds a reference to an event id as an ‘e’ tag.
-
#add_pubkey_reference(pubkey) ⇒ Array<String>
Adds a reference to a pubkey as a ‘p’ tag.
-
#initialize(pubkey:, kind:, content:, created_at: Time.now.to_i, tags: [], id: nil, sig: nil) ⇒ Event
constructor
Instantiates a new Event.
-
#serialize ⇒ Array
Serializes the event, to obtain a SHA256 digest of it.
-
#sign(private_key) ⇒ Event
Signs an event with the user’s private key.
-
#to_h ⇒ Hash
Converts the event to a hash.
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
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 = @content = content end |
Instance Attribute Details
#content ⇒ String (readonly)
An arbitrary string
62 63 64 |
# File 'lib/nostr/event.rb', line 62 def content @content end |
#created_at ⇒ Integer (readonly)
Date of the creation of the vent. A UNIX timestamp, in seconds
26 27 28 |
# File 'lib/nostr/event.rb', line 26 def created_at @created_at end |
#id ⇒ String|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)
79 80 81 |
# File 'lib/nostr/event.rb', line 79 def id @id end |
#kind ⇒ Integer (readonly)
The kind of the event. An integer from 0 to 3
37 38 39 |
# File 'lib/nostr/event.rb', line 37 def kind @kind end |
#pubkey ⇒ String (readonly)
32-bytes hex-encoded public key of the event creator
15 16 17 |
# File 'lib/nostr/event.rb', line 15 def pubkey @pubkey end |
#sig ⇒ String|nil
64-bytes signature of the sha256 hash of the serialized event data, which is the same as the “id” field
95 96 97 |
# File 'lib/nostr/event.rb', line 95 def sig @sig end |
#tags ⇒ Array<Array> (readonly)
An array of tags. Each tag is an array of strings
51 52 53 |
# File 'lib/nostr/event.rb', line 51 def end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two events. Returns 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
153 |
# File 'lib/nostr/event.rb', line 153 def add_event_reference(event_id) = .push(['e', event_id]) |
#add_pubkey_reference(pubkey) ⇒ Array<String>
Adds a reference to a pubkey as a ‘p’ tag
167 |
# File 'lib/nostr/event.rb', line 167 def add_pubkey_reference(pubkey) = .push(['p', pubkey]) |
#serialize ⇒ Array
Serializes the event, to obtain a SHA256 digest of it
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/nostr/event.rb', line 194 def serialize [ 0, pubkey, created_at, kind, , content ] end |
#sign(private_key) ⇒ Event
Signs an event with the user’s private key
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_h ⇒ Hash
Converts the event to 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 |