Class: Lnrpc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lnrpc/client.rb

Constant Summary collapse

LND_HOME_DIR =
ENV['LND_HOME'] || "~/.lnd"
DEFAULT_ADDRESS =
'localhost:10009'
DEFAULT_CREDENTIALS_PATH =
"#{LND_HOME_DIR}/tls.cert"
DEFAULT_MACAROON_PATH =
"#{LND_HOME_DIR}/data/chain/bitcoin/mainnet/admin.macaroon"
NON_CONVENTION_REQUEST_CLASSES =
{
  add_invoice: Lnrpc::Invoice,
  send_payment: Lnrpc::SendRequest,
  send_payment_sync: Lnrpc::SendRequest,
  open_channel_sync: Lnrpc::OpenChannelRequest,
  send_to_route_sync: Lnrpc::SendToRouteRequest,
  lookup_invoice: Lnrpc::PaymentHash,
  decode_pay_req: Lnrpc::PayReqString,
  describe_graph: Lnrpc::ChannelGraphRequest,
  get_chan_info: Lnrpc::ChanInfoRequest,
  get_node_info: Lnrpc::NodeInfoRequest,
  get_network_info: Lnrpc::NetworkInfoRequest,
  stop_daemon: Lnrpc::StopRequest,
  update_channel_policy: Lnrpc::PolicyUpdateResponse,
  subscribe_channel_graph: Lnrpc::GraphTopologySubscription,
  subscribe_invoices: Lnrpc::InvoiceSubscription,
  subscribe_transactions: Lnrpc::GetTransactionsRequest
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lnrpc/client.rb', line 32

def initialize(options={})
  self.address = options[:address] || DEFAULT_ADDRESS

  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

  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*")
  end
  self.macaroon = options[:macaroon]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/lnrpc/client.rb', line 60

def method_missing(m, *args, &block)
  if self.grpc_client.respond_to?(m)
    params  = args[0]

    args[0] = params.nil? ? request_class_for(m).new : request_class_for(m).new(params)
    self.grpc_client.send(m, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/lnrpc/client.rb', line 5

def address
  @address
end

#credentialsObject

Returns the value of attribute credentials.



5
6
7
# File 'lib/lnrpc/client.rb', line 5

def credentials
  @credentials
end

#grpc_clientObject



49
50
51
52
53
54
# File 'lib/lnrpc/client.rb', line 49

def grpc_client
  @grpc_client ||= Lnrpc::Lightning::Stub.new(self.address,
                                                GRPC::Core::ChannelCredentials.new(self.credentials),
                                                interceptors: [Lnrpc::MacaroonInterceptor.new(self.macaroon)]
                                              )
end

#macaroonObject

Returns the value of attribute macaroon.



5
6
7
# File 'lib/lnrpc/client.rb', line 5

def macaroon
  @macaroon
end

Instance Method Details

#inspectObject



71
72
73
# File 'lib/lnrpc/client.rb', line 71

def inspect
  "#{self.to_s} @address=\"#{self.address}\""
end

#pay(payreq) ⇒ Object



56
57
58
# File 'lib/lnrpc/client.rb', line 56

def pay(payreq)
  self.send_payment_sync(Lnrpc::SendRequest.new(payment_request: payreq))
end