Class: NwcRuby::ConnectionString

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

Overview

Parses a ‘nostr+walletconnect://` URI.

Canonical form:

nostr+walletconnect://<wallet_service_pubkey_hex>?
  relay=wss://...
 &secret=<32_byte_hex>
 &lud16=<optional>

‘relay` may repeat. `secret` is the CLIENT private key (32 bytes hex); the derived client pubkey is what the wallet encrypts responses to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_string) ⇒ ConnectionString

Returns a new instance of ConnectionString.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nwc_ruby/connection_string.rb', line 21

def initialize(uri_string)
  raise InvalidConnectionStringError, 'connection string is empty' if uri_string.nil? || uri_string.empty?

  # The built-in URI parser chokes on `nostr+walletconnect://` because of
  # the `+`, so normalize the scheme before parsing.
  normalized = uri_string.sub(%r{\Anostr\+walletconnect://}, 'https://')
  uri = URI.parse(normalized)

  unless uri_string.start_with?('nostr+walletconnect://') ||
         uri_string.start_with?('nostrwalletconnect://')
    raise InvalidConnectionStringError,
          'expected nostr+walletconnect:// scheme'
  end

  @wallet_pubkey = uri.host&.downcase
  Crypto::Keys.validate_hex32!(@wallet_pubkey, 'wallet service pubkey (host)')

  params = decode_query(uri.query || '')

  @relays = Array(params['relay'])
  raise InvalidConnectionStringError, 'at least one `relay` parameter is required' if @relays.empty?

  @secret = params['secret']&.first&.downcase
  Crypto::Keys.validate_hex32!(@secret, 'secret')

  @lud16 = params['lud16']&.first
rescue URI::InvalidURIError => e
  raise InvalidConnectionStringError, "malformed URI: #{e.message}"
end

Instance Attribute Details

#lud16Object (readonly)

Returns the value of attribute lud16.



15
16
17
# File 'lib/nwc_ruby/connection_string.rb', line 15

def lud16
  @lud16
end

#relaysObject (readonly)

Returns the value of attribute relays.



15
16
17
# File 'lib/nwc_ruby/connection_string.rb', line 15

def relays
  @relays
end

#secretObject (readonly)

Returns the value of attribute secret.



15
16
17
# File 'lib/nwc_ruby/connection_string.rb', line 15

def secret
  @secret
end

#wallet_pubkeyObject (readonly)

Returns the value of attribute wallet_pubkey.



15
16
17
# File 'lib/nwc_ruby/connection_string.rb', line 15

def wallet_pubkey
  @wallet_pubkey
end

Class Method Details

.parse(uri_string) ⇒ Object



17
18
19
# File 'lib/nwc_ruby/connection_string.rb', line 17

def self.parse(uri_string)
  new(uri_string)
end

Instance Method Details

#client_pubkeyObject

The client’s own x-only pubkey, derived from ‘secret`.



52
53
54
# File 'lib/nwc_ruby/connection_string.rb', line 52

def client_pubkey
  @client_pubkey ||= Crypto::Keys.public_key_from_private(@secret)
end

#to_sObject



56
57
58
59
60
# File 'lib/nwc_ruby/connection_string.rb', line 56

def to_s
  params = { 'relay' => @relays, 'secret' => [@secret] }
  params['lud16'] = [@lud16] if @lud16
  "nostr+walletconnect://#{@wallet_pubkey}?#{encode_query(params)}"
end