Class: Nostrb::SignedEvent

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

Overview

SignedEvent id: 64 hex chars (32B binary) created_at: unix seconds, integer sig: 128 hex chars (64B binary)

Defined Under Namespace

Classes: Error, IdCheck, SignatureCheck

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, sk) ⇒ SignedEvent

sk is used to generate @signature and then discarded



141
142
143
144
145
146
# File 'lib/nostrb/event.rb', line 141

def initialize(event, sk)
  @event = Nostrb.check!(event, Event)
  @created_at = Time.now.to_i
  @digest = @event.digest(@created_at).freeze
  @signature = SchnorrSig.sign(Nostrb.key!(sk), @digest).freeze
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



138
139
140
# File 'lib/nostrb/event.rb', line 138

def created_at
  @created_at
end

#digestObject (readonly)

Returns the value of attribute digest.



138
139
140
# File 'lib/nostrb/event.rb', line 138

def digest
  @digest
end

#eventObject (readonly)

Returns the value of attribute event.



138
139
140
# File 'lib/nostrb/event.rb', line 138

def event
  @event
end

#signatureObject (readonly)

Returns the value of attribute signature.



138
139
140
# File 'lib/nostrb/event.rb', line 138

def signature
  @signature
end

Class Method Details

.digest(valid) ⇒ Object



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

def self.digest(valid) = Nostrb.digest(Nostrb.json(serialize(valid)))

.serialize(valid) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/nostrb/event.rb', line 107

def self.serialize(valid)
  Array[ 0,
         valid["pubkey"],
         valid["created_at"],
         valid["kind"],
         valid["tags"],
         valid["content"], ].freeze
end

.validate!(parsed) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nostrb/event.rb', line 92

def self.validate!(parsed)
  Nostrb.check!(parsed, Hash)
  Nostrb.txt!(parsed.fetch("content"))
  Nostrb.pubkey!(parsed.fetch("pubkey"))
  Nostrb.kind!(parsed.fetch("kind"))
  Nostrb.tags!(parsed.fetch("tags"))
  Event.freeze_tags(parsed['tags'])
  Nostrb.int!(parsed.fetch("created_at"))
  Nostrb.id!(parsed.fetch("id"))
  Nostrb.sig!(parsed.fetch("sig"))
  parsed.freeze
end

.verify(valid, check_id: true) ⇒ Object

Validate the id (optional) and signature May raise explicitly: IdCheck, SignatureCheck May raise implicitly: Nostrb::SizeError, EncodingError, TypeError,

SchnorrSig::Error

Return a _completely validated_ hash

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/nostrb/event.rb', line 121

def self.verify(valid, check_id: true)
  id, pubkey, sig = valid["id"], valid["pubkey"], valid["sig"]

  # extract binary values for signature verification
  digest = SchnorrSig.hex2bin id
  pk = SchnorrSig.hex2bin pubkey
  signature = SchnorrSig.hex2bin sig

  # verify the signature
  unless SchnorrSig.verify?(pk, digest, signature)
    raise(SignatureCheck, sig)
  end
  # (optional) verify the id / digest
  raise(IdCheck, id) if check_id and digest != SignedEvent.digest(valid)
  valid
end

Instance Method Details

#contentObject



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

def content = @event.content

#idObject



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

def id = SchnorrSig.bin2hex(@digest).freeze

#kindObject



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

def kind = @event.kind

#pubkeyObject



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

def pubkey = @event.pubkey

#serializeObject



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

def serialize = @event.serialize(@created_at)

#sigObject



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

def sig = SchnorrSig.bin2hex(@signature).freeze

#tagsObject



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

def tags = @event.tags

#to_hObject



158
159
160
161
162
163
164
165
166
# File 'lib/nostrb/event.rb', line 158

def to_h
  Hash[ "content" => @event.content,
        "kind" => @event.kind,
        "tags" => @event.tags,
        "pubkey" => @event.pubkey,
        "created_at" => @created_at,
        "id" => self.id,
        "sig" => self.sig ].freeze
end

#to_sObject



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

def to_s = @event.to_s