Class: Nostrb::Event
- Inherits:
-
Object
- Object
- Nostrb::Event
- Defined in:
- lib/nostrb/event.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute content.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#pk ⇒ Object
readonly
Returns the value of attribute pk.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
- .d_tag(tags) ⇒ Object
-
.digest(ary) ⇒ Object
Event content: any string kind: 0..65535 tags: Array[Array] pubkey: 64 hex chars (32B binary).
- .freeze_tags(tags) ⇒ Object
- .tag_values(tag, tags) ⇒ Object
Instance Method Summary collapse
-
#add_tag(tag, value, *rest) ⇒ Object
add an array of 2+ strings to @tags.
- #digest(created_at) ⇒ Object
- #freeze ⇒ Object
-
#initialize(content = '', kind: 1, tags: [], pk:) ⇒ Event
constructor
A new instance of Event.
- #pubkey ⇒ Object
-
#ref_event(eid_hex, *rest) ⇒ Object
add an event tag based on event id, hex encoded.
-
#ref_pubkey(pubkey, *rest) ⇒ Object
add a pubkey tag based on pubkey, 64 bytes hex encoded.
-
#ref_replace(*rest, kind:, pubkey:, d_tag: '') ⇒ Object
kind: and pubkey: required.
- #serialize(created_at) ⇒ Object
- #sign(sk) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(content = '', kind: 1, tags: [], pk:) ⇒ Event
Returns a new instance of Event.
32 33 34 35 36 37 |
# File 'lib/nostrb/event.rb', line 32 def initialize(content = '', kind: 1, tags: [], pk:) @content = Nostrb.txt!(content) # frozen @kind = Nostrb.kind!(kind) @tags = Nostrb.() @pk = Nostrb.key!(pk) # frozen end |
Instance Attribute Details
#content ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute content.
30 31 32 |
# File 'lib/nostrb/event.rb', line 30 def content @content end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
30 31 32 |
# File 'lib/nostrb/event.rb', line 30 def kind @kind end |
#pk ⇒ Object (readonly)
Returns the value of attribute pk.
30 31 32 |
# File 'lib/nostrb/event.rb', line 30 def pk @pk end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
30 31 32 |
# File 'lib/nostrb/event.rb', line 30 def @tags end |
Class Method Details
.d_tag(tags) ⇒ Object
18 19 20 |
# File 'lib/nostrb/event.rb', line 18 def self.d_tag() tag_values('d', ).first end |
.digest(ary) ⇒ Object
Event
content: any string
kind: 0..65535
tags: Array[Array[string]]
pubkey: 64 hex chars (32B binary)
12 |
# File 'lib/nostrb/event.rb', line 12 def self.digest(ary) = Nostrb.digest(Nostrb.json(Nostrb.ary!(ary))) |
.freeze_tags(tags) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/nostrb/event.rb', line 22 def self.() .each { |a| a.each { |s| s.freeze } a.freeze } .freeze end |
.tag_values(tag, tags) ⇒ Object
14 15 16 |
# File 'lib/nostrb/event.rb', line 14 def self.tag_values(tag, ) .select { |a| a[0] == tag }.map { |a| a[1] } end |
Instance Method Details
#add_tag(tag, value, *rest) ⇒ Object
add an array of 2+ strings to @tags
60 61 62 63 |
# File 'lib/nostrb/event.rb', line 60 def add_tag(tag, value, *rest) @tags.push([Nostrb.txt!(tag), Nostrb.txt!(value)] + rest.each { |s| Nostrb.txt!(s) }) end |
#digest(created_at) ⇒ Object
52 |
# File 'lib/nostrb/event.rb', line 52 def digest(created_at) = Event.digest(serialize(created_at)) |
#freeze ⇒ Object
45 46 47 48 |
# File 'lib/nostrb/event.rb', line 45 def freeze Event.(@tags) self end |
#pubkey ⇒ Object
51 |
# File 'lib/nostrb/event.rb', line 51 def pubkey = SchnorrSig.bin2hex(@pk) |
#ref_event(eid_hex, *rest) ⇒ Object
add an event tag based on event id, hex encoded
66 67 68 |
# File 'lib/nostrb/event.rb', line 66 def ref_event(eid_hex, *rest) add_tag('e', Nostrb.id!(eid_hex), *rest) end |
#ref_pubkey(pubkey, *rest) ⇒ Object
add a pubkey tag based on pubkey, 64 bytes hex encoded
71 72 73 |
# File 'lib/nostrb/event.rb', line 71 def ref_pubkey(pubkey, *rest) add_tag('p', Nostrb.pubkey!(pubkey), *rest) end |
#ref_replace(*rest, kind:, pubkey:, d_tag: '') ⇒ Object
kind: and pubkey: required
76 77 78 79 |
# File 'lib/nostrb/event.rb', line 76 def ref_replace(*rest, kind:, pubkey:, d_tag: '') val = [Nostrb.kind!(kind), Nostrb.pubkey!(pubkey), d_tag].join(':') add_tag('a', val, *rest) end |
#serialize(created_at) ⇒ Object
41 42 43 |
# File 'lib/nostrb/event.rb', line 41 def serialize(created_at) [0, self.pubkey, Nostrb.int!(created_at), @kind, @tags, @content].freeze end |
#sign(sk) ⇒ Object
53 |
# File 'lib/nostrb/event.rb', line 53 def sign(sk) = SignedEvent.new(self.freeze, sk) |
#to_a ⇒ Object
50 |
# File 'lib/nostrb/event.rb', line 50 def to_a = serialize(Time.now.to_i) |