Browse Source

Printing unique LNURLS

LNURL redeeming still needs testing
WithdrawExt
benarc 4 years ago
parent
commit
613f052f89
  1. 30
      lnbits/extensions/withdraw/crud.py
  2. 3
      lnbits/extensions/withdraw/migrations.py
  3. 9
      lnbits/extensions/withdraw/models.py
  4. 21
      lnbits/extensions/withdraw/templates/withdraw/print_qr.html
  5. 7
      lnbits/extensions/withdraw/views.py
  6. 14
      lnbits/extensions/withdraw/views_api.py

30
lnbits/extensions/withdraw/crud.py

@ -17,6 +17,14 @@ def create_withdraw_link(
wait_time: int,
is_unique: bool,
) -> WithdrawLink:
if is_unique:
uniques = ""
for i in range(uses):
uniques += "," + urlsafe_short_hash()
uniques = uniques[1:]
else:
uniques = urlsafe_short_hash()
with open_ext_db("withdraw") as db:
link_id = urlsafe_short_hash()
db.execute(
@ -45,7 +53,7 @@ def create_withdraw_link(
uses,
wait_time,
int(is_unique),
urlsafe_short_hash(),
uniques,
urlsafe_short_hash(),
int(datetime.now().timestamp()) + wait_time,
),
@ -65,13 +73,15 @@ def get_withdraw_link(link_id: str, unique_hash_int: int) -> Optional[WithdrawLi
link = []
for item in row:
link.append(item)
link[12] = unique_hash_int
hashes = link[8].split(",")
link[8] = hashes[unique_hash_int]
print(link[8])
print(unique_hash_int)
return WithdrawLink._make(link)
def get_withdraw_link_by_hash(unique_hash: str) -> Optional[WithdrawLink]:
with open_ext_db("withdraw") as db:
row = db.fetchone("SELECT * FROM withdraw_links WHERE unique_hash = ?", (unique_hash,))
row = db.fetchone("SELECT * FROM withdraw_links WHERE unique_hash LIKE = ?", (unique_hash,))
return WithdrawLink.from_row(row) if row else None
@ -88,8 +98,17 @@ def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
return [WithdrawLink.from_row(row) for row in rows]
def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
with open_ext_db("withdraw") as db:
row = db.fetchone("SELECT * FROM withdraw_links WHERE id = ?", (link_id,))
if kwargs["uses"] != row["uses"] or kwargs["is_unique"] == True and row["is_unique"] == False:
uniques = ""
for i in range(kwargs["uses"]):
uniques += "," + urlsafe_short_hash()
uniques = uniques[1:]
kwargs.update( {'unique_hash' : uniques} )
print(kwargs["unique_hash"])
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
with open_ext_db("withdraw") as db:
db.execute(f"UPDATE withdraw_links SET {q} WHERE id = ?", (*kwargs.values(), link_id))
row = db.fetchone("SELECT * FROM withdraw_links WHERE id = ?", (link_id,))
@ -97,6 +116,7 @@ def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
return WithdrawLink.from_row(row) if row else None
def delete_withdraw_link(link_id: str) -> None:
with open_ext_db("withdraw") as db:
db.execute("DELETE FROM withdraw_links WHERE id = ?", (link_id,))

3
lnbits/extensions/withdraw/migrations.py

@ -22,8 +22,7 @@ def m001_initial(db):
unique_hash TEXT UNIQUE,
k1 TEXT,
open_time INTEGER,
used INTEGER DEFAULT 0,
unique_hash_int INTEGER DEFAULT 0
used INTEGER DEFAULT 0
);
""")

9
lnbits/extensions/withdraw/models.py

@ -19,7 +19,6 @@ class WithdrawLink(NamedTuple):
k1: str
open_time: int
used: int
unique_hash_int: int
@classmethod
def from_row(cls, row: Row) -> "WithdrawLink":
@ -34,9 +33,9 @@ class WithdrawLink(NamedTuple):
@property
def lnurl(self) -> Lnurl:
scheme = "https" if FORCE_HTTPS else None
if self.is_unique == 1:
if self.unique_hash.find(","):
unique_hashs = self.unique_hash.split(",")
unique_hash = unique_hashs[- self.unique_hash_int]
unique_hash = unique_hashs[0]
else:
unique_hash = self.unique_hash
url = url_for("withdraw.api_lnurl_response", unique_hash=unique_hash, _external=True, _scheme=scheme)
@ -45,9 +44,9 @@ class WithdrawLink(NamedTuple):
@property
def lnurl_response(self) -> LnurlWithdrawResponse:
scheme = "https" if FORCE_HTTPS else None
if self.is_unique == 1:
if self.unique_hash.find(","):
unique_hashs = self.unique_hash.split(",")
unique_hash = unique_hashs[- self.unique_hash_int]
unique_hash = unique_hashs[0]
else:
unique_hash = self.unique_hash
url = url_for("withdraw.api_lnurl_callback", unique_hash=unique_hash, _external=True, _scheme=scheme)

21
lnbits/extensions/withdraw/templates/withdraw/print_qr.html

@ -1,33 +1,20 @@
{% extends "print.html" %} {% block page %}
<div class="row justify-center">
<div class="col-12 col-sm-8 col-lg-6 text-center">
{% if unique %} {% for i in range(links) %}
{{links}} {% for linked in links %}
<div class="zimbabwe">
<div class="qr">
<qrcode value="{{ links[i].lnurl }}" :options="{width: 150}"></qrcode>
<qrcode value="{{ linked.lnurl }}" :options="{width: 150}"></qrcode>
<br /><br />
<strong>{{ SITE_TITLE }}</strong><br />
<strong>{{ link.max_withdrawable }} FREE SATS</strong><br />
<strong>{{ linked.max_withdrawable }} FREE SATS</strong><br />
<small>Scan and follow link<br />or use Lightning wallet</small>
</div>
<img src="{{ url_for('static', filename='images/note.jpg') }}" />
</div>
{% endfor %} {% elif not unique %} {% for i in range(link.uses) %}
<div class="zimbabwe">
<div class="qr">
<qrcode value="{{ links.lnurl }}" :options="{width: 150}"></qrcode>
<br /><br />
<strong>{{ SITE_TITLE }}</strong><br />
<strong>{{ link.max_withdrawable }} FREE SATS</strong><br />
<small>Scan and follow link<br />or use Lightning wallet</small>
</div>
<img src="{{ url_for('static', filename='images/note.jpg') }}" />
</div>
{% endfor %} {% endif %}
{% endfor %}
</div>
</div>
{% endblock %} {% block styles %}

7
lnbits/extensions/withdraw/views.py

@ -26,10 +26,7 @@ def print_qr(link_id):
links = []
link = get_withdraw_link(link_id, 0) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
if link.is_unique == True:
unique_hashs = link.unique_hash.split(",")
count = 1
for item in unique_hashs:
links.append(get_withdraw_link(link_id, count))
count + 1
for i in range(link.uses):
links.append(get_withdraw_link(link_id, i))
return render_template("withdraw/print_qr.html", link=link, unique=True, links=links)

14
lnbits/extensions/withdraw/views_api.py

@ -120,9 +120,9 @@ def api_lnurl_response(unique_hash):
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK
@withdraw_ext.route("/api/v1/lnurl/cb/<link_id>", methods=["GET"])
def api_lnurl_callback(link_id):
link = get_withdraw_link_by_hash(link_id)
@withdraw_ext.route("/api/v1/lnurl/cb/<unique_hash>", methods=["GET"])
def api_lnurl_callback(unique_hash):
link = get_withdraw_link_by_hash(unique_hash)
k1 = request.args.get("k1", type=str)
payment_request = request.args.get("pr", type=str)
now = int(datetime.now().timestamp())
@ -148,9 +148,11 @@ def api_lnurl_callback(link_id):
}
if link.is_unique:
changes["unique_hash"] = link.unique_hash[:-1]
update_withdraw_link(link_id, **changes)
hashes = link.unique_hash.split(",")
hashes.remove(unique_hash)
changes["unique_hash"] = ','.join(hashes)
update_withdraw_link(link.id, **changes)
except ValueError as e:
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK

Loading…
Cancel
Save