Class: OpenReceive::NwcRubyReceiveClient

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

Overview

Thin adapter binding the engine to the nwc-ruby gem (NwcRuby::Client). The engine speaks NIP-47 wire names in string-keyed hashes; nwc-ruby declares snake_case keyword arguments, and spells the list_transactions window until_ts because until is a Ruby keyword.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, connection_uri: nil) ⇒ NwcRubyReceiveClient

Returns a new instance of NwcRubyReceiveClient.



18
19
20
21
22
# File 'lib/openreceive/nwc_ruby.rb', line 18

def initialize(client:, connection_uri: nil)
  @client = client
  @redacted_connection_uri = connection_uri.nil? ? nil : OpenReceive.redact_nwc_uri(connection_uri)
  OpenReceive.parse_nwc_uri(connection_uri) unless connection_uri.nil?
end

Instance Attribute Details

#redacted_connection_uriObject (readonly)

Returns the value of attribute redacted_connection_uri.



16
17
18
# File 'lib/openreceive/nwc_ruby.rb', line 16

def redacted_connection_uri
  @redacted_connection_uri
end

Instance Method Details

#list_transactions(request) ⇒ Object



29
30
31
32
33
# File 'lib/openreceive/nwc_ruby.rb', line 29

def list_transactions(request)
  params = symbolize_keys(OpenReceive.list_transactions_nip47_request(request))
  params[:until_ts] = params.delete(:until) if params.key?(:until)
  OpenReceive.normalize_list_transactions_response(@client.list_transactions(**params))
end

#make_invoice(request) ⇒ Object



24
25
26
27
# File 'lib/openreceive/nwc_ruby.rb', line 24

def make_invoice(request)
  params = symbolize_keys(OpenReceive.make_invoice_nip47_request(request))
  OpenReceive.normalize_make_invoice_response(@client.make_invoice(**params))
end

#preflightObject



35
36
37
# File 'lib/openreceive/nwc_ruby.rb', line 35

def preflight
  OpenReceive.stringify(@client.get_info)
end

#subscribe_notifications(&handler) ⇒ Object

Opt-in NWC-02 notifications, forwarded from the wrapped client.

nwc-ruby yields a NwcRuby::NIP47::Notification value object, while every other method in that gem returns the NIP-47 payload as a hash and OpenReceive.listen_for_notifications! consumes the NWC-02 wire shape (notification_type plus the transaction-shaped notification). This translates the object back to that shape, so the shared settlement rule reads the same fields it reads on a list_transactions row — state/settled_at, never a preimage alone. Every notification type is forwarded; the engine filters payment_received itself, because an NWC-02 subscription is not type-filtered — the wallet decides what it publishes.

Returns whatever subscribe_to_notifications returns; nwc-ruby's blocks until the subscription ends, which is the contract listen_for_notifications! documents for blocking clients.



55
56
57
58
59
60
61
62
# File 'lib/openreceive/nwc_ruby.rb', line 55

def subscribe_notifications(&handler)
  @client.subscribe_to_notifications do |notification|
    handler.call(
      "notification_type" => notification.type.to_s,
      "notification" => OpenReceive.stringify(notification.data)
    )
  end
end