Class: Nostrbase::Event

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_json_payload) ⇒ Event

Returns a new instance of Event.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nostrbase/event.rb', line 13

def initialize(hash_or_json_payload)
  payload = hash_or_json_payload.is_a?(Hash) ? hash_or_json_payload.map { |key, v| [key.to_s, v] }.to_h : JSON.parse(hash_or_json_payload)

  @id = payload["id"]
  @pubkey = payload["pubkey"]
  @created_at = payload["created_at"]
  @kind = payload["kind"]
  @tags = payload["tags"]
  @content = payload["content"]
  @sig = payload["sig"]

  @raw_payload = hash_or_json_payload
  @errors = []
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def created_at
  @created_at
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def id
  @id
end

#kindObject (readonly)

Returns the value of attribute kind.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def kind
  @kind
end

#pubkeyObject (readonly)

Returns the value of attribute pubkey.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def pubkey
  @pubkey
end

#sigObject (readonly)

Returns the value of attribute sig.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def sig
  @sig
end

#tagsObject (readonly)

Returns the value of attribute tags.



3
4
5
# File 'lib/nostrbase/event.rb', line 3

def tags
  @tags
end

Class Method Details

.sign_keysObject



5
6
7
# File 'lib/nostrbase/event.rb', line 5

def self.sign_keys
  @keys ||= Keys.new
end

.sign_secret=(secret_hex) ⇒ Object



9
10
11
# File 'lib/nostrbase/event.rb', line 9

def self.sign_secret=(secret_hex)
  @keys = Keys.new(secret_hex)
end

Instance Method Details

#as_jsonObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nostrbase/event.rb', line 80

def as_json
  {
    id:,
    pubkey:,
    created_at:,
    kind:,
    tags:,
    content:,
    sig:
  }
end

#resign(keys: nil) ⇒ Object



47
48
49
# File 'lib/nostrbase/event.rb', line 47

def resign(keys: nil)
  sign(allow_resign: true, keys: keys)
end

#sign(allow_resign: false, keys: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/nostrbase/event.rb', line 32

def sign(allow_resign: false, keys: nil)
  return false if signed? && !allow_resign

  keys = self.class.sign_keys if keys.nil?

  @pubkey = keys.public
  @id = Digest::SHA256.hexdigest(JSON.dump(to_nostr_serialized))
  @sig = keys.sign(id)
end

#sign!(keys: nil) ⇒ Object

Raises:



42
43
44
45
# File 'lib/nostrbase/event.rb', line 42

def sign!(keys: nil)
  raise AlreadySigned, "Can't signed event that already has sig, id or pubkey present" if signed?
  sign(keys: keys)
end

#signed?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/nostrbase/event.rb', line 28

def signed?
  sig.to_s.length.positive? && id.to_s.length.positive? && pubkey.to_s.length.positive?
end

#to_nostr_serializedObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/nostrbase/event.rb', line 69

def to_nostr_serialized
  [
    0,
    pubkey,
    created_at.to_i,
    kind.to_i,
    tags,
    content.to_s
  ]
end

#valid_id?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/nostrbase/event.rb', line 56

def valid_id?
  Digest::SHA256.hexdigest(JSON.dump(to_nostr_serialized)) === id
end

#valid_schema?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/nostrbase/event.rb', line 51

def valid_schema?
  @errors = NostrEventSchema.validate(@raw_payload)
  @errors.size.negative?
end

#valid_signature?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/nostrbase/event.rb', line 60

def valid_signature?
  schnorr_sig = Secp256k1::SchnorrSignature.from_data([sig].pack("H*"))
  schnorr_pubkey = Secp256k1::XOnlyPublicKey.from_data([pubkey].pack("H*"))

  schnorr_sig.verify([id].pack("H*"), schnorr_pubkey)
rescue Secp256k1::DeserializationError
  false
end