lnrpc/lib/lnrpc/client.rb

105 lines
3.2 KiB
Ruby
Raw Normal View History

require "grpc"
2022-05-26 15:18:46 +00:00
require "digest"
require "lnrpc/macaroon_interceptor"
2019-02-08 05:51:42 +00:00
module Lnrpc
class Client
attr_accessor :address, :credentials, :macaroon
2019-02-08 05:51:42 +00:00
2019-02-12 02:12:48 +00:00
LND_HOME_DIR = ENV['LND_HOME'] || "~/.lnd"
2019-02-08 05:51:42 +00:00
DEFAULT_ADDRESS = 'localhost:10009'
2019-02-12 02:12:48 +00:00
DEFAULT_CREDENTIALS_PATH = "#{LND_HOME_DIR}/tls.cert"
DEFAULT_MACAROON_PATH = "#{LND_HOME_DIR}/data/chain/bitcoin/mainnet/admin.macaroon"
2019-02-08 05:51:42 +00:00
2019-02-12 02:12:48 +00:00
def initialize(options={})
2019-02-08 05:51:42 +00:00
self.address = options[:address] || DEFAULT_ADDRESS
if options.has_key?(:credentials)
2020-11-20 22:46:43 +00:00
# if there are non hex values prvided we assume it's the certificate file as string otherwise we assume it's the hex value
self.credentials = options[:credentials].match?(/\H/) ? options[:credentials] : [options[:credentials]].pack('H*')
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
2019-02-08 05:51:42 +00:00
2019-05-15 09:38:52 +00:00
if !options.has_key?(:macaroon) && File.exists?(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH))
options[:macaroon] = ::File.read(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH)).unpack("H*")
2019-02-08 05:51:42 +00:00
end
self.macaroon = options[:macaroon]
end
2019-02-08 05:51:42 +00:00
2020-05-05 20:08:35 +00:00
def lightning
@lightning ||= grpc_wrapper_for(Lnrpc::Lightning)
end
def autopilot
@autopilot ||= grpc_wrapper_for(Autopilotrpc::Autopilot)
end
def chain_notifier
@chain_notifier ||= grpc_wrapper_for(Chainrpc::ChainNotifier)
end
def invoices
@invoices ||= grpc_wrapper_for(Invoicesrpc::Invoices)
2019-02-08 05:51:42 +00:00
end
2020-05-05 20:08:35 +00:00
def router
@router ||= grpc_wrapper_for(Routerrpc::Router)
end
def signer
@signer ||= grpc_wrapper_for(Signrpc::Signer)
end
def versioner
@versioner ||= grpc_wrapper_for(Verrpc::Versioner)
end
def wallet_kit
@wallet_kit ||= grpc_wrapper_for(Walletrpc::WalletKit)
end
def wallet_unlocker
@wallet_unlocker ||= grpc_wrapper_for(Lnrpc::WalletUnlocker)
end
def watchtower
@watchtower ||= grpc_wrapper_for(Watchtowerrpc::Watchtower)
end
def watchtower_client
@watchtower_client ||= grpc_wrapper_for(Wtclientrpc::WatchtowerClient)
2019-02-12 02:12:48 +00:00
end
2020-05-05 20:08:35 +00:00
def keysend(args)
args[:dest_custom_records] ||= {}
args[:dest_custom_records][Lnrpc::KEY_SEND_PREIMAGE_TYPE] ||= Lnrpc.create_preimage
args[:payment_hash] ||= Digest::SHA256.digest(args[:dest_custom_records][Lnrpc::KEY_SEND_PREIMAGE_TYPE])
args[:timeout_seconds] ||= 60
router.send_payment_v2(args)
end
2019-02-12 02:12:48 +00:00
2020-05-05 20:08:35 +00:00
def pay(args)
args[:timeout_seconds] ||= 60
router.send_payment_v2(args)
2019-02-12 02:12:48 +00:00
end
2019-02-12 06:00:50 +00:00
def inspect
"#{self} @address=\"#{address}\""
2019-02-12 06:00:50 +00:00
end
private
def grpc_wrapper_for(grpc_module)
stub = grpc_module.const_get(:Stub)
service = grpc_module.const_get(:Service)
GrpcWrapper.new(service: service,
grpc: stub.new(address,
GRPC::Core::ChannelCredentials.new(credentials),
interceptors: [Lnrpc::MacaroonInterceptor.new(macaroon)]
))
end
2019-02-08 05:51:42 +00:00
end
end