Class: Nostrb::Source

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

Overview

A Source holds a public key and creates Events.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pk) ⇒ Source

Returns a new instance of Source.



33
34
35
# File 'lib/nostrb/source.rb', line 33

def initialize(pk)
  @pk = Nostrb.key!(pk).freeze
end

Instance Attribute Details

#pkObject (readonly)

Returns the value of attribute pk.



31
32
33
# File 'lib/nostrb/source.rb', line 31

def pk
  @pk
end

Class Method Details

.close(sid) ⇒ Object



22
# File 'lib/nostrb/source.rb', line 22

def self.close(sid) = ["CLOSE", Nostrb.sid!(sid)].freeze

.publish(signed) ⇒ Object

Client Requests



14
# File 'lib/nostrb/source.rb', line 14

def self.publish(signed) = ["EVENT", signed.to_h].freeze

.random_sidObject

Utils / Init



27
28
29
# File 'lib/nostrb/source.rb', line 27

def self.random_sid
  SchnorrSig.bin2hex(Random.bytes(32)).freeze
end

.subscribe(sid, *filters) ⇒ Object



16
17
18
19
20
# File 'lib/nostrb/source.rb', line 16

def self.subscribe(sid, *filters)
  ["REQ", Nostrb.sid!(sid), *filters.map { |f|
     Nostrb.check!(f, Filter).to_h
  }].freeze
end

Instance Method Details

#deletion_request(explanation, *event_ids) ⇒ Object Also known as: delete

NIP-09 Input

explanation: content string
*event_ids: array of event ids, hex format

Output

Event
  content: explanation
  kind: 5, deletion request
  tags: [['e', event_id]]

TODO: support deletion of replaceable events (‘a’ tags)



104
105
106
107
108
# File 'lib/nostrb/source.rb', line 104

def deletion_request(explanation, *event_ids)
  e = event(explanation, 5)
  event_ids.each { |eid| e.ref_event(eid) }
  e
end

#event(content, kind) ⇒ Object

Event Creation



42
43
44
# File 'lib/nostrb/source.rb', line 42

def event(content, kind)
  Event.new(content, kind: kind, pk: @pk)
end

#follow_list(pubkey_hsh) ⇒ Object Also known as: follows

NIP-02 Input

pubkey_hsh: a ruby hash of the form: pubkey => [relay_url, petname]
  "deadbeef1234abcdef" => ["wss://alicerelay.com/", "alice"]

Output

Event
  content: ""
  kind: 3, follow list
  tags: [['p', pubkey, relay_url, petname]]


83
84
85
86
87
88
89
90
91
# File 'lib/nostrb/source.rb', line 83

def follow_list(pubkey_hsh)
  list = event('', 3)
  pubkey_hsh.each { |pubkey, (url, name)|
    list.ref_pubkey(Nostrb.pubkey!(pubkey),
                    Nostrb.txt!(url),
                    Nostrb.txt!(name))
  }
  list
end

#pubkeyObject



37
# File 'lib/nostrb/source.rb', line 37

def pubkey = SchnorrSig.bin2hex(@pk).freeze

#text_note(content) ⇒ Object

NIP-01 Input

content: string

Output

Event
  content: <content>
  kind: 1


53
54
55
# File 'lib/nostrb/source.rb', line 53

def text_note(content)
  event(content, 1)
end

#user_metadata(name:, about:, picture:, **kwargs) ⇒ Object Also known as: profile

NIP-01 Input

name: string
about: string
picture: string, URL

Output

Event
  content: {"name":<username>,"about":<string>,"picture":<url>}
  kind: 0, user metadata


66
67
68
69
70
71
# File 'lib/nostrb/source.rb', line 66

def (name:, about:, picture:, **kwargs)
  full = kwargs.merge(name: Nostrb.txt!(name),
                      about: Nostrb.txt!(about),
                      picture: Nostrb.txt!(picture))
  event(Nostrb.json(full), 0)
end