Class: NwcRuby::Event

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

Overview

A Nostr event (NIP-01).

The canonical serialization for ID computation is a JSON array with no whitespace between elements:

[0, pubkey, created_at, kind, tags, content]

SHA-256 of that JSON is the event id; the event id is signed with BIP-340 Schnorr against the author’s private key.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pubkey:, kind:, content:, tags: [], created_at: Time.now.to_i) ⇒ Event

Returns a new instance of Event.



14
15
16
17
18
19
20
# File 'lib/nwc_ruby/event.rb', line 14

def initialize(pubkey:, kind:, content:, tags: [], created_at: Time.now.to_i)
  @pubkey     = pubkey
  @kind       = kind
  @content    = content
  @tags       = tags
  @created_at = created_at
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def content
  @content
end

#created_atObject

Returns the value of attribute created_at.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def id
  @id
end

#kindObject

Returns the value of attribute kind.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def kind
  @kind
end

#pubkeyObject

Returns the value of attribute pubkey.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def pubkey
  @pubkey
end

#sigObject

Returns the value of attribute sig.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def sig
  @sig
end

#tagsObject

Returns the value of attribute tags.



12
13
14
# File 'lib/nwc_ruby/event.rb', line 12

def tags
  @tags
end

Class Method Details

.from_hash(h) ⇒ Object

Build from a hash as received from the relay.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nwc_ruby/event.rb', line 23

def self.from_hash(h)
  e = allocate
  e.id         = h['id']
  e.pubkey     = h['pubkey']
  e.created_at = h['created_at']
  e.kind       = h['kind']
  e.tags       = h['tags'] || []
  e.content    = h['content'] || ''
  e.sig        = h['sig']
  e
end

Instance Method Details

#compute_id!Object



41
42
43
44
# File 'lib/nwc_ruby/event.rb', line 41

def compute_id!
  @id = OpenSSL::Digest::SHA256.hexdigest(serialize_for_id)
  self
end

#serialize_for_idObject

JSON used for ID computation. MUST NOT contain whitespace between tokens and MUST use the array form below.



37
38
39
# File 'lib/nwc_ruby/event.rb', line 37

def serialize_for_id
  JSON.generate([0, @pubkey, @created_at, @kind, @tags, @content])
end

#sign!(privkey_hex) ⇒ Object



46
47
48
49
50
51
# File 'lib/nwc_ruby/event.rb', line 46

def sign!(privkey_hex)
  compute_id!
  digest_bytes = Crypto::Keys.hex_to_bytes(@id)
  @sig = Crypto::Schnorr.sign(digest_bytes, privkey_hex)
  self
end

#to_hObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nwc_ruby/event.rb', line 60

def to_h
  {
    'id' => @id,
    'pubkey' => @pubkey,
    'created_at' => @created_at,
    'kind' => @kind,
    'tags' => @tags,
    'content' => @content,
    'sig' => @sig
  }
end

#valid_signature?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/nwc_ruby/event.rb', line 53

def valid_signature?
  return false unless @id && @sig && @pubkey

  digest_bytes = Crypto::Keys.hex_to_bytes(@id)
  Crypto::Schnorr.verify(digest_bytes, @sig, @pubkey)
end