Module: NwcRuby::NIP47::Request

Defined in:
lib/nwc_ruby/nip47/request.rb

Overview

Builds a signed kind 23194 request event with the JSON-RPC payload encrypted using either NIP-44 v2 or NIP-04.

Class Method Summary collapse

Class Method Details

.build(method:, params:, client_privkey:, wallet_pubkey:, encryption: :nip44_v2, expiration: nil) ⇒ Event

Parameters:

  • method (String)

    NIP-47 method name

  • params (Hash)

    method-specific params

  • client_privkey (String)

    hex

  • wallet_pubkey (String)

    hex

  • encryption (Symbol) (defaults to: :nip44_v2)

    :nip44_v2 or :nip04

  • expiration (Integer, nil) (defaults to: nil)

    optional unix timestamp

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nwc_ruby/nip47/request.rb', line 17

def build(method:, params:, client_privkey:, wallet_pubkey:, encryption: :nip44_v2, expiration: nil)
  payload = JSON.generate({ 'method' => method, 'params' => params })
  ciphertext = case encryption
               when :nip44_v2 then NIP44::Cipher.encrypt(payload, client_privkey, wallet_pubkey)
               when :nip04    then NIP04::Cipher.encrypt(payload, client_privkey, wallet_pubkey)
               else raise ArgumentError, "unknown encryption: #{encryption}"
               end

  tags = [['p', wallet_pubkey]]
  tags << %w[encryption nip44_v2] if encryption == :nip44_v2
  tags << ['expiration', expiration.to_s] if expiration

  client_pubkey = Crypto::Keys.public_key_from_private(client_privkey)
  event = Event.new(pubkey: client_pubkey, kind: Methods::KIND_REQUEST, content: ciphertext, tags: tags)
  event.sign!(client_privkey)
end