Class: Nostrb::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/nostrb/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.tags!(tags)
  @pk      = Nostrb.key!(pk)      # frozen
end

Instance Attribute Details

#contentObject (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

#kindObject (readonly)

Returns the value of attribute kind.



30
31
32
# File 'lib/nostrb/event.rb', line 30

def kind
  @kind
end

#pkObject (readonly)

Returns the value of attribute pk.



30
31
32
# File 'lib/nostrb/event.rb', line 30

def pk
  @pk
end

#tagsObject (readonly)

Returns the value of attribute tags.



30
31
32
# File 'lib/nostrb/event.rb', line 30

def tags
  @tags
end

Class Method Details

.d_tag(tags) ⇒ Object



18
19
20
# File 'lib/nostrb/event.rb', line 18

def self.d_tag(tags)
  tag_values('d', tags).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.freeze_tags(tags)
  tags.each { |a|
    a.each { |s| s.freeze }
    a.freeze
  }
  tags.freeze
end

.tag_values(tag, tags) ⇒ Object



14
15
16
# File 'lib/nostrb/event.rb', line 14

def self.tag_values(tag, tags)
  tags.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))

#freezeObject



45
46
47
48
# File 'lib/nostrb/event.rb', line 45

def freeze
  Event.freeze_tags(@tags)
  self
end

#pubkeyObject



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_aObject



50
# File 'lib/nostrb/event.rb', line 50

def to_a = serialize(Time.now.to_i)