Class: Nostrb::SignedEvent
- Inherits:
-
Object
- Object
- Nostrb::SignedEvent
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_at ⇒ Object
Returns the value of attribute created_at.
138
139
140
|
# File 'lib/nostrb/event.rb', line 138
def created_at
@created_at
end
|
#digest ⇒ Object
Returns the value of attribute digest.
138
139
140
|
# File 'lib/nostrb/event.rb', line 138
def digest
@digest
end
|
#event ⇒ Object
Returns the value of attribute event.
138
139
140
|
# File 'lib/nostrb/event.rb', line 138
def event
@event
end
|
#signature ⇒ Object
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
.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
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"]
digest = SchnorrSig.hex2bin id
pk = SchnorrSig.hex2bin pubkey
signature = SchnorrSig.hex2bin sig
unless SchnorrSig.verify?(pk, digest, signature)
raise(SignatureCheck, sig)
end
raise(IdCheck, id) if check_id and digest != SignedEvent.digest(valid)
valid
end
|
Instance Method Details
#content ⇒ Object
148
|
# File 'lib/nostrb/event.rb', line 148
def content = @event.content
|
#id ⇒ Object
155
|
# File 'lib/nostrb/event.rb', line 155
def id = SchnorrSig.bin2hex(@digest).freeze
|
#kind ⇒ Object
149
|
# File 'lib/nostrb/event.rb', line 149
def kind = @event.kind
|
#pubkey ⇒ Object
151
|
# File 'lib/nostrb/event.rb', line 151
def pubkey = @event.pubkey
|
#serialize ⇒ Object
153
|
# File 'lib/nostrb/event.rb', line 153
def serialize = @event.serialize(@created_at)
|
#sig ⇒ Object
156
|
# File 'lib/nostrb/event.rb', line 156
def sig = SchnorrSig.bin2hex(@signature).freeze
|
150
|
# File 'lib/nostrb/event.rb', line 150
def tags = @event.tags
|
#to_h ⇒ Object
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_s ⇒ Object
152
|
# File 'lib/nostrb/event.rb', line 152
def to_s = @event.to_s
|