mirror of https://github.com/bumi/lnrpc
Refactor client helper
This commit is contained in:
parent
cd6ef20b60
commit
ae08c564aa
|
@ -1,8 +1,9 @@
|
||||||
require "lnrpc/version"
|
require "lnrpc/version"
|
||||||
|
require "lnrpc/rpc_services_pb"
|
||||||
|
|
||||||
module Lnrpc
|
module Lnrpc
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
autoload :WalletUnlocker, 'lnrpc/rpc_services_pb'
|
|
||||||
autoload :Lightning, 'lnrpc/rpc_services_pb'
|
|
||||||
autoload :Client, 'lnrpc/client'
|
autoload :Client, 'lnrpc/client'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,12 @@ module Lnrpc
|
||||||
class Client
|
class Client
|
||||||
attr_accessor :address, :credentials, :macaroon, :grpc_client
|
attr_accessor :address, :credentials, :macaroon, :grpc_client
|
||||||
|
|
||||||
|
LND_HOME_DIR = ENV['LND_HOME'] || "~/.lnd"
|
||||||
DEFAULT_ADDRESS = 'localhost:10009'
|
DEFAULT_ADDRESS = 'localhost:10009'
|
||||||
DEFAULT_CREDENTIALS_PATH = "~/.lnd/tls.cert"
|
DEFAULT_CREDENTIALS_PATH = "#{LND_HOME_DIR}/tls.cert"
|
||||||
DEFAULT_MACAROON_PATH = "~/.lnd/data/chain/bitcoin/testnet/admin.macaroon"
|
DEFAULT_MACAROON_PATH = "#{LND_HOME_DIR}/data/chain/bitcoin/mainnet/admin.macaroon"
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options={})
|
||||||
self.address = options[:address] || DEFAULT_ADDRESS
|
self.address = options[:address] || DEFAULT_ADDRESS
|
||||||
|
|
||||||
options[:credentials] ||= ::File.read(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
|
options[:credentials] ||= ::File.read(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
|
||||||
|
@ -14,21 +15,34 @@ module Lnrpc
|
||||||
|
|
||||||
options[:macaroon] ||= begin
|
options[:macaroon] ||= begin
|
||||||
macaroon_binary = ::File.read(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH))
|
macaroon_binary = ::File.read(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH))
|
||||||
macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
|
macaroon_binary.unpack("H*")
|
||||||
end
|
end
|
||||||
self.macaroon = options[:macaroon]
|
self.macaroon = options[:macaroon]
|
||||||
|
|
||||||
self.grpc_client = Lnrpc::Lightning::Stub.new(self.address, GRPC::Core::ChannelCredentials.new(self.credentials))
|
self.grpc_client = Lnrpc::Lightning::Stub.new(self.address, GRPC::Core::ChannelCredentials.new(self.credentials))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pay(payreq)
|
||||||
|
self.send_payment_sync(Lnrpc::SendRequest.new(payment_request: payreq))
|
||||||
|
end
|
||||||
|
|
||||||
def method_missing(m, *args, &block)
|
def method_missing(m, *args, &block)
|
||||||
if args.last.is_a?(Hash)
|
if self.grpc_client.respond_to?(m)
|
||||||
args[-1][:metadata] ||= {}
|
params = args[0]
|
||||||
args[-1][:metadata].merge!(macaroon: self.macaroon)
|
options = args[1] || { metadata: { macaroon: self.macaroon } }
|
||||||
|
|
||||||
|
request = params.nil? ? request_class_for(m).new : request_class_for(m).new(params)
|
||||||
|
self.grpc_client.send(m, request, options)
|
||||||
else
|
else
|
||||||
args.push({metadata: { macaroon: self.macaroon }})
|
super
|
||||||
end
|
end
|
||||||
self.grpc_client.send(m, *args)
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def request_class_for(method_name)
|
||||||
|
string = method_name.to_s.sub(/^[a-z\d]*/) { |match| match.capitalize }
|
||||||
|
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
|
||||||
|
Lnrpc.const_get("#{string}Request")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue