Module: NwcRuby

Defined in:
lib/nwc_ruby.rb,
lib/nwc_ruby/event.rb,
lib/nwc_ruby/client.rb,
lib/nwc_ruby/errors.rb,
lib/nwc_ruby/version.rb,
lib/nwc_ruby/nip47/info.rb,
lib/nwc_ruby/crypto/ecdh.rb,
lib/nwc_ruby/crypto/keys.rb,
lib/nwc_ruby/test_runner.rb,
lib/nwc_ruby/nip04/cipher.rb,
lib/nwc_ruby/nip44/cipher.rb,
lib/nwc_ruby/nip47/methods.rb,
lib/nwc_ruby/nip47/request.rb,
lib/nwc_ruby/crypto/schnorr.rb,
lib/nwc_ruby/nip47/response.rb,
lib/nwc_ruby/connection_string.rb,
lib/nwc_ruby/nip47/notification.rb,
lib/nwc_ruby/transport/relay_connection.rb

Overview

NwcRuby is a Ruby client for NIP-47 (Nostr Wallet Connect).

Quick start:

client = NwcRuby::Client.from_uri(ENV["NWC_URL"])
invoice = client.make_invoice(amount: 1_000) # msats
puts invoice["invoice"]

client.subscribe_to_notifications do |n|
  puts "Got paid: #{n['amount']} msats for #{n['payment_hash']}"
end

To exercise a connection string end-to-end (from IRB, a Rails console, a spec, or a rake task in your own app), use the top-level test method:

NwcRuby.test(
  nwc_url: ENV["NWC_URL"],
  pay_to_lightning_address: "you@getalby.com", # optional — only used for write tests
  pay_to_satoshis_amount: 10                    # used for both outbound and inbound tests
)

Returns true if every check passed, false otherwise. Output is streamed to $stdout by default; pass ‘output: some_io` to redirect.

Defined Under Namespace

Modules: Crypto, NIP04, NIP44, NIP47, Transport Classes: Client, ConnectionString, EncryptionError, Error, Event, InvalidConnectionStringError, InvalidSignatureError, TestRunner, TimeoutError, TransportError, UnsupportedMethodError, WalletServiceError

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.test(nwc_url:, pay_to_lightning_address: nil, pay_to_satoshis_amount: TestRunner::DEFAULT_SATOSHIS, output: $stdout) ⇒ Boolean

Run the diagnostic: info, read tests, and (if the code is read+write and a Lightning address is provided) a write test. Does NOT test notifications — run test_notifications in a separate process for that.

If pay_to_lightning_address is provided but the code is read-only, a helpful warning is printed instead of attempting the payment.

Returns:

  • (Boolean)

    true if all checks passed.



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

def self.test(nwc_url:,
              pay_to_lightning_address: nil,
              pay_to_satoshis_amount: TestRunner::DEFAULT_SATOSHIS,
              output: $stdout)
  TestRunner.new(
    nwc_url: nwc_url,
    pay_to_lightning_address: pay_to_lightning_address,
    pay_to_satoshis_amount: pay_to_satoshis_amount,
    output: output
  ).run
end

.test_notifications(nwc_url:, output: $stdout) ⇒ Boolean

Subscribe to notifications and block forever, printing each one. Run this in a separate process / terminal. Ctrl-C to stop.

Returns:

  • (Boolean)

    true (only returns on clean shutdown).



76
77
78
79
80
81
# File 'lib/nwc_ruby.rb', line 76

def self.test_notifications(nwc_url:, output: $stdout)
  TestRunner.new(
    nwc_url: nwc_url,
    output: output
  ).run_notifications
end