Class: Nostrbase::Event
- Inherits:
-
Object
- Object
- Nostrbase::Event
- Defined in:
- lib/nostrbase/event.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#pubkey ⇒ Object
readonly
Returns the value of attribute pubkey.
-
#sig ⇒ Object
readonly
Returns the value of attribute sig.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(hash_or_json_payload) ⇒ Event
constructor
A new instance of Event.
- #resign(keys: nil) ⇒ Object
- #sign(allow_resign: false, keys: nil) ⇒ Object
- #sign!(keys: nil) ⇒ Object
- #signed? ⇒ Boolean
- #to_nostr_serialized ⇒ Object
- #valid_id? ⇒ Boolean
- #valid_schema? ⇒ Boolean
- #valid_signature? ⇒ Boolean
Constructor Details
#initialize(hash_or_json_payload) ⇒ Event
Returns a new instance of Event.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/nostrbase/event.rb', line 13 def initialize(hash_or_json_payload) payload = hash_or_json_payload.is_a?(Hash) ? hash_or_json_payload.map { |key, v| [key.to_s, v] }.to_h : JSON.parse(hash_or_json_payload) @id = payload["id"] @pubkey = payload["pubkey"] @created_at = payload["created_at"] @kind = payload["kind"] @tags = payload["tags"] @content = payload["content"] @sig = payload["sig"] @raw_payload = hash_or_json_payload @errors = [] end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def content @content end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def created_at @created_at end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def errors @errors end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def id @id end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def kind @kind end |
#pubkey ⇒ Object (readonly)
Returns the value of attribute pubkey.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def pubkey @pubkey end |
#sig ⇒ Object (readonly)
Returns the value of attribute sig.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def sig @sig end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
3 4 5 |
# File 'lib/nostrbase/event.rb', line 3 def @tags end |
Class Method Details
Instance Method Details
#as_json ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/nostrbase/event.rb', line 80 def as_json { id:, pubkey:, created_at:, kind:, tags:, content:, sig: } end |
#resign(keys: nil) ⇒ Object
47 48 49 |
# File 'lib/nostrbase/event.rb', line 47 def resign(keys: nil) sign(allow_resign: true, keys: keys) end |
#sign(allow_resign: false, keys: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/nostrbase/event.rb', line 32 def sign(allow_resign: false, keys: nil) return false if signed? && !allow_resign keys = self.class.sign_keys if keys.nil? @pubkey = keys.public @id = Digest::SHA256.hexdigest(JSON.dump(to_nostr_serialized)) @sig = keys.sign(id) end |
#sign!(keys: nil) ⇒ Object
42 43 44 45 |
# File 'lib/nostrbase/event.rb', line 42 def sign!(keys: nil) raise AlreadySigned, "Can't signed event that already has sig, id or pubkey present" if signed? sign(keys: keys) end |
#signed? ⇒ Boolean
28 29 30 |
# File 'lib/nostrbase/event.rb', line 28 def signed? sig.to_s.length.positive? && id.to_s.length.positive? && pubkey.to_s.length.positive? end |
#to_nostr_serialized ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nostrbase/event.rb', line 69 def to_nostr_serialized [ 0, pubkey, created_at.to_i, kind.to_i, , content.to_s ] end |
#valid_id? ⇒ Boolean
56 57 58 |
# File 'lib/nostrbase/event.rb', line 56 def valid_id? Digest::SHA256.hexdigest(JSON.dump(to_nostr_serialized)) === id end |
#valid_schema? ⇒ Boolean
51 52 53 54 |
# File 'lib/nostrbase/event.rb', line 51 def valid_schema? @errors = NostrEventSchema.validate(@raw_payload) @errors.size.negative? end |
#valid_signature? ⇒ Boolean
60 61 62 63 64 65 66 67 |
# File 'lib/nostrbase/event.rb', line 60 def valid_signature? schnorr_sig = Secp256k1::SchnorrSignature.from_data([sig].pack("H*")) schnorr_pubkey = Secp256k1::XOnlyPublicKey.from_data([pubkey].pack("H*")) schnorr_sig.verify([id].pack("H*"), schnorr_pubkey) rescue Secp256k1::DeserializationError false end |