commit 60a3b30547de4827cb4821dcf03a350b8a9ee627
parent f46daf751351e120065ef4536fdf06a546e89ab0
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date: Tue, 12 Jan 2021 23:23:34 +1100
initial support for lightning network daemon
Diffstat:
M | pay/lnd.py | | | 57 | ++++++++++++++++++++++++++++++++++++--------------------- |
M | server.py | | | 18 | ++++++++++++++++-- |
2 files changed, 52 insertions(+), 23 deletions(-)
diff --git a/pay/lnd.py b/pay/lnd.py
@@ -13,7 +13,7 @@ class lnd(invoice):
super().__init__(dollar_value, currency, label)
print(self.__dict__)
- from lndgrpc import LNDClient
+ from lnd_grpc import Client
# Copy admin macaroon and tls cert to local machine
try:
@@ -33,10 +33,10 @@ class lnd(invoice):
try:
# Require admin=True for creating invoices
print("Attempting to initialise lnd rpc client...")
- self.lnd = LNDClient("{}:{}".format(config.host, config.rpcport),
- admin=True,
- macaroon_filepath="admin.macaroon",
- cert_filepath="tls.cert")
+ self.lnd = Client(grpc_host=config.host,
+ grpc_port=config.rpcport,
+ macaroon_path="admin.macaroon",
+ tls_cert_path="tls.cert")
# print("Getting lnd info...")
# info = self.lnd.get_info()
@@ -56,33 +56,48 @@ class lnd(invoice):
def create_lnd_invoice(self, btc_amount):
# Multiplying by 10^8 to convert to satoshi units
sats_amount = int(btc_amount*10**8)
- self.lnd_invoice = json.loads(MessageToJson(self.lnd.add_invoice(sats_amount)))
- print(self.lnd_invoice)
- print("printed")
-
- self.hash = str(b64decode(self.lnd_invoice['r_hash']).hex())
- # self.hash = str(b64decode(self.lnd_invoice['rHash']).hex())
-
- print("Created invoice: {}".format(self.hash))
+ res = self.lnd.add_invoice(value=sats_amount)
+ self.lnd_invoice = json.loads(MessageToJson(res))
+ self.hash = res.r_hash #self.lnd_invoice['r_hash']
+ print("Create invoice response:")
+ print(res)
return self.lnd_invoice['payment_request']
+
+ # self.lnd_invoice = json.loads(MessageToJson(self.lnd.add_invoice(sats_amount)))
+ # print(self.lnd_invoice)
+ # print("printed")
+ #
+ # self.hash = str(b64decode(self.lnd_invoice['r_hash']).hex())
+ # # self.hash = str(b64decode(self.lnd_invoice['rHash']).hex())
+ #
+ # print("Created invoice: {}".format(self.hash))
+ # return self.lnd_invoice['payment_request']
# return self.lnd_invoice['paymentRequest']
def get_address(self):
- for i in range(config.connection_attempts):
- try:
- self.address = self.create_lnd_invoice(self.value)
- except Exception as e:
- print(e)
- print("Attempting again... {}/{}...".format(i+1, config.connection_attempts))
+ self.address = self.create_lnd_invoice(self.value)
+
+ # for i in range(config.connection_attempts):
+ # try:
+ # self.address = self.create_lnd_invoice(self.value)
+ # except Exception as e:
+ # print(e)
+ # print("Attempting again... {}/{}...".format(i+1, config.connection_attempts))
return
def check_payment(self):
- print("Looking up...")
+ print("Looking up invoice")
# For some reason this does not work, I think lookup_invoice() may be broken
# as it does not return the correct response that includes the amount paid among other fields.
- print(self.lnd.lookup_invoice(self.hash))
+ print(self.lnd.list_invoices())
+ print(self.lnd.lookup_invoice(r_hash=self.hash))
+ print("Invoice ^")
+ print(type(self.hash))
+ print(str(self.hash.hex()))
+
+ # print(str(b64decode(self.hash.strip('\\'))))
# invoice_status = json.loads(MessageToJson(self.lnd.lookup_invoice(self.hash)))
# print(invoice_status)
# print(self.lnd.lookup_invoice("8893044a07c2c5e2a50252f044224f297487242e05758a970d5ba28ece75f66d"))
diff --git a/server.py b/server.py
@@ -7,6 +7,7 @@ import ssh_tunnel
import config
import invoice
from pay import bitcoind
+from pay import lnd
# Begin websocket
async_mode = None
@@ -69,7 +70,16 @@ def make_payment(payload):
# Initialise the payment via the payment method (bitcoind / lightningc / etc),
# create qr code for the payment.
def create_invoice(dollar_amount, currency, label):
- payment = bitcoind.btcd(dollar_amount, currency, label)
+ if config.pay_method == "bitcoind":
+ print("AHHHHHhhhhhhh")
+ payment = bitcoind.btcd(dollar_amount, currency, label)
+ elif config.pay_method == "lnd":
+ payment = lnd.lnd(dollar_amount, currency, label)
+ else:
+ # There probably should be config checking code within main.py
+ print("Invalid payment method")
+ return
+
payment.get_address()
payment.create_qr()
return payment
@@ -133,9 +143,13 @@ def process_payment(payment):
# Test Bitcoind connection on startup:
print("Checking node connectivity...")
-bitcoind.btcd(1, 'USD', 'Init test.')
+if config.pay_method == "bitcoind":
+ bitcoind.btcd(1, 'USD', 'Init test.')
+elif config.pay_method == "lnd":
+ lnd.lnd(1, 'USD', 'Init test')
print("Connection successful.")
if __name__ == '__main__':
socket_.run(app, debug=True)
+ socket_.run(app, debug=False)