Skip to content

Instantly share code, notes, and snippets.

@willcl-ark
Created January 23, 2019 17:17
Show Gist options
  • Save willcl-ark/d2d54feb2a7968ee5073f48d541953cc to your computer and use it in GitHub Desktop.
Save willcl-ark/d2d54feb2a7968ee5073f48d541953cc to your computer and use it in GitHub Desktop.
import codecs
import json
import os
import sys
import pprint
import grpc
from google.protobuf.json_format import MessageToJson
from tabulate import tabulate
import rpc_pb2 as ln
import rpc_pb2_grpc as lnrpc
#-------------------------
# Generate RPC credentials
#-------------------------
# tell gRPC which cypher suite to use
os.environ["GRPC_SSL_CIPHER_SUITES"] = 'HIGH+ECDSA'
# grab tls cert
cert = open(os.path.expanduser('~/.lnd/tls.cert'), 'rb').read()
# grab the macaroon file for authentication
with open(os.path.expanduser('~/.lnd/data/chain/bitcoin/mainnet/admin.macaroon'), 'rb') as f:
macaroon_bytes = f.read()
macaroon = codecs.encode(macaroon_bytes, 'hex')
# helper function to return the macaroon when requested
def metadata_callback(context, callback):
callback([('macaroon', macaroon)], None)
# build ssl credentials using the cert the same as before
cert_creds = grpc.ssl_channel_credentials(cert)
# now build meta data credentials
auth_creds = grpc.metadata_call_credentials(metadata_callback)
# combine the cert credentials and the macaroon auth credentials
# such that every call is properly encrypted and authenticated
combined_creds = grpc.composite_channel_credentials(cert_creds, auth_creds)
# finally pass in the combined credentials when creating a channel
channel = grpc.secure_channel('localhost:10009', combined_creds)
stub = lnrpc.LightningStub(channel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment