Class: NwcRuby::Event
- Inherits:
-
Object
- Object
- NwcRuby::Event
- Defined in:
- lib/nwc_ruby/event.rb
Overview
A Nostr event (NIP-01).
The canonical serialization for ID computation is a JSON array with no whitespace between elements:
[0, pubkey, created_at, kind, , content]
SHA-256 of that JSON is the event id; the event id is signed with BIP-340 Schnorr against the author’s private key.
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#pubkey ⇒ Object
Returns the value of attribute pubkey.
-
#sig ⇒ Object
Returns the value of attribute sig.
-
#tags ⇒ Object
Returns the value of attribute tags.
Class Method Summary collapse
-
.from_hash(h) ⇒ Object
Build from a hash as received from the relay.
Instance Method Summary collapse
- #compute_id! ⇒ Object
-
#initialize(pubkey:, kind:, content:, tags: [], created_at: Time.now.to_i) ⇒ Event
constructor
A new instance of Event.
-
#serialize_for_id ⇒ Object
JSON used for ID computation.
- #sign!(privkey_hex) ⇒ Object
- #to_h ⇒ Object
- #valid_signature? ⇒ Boolean
Constructor Details
#initialize(pubkey:, kind:, content:, tags: [], created_at: Time.now.to_i) ⇒ Event
Returns a new instance of Event.
14 15 16 17 18 19 20 |
# File 'lib/nwc_ruby/event.rb', line 14 def initialize(pubkey:, kind:, content:, tags: [], created_at: Time.now.to_i) @pubkey = pubkey @kind = kind @content = content @tags = @created_at = created_at end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def content @content end |
#created_at ⇒ Object
Returns the value of attribute created_at.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def created_at @created_at end |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def id @id end |
#kind ⇒ Object
Returns the value of attribute kind.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def kind @kind end |
#pubkey ⇒ Object
Returns the value of attribute pubkey.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def pubkey @pubkey end |
#sig ⇒ Object
Returns the value of attribute sig.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def sig @sig end |
#tags ⇒ Object
Returns the value of attribute tags.
12 13 14 |
# File 'lib/nwc_ruby/event.rb', line 12 def @tags end |
Class Method Details
.from_hash(h) ⇒ Object
Build from a hash as received from the relay.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/nwc_ruby/event.rb', line 23 def self.from_hash(h) e = allocate e.id = h['id'] e.pubkey = h['pubkey'] e.created_at = h['created_at'] e.kind = h['kind'] e. = h['tags'] || [] e.content = h['content'] || '' e.sig = h['sig'] e end |
Instance Method Details
#compute_id! ⇒ Object
41 42 43 44 |
# File 'lib/nwc_ruby/event.rb', line 41 def compute_id! @id = OpenSSL::Digest::SHA256.hexdigest(serialize_for_id) self end |
#serialize_for_id ⇒ Object
JSON used for ID computation. MUST NOT contain whitespace between tokens and MUST use the array form below.
37 38 39 |
# File 'lib/nwc_ruby/event.rb', line 37 def serialize_for_id JSON.generate([0, @pubkey, @created_at, @kind, @tags, @content]) end |
#sign!(privkey_hex) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/nwc_ruby/event.rb', line 46 def sign!(privkey_hex) compute_id! digest_bytes = Crypto::Keys.hex_to_bytes(@id) @sig = Crypto::Schnorr.sign(digest_bytes, privkey_hex) self end |
#to_h ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nwc_ruby/event.rb', line 60 def to_h { 'id' => @id, 'pubkey' => @pubkey, 'created_at' => @created_at, 'kind' => @kind, 'tags' => @tags, 'content' => @content, 'sig' => @sig } end |