Update lndgrpc.py
This commit is contained in:
parent
b02bee01e5
commit
efc847d381
1 changed files with 20 additions and 17 deletions
|
|
@ -1,9 +1,11 @@
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
import os
|
||||||
|
import base64
|
||||||
import lnd_grpc # https://github.com/willcl-ark/lnd_grpc/blob/master/lnd_grpc/lightning.py
|
import lnd_grpc # https://github.com/willcl-ark/lnd_grpc/blob/master/lnd_grpc/lightning.py
|
||||||
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
||||||
|
|
||||||
|
|
||||||
class LndWalletgrpc(Wallet):
|
class LndWallet(Wallet):
|
||||||
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""
|
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -11,10 +13,10 @@ class LndWalletgrpc(Wallet):
|
||||||
endpoint = getenv("LND_GRPC_ENDPOINT")
|
endpoint = getenv("LND_GRPC_ENDPOINT")
|
||||||
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
||||||
self.port = getenv("LND_GRPC_PORT")
|
self.port = getenv("LND_GRPC_PORT")
|
||||||
self.auth_admin = getenv("LND_ADMIN_MACAROON")
|
self.auth_admin = os.path.expanduser("~/")+(getenv("LND_ADMIN_MACAROON"))
|
||||||
self.auth_invoice = getenv("LND_INVOICE_MACAROON")
|
self.auth_invoice = os.path.expanduser("~/")+(getenv("LND_INVOICE_MACAROON"))
|
||||||
self.auth_read = getenv("LND_READ_MACAROON")
|
self.auth_read = os.path.expanduser("~/")+(getenv("LND_READ_MACAROON"))
|
||||||
self.auth_cert = getenv("LND_CERT")
|
self.auth_cert = os.path.expanduser("~/")+(getenv("LND_CERT"))
|
||||||
|
|
||||||
def create_invoice(self, amount: int, mem: str = "") -> InvoiceResponse:
|
def create_invoice(self, amount: int, mem: str = "") -> InvoiceResponse:
|
||||||
|
|
||||||
|
|
@ -28,14 +30,14 @@ class LndWalletgrpc(Wallet):
|
||||||
)
|
)
|
||||||
|
|
||||||
lndResponse = lnd_rpc.add_invoice(
|
lndResponse = lnd_rpc.add_invoice(
|
||||||
memo = mem,
|
memo = "mem",
|
||||||
value = amount,
|
value = 20,
|
||||||
expiry = 600,
|
expiry = 600,
|
||||||
private = True
|
private = True
|
||||||
)
|
)
|
||||||
|
decoded_hash = base64.b64encode(lndResponse.r_hash).decode('utf-8').replace("/","_")
|
||||||
ok, checking_id, payment_request, error_message = True, lndResponse.r_hash, lndResponse.payment_request, None
|
print(lndResponse.r_hash)
|
||||||
|
ok, checking_id, payment_request, error_message = True, decoded_hash, str(lndResponse.payment_request), None
|
||||||
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
||||||
|
|
||||||
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
||||||
|
|
@ -50,7 +52,7 @@ class LndWalletgrpc(Wallet):
|
||||||
)
|
)
|
||||||
|
|
||||||
payinvoice = lnd_rpc.pay_invoice( # https://github.com/willcl-ark/lnd_grpc/blob/cf938c51c201f078e8bbe9e19ffc2d038f3abf7f/lnd_grpc/lightning.py#L439
|
payinvoice = lnd_rpc.pay_invoice( # https://github.com/willcl-ark/lnd_grpc/blob/cf938c51c201f078e8bbe9e19ffc2d038f3abf7f/lnd_grpc/lightning.py#L439
|
||||||
payment_request = _payreq,
|
payment_request = bolt11,
|
||||||
)
|
)
|
||||||
|
|
||||||
ok, checking_id, fee_msat, error_message = True, None, 0, None
|
ok, checking_id, fee_msat, error_message = True, None, 0, None
|
||||||
|
|
@ -58,12 +60,14 @@ class LndWalletgrpc(Wallet):
|
||||||
if payinvoice.payment_error:
|
if payinvoice.payment_error:
|
||||||
ok, error_message = False, payinvoice.payment_error
|
ok, error_message = False, payinvoice.payment_error
|
||||||
else:
|
else:
|
||||||
checking_id = payinvoice.payment_hash
|
checking_id = base64.b64encode(payinvoice.payment_hash).decode('utf-8').replace("/","_")
|
||||||
|
|
||||||
return PaymentResponse(ok, checking_id, fee_msat, error_message)
|
return PaymentResponse(ok, checking_id, fee_msat, error_message)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
|
|
||||||
|
check_id = base64.b64decode(checking_id.replace("_","/"))
|
||||||
|
print(check_id)
|
||||||
lnd_rpc = lnd_grpc.Client(
|
lnd_rpc = lnd_grpc.Client(
|
||||||
lnd_dir = None,
|
lnd_dir = None,
|
||||||
macaroon_path = self.auth_invoice,
|
macaroon_path = self.auth_invoice,
|
||||||
|
|
@ -73,16 +77,15 @@ class LndWalletgrpc(Wallet):
|
||||||
grpc_port = self.port
|
grpc_port = self.port
|
||||||
)
|
)
|
||||||
|
|
||||||
for _response in lnd_rpc.subscribe_single_invoice(_hash):
|
for _response in lnd_rpc.subscribe_single_invoice(check_id):
|
||||||
# myQueue.put(_response) # ???
|
|
||||||
|
|
||||||
if _response.state == 1:
|
if _response.state == 1:
|
||||||
|
|
||||||
return PaymentStatus("settled")
|
return PaymentStatus(True)
|
||||||
|
|
||||||
invoiceThread = threading.Thread(
|
invoiceThread = threading.Thread(
|
||||||
target=detectPayment,
|
target=detectPayment,
|
||||||
args=[lndResponse.r_hash, ],
|
args=[lndResponse.check_id, ],
|
||||||
daemon=True
|
daemon=True
|
||||||
)
|
)
|
||||||
invoiceThread.start()
|
invoiceThread.start()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue