lnbits-legend/lnbits/wallets/lndrest.py

97 lines
3.6 KiB
Python
Raw Normal View History

2020-04-25 23:10:45 +02:00
from os import getenv
from typing import Optional, Dict
2020-04-26 02:10:57 +02:00
import base64
2020-04-25 23:10:45 +02:00
from requests import get, post
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-04-25 23:10:45 +02:00
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
print(self.endpoint)
2020-04-25 23:41:27 +02:00
self.auth_admin = {"Grpc-Metadata-macaroon": getenv("LND_REST_ADMIN_MACAROON")}
self.auth_invoice = {"Grpc-Metadata-macaroon": getenv("LND_REST_INVOICE_MACAROON")}
self.auth_read = {"Grpc-Metadata-macaroon": getenv("LND_REST_READ_MACAROON")}
2020-04-25 23:10:45 +02:00
self.auth_cert = getenv("LND_REST_CERT")
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 ""
r = 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
ok, checking_id, payment_request, error_message = r.ok, None, None, None
if r.ok:
data = r.json()
payment_request = data["payment_request"]
2020-04-25 23:41:27 +02:00
r = get(url=f"{self.endpoint}/v1/payreq/{payment_request}", headers=self.auth_read, verify=self.auth_cert,)
print(r)
2020-04-25 23:10:45 +02:00
if r.ok:
checking_id = r.json()["payment_hash"].replace("/", "_")
2020-04-26 02:10:57 +02:00
print(checking_id)
2020-04-25 23:10:45 +02:00
error_message = None
ok = True
return InvoiceResponse(ok, checking_id, payment_request, error_message)
def pay_invoice(self, bolt11: str) -> PaymentResponse:
2020-04-26 02:10:57 +02:00
r = post(
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
ok, checking_id, fee_msat, error_message = r.ok, None, 0, None
2020-04-26 02:10:57 +02:00
r = get(url=f"{self.endpoint}/v1/payreq/{bolt11}", headers=self.auth_admin, verify=self.auth_cert,)
2020-04-25 23:10:45 +02:00
if r.ok:
2020-04-26 02:10:57 +02:00
checking_id = r.json()["payment_hash"]
2020-04-25 23:10:45 +02:00
else:
2020-04-26 02:10:57 +02:00
error_message = r.json()["error"]
2020-04-26 13:28:19 +02:00
2020-04-25 23:10:45 +02:00
return PaymentResponse(ok, checking_id, fee_msat, error_message)
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
checking_id = checking_id.replace("_", "/")
2020-04-26 02:10:57 +02:00
print(checking_id)
r = get(url=f"{self.endpoint}/v1/invoice/{checking_id}", headers=self.auth_invoice, verify=self.auth_cert,)
print(r.json()["settled"])
if not r or r.json()["settled"] == False:
2020-04-25 23:10:45 +02:00
return PaymentStatus(None)
2020-04-26 02:10:57 +02:00
return PaymentStatus(r.json()["settled"])
2020-04-25 23:10:45 +02:00
def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = get(
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-04-25 23:10:45 +02:00
if not r.ok:
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]
print(checking_id)
payment = payments[0] if payments else None
# check payment.status: https://api.lightning.community/rest/index.html?python#peersynctype
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"]])