2023-11-14 20:28:25 +01:00
|
|
|
import asyncio
|
|
|
|
import hashlib
|
2024-04-08 12:26:00 +02:00
|
|
|
import json
|
2023-11-14 20:28:25 +01:00
|
|
|
from typing import AsyncGenerator, Dict, Optional
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
from lnbits.settings import settings
|
|
|
|
|
|
|
|
from .base import (
|
|
|
|
InvoiceResponse,
|
2024-03-13 16:17:33 +01:00
|
|
|
PaymentPendingStatus,
|
2023-11-14 20:28:25 +01:00
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
|
|
|
StatusResponse,
|
|
|
|
Wallet,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AlbyWallet(Wallet):
|
|
|
|
"""https://guides.getalby.com/alby-wallet-api/reference/api-reference"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2023-12-24 14:23:21 +01:00
|
|
|
if not settings.alby_api_endpoint:
|
|
|
|
raise ValueError("cannot initialize AlbyWallet: missing alby_api_endpoint")
|
|
|
|
if not settings.alby_access_token:
|
|
|
|
raise ValueError("cannot initialize AlbyWallet: missing alby_access_token")
|
2023-11-14 20:28:25 +01:00
|
|
|
|
2023-12-24 15:03:58 +01:00
|
|
|
self.endpoint = self.normalize_endpoint(settings.alby_api_endpoint)
|
2023-11-14 20:28:25 +01:00
|
|
|
self.auth = {
|
|
|
|
"Authorization": "Bearer " + settings.alby_access_token,
|
2023-11-30 13:54:07 +01:00
|
|
|
"User-Agent": settings.user_agent,
|
2023-11-14 20:28:25 +01:00
|
|
|
}
|
|
|
|
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.auth)
|
|
|
|
|
|
|
|
async def cleanup(self):
|
|
|
|
try:
|
|
|
|
await self.client.aclose()
|
|
|
|
except RuntimeError as e:
|
|
|
|
logger.warning(f"Error closing wallet connection: {e}")
|
|
|
|
|
|
|
|
async def status(self) -> StatusResponse:
|
|
|
|
try:
|
|
|
|
r = await self.client.get("/balance", timeout=10)
|
2024-04-08 12:26:00 +02:00
|
|
|
r.raise_for_status()
|
2023-11-14 20:28:25 +01:00
|
|
|
|
2024-04-08 12:26:00 +02:00
|
|
|
data = r.json()
|
2023-11-14 20:28:25 +01:00
|
|
|
|
2024-04-08 12:26:00 +02:00
|
|
|
if len(data) == 0:
|
|
|
|
return StatusResponse("no data", 0)
|
|
|
|
|
|
|
|
if r.is_error or data["unit"] != "sat":
|
|
|
|
error_message = data["message"] if "message" in data else r.text
|
|
|
|
return StatusResponse(f"Server error: '{error_message}'", 0)
|
|
|
|
|
|
|
|
# multiply balance by 1000 to get msats balance
|
|
|
|
return StatusResponse(None, data["balance"] * 1000)
|
|
|
|
except KeyError as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return StatusResponse("Server error: 'missing required fields'", 0)
|
|
|
|
except json.JSONDecodeError as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return StatusResponse("Server error: 'invalid json response'", 0)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return StatusResponse(f"Unable to connect to {self.endpoint}.", 0)
|
2023-11-14 20:28:25 +01:00
|
|
|
|
|
|
|
async def create_invoice(
|
|
|
|
self,
|
|
|
|
amount: int,
|
|
|
|
memo: Optional[str] = None,
|
|
|
|
description_hash: Optional[bytes] = None,
|
|
|
|
unhashed_description: Optional[bytes] = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> InvoiceResponse:
|
|
|
|
# https://api.getalby.com/invoices
|
|
|
|
data: Dict = {"amount": f"{amount}"}
|
|
|
|
if description_hash:
|
|
|
|
data["description_hash"] = description_hash.hex()
|
|
|
|
elif unhashed_description:
|
|
|
|
data["description_hash"] = hashlib.sha256(unhashed_description).hexdigest()
|
|
|
|
else:
|
|
|
|
data["memo"] = memo or ""
|
|
|
|
|
2024-04-08 12:26:00 +02:00
|
|
|
try:
|
|
|
|
r = await self.client.post(
|
|
|
|
"/invoices",
|
|
|
|
json=data,
|
|
|
|
timeout=40,
|
|
|
|
)
|
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
|
|
|
|
if r.is_error:
|
|
|
|
error_message = data["message"] if "message" in data else r.text
|
|
|
|
return InvoiceResponse(False, None, None, error_message)
|
|
|
|
|
|
|
|
checking_id = data["payment_hash"]
|
|
|
|
payment_request = data["payment_request"]
|
|
|
|
return InvoiceResponse(True, checking_id, payment_request, None)
|
|
|
|
except KeyError as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return InvoiceResponse(
|
|
|
|
False, None, None, "Server error: 'missing required fields'"
|
|
|
|
)
|
|
|
|
except json.JSONDecodeError as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return InvoiceResponse(
|
|
|
|
False, None, None, "Server error: 'invalid json response'"
|
|
|
|
)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return InvoiceResponse(
|
|
|
|
False, None, None, f"Unable to connect to {self.endpoint}."
|
|
|
|
)
|
2023-11-14 20:28:25 +01:00
|
|
|
|
|
|
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
2024-04-08 12:26:00 +02:00
|
|
|
try:
|
|
|
|
# https://api.getalby.com/payments/bolt11
|
|
|
|
r = await self.client.post(
|
|
|
|
"/payments/bolt11",
|
|
|
|
json={"invoice": bolt11}, # assume never need amount in body
|
|
|
|
timeout=None,
|
|
|
|
)
|
|
|
|
r.raise_for_status()
|
|
|
|
data = r.json()
|
|
|
|
|
|
|
|
if r.is_error:
|
|
|
|
error_message = data["message"] if "message" in data else r.text
|
2024-05-10 11:49:50 +02:00
|
|
|
return PaymentResponse(None, None, None, None, error_message)
|
2024-04-08 12:26:00 +02:00
|
|
|
|
|
|
|
checking_id = data["payment_hash"]
|
|
|
|
# todo: confirm with bitkarrot that having the minus is fine
|
|
|
|
# other funding sources return a positive fee value
|
|
|
|
fee_msat = -data["fee"]
|
|
|
|
preimage = data["payment_preimage"]
|
|
|
|
|
|
|
|
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
|
|
|
|
except KeyError as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return PaymentResponse(
|
2024-05-10 11:49:50 +02:00
|
|
|
None, None, None, None, "Server error: 'missing required fields'"
|
2024-04-08 12:26:00 +02:00
|
|
|
)
|
|
|
|
except json.JSONDecodeError as exc:
|
|
|
|
logger.warning(exc)
|
|
|
|
return PaymentResponse(
|
2024-05-10 11:49:50 +02:00
|
|
|
None, None, None, None, "Server error: 'invalid json response'"
|
2024-04-08 12:26:00 +02:00
|
|
|
)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.info(f"Failed to pay invoice {bolt11}")
|
|
|
|
logger.warning(exc)
|
|
|
|
return PaymentResponse(
|
2024-05-10 11:49:50 +02:00
|
|
|
None, None, None, None, f"Unable to connect to {self.endpoint}."
|
2024-04-08 12:26:00 +02:00
|
|
|
)
|
2023-11-14 20:28:25 +01:00
|
|
|
|
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
|
|
|
return await self.get_payment_status(checking_id)
|
|
|
|
|
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2024-04-08 12:26:00 +02:00
|
|
|
try:
|
|
|
|
r = await self.client.get(f"/invoices/{checking_id}")
|
|
|
|
|
|
|
|
if r.is_error:
|
|
|
|
return PaymentPendingStatus()
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
|
2024-05-10 11:49:50 +02:00
|
|
|
# TODO: how can we detect a failed payment?
|
2024-04-08 12:26:00 +02:00
|
|
|
statuses = {
|
|
|
|
"CREATED": None,
|
|
|
|
"SETTLED": True,
|
|
|
|
}
|
|
|
|
# todo: extract fee and preimage
|
|
|
|
# maybe use the more specific endpoints:
|
|
|
|
# - https://api.getalby.com/invoices/incoming
|
|
|
|
# - https://api.getalby.com/invoices/outgoing
|
|
|
|
return PaymentStatus(
|
|
|
|
statuses[data.get("state")], fee_msat=None, preimage=None
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error getting invoice status: {e}")
|
2024-03-13 16:17:33 +01:00
|
|
|
return PaymentPendingStatus()
|
2023-11-14 20:28:25 +01:00
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
|
|
|
self.queue: asyncio.Queue = asyncio.Queue(0)
|
2024-04-22 11:33:53 +02:00
|
|
|
while settings.lnbits_running:
|
2023-11-14 20:28:25 +01:00
|
|
|
value = await self.queue.get()
|
|
|
|
yield value
|