Skip to content

Instantly share code, notes, and snippets.

@webzorg
Created February 16, 2018 09:05
Show Gist options
  • Save webzorg/3c6f22ac650846d2462f600688990518 to your computer and use it in GitHub Desktop.
Save webzorg/3c6f22ac650846d2462f600688990518 to your computer and use it in GitHub Desktop.
How to write a Ruby gRPC client for the Lightning Network Daemon

How to write a Ruby gRPC client for the Lightning Network Daemon

Command sequence assumes you will stay in the same dir

  • Install dependencies (googleapis-common-protos is required due to the use of google/api/annotations.proto)
$ gem install grpc grpc-tools googleapis-common-protos
  • Clone the google api's repository (required due to the use of google/api/annotations.proto)
$ git clone https://github.com/googleapis/googleapis.git
  • Copy the lnd rpc.proto file (you'll find this in lnrpc/rpc.proto) or just download it
$ curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
  • Copy the rpc.proto file inside googleapis dir
cp rpc.proto googleapis
  • Compile the proto file
$ grpc_tools_ruby_protoc -I googleapis --ruby_out=. --grpc_out=. rpc.proto

After following these steps, two files rpc_pb2.rb and rpc_services_pb.rb will be generated. These files will be imported in your project while writing your clients. Here's an example of a simple client that was built using these.

$LOAD_PATH.unshift(".")
require "grpc"
require "rpc_pb"
require "rpc_services_pb"
require "pp"

user_name = user # well, set this ;)

channel_creds = GRPC::Core::ChannelCredentials.new(File.read("/home/#{user_name}/.lnd/tls.cert"))
stub = Lnrpc::Lightning::Stub.new("127.0.0.1:10009", channel_creds)


pp stub.get_info(Lnrpc::GetInfoRequest.new).to_h
puts

pp stub.wallet_balance(Lnrpc::WalletBalanceRequest.new(witness_only: true)).to_h

# can't figure out how to pass in macaroons if you do, please let me know :) 
# macaroon = File.read("/home/#{user_name}/.lnd/admin.macaroon")
# macaroon = macaroon.force_encoding("utf-8")
# macaroon = macaroon.each_byte.map { |b| b.to_s(16) }.join

# This is python implementation for inputing macaroons in a request for reference, thanks to Freenode:lnd Veggen
# macaroon= binascii.hexlify(open(os.path.expanduser("~/.lnd/admin.macaroon"), 'rb').read()).encode('utf-8')
# metadata = [("macaroon", macaroon)]
# stub = lnrpc.LightningStub(channel)
# response = stub.ListChannels(ln.ListChannelsRequest(), metadata=metadata)

# Thanks takinbo
# Reference https://gist.github.com/takinbo/27b58c84dcb58abf8c556caad3949aea
@ryan-lingle
Copy link

did you ever figure out passing macaroons?

@bumi
Copy link

bumi commented Oct 26, 2018

passing the macaroon should be something like this:

macaroon_binary = File.read("/home/#{user_name}/.lnd/admin.macaroon")
macaroon = macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join

pp stub.get_info(Lnrpc::GetInfoRequest.new, metadata: { macaroon: macaroon }).to_h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment