2021-10-06 14:40:12 +01:00
|
|
|
from starlette.requests import Request
|
|
|
|
from fastapi.param_functions import Query
|
2021-08-20 12:44:03 +01:00
|
|
|
from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode # type: ignore
|
|
|
|
from sqlite3 import Row
|
2021-08-20 14:46:08 +01:00
|
|
|
from pydantic import BaseModel
|
2021-08-20 12:44:03 +01:00
|
|
|
import shortuuid # type: ignore
|
|
|
|
|
2021-10-06 14:40:12 +01:00
|
|
|
class CreateWithdrawData(BaseModel):
|
|
|
|
title: str = Query(...)
|
|
|
|
min_withdrawable: int = Query(..., ge=1)
|
|
|
|
max_withdrawable: int = Query(..., ge=1)
|
|
|
|
uses: int = Query(..., ge=1)
|
|
|
|
wait_time: int = Query(..., ge=1)
|
|
|
|
is_unique: bool
|
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-20 14:46:08 +01:00
|
|
|
class WithdrawLink(BaseModel):
|
2021-08-20 12:44:03 +01:00
|
|
|
id: str
|
|
|
|
wallet: str
|
|
|
|
title: str
|
|
|
|
min_withdrawable: int
|
|
|
|
max_withdrawable: int
|
|
|
|
uses: int
|
|
|
|
wait_time: int
|
|
|
|
is_unique: bool
|
|
|
|
unique_hash: str
|
|
|
|
k1: str
|
|
|
|
open_time: int
|
|
|
|
used: int
|
|
|
|
usescsv: str
|
|
|
|
number: int
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_row(cls, row: Row) -> "WithdrawLink":
|
|
|
|
data = dict(row)
|
|
|
|
data["is_unique"] = bool(data["is_unique"])
|
|
|
|
data["number"] = 0
|
|
|
|
return cls(**data)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_spent(self) -> bool:
|
|
|
|
return self.used >= self.uses
|
|
|
|
|
2021-10-05 09:19:21 +01:00
|
|
|
def lnurl(self, req: Request) -> Lnurl:
|
2021-08-20 12:44:03 +01:00
|
|
|
if self.is_unique:
|
|
|
|
usescssv = self.usescsv.split(",")
|
|
|
|
tohash = self.id + self.unique_hash + usescssv[self.number]
|
|
|
|
multihash = shortuuid.uuid(name=tohash)
|
2021-10-05 09:19:21 +01:00
|
|
|
url = req.url_for(
|
2021-08-20 12:44:03 +01:00
|
|
|
"withdraw.api_lnurl_multi_response",
|
|
|
|
unique_hash=self.unique_hash,
|
2021-10-08 17:10:25 +01:00
|
|
|
id_unique_hash=multihash
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
|
|
|
else:
|
2021-10-05 09:19:21 +01:00
|
|
|
url = req.url_for(
|
2021-08-20 12:44:03 +01:00
|
|
|
"withdraw.api_lnurl_response",
|
2021-10-08 17:10:25 +01:00
|
|
|
unique_hash=self.unique_hash
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return lnurl_encode(url)
|
|
|
|
|
2021-10-08 17:10:25 +01:00
|
|
|
|
|
|
|
def lnurl_response(self, req: Request) -> LnurlWithdrawResponse:
|
2021-10-05 09:19:21 +01:00
|
|
|
url = req.url_for(
|
2021-08-20 12:44:03 +01:00
|
|
|
"withdraw.api_lnurl_callback", unique_hash=self.unique_hash, _external=True
|
|
|
|
)
|
|
|
|
return LnurlWithdrawResponse(
|
|
|
|
callback=url,
|
|
|
|
k1=self.k1,
|
|
|
|
min_withdrawable=self.min_withdrawable * 1000,
|
|
|
|
max_withdrawable=self.max_withdrawable * 1000,
|
|
|
|
default_description=self.title,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-20 14:46:08 +01:00
|
|
|
class HashCheck(BaseModel):
|
2021-08-20 12:44:03 +01:00
|
|
|
id: str
|
|
|
|
lnurl_id: str
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_row(cls, row: Row) -> "Hash":
|
|
|
|
return cls(**dict(row))
|