Module: Lightning::Stub

Included in:
Lightning, Invoice
Defined in:
lib/lightning/stub.rb

Overview

When this module is included in another class, we have access to the pre-generated stub (client) and can use it to communicate with the gRPC api.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#credentialsGRPC::Core::ChannelCredentials

Get SSL credentials from the tls.cert file generated by LND. This file is normally generated by LND and will be created by default at ~/.lnd/tls.cert on the server running your LND node. We will use it to establish a secured connection between your app and the node/server.

Returns:

  • (GRPC::Core::ChannelCredentials)

Since:

  • 0.1.0



51
52
53
54
55
56
57
# File 'lib/lightning/stub.rb', line 51

def credentials
  ENV['GRPC_SSL_CIPHER_SUITES'] =
    ENV['GRPC_SSL_CIPHER_SUITES'] || 'HIGH+ECDSA'

  certificate = File.read(File.expand_path('~/.lnd/tls.cert'))
  GRPC::Core::ChannelCredentials.new(certificate)
end

#macaroonBinary

Macaroon files works like cookies and are used to authenticate with LND gRPC. By default, when lnd starts, it creates three files which contain macaroons: a file called admin.macaroon, which contains a macaroon with no caveats, a file called readonly.macaroon, which is the same macaroon but with an additional caveat, that permits only methods that don't change the state of lnd, and invoice.macaroon, which only has access to invoice related methods. You can learn more about LND macaroons at: tiny.cc/1nuc5y

Returns:

  • (Binary)

Since:

  • 0.1.0



36
37
38
39
40
41
42
# File 'lib/lightning/stub.rb', line 36

def macaroon
  macaroon_binary = File.read(
    File.expand_path('~/.lnd/data/chain/bitcoin/mainnet/admin.macaroon')
  )

  macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
end

#return_res(response) ⇒ Object

A placeholder function added temporary so that we easly can manipulate the response of all request in the future in case we feel that's necessary.

Returns:

  • gRPC response

Since:

  • 0.1.0



64
65
66
# File 'lib/lightning/stub.rb', line 64

def return_res(response)
  response
end

#stubLnrpc::Lightning::Stub

On the client side, the client has a local object known as stub (for other languages, the preferred term is client) that implements the same methods as the service. The client can then just call those methods on the local object, wrapping the parameters for the call in the appropriate protocol buffer message type - gRPC looks after sending the request(s) to the server and returning the server's protocol buffer response(s). Read more about stubs at: tiny.cc/nwuc5y. We're using a pre-generated stub created with inspiration from the tutorial at: github.com/lightningnetwork/lnd/blob/master/docs/grpc/ruby.md

Returns:

  • (Lnrpc::Lightning::Stub)

Since:

  • 0.1.0



18
19
20
21
22
23
24
# File 'lib/lightning/stub.rb', line 18

def stub
  Lnrpc::Lightning::Stub.new(
    '127.0.0.1:10009',
    credentials,
    interceptors: [MacaroonInterceptor.new(macaroon)]
  )
end