forked from C-Otto/rebalance-lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnd.py
98 lines (80 loc) · 3.28 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
from os.path import expanduser
import codecs
import grpc
import rpc_pb2 as ln
import rpc_pb2_grpc as lnrpc
SERVER = 'localhost:10009'
LND_DIR = expanduser("~/.lnd")
MESSAGE_SIZE_MB = 50 * 1024 * 1024
class Lnd:
def __init__(self):
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
combined_credentials = self.get_credentials(LND_DIR)
channel_options = [
('grpc.max_message_length', MESSAGE_SIZE_MB),
('grpc.max_receive_message_length', MESSAGE_SIZE_MB)
]
grpc_channel = grpc.secure_channel(SERVER, combined_credentials, channel_options)
self.stub = lnrpc.LightningStub(grpc_channel)
self.graph = None
@staticmethod
def get_credentials(lnd_dir):
tls_certificate = open(lnd_dir + '/tls.cert', 'rb').read()
ssl_credentials = grpc.ssl_channel_credentials(tls_certificate)
macaroon = codecs.encode(open(lnd_dir + '/data/chain/bitcoin/mainnet/admin.macaroon', '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):
return self.stub.GetInfo(ln.GetInfoRequest())
def get_graph(self):
if self.graph is None:
self.graph = self.stub.DescribeGraph(ln.ChannelGraphRequest())
return self.graph
def get_own_pubkey(self):
return self.get_info().identity_pubkey
def get_current_height(self):
return self.get_info().block_height
def get_edges(self):
return self.get_graph().edges
def generate_invoice(self, memo, amount):
invoice_request = ln.Invoice(
memo=memo,
value=amount,
)
add_invoice_response = self.stub.AddInvoice(invoice_request)
return self.decode_payment_request(add_invoice_response.payment_request)
def decode_payment_request(self, payment_request):
request = ln.PayReqString(
pay_req=payment_request,
)
return self.stub.DecodePayReq(request)
def get_channels(self):
request = ln.ListChannelsRequest(
active_only=True,
)
return self.stub.ListChannels(request).channels
def get_routes(self, pub_key, amount, num_routes):
request = ln.QueryRoutesRequest(
pub_key=pub_key,
amt=amount,
num_routes=num_routes,
)
response = self.stub.QueryRoutes(request)
return response.routes
def get_policy(self, channel_id, source_pubkey):
# node1_policy contains the fee base and rate for payments from node1 to node2
for edge in self.get_edges():
if edge.channel_id == channel_id:
if edge.node1_pub == source_pubkey:
result = edge.node1_policy
else:
result = edge.node2_policy
return result
def send_payment(self, payment_request, routes):
payment_hash = payment_request.payment_hash
request = ln.SendToRouteRequest()
request.payment_hash_string = payment_hash
request.routes.extend(routes)
return self.stub.SendToRouteSync(request)