Class: NwcRuby::NIP47::Response

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

Overview

Parses an incoming kind 23195 response event.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_type:, result:, error:, request_id:, event:) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
# File 'lib/nwc_ruby/nip47/response.rb', line 9

def initialize(result_type:, result:, error:, request_id:, event:)
  @result_type = result_type
  @result      = result
  @error       = error
  @request_id  = request_id
  @event       = event
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/nwc_ruby/nip47/response.rb', line 7

def error
  @error
end

#eventObject (readonly)

Returns the value of attribute event.



7
8
9
# File 'lib/nwc_ruby/nip47/response.rb', line 7

def event
  @event
end

#request_idObject (readonly)

Returns the value of attribute request_id.



7
8
9
# File 'lib/nwc_ruby/nip47/response.rb', line 7

def request_id
  @request_id
end

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/nwc_ruby/nip47/response.rb', line 7

def result
  @result
end

#result_typeObject (readonly)

Returns the value of attribute result_type.



7
8
9
# File 'lib/nwc_ruby/nip47/response.rb', line 7

def result_type
  @result_type
end

Class Method Details

.decrypt(content, client_privkey, wallet_pubkey) ⇒ Object

Try NIP-44 v2 first; fall back to NIP-04 if the version byte is wrong.



51
52
53
54
55
# File 'lib/nwc_ruby/nip47/response.rb', line 51

def self.decrypt(content, client_privkey, wallet_pubkey)
  NIP44::Cipher.decrypt(content, client_privkey, wallet_pubkey)
rescue EncryptionError
  NIP04::Cipher.decrypt(content, client_privkey, wallet_pubkey)
end

.parse(event, client_privkey, wallet_pubkey) ⇒ Response

Parameters:

  • event (Event)

    the kind 23195 event (already signature-verified)

  • client_privkey (String)

    hex

  • wallet_pubkey (String)

    hex

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nwc_ruby/nip47/response.rb', line 33

def self.parse(event, client_privkey, wallet_pubkey)
  # Tags: ["p", client_pubkey], ["e", request_event_id]
  e_tag = event.tags.find { |t| t[0] == 'e' }
  request_id = e_tag && e_tag[1]

  plaintext = decrypt(event.content, client_privkey, wallet_pubkey)
  data = JSON.parse(plaintext)

  new(
    result_type: data['result_type'],
    result: data['result'],
    error: data['error'],
    request_id: request_id,
    event: event
  )
end

Instance Method Details

#error_codeObject



21
22
23
# File 'lib/nwc_ruby/nip47/response.rb', line 21

def error_code
  @error && @error['code']
end

#error_messageObject



25
26
27
# File 'lib/nwc_ruby/nip47/response.rb', line 25

def error_message
  @error && @error['message']
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  @error.nil? && !@result.nil?
end