2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2020-09-28 04:12:55 +02:00
|
|
|
import json
|
2020-09-30 03:04:51 +02:00
|
|
|
import httpx
|
2021-03-28 01:24:08 +01:00
|
|
|
import random
|
2020-08-29 17:23:01 +02:00
|
|
|
from os import getenv
|
2020-09-28 04:12:55 +02:00
|
|
|
from typing import Optional, AsyncGenerator
|
2020-08-29 17:23:01 +02:00
|
|
|
|
2021-03-24 04:40:32 +01:00
|
|
|
from .base import (
|
|
|
|
StatusResponse,
|
|
|
|
InvoiceResponse,
|
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
|
|
|
Wallet,
|
|
|
|
)
|
2020-08-29 17:23:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SparkError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SparkWallet(Wallet):
|
|
|
|
def __init__(self):
|
2020-09-28 04:12:55 +02:00
|
|
|
self.url = getenv("SPARK_URL").replace("/rpc", "")
|
2020-08-29 17:23:01 +02:00
|
|
|
self.token = getenv("SPARK_TOKEN")
|
|
|
|
|
|
|
|
def __getattr__(self, key):
|
2021-03-24 05:01:09 +01:00
|
|
|
async def call(*args, **kwargs):
|
2020-08-29 17:23:01 +02:00
|
|
|
if args and kwargs:
|
2021-03-24 04:40:32 +01:00
|
|
|
raise TypeError(
|
|
|
|
f"must supply either named arguments or a list of arguments, not both: {args} {kwargs}"
|
|
|
|
)
|
2020-08-29 17:23:01 +02:00
|
|
|
elif args:
|
|
|
|
params = args
|
|
|
|
elif kwargs:
|
|
|
|
params = kwargs
|
2020-10-13 18:57:26 +02:00
|
|
|
else:
|
|
|
|
params = {}
|
2020-08-29 17:23:01 +02:00
|
|
|
|
2021-03-28 01:24:08 +01:00
|
|
|
try:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.post(
|
|
|
|
self.url + "/rpc",
|
|
|
|
headers={"X-Access": self.token},
|
|
|
|
json={"method": key, "params": params},
|
|
|
|
timeout=40,
|
|
|
|
)
|
|
|
|
except (OSError, httpx.ConnectError, httpx.RequestError) as exc:
|
2021-04-10 22:37:48 +02:00
|
|
|
raise UnknownError("error connecting to spark: " + str(exc))
|
2021-03-24 05:01:09 +01:00
|
|
|
|
2020-08-29 17:23:01 +02:00
|
|
|
try:
|
|
|
|
data = r.json()
|
|
|
|
except:
|
|
|
|
raise UnknownError(r.text)
|
2020-10-13 03:25:55 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
if r.is_error:
|
2020-10-13 03:25:55 +02:00
|
|
|
if r.status_code == 401:
|
|
|
|
raise SparkError("Access key invalid!")
|
|
|
|
|
2020-08-29 17:23:01 +02:00
|
|
|
raise SparkError(data["message"])
|
2020-10-13 03:25:55 +02:00
|
|
|
|
2020-08-29 17:23:01 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
return call
|
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def status(self) -> StatusResponse:
|
2020-10-13 03:25:55 +02:00
|
|
|
try:
|
2021-03-24 05:01:09 +01:00
|
|
|
funds = await self.listfunds()
|
2020-10-13 03:25:55 +02:00
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
|
|
|
return StatusResponse("Couldn't connect to Spark server", 0)
|
|
|
|
except (SparkError, UnknownError) as e:
|
|
|
|
return StatusResponse(str(e), 0)
|
|
|
|
|
|
|
|
return StatusResponse(
|
2021-10-17 19:33:29 +02:00
|
|
|
None, sum([ch["channel_sat"] * 1000 for ch in funds["channels"]])
|
2020-10-13 03:25:55 +02:00
|
|
|
)
|
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def create_invoice(
|
2021-03-24 04:40:32 +01:00
|
|
|
self,
|
|
|
|
amount: int,
|
|
|
|
memo: Optional[str] = None,
|
|
|
|
description_hash: Optional[bytes] = None,
|
2020-08-31 04:48:46 +02:00
|
|
|
) -> InvoiceResponse:
|
2020-08-29 17:23:01 +02:00
|
|
|
label = "lbs{}".format(random.random())
|
2020-08-29 18:48:16 +02:00
|
|
|
checking_id = label
|
2020-08-29 17:23:01 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
if description_hash:
|
2021-03-24 05:01:09 +01:00
|
|
|
r = await self.invoicewithdescriptionhash(
|
2020-09-03 23:02:15 +02:00
|
|
|
msatoshi=amount * 1000,
|
|
|
|
label=label,
|
|
|
|
description_hash=description_hash.hex(),
|
2020-08-29 17:23:01 +02:00
|
|
|
)
|
|
|
|
else:
|
2021-03-24 05:01:09 +01:00
|
|
|
r = await self.invoice(
|
2021-03-24 04:40:32 +01:00
|
|
|
msatoshi=amount * 1000,
|
|
|
|
label=label,
|
|
|
|
description=memo or "",
|
|
|
|
exposeprivatechannels=True,
|
2020-08-31 04:48:46 +02:00
|
|
|
)
|
2020-08-29 18:48:16 +02:00
|
|
|
ok, payment_request, error_message = True, r["bolt11"], ""
|
2020-08-29 17:23:01 +02:00
|
|
|
except (SparkError, UnknownError) as e:
|
2020-08-29 18:48:16 +02:00
|
|
|
ok, payment_request, error_message = False, None, str(e)
|
2020-08-29 17:23:01 +02:00
|
|
|
|
|
|
|
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
|
|
|
|
2022-03-16 07:20:15 +01:00
|
|
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
2020-08-29 17:23:01 +02:00
|
|
|
try:
|
2021-03-24 05:01:09 +01:00
|
|
|
r = await self.pay(bolt11)
|
2020-10-13 04:18:37 +02:00
|
|
|
except (SparkError, UnknownError) as exc:
|
2021-03-24 05:51:15 +01:00
|
|
|
listpays = await self.listpays(bolt11)
|
|
|
|
if listpays:
|
|
|
|
pays = listpays["pays"]
|
|
|
|
|
|
|
|
if len(pays) == 0:
|
|
|
|
return PaymentResponse(False, None, 0, None, str(exc))
|
|
|
|
|
|
|
|
pay = pays[0]
|
|
|
|
payment_hash = pay["payment_hash"]
|
|
|
|
|
|
|
|
if len(pays) > 1:
|
2021-04-10 22:37:48 +02:00
|
|
|
raise SparkError(
|
2021-03-24 05:51:15 +01:00
|
|
|
f"listpays({payment_hash}) returned an unexpected response: {listpays}"
|
|
|
|
)
|
|
|
|
|
|
|
|
if pay["status"] == "failed":
|
|
|
|
return PaymentResponse(False, None, 0, None, str(exc))
|
|
|
|
elif pay["status"] == "pending":
|
2021-03-28 05:11:41 +02:00
|
|
|
return PaymentResponse(None, payment_hash, 0, None, None)
|
2021-03-24 05:51:15 +01:00
|
|
|
elif pay["status"] == "complete":
|
|
|
|
r = pay
|
|
|
|
r["payment_preimage"] = pay["preimage"]
|
|
|
|
r["msatoshi"] = int(pay["amount_msat"][0:-4])
|
|
|
|
r["msatoshi_sent"] = int(pay["amount_sent_msat"][0:-4])
|
|
|
|
# this may result in an error if it was paid previously
|
|
|
|
# our database won't allow the same payment_hash to be added twice
|
|
|
|
# this is good
|
|
|
|
pass
|
2020-08-29 17:23:01 +02:00
|
|
|
|
2020-10-13 04:18:37 +02:00
|
|
|
fee_msat = r["msatoshi_sent"] - r["msatoshi"]
|
|
|
|
preimage = r["payment_preimage"]
|
|
|
|
return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None)
|
2020-08-29 17:23:01 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
2021-03-28 01:24:08 +01:00
|
|
|
try:
|
|
|
|
r = await self.listinvoices(label=checking_id)
|
|
|
|
except (SparkError, UnknownError):
|
|
|
|
return PaymentStatus(None)
|
|
|
|
|
2020-08-29 17:23:01 +02:00
|
|
|
if not r or not r.get("invoices"):
|
|
|
|
return PaymentStatus(None)
|
2022-04-05 13:44:38 +02:00
|
|
|
|
|
|
|
if r["invoices"][0]["status"] == "paid":
|
|
|
|
return PaymentStatus(True)
|
|
|
|
else:
|
2020-08-29 17:23:01 +02:00
|
|
|
return PaymentStatus(False)
|
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2021-03-21 22:12:26 +01:00
|
|
|
# check if it's 32 bytes hex
|
|
|
|
if len(checking_id) != 64:
|
|
|
|
return PaymentStatus(None)
|
|
|
|
try:
|
|
|
|
int(checking_id, 16)
|
|
|
|
except ValueError:
|
|
|
|
return PaymentStatus(None)
|
|
|
|
|
|
|
|
# ask sparko
|
2021-03-28 01:24:08 +01:00
|
|
|
try:
|
|
|
|
r = await self.listpays(payment_hash=checking_id)
|
|
|
|
except (SparkError, UnknownError):
|
|
|
|
return PaymentStatus(None)
|
|
|
|
|
2020-08-29 17:23:01 +02:00
|
|
|
if not r["pays"]:
|
|
|
|
return PaymentStatus(False)
|
|
|
|
if r["pays"][0]["payment_hash"] == checking_id:
|
|
|
|
status = r["pays"][0]["status"]
|
|
|
|
if status == "complete":
|
|
|
|
return PaymentStatus(True)
|
|
|
|
elif status == "failed":
|
|
|
|
return PaymentStatus(False)
|
|
|
|
return PaymentStatus(None)
|
|
|
|
raise KeyError("supplied an invalid checking_id")
|
2020-09-28 04:12:55 +02:00
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
2021-11-03 23:35:01 +01:00
|
|
|
url = f"{self.url}/stream?access-key={self.token}"
|
2020-09-30 03:04:51 +02:00
|
|
|
|
2020-10-10 01:55:58 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2021-11-03 23:55:44 +01:00
|
|
|
async with httpx.AsyncClient(timeout=None) as client:
|
2020-10-10 01:55:58 +02:00
|
|
|
async with client.stream("GET", url) as r:
|
|
|
|
async for line in r.aiter_lines():
|
|
|
|
if line.startswith("data:"):
|
|
|
|
data = json.loads(line[5:])
|
|
|
|
if "pay_index" in data and data.get("status") == "paid":
|
|
|
|
yield data["label"]
|
2021-04-18 15:50:07 +02:00
|
|
|
except (OSError, httpx.ReadError, httpx.ConnectError, httpx.ReadTimeout):
|
2020-10-10 01:55:58 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
print("lost connection to spark /stream, retrying in 5 seconds")
|
2021-08-30 19:55:02 +02:00
|
|
|
await asyncio.sleep(5)
|