|
@ -1,7 +1,10 @@ |
|
|
from os import getenv |
|
|
import httpx |
|
|
from typing import Optional, Dict |
|
|
import json |
|
|
import base64 |
|
|
import base64 |
|
|
from requests import get, post |
|
|
from os import getenv |
|
|
|
|
|
from typing import Optional, Dict, AsyncGenerator |
|
|
|
|
|
|
|
|
|
|
|
from lnbits import bolt11 |
|
|
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet |
|
|
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -12,10 +15,8 @@ class LndRestWallet(Wallet): |
|
|
|
|
|
|
|
|
endpoint = getenv("LND_REST_ENDPOINT") |
|
|
endpoint = getenv("LND_REST_ENDPOINT") |
|
|
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint |
|
|
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint |
|
|
print(self.endpoint) |
|
|
|
|
|
self.auth_admin = {"Grpc-Metadata-macaroon": getenv("LND_REST_ADMIN_MACAROON")} |
|
|
self.auth_admin = {"Grpc-Metadata-macaroon": getenv("LND_REST_ADMIN_MACAROON")} |
|
|
self.auth_invoice = {"Grpc-Metadata-macaroon": getenv("LND_REST_INVOICE_MACAROON")} |
|
|
self.auth_invoice = {"Grpc-Metadata-macaroon": getenv("LND_REST_INVOICE_MACAROON")} |
|
|
self.auth_read = {"Grpc-Metadata-macaroon": getenv("LND_REST_READ_MACAROON")} |
|
|
|
|
|
self.auth_cert = getenv("LND_REST_CERT") |
|
|
self.auth_cert = getenv("LND_REST_CERT") |
|
|
|
|
|
|
|
|
def create_invoice( |
|
|
def create_invoice( |
|
@ -30,84 +31,97 @@ class LndRestWallet(Wallet): |
|
|
else: |
|
|
else: |
|
|
data["memo"] = memo or "" |
|
|
data["memo"] = memo or "" |
|
|
|
|
|
|
|
|
r = post( |
|
|
r = httpx.post( |
|
|
url=f"{self.endpoint}/v1/invoices", |
|
|
url=f"{self.endpoint}/v1/invoices", |
|
|
headers=self.auth_invoice, |
|
|
headers=self.auth_invoice, |
|
|
verify=self.auth_cert, |
|
|
verify=self.auth_cert, |
|
|
json=data, |
|
|
json=data, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
ok, checking_id, payment_request, error_message = r.ok, None, None, None |
|
|
if r.is_error: |
|
|
|
|
|
error_message = r.text |
|
|
|
|
|
try: |
|
|
|
|
|
error_message = r.json()["error"] |
|
|
|
|
|
except Exception: |
|
|
|
|
|
pass |
|
|
|
|
|
return InvoiceResponse(False, None, None, error_message) |
|
|
|
|
|
|
|
|
if r.ok: |
|
|
data = r.json() |
|
|
data = r.json() |
|
|
payment_request = data["payment_request"] |
|
|
payment_request = data["payment_request"] |
|
|
payment_hash = base64.b64decode(data["r_hash"]).hex() |
|
|
|
|
|
checking_id = payment_hash |
|
|
r = get( |
|
|
|
|
|
url=f"{self.endpoint}/v1/payreq/{payment_request}", |
|
|
|
|
|
headers=self.auth_read, |
|
|
|
|
|
verify=self.auth_cert, |
|
|
|
|
|
) |
|
|
|
|
|
print(r) |
|
|
|
|
|
if r.ok: |
|
|
|
|
|
checking_id = r.json()["payment_hash"].replace("/", "_") |
|
|
|
|
|
print(checking_id) |
|
|
|
|
|
error_message = None |
|
|
|
|
|
ok = True |
|
|
|
|
|
|
|
|
|
|
|
return InvoiceResponse(ok, checking_id, payment_request, error_message) |
|
|
return InvoiceResponse(True, checking_id, payment_request, None) |
|
|
|
|
|
|
|
|
def pay_invoice(self, bolt11: str) -> PaymentResponse: |
|
|
def pay_invoice(self, bolt11: str) -> PaymentResponse: |
|
|
r = post( |
|
|
r = httpx.post( |
|
|
url=f"{self.endpoint}/v1/channels/transactions", |
|
|
url=f"{self.endpoint}/v1/channels/transactions", |
|
|
headers=self.auth_admin, |
|
|
headers=self.auth_admin, |
|
|
verify=self.auth_cert, |
|
|
verify=self.auth_cert, |
|
|
json={"payment_request": bolt11}, |
|
|
json={"payment_request": bolt11}, |
|
|
) |
|
|
) |
|
|
ok, checking_id, fee_msat, error_message = r.ok, None, 0, None |
|
|
|
|
|
r = get( |
|
|
|
|
|
url=f"{self.endpoint}/v1/payreq/{bolt11}", |
|
|
|
|
|
headers=self.auth_admin, |
|
|
|
|
|
verify=self.auth_cert, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if r.ok: |
|
|
if r.is_error: |
|
|
checking_id = r.json()["payment_hash"] |
|
|
error_message = r.text |
|
|
else: |
|
|
try: |
|
|
error_message = r.json()["error"] |
|
|
error_message = r.json()["error"] |
|
|
|
|
|
except: |
|
|
|
|
|
pass |
|
|
|
|
|
return PaymentResponse(False, None, 0, error_message) |
|
|
|
|
|
|
|
|
|
|
|
payment_hash = r.json()["payment_hash"] |
|
|
|
|
|
checking_id = payment_hash |
|
|
|
|
|
|
|
|
return PaymentResponse(ok, checking_id, fee_msat, error_message) |
|
|
return PaymentResponse(True, checking_id, 0, None) |
|
|
|
|
|
|
|
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus: |
|
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus: |
|
|
checking_id = checking_id.replace("_", "/") |
|
|
checking_id = checking_id.replace("_", "/") |
|
|
print(checking_id) |
|
|
r = httpx.get( |
|
|
r = get( |
|
|
|
|
|
url=f"{self.endpoint}/v1/invoice/{checking_id}", |
|
|
url=f"{self.endpoint}/v1/invoice/{checking_id}", |
|
|
headers=self.auth_invoice, |
|
|
headers=self.auth_invoice, |
|
|
verify=self.auth_cert, |
|
|
verify=self.auth_cert, |
|
|
) |
|
|
) |
|
|
print(r.json()["settled"]) |
|
|
|
|
|
if not r or r.json()["settled"] == False: |
|
|
if not r or r.json()["settled"] == False: |
|
|
return PaymentStatus(None) |
|
|
return PaymentStatus(None) |
|
|
|
|
|
|
|
|
return PaymentStatus(r.json()["settled"]) |
|
|
return PaymentStatus(r.json()["settled"]) |
|
|
|
|
|
|
|
|
def get_payment_status(self, checking_id: str) -> PaymentStatus: |
|
|
def get_payment_status(self, checking_id: str) -> PaymentStatus: |
|
|
r = get( |
|
|
r = httpx.get( |
|
|
url=f"{self.endpoint}/v1/payments", |
|
|
url=f"{self.endpoint}/v1/payments", |
|
|
headers=self.auth_admin, |
|
|
headers=self.auth_admin, |
|
|
verify=self.auth_cert, |
|
|
verify=self.auth_cert, |
|
|
params={"include_incomplete": "True", "max_payments": "20"}, |
|
|
params={"include_incomplete": "True", "max_payments": "20"}, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
if not r.ok: |
|
|
if r.is_error: |
|
|
return PaymentStatus(None) |
|
|
return PaymentStatus(None) |
|
|
|
|
|
|
|
|
payments = [p for p in r.json()["payments"] if p["payment_hash"] == checking_id] |
|
|
payments = [p for p in r.json()["payments"] if p["payment_hash"] == checking_id] |
|
|
print(checking_id) |
|
|
|
|
|
payment = payments[0] if payments else None |
|
|
payment = payments[0] if payments else None |
|
|
|
|
|
|
|
|
# check payment.status: https://api.lightning.community/rest/index.html?python#peersynctype |
|
|
# check payment.status: |
|
|
|
|
|
# https://api.lightning.community/rest/index.html?python#peersynctype |
|
|
statuses = {"UNKNOWN": None, "IN_FLIGHT": None, "SUCCEEDED": True, "FAILED": False} |
|
|
statuses = {"UNKNOWN": None, "IN_FLIGHT": None, "SUCCEEDED": True, "FAILED": False} |
|
|
|
|
|
|
|
|
return PaymentStatus(statuses[payment["status"]]) |
|
|
return PaymentStatus(statuses[payment["status"]]) |
|
|
|
|
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: |
|
|
|
|
|
url = self.endpoint + "/v1/invoices/subscribe" |
|
|
|
|
|
|
|
|
|
|
|
async with httpx.AsyncClient(timeout=None, headers=self.auth_admin, verify=self.auth_cert) as client: |
|
|
|
|
|
async with client.stream("GET", url) as r: |
|
|
|
|
|
print("ok") |
|
|
|
|
|
print(r) |
|
|
|
|
|
print(r.is_error) |
|
|
|
|
|
print("ok") |
|
|
|
|
|
async for line in r.aiter_lines(): |
|
|
|
|
|
print("line", line) |
|
|
|
|
|
try: |
|
|
|
|
|
event = json.loads(line)["result"] |
|
|
|
|
|
print(event) |
|
|
|
|
|
except: |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
payment_hash = bolt11.decode(event["payment_request"]).payment_hash |
|
|
|
|
|
yield payment_hash |
|
|