2020-04-16 17:07:53 +02:00
|
|
|
from flask import url_for
|
|
|
|
from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode
|
2020-05-08 21:12:31 +02:00
|
|
|
from sqlite3 import Row
|
2020-04-16 17:07:53 +02:00
|
|
|
from typing import NamedTuple
|
|
|
|
|
2020-04-23 18:30:16 +02:00
|
|
|
from lnbits.settings import FORCE_HTTPS
|
|
|
|
|
2020-04-16 17:07:53 +02:00
|
|
|
|
|
|
|
class WithdrawLink(NamedTuple):
|
|
|
|
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
|
2020-07-20 21:03:23 +01:00
|
|
|
usescsv: str
|
|
|
|
multihash: str
|
2020-04-16 17:07:53 +02:00
|
|
|
|
2020-05-08 21:12:31 +02:00
|
|
|
@classmethod
|
|
|
|
def from_row(cls, row: Row) -> "WithdrawLink":
|
|
|
|
data = dict(row)
|
|
|
|
data["is_unique"] = bool(data["is_unique"])
|
2020-07-20 21:03:23 +01:00
|
|
|
data["multihash"] = ""
|
2020-05-08 21:12:31 +02:00
|
|
|
return cls(**data)
|
|
|
|
|
2020-04-16 17:07:53 +02:00
|
|
|
@property
|
|
|
|
def is_spent(self) -> bool:
|
|
|
|
return self.used >= self.uses
|
|
|
|
|
|
|
|
@property
|
|
|
|
def lnurl(self) -> Lnurl:
|
2020-04-23 18:30:16 +02:00
|
|
|
scheme = "https" if FORCE_HTTPS else None
|
2020-07-20 21:03:23 +01:00
|
|
|
print(self.is_unique)
|
|
|
|
if self.is_unique:
|
|
|
|
url = url_for("withdraw.api_lnurl_multi_response", unique_hash=self.unique_hash, id_unique_hash=self.multihash, _external=True, _scheme=scheme)
|
|
|
|
else:
|
|
|
|
url = url_for("withdraw.api_lnurl_response", unique_hash=self.unique_hash, _external=True, _scheme=scheme)
|
|
|
|
|
2020-04-16 17:07:53 +02:00
|
|
|
return lnurl_encode(url)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def lnurl_response(self) -> LnurlWithdrawResponse:
|
2020-04-23 18:30:16 +02:00
|
|
|
scheme = "https" if FORCE_HTTPS else None
|
2020-04-16 17:07:53 +02:00
|
|
|
|
2020-07-20 21:03:23 +01:00
|
|
|
url = url_for("withdraw.api_lnurl_callback", unique_hash=self.unique_hash, _external=True, _scheme=scheme)
|
|
|
|
print(url)
|
2020-04-16 17:07:53 +02:00
|
|
|
return LnurlWithdrawResponse(
|
|
|
|
callback=url,
|
|
|
|
k1=self.k1,
|
|
|
|
min_withdrawable=self.min_withdrawable * 1000,
|
|
|
|
max_withdrawable=self.max_withdrawable * 1000,
|
2020-04-17 21:13:32 +02:00
|
|
|
default_description="#withdraw LNbits LNURL",
|
2020-04-16 17:07:53 +02:00
|
|
|
)
|