Module: Nostrb

Defined in:
lib/nostrb/relay.rb,
lib/nostrb.rb,
lib/nostrb/oj.rb,
lib/nostrb/json.rb,
lib/nostrb/event.rb,
lib/nostrb/names.rb,
lib/nostrb/client.rb,
lib/nostrb/filter.rb,
lib/nostrb/sequel.rb,
lib/nostrb/source.rb,
lib/nostrb/sqlite.rb

Overview

for replaceable events with same timestamp, lowest id wins

Defined Under Namespace

Modules: Names, Optional, SQLite, Seconds, Sequel Classes: Client, Error, Event, Filter, FormatError, Relay, SignedEvent, SizeError, Source

Constant Summary collapse

HELP_MSG =
/\A[a-zA-Z0-9\-_]+: [[:print:]]*\z/
JSON_OPTIONS =

per NIP-01

{
  allow_nan: false,
  max_nesting: 4,     # event is 3 deep, wire format is 4 deep
  script_safe: false,
  ascii_only: false,
  array_nl: '',
  object_nl: '',
  indent: '',
  space: '',
  space_before: '',
}

Class Method Summary collapse

Class Method Details

.ary!(ary, max: nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/nostrb.rb', line 31

def self.ary!(ary, max: nil)
  check!(ary, Array)
  if !max.nil? and ary.length > max
    raise(SizeError, "#{ary.length} > #{max} (max)")
  end
  ary
end

.bin!(str, length: nil, max: nil) ⇒ Object



49
50
51
# File 'lib/nostrb.rb', line 49

def self.bin!(str, length: nil, max: nil)
  str!(str, binary: true, length: length, max: max)
end

.check!(val, cls) ⇒ Object



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

def self.check!(val, cls)
  val.is_a?(cls) ? val : raise(TypeError, "#{cls} expected: #{val.inspect}")
end

.digest(str) ⇒ Object

return 32 bytes binary



15
# File 'lib/nostrb.rb', line 15

def self.digest(str) = Digest::SHA256.digest(str).freeze

.help!(str) ⇒ Object

Raises:



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

def self.help!(str)
  raise(FormatError, str) unless txt!(str, max: 1024).match HELP_MSG
  str
end

.id!(str) ⇒ Object



60
# File 'lib/nostrb.rb', line 60

def self.id!(str) = txt!(str, length: 64)

.int!(int, max: nil) ⇒ Object

Raises:



21
22
23
24
25
# File 'lib/nostrb.rb', line 21

def self.int!(int, max: nil)
  check!(int, Integer)
  raise(SizeError, "#{int} > #{max} (max)") if !max.nil? and int > max
  int
end

.json(object) ⇒ Object



5
# File 'lib/nostrb/oj.rb', line 5

def self.json(object) = Oj.dump(object, mode: :strict).freeze

.key!(str) ⇒ Object



52
# File 'lib/nostrb.rb', line 52

def self.key!(str) = bin!(str, length: 32)

.kind!(kind) ⇒ Object



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

def self.kind!(kind)
  int!(kind, max: 65535)
end

.parse(json) ⇒ Object



4
# File 'lib/nostrb/oj.rb', line 4

def self.parse(json)  = Oj.load(json, mode: :strict).freeze

.pubkey!(str) ⇒ Object



58
# File 'lib/nostrb.rb', line 58

def self.pubkey!(str) = txt!(str, length: 64)

.sid!(str) ⇒ Object



61
# File 'lib/nostrb.rb', line 61

def self.sid!(str) = txt!(str, max: 64)

.sig!(str) ⇒ Object



62
# File 'lib/nostrb.rb', line 62

def self.sig!(str) = txt!(str, length: 128)

.str!(str, binary: nil, length: nil, max: nil) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/nostrb.rb', line 39

def self.str!(str, binary: nil, length: nil, max: nil)
  check!(str, String)
  if !binary.nil? and !binary == (str.encoding == Encoding::BINARY)
    raise(EncodingError, str.encoding)
  end
  raise(SizeError, str.length) if !length.nil? and str.length != length
  raise(SizeError, str.length) if !max.nil? and str.length > max
  str.freeze
end

.tags!(ary) ⇒ Object



71
72
73
# File 'lib/nostrb.rb', line 71

def self.tags!(ary)
  ary!(ary, max: 9999).each { |a| ary!(a, max: 99).each { |s| txt!(s) } }
end

.txt!(str, length: nil, max: nil) ⇒ Object



55
56
57
# File 'lib/nostrb.rb', line 55

def self.txt!(str, length: nil, max: nil)
  str!(str, binary: false, length: length, max: max)
end