2020-10-02 22:13:33 +02:00
|
|
|
import httpx
|
|
|
|
import json
|
2020-04-26 02:10:57 +02:00
|
|
|
import base64
|
2020-10-02 22:13:33 +02:00
|
|
|
from os import getenv
|
|
|
|
from typing import Optional, Dict, AsyncGenerator
|
|
|
|
|
2020-04-25 23:10:45 +02:00
|
|
|
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
|
|
|
|
|
|
|
|
|
|
|
class LndRestWallet(Wallet):
|
|
|
|
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2020-04-25 23:41:27 +02:00
|
|
|
endpoint = getenv("LND_REST_ENDPOINT")
|
2020-10-05 13:45:57 +02:00
|
|
|
endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
|
|
|
endpoint = "https://" + endpoint if not endpoint.startswith("http") else endpoint
|
|
|
|
self.endpoint = endpoint
|
|
|
|
|
2020-10-03 22:27:55 +02:00
|
|
|
self.auth_admin = {
|
|
|
|
"Grpc-Metadata-macaroon": getenv("LND_ADMIN_MACAROON") or getenv("LND_REST_ADMIN_MACAROON"),
|
|
|
|
}
|
|
|
|
self.auth_invoice = {
|
|
|
|
"Grpc-Metadata-macaroon": getenv("LND_INVOICE_MACAROON") or getenv("LND_REST_INVOICE_MACAROON")
|
|
|
|
}
|
2020-04-25 23:10:45 +02:00
|
|
|
self.auth_cert = getenv("LND_REST_CERT")
|
|
|
|
|
2020-08-31 04:48:46 +02:00
|
|
|
def create_invoice(
|
|
|
|
self, amount: int, memo: Optional[str] = None, description_hash: Optional[bytes] = None
|
|
|
|
) -> InvoiceResponse:
|
|
|
|
data: Dict = {
|
|
|
|
"value": amount,
|
|
|
|
"private": True,
|
|
|
|
}
|
|
|
|
if description_hash:
|
|
|
|
data["description_hash"] = base64.b64encode(description_hash).decode("ascii")
|
|
|
|
else:
|
|
|
|
data["memo"] = memo or ""
|
|
|
|
|
2020-10-06 03:16:28 +02:00
|
|
|
r = httpx.post(url=f"{self.endpoint}/v1/invoices", headers=self.auth_invoice, verify=self.auth_cert, json=data,)
|
2020-04-25 23:10:45 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
if r.is_error:
|
|
|
|
error_message = r.text
|
|
|
|
try:
|
|
|
|
error_message = r.json()["error"]
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return InvoiceResponse(False, None, None, error_message)
|
2020-04-25 23:10:45 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
data = r.json()
|
|
|
|
payment_request = data["payment_request"]
|
|
|
|
payment_hash = base64.b64decode(data["r_hash"]).hex()
|
|
|
|
checking_id = payment_hash
|
2020-04-25 23:10:45 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
return InvoiceResponse(True, checking_id, payment_request, None)
|
2020-04-25 23:10:45 +02:00
|
|
|
|
|
|
|
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
2020-10-02 22:13:33 +02:00
|
|
|
r = httpx.post(
|
2020-06-08 00:46:16 +02:00
|
|
|
url=f"{self.endpoint}/v1/channels/transactions",
|
|
|
|
headers=self.auth_admin,
|
|
|
|
verify=self.auth_cert,
|
|
|
|
json={"payment_request": bolt11},
|
2020-04-26 02:10:57 +02:00
|
|
|
)
|
2020-04-25 23:10:45 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
if r.is_error:
|
|
|
|
error_message = r.text
|
|
|
|
try:
|
|
|
|
error_message = r.json()["error"]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return PaymentResponse(False, None, 0, error_message)
|
|
|
|
|
|
|
|
payment_hash = r.json()["payment_hash"]
|
|
|
|
checking_id = payment_hash
|
2020-04-26 13:28:19 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
return PaymentResponse(True, checking_id, 0, None)
|
2020-04-25 23:10:45 +02:00
|
|
|
|
|
|
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
2020-06-08 00:46:16 +02:00
|
|
|
checking_id = checking_id.replace("_", "/")
|
2020-10-02 22:13:33 +02:00
|
|
|
r = httpx.get(
|
2020-10-06 03:16:28 +02:00
|
|
|
url=f"{self.endpoint}/v1/invoice/{checking_id}", headers=self.auth_invoice, verify=self.auth_cert,
|
2020-09-03 23:02:15 +02:00
|
|
|
)
|
2020-10-06 03:16:28 +02:00
|
|
|
|
2020-10-06 03:43:49 +02:00
|
|
|
if r.is_error or not r.json().get("settled"):
|
2020-10-06 03:16:28 +02:00
|
|
|
# this must also work when checking_id is not a hex recognizable by lnd
|
|
|
|
# it will return an error and no "settled" attribute on the object
|
2020-04-25 23:10:45 +02:00
|
|
|
return PaymentStatus(None)
|
|
|
|
|
2020-10-06 03:43:49 +02:00
|
|
|
return PaymentStatus(True)
|
2020-04-25 23:10:45 +02:00
|
|
|
|
|
|
|
def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2020-10-02 22:13:33 +02:00
|
|
|
r = httpx.get(
|
2020-06-08 00:46:16 +02:00
|
|
|
url=f"{self.endpoint}/v1/payments",
|
|
|
|
headers=self.auth_admin,
|
|
|
|
verify=self.auth_cert,
|
|
|
|
params={"include_incomplete": "True", "max_payments": "20"},
|
|
|
|
)
|
2020-04-26 13:28:19 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
if r.is_error:
|
2020-04-26 13:28:19 +02:00
|
|
|
return PaymentStatus(None)
|
2020-04-26 02:10:57 +02:00
|
|
|
|
|
|
|
payments = [p for p in r.json()["payments"] if p["payment_hash"] == checking_id]
|
|
|
|
payment = payments[0] if payments else None
|
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
# check payment.status:
|
|
|
|
# https://api.lightning.community/rest/index.html?python#peersynctype
|
2020-04-26 02:10:57 +02:00
|
|
|
statuses = {"UNKNOWN": None, "IN_FLIGHT": None, "SUCCEEDED": True, "FAILED": False}
|
2020-04-25 23:10:45 +02:00
|
|
|
|
2020-04-26 02:10:57 +02:00
|
|
|
return PaymentStatus(statuses[payment["status"]])
|
2020-10-02 22:13:33 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
async for line in r.aiter_lines():
|
|
|
|
try:
|
2020-10-03 22:27:55 +02:00
|
|
|
inv = json.loads(line)["result"]
|
|
|
|
if not inv["settled"]:
|
|
|
|
continue
|
2020-10-02 22:13:33 +02:00
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2020-10-03 22:27:55 +02:00
|
|
|
payment_hash = base64.b64decode(inv["r_hash"]).hex()
|
2020-10-02 22:13:33 +02:00
|
|
|
yield payment_hash
|