-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlnd.py
84 lines (70 loc) · 2.83 KB
/
lnd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from lnd_grpc import lightning_pb2 as ln
from lnd_grpc import lightning_pb2_grpc as lnrpc
import os
import codecs
import grpc
from os.path import join, dirname
from dotenv import load_dotenv
load_dotenv(verbose=True)
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
MESSAGE_SIZE_MB = 50 * 1024 * 1024
class Lnd:
def __init__(self):
endpoint = os.getenv("LND_GRPC_ENDPOINT")
port = int(os.getenv("LND_GRPC_PORT"))
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
channel_options = [
('grpc.max_message_length', MESSAGE_SIZE_MB),
('grpc.max_receive_message_length', MESSAGE_SIZE_MB)
]
combined_creds = self.get_credentials(os.getenv("LND_GRPC_MACAROON"))
grpc_channel = grpc.secure_channel(f"{endpoint}:{port}", combined_creds, channel_options)
self.stub = lnrpc.LightningStub(grpc_channel)
combined_creds = self.get_credentials(os.getenv("LND_GRPC_MACAROON_INVOICE"))
grpc_channel = grpc.secure_channel(f"{endpoint}:{port}", combined_creds, channel_options)
self.stub_invoice = lnrpc.LightningStub(grpc_channel)
self.info = None
self.valid = True
try:
self.info = self.get_info()
except grpc._channel._InactiveRpcError:
self.valid = False
@staticmethod
def get_credentials(macaroon_path):
tls_cert = open(os.getenv("LND_GRPC_CERT"), 'rb').read()
ssl_credentials = grpc.ssl_channel_credentials(tls_cert)
macaroon = codecs.encode(open(macaroon_path, 'rb').read(), 'hex')
auth_credentials = grpc.metadata_call_credentials(lambda _, callback: callback([('macaroon', macaroon)], None))
combined_credentials = grpc.composite_channel_credentials(ssl_credentials, auth_credentials)
return combined_credentials
def get_info(self):
if self.info is None:
self.info = self.stub.GetInfo(ln.GetInfoRequest())
return self.info
def get_own_pubkey(self):
return self.get_info().identity_pubkey
def get_channels(self):
request = ln.ListChannelsRequest()
channels = self.stub.ListChannels(request)
return channels
def get_nodeinfo(self, pubkey):
request = ln.NodeInfoRequest(
pub_key = pubkey,
include_channels = True,
)
nodeinfo = self.stub.GetNodeInfo(request)
return nodeinfo
def get_invoice(self, amount=100, memo='memo'):
request = ln.Invoice(
memo=memo,
value=amount,
)
invoice = self.stub_invoice.AddInvoice(request)
return invoice
def get_lookupinvoice(self, payment_hash):
request = ln.PaymentHash(
r_hash_str=payment_hash,
)
lookupinvoice = self.stub.LookupInvoice(request)
return lookupinvoice