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.
-
#verify_signature ⇒ Boolean
Verifies if the signature of the event is valid.
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 |
# 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 = @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 @tags end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two events. Returns 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
152 |
# File 'lib/nostr/event.rb', line 152 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
166 |
# File 'lib/nostr/event.rb', line 166 def add_pubkey_reference(pubkey) = .push(['p', pubkey.to_s]) |
#serialize ⇒ Array
Serializes the event, to obtain a SHA256 digest of it
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/nostr/event.rb', line 220 def serialize [ 0, pubkey, created_at, kind, , content ] end |
#sign(private_key) ⇒ Event
Signs an event with the user’s private key
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_h ⇒ Hash
Converts the event to 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_signature ⇒ Boolean
Verifies if the signature of the event is valid. A valid signature means that the event was signed by the owner
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 |