Class: NwcRuby::NIP47::Notification

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

Overview

Parses kind 23196 (NIP-04) or 23197 (NIP-44 v2) notification events.

Inner payload shape:

{ "notification_type": "payment_received"|"payment_sent",
  "notification":      { ...transaction fields... } }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, data:, event:) ⇒ Notification

Returns a new instance of Notification.



13
14
15
16
17
# File 'lib/nwc_ruby/nip47/notification.rb', line 13

def initialize(type:, data:, event:)
  @type  = type
  @data  = data
  @event = event
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/nwc_ruby/nip47/notification.rb', line 11

def data
  @data
end

#eventObject (readonly)

Returns the value of attribute event.



11
12
13
# File 'lib/nwc_ruby/nip47/notification.rb', line 11

def event
  @event
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/nwc_ruby/nip47/notification.rb', line 11

def type
  @type
end

Class Method Details

.parse(event, client_privkey, wallet_pubkey) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nwc_ruby/nip47/notification.rb', line 35

def self.parse(event, client_privkey, wallet_pubkey)
  plaintext = case event.kind
              when Methods::KIND_NOTIFICATION_NIP44
                NIP44::Cipher.decrypt(event.content, client_privkey, wallet_pubkey)
              when Methods::KIND_NOTIFICATION_NIP04
                NIP04::Cipher.decrypt(event.content, client_privkey, wallet_pubkey)
              else
                raise ArgumentError, "not a notification kind: #{event.kind}"
              end

  data = JSON.parse(plaintext)
  new(type: data['notification_type'], data: data['notification'] || {}, event: event)
end

Instance Method Details

#amount_msatsObject



23
24
25
# File 'lib/nwc_ruby/nip47/notification.rb', line 23

def amount_msats
  @data['amount']
end

#payment_hashObject



19
20
21
# File 'lib/nwc_ruby/nip47/notification.rb', line 19

def payment_hash
  @data['payment_hash']
end

#payment_received?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/nwc_ruby/nip47/notification.rb', line 27

def payment_received?
  @type == 'payment_received'
end

#payment_sent?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/nwc_ruby/nip47/notification.rb', line 31

def payment_sent?
  @type == 'payment_sent'
end