class Tapyrus::RPC::TapyrusCoreClient
Client implementation for RPC
to Tapyrus
Core.
- Usage
-
config = {schema: 'http', host: 'localhost', port: 18332, user: 'xxx', password: 'yyy'} client =
Tapyrus::RPC::TapyrusCoreClient.new(config)
You can execute the CLI command supported by
Tapyrus
Core as follows:client.listunspent client.getblockchaininfo
Attributes
config[R]
Public Class Methods
new(config)
click to toggle source
@param [Hash] config a configuration required to connect to Bitcoin Core.
# File lib/tapyrus/rpc/tapyrus_core_client.rb, line 56 def initialize(config) @config = config commands = request(:help) .split("\n") .inject([]) do |memo_ary, line| memo_ary << line.split(' ').first.to_sym if !line.empty? && !line.start_with?('==') memo_ary end TapyrusCoreClient.class_eval do commands.each { |command| define_method(command) { |*params| request(command, *params) } } end end
Private Instance Methods
error!(response)
click to toggle source
# File lib/tapyrus/rpc/tapyrus_core_client.rb, line 94 def error!(response) rpc_error = begin Tapyrus::RPC.response_body2json(response.body)['error'] rescue JSON::ParserError => _ # if RPC server don't send error message. end raise Error.new(response.code, response.msg, rpc_error) end
request(command, *params)
click to toggle source
# File lib/tapyrus/rpc/tapyrus_core_client.rb, line 79 def request(command, *params) data = { method: command, params: params, id: 'jsonrpc' } uri = URI.parse(server_url) http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = uri.scheme === 'https' request = Net::HTTP::Post.new(uri.path.empty? ? '/' : uri.path) request.basic_auth(uri.user, uri.password) request.content_type = 'application/json' request.body = data.to_json response = http.request(request) raise error!(response) unless response.is_a? Net::HTTPOK response = Tapyrus::RPC.response_body2json(response.body) response['result'] end
server_url()
click to toggle source
# File lib/tapyrus/rpc/tapyrus_core_client.rb, line 73 def server_url url = "#{config[:schema]}://#{config[:user]}:#{config[:password]}@#{config[:host]}:#{config[:port]}" url += "/wallet/#{config[:wallet]}" if !config[:wallet].nil? && !config[:wallet].empty? url end