Merge pull request #5 from bumi/support-for-system-certs

Add support for system/default certs/credentials
This commit is contained in:
bumi 2019-03-04 20:42:22 +00:00 committed by GitHub
commit cee1f7589c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View File

@ -75,7 +75,7 @@ Also have a look at [examples.rb](https://github.com/bumi/lnrpc/blob/master/exam
The Lnrpc::Client constructor allows the following options:
* credentials:
- `credentials` : the credentials data as string
- `credentials` : the credentials data as string (pass nil if a "trusted" cert (e.g. from letsencrypt is used)
- `credentials_path` : the path to a credentials file (tls.cert) as string (default: `"#{LND_HOME_DIR}/tls.cert"` )
* macaroon:
- `macaroon` : the macaroon as hex string

View File

@ -2,7 +2,8 @@ require "grpc"
require "lnrpc/macaroon_interceptor"
module Lnrpc
class Client
attr_accessor :address, :credentials, :macaroon, :grpc_client
attr_accessor :address, :credentials, :macaroon
attr_writer :grpc_client
LND_HOME_DIR = ENV['LND_HOME'] || "~/.lnd"
DEFAULT_ADDRESS = 'localhost:10009'
@ -31,19 +32,25 @@ module Lnrpc
def initialize(options={})
self.address = options[:address] || DEFAULT_ADDRESS
options[:credentials] ||= ::File.read(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
self.credentials = options[:credentials]
if options.has_key?(:credentials)
self.credentials = options[:credentials]
elsif File.exists?(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
self.credentials = ::File.read(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
else
self.credentials = nil
end
options[:macaroon] ||= begin
macaroon_binary = ::File.read(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH))
macaroon_binary.unpack("H*")
unless options.has_key?(:macaroon)
options[:macaroon] = ::File.read(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH)).unpack("H*")
end
self.macaroon = options[:macaroon]
end
self.grpc_client = Lnrpc::Lightning::Stub.new(self.address,
def grpc_client
@grpc_client ||= Lnrpc::Lightning::Stub.new(self.address,
GRPC::Core::ChannelCredentials.new(self.credentials),
interceptors: [Lnrpc::MacaroonInterceptor.new(self.macaroon)]
)
)
end
def pay(payreq)