Class: NwcRuby::NIP47::Response
- Inherits:
-
Object
- Object
- NwcRuby::NIP47::Response
- Defined in:
- lib/nwc_ruby/nip47/response.rb
Overview
Parses an incoming kind 23195 response event.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#result_type ⇒ Object
readonly
Returns the value of attribute result_type.
Class Method Summary collapse
-
.decrypt(content, client_privkey, wallet_pubkey) ⇒ Object
Try NIP-44 v2 first; fall back to NIP-04 if the version byte is wrong.
- .parse(event, client_privkey, wallet_pubkey) ⇒ Response
Instance Method Summary collapse
- #error_code ⇒ Object
- #error_message ⇒ Object
-
#initialize(result_type:, result:, error:, request_id:, event:) ⇒ Response
constructor
A new instance of Response.
- #success? ⇒ Boolean
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
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/nwc_ruby/nip47/response.rb', line 7 def error @error end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
7 8 9 |
# File 'lib/nwc_ruby/nip47/response.rb', line 7 def event @event end |
#request_id ⇒ Object (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 |
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/nwc_ruby/nip47/response.rb', line 7 def result @result end |
#result_type ⇒ Object (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
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..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_code ⇒ Object
21 22 23 |
# File 'lib/nwc_ruby/nip47/response.rb', line 21 def error_code @error && @error['code'] end |
#error_message ⇒ Object
25 26 27 |
# File 'lib/nwc_ruby/nip47/response.rb', line 25 def @error && @error['message'] end |
#success? ⇒ Boolean
17 18 19 |
# File 'lib/nwc_ruby/nip47/response.rb', line 17 def success? @error.nil? && !@result.nil? end |