Class: NwcRuby::NIP47::Info

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

Overview

Parses the kind 13194 “info” event — the wallet service’s capability advertisement. The ‘content` is a plaintext space-separated list of supported methods. Tags optionally include:

["encryption",    "nip44_v2 nip04"]   -- supported encryption schemes
["notifications", "payment_received payment_sent"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(methods:, encryption_schemes:, notification_types:, event:) ⇒ Info

Returns a new instance of Info.



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

def initialize(methods:, encryption_schemes:, notification_types:, event:)
  @methods            = methods
  @encryption_schemes = encryption_schemes
  @notification_types = notification_types
  @event              = event
end

Instance Attribute Details

#encryption_schemesObject (readonly)

Returns the value of attribute encryption_schemes.



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

def encryption_schemes
  @encryption_schemes
end

#eventObject (readonly)

Returns the value of attribute event.



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

def event
  @event
end

#methodsObject (readonly)

Returns the value of attribute methods.



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

def methods
  @methods
end

#notification_typesObject (readonly)

Returns the value of attribute notification_types.



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

def notification_types
  @notification_types
end

Class Method Details

.parse(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nwc_ruby/nip47/info.rb', line 20

def self.parse(event)
  methods = event.content.to_s.strip.split(/\s+/).reject(&:empty?)

  enc_tag = event.tags.find { |t| t[0] == 'encryption' }
  schemes = if enc_tag
              enc_tag[1].to_s.strip.split(/\s+/)
            else
              # Spec: absence means nip04 only.
              ['nip04']
            end

  notif_tag = event.tags.find { |t| t[0] == 'notifications' }
  notif_types = notif_tag ? notif_tag[1].to_s.strip.split(/\s+/) : []

  new(methods: methods, encryption_schemes: schemes, notification_types: notif_types, event: event)
end

Instance Method Details

#preferred_encryptionObject



49
50
51
# File 'lib/nwc_ruby/nip47/info.rb', line 49

def preferred_encryption
  supports_nip44? ? :nip44_v2 : :nip04
end

#read_only?Boolean

True when the connection exposes no fund-moving methods.

Returns:

  • (Boolean)


54
55
56
# File 'lib/nwc_ruby/nip47/info.rb', line 54

def read_only?
  !@methods.intersect?(Methods::MUTATING)
end

#read_write?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/nwc_ruby/nip47/info.rb', line 58

def read_write?
  !read_only?
end

#supports?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def supports?(method)
  @methods.include?(method)
end

#supports_nip04?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/nwc_ruby/nip47/info.rb', line 45

def supports_nip04?
  @encryption_schemes.include?('nip04')
end

#supports_nip44?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/nwc_ruby/nip47/info.rb', line 41

def supports_nip44?
  @encryption_schemes.include?('nip44_v2')
end

#supports_notifications?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/nwc_ruby/nip47/info.rb', line 62

def supports_notifications?
  !@notification_types.empty?
end