diff --git a/__pycache__/lnurl.cpython-39.pyc b/__pycache__/lnurl.cpython-39.pyc index 1fe4cd5..4e309d6 100644 Binary files a/__pycache__/lnurl.cpython-39.pyc and b/__pycache__/lnurl.cpython-39.pyc differ diff --git a/__pycache__/tasks.cpython-39.pyc b/__pycache__/tasks.cpython-39.pyc index 1e4e1dc..3dfb88c 100644 Binary files a/__pycache__/tasks.cpython-39.pyc and b/__pycache__/tasks.cpython-39.pyc differ diff --git a/lnurl.py b/lnurl.py index 058e2d6..2588f8d 100644 --- a/lnurl.py +++ b/lnurl.py @@ -7,7 +7,8 @@ from fastapi import Depends, Query, Request from . import myextension_ext from .crud import get_myextension from lnbits.core.services import create_invoice - +from loguru import logger +from uuid import UUID ################################################# ########### A very simple LNURLpay ############## @@ -29,8 +30,8 @@ async def api_lnurl_pay( return {"status": "ERROR", "reason": "No myextension found"} return { "callback": str(request.url_for("myextension.api_lnurl_pay_callback", myextension_id=myextension_id)), - "maxSendable": myextension.lnurlpayamount, - "minSendable": myextension.lnurlpayamount, + "maxSendable": myextension.lnurlpayamount * 1000, + "minSendable": myextension.lnurlpayamount * 1000, "metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]", "tag": "payRequest" } @@ -46,21 +47,29 @@ async def api_lnurl_pay_cb( amount: int = Query(...), ): myextension = await get_myextension(myextension_id) + logger.debug(myextension) if not myextension: return {"status": "ERROR", "reason": "No myextension found"} - payment_request = await create_invoice( + payment_hash, payment_request = await create_invoice( wallet_id=myextension.wallet, amount=int(amount / 1000), memo=myextension.name, - unhashed_description="[[\"text/plain\", \"" + myextension.name + "\"]]".encode(), + unhashed_description=f"[[\"text/plain\", \"{myextension.name}\"]]".encode(), extra= { - "tag": "myextension", - "link": myextension_id, + "tag": "MyExtension", + "myextensionId": myextension_id, "extra": request.query_params.get("amount"), }, ) - return { "pr": payment_request, "routes": []} + return { + "pr": payment_request, + "routes": [], + "successAction": { + "tag": "message", + "message": f"Paid {myextension.name}" + } + } ################################################# ######## A very simple LNURLwithdraw ############ @@ -81,13 +90,20 @@ async def api_lnurl_pay( myextension = await get_myextension(myextension_id) if not myextension: return {"status": "ERROR", "reason": "No myextension found"} + k1 = UUID(myextension_id + str(myextension.ticker), version=4) + data_to_update = { + "ticker": myextension.ticker + 1 + } + + await update_myextension(myextension_id=myextension_id, **data_to_update) + return { "callback": str(request.url_for("myextension.api_lnurl_withdraw_callback", myextension_id=myextension_id)), "maxSendable": myextension.lnurlwithdrawamount, "minSendable": myextension.lnurlwithdrawamount, - "k1": "", + "k1": k1, "defaultDescription": myextension.name, - "metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]", + "metadata":f"[[\"text/plain\", \"{myextension.name}\"]]", "tag": "withdrawRequest" } @@ -96,24 +112,31 @@ async def api_lnurl_pay( status_code=HTTPStatus.OK, name="myextension.api_lnurl_withdraw_callback", ) -async def api_lnurl_pay_cb( +async def api_lnurl_withdraw_cb( request: Request, myextension_id: str, - amount: int = Query(...), + pr: Optional[str] = None, + k1: Optional[str] = None, ): + assert k1, "k1 is required" + assert pr, "pr is required" + myextension = await get_myextension(myextension_id) if not myextension: return {"status": "ERROR", "reason": "No myextension found"} - payment_request = await create_invoice( - wallet_id=myextension.wallet, - amount=int(amount / 1000), - memo=myextension.name, - unhashed_description="[[\"text/plain\", \"" + myextension.name + "\"]]".encode(), - extra= { - "tag": "myextension", - "link": myextension_id, - "extra": request.query_params.get("amount"), - }, - ) - return { "pr": payment_request, "routes": []} \ No newline at end of file + k1Check = UUID(myextension_id + str(myextension.ticker - 1), version=4) + if k1Check != k1: + return {"status": "ERROR", "reason": "Already spent"} + try: + await pay_invoice( + wallet_id=tpos.wallet, + payment_request=pr, + max_sat=myextension.lnurlwithdrawamount * 1000, + extra={"tag": "MyExtension", "myextensionId": myextension_id,} + ) + except Exception as e: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail=f"withdraw not working. {str(e)}" + ) + return {"status": "OK"} diff --git a/migrations.py b/migrations.py index de2300d..40725e3 100644 --- a/migrations.py +++ b/migrations.py @@ -11,12 +11,15 @@ async def m001_initial(db): wallet TEXT NOT NULL, name TEXT NOT NULL, total INTEGER DEFAULT 0, - lnurlpayamount INTEGER DEFAULT 0 + lnurlpayamount INTEGER DEFAULT 0, + lnurlwithdrawamount INTEGER DEFAULT 0, + lnurlwithdraw TEXT, + lnurlpay TEXT ); """ ) -# Here we are adding an extra field to the database +# Here we add another field to the database async def m002_addtip_wallet(db): """ @@ -24,30 +27,6 @@ async def m002_addtip_wallet(db): """ await db.execute( """ - ALTER TABLE myextension.maintable ADD lnurlwithdrawamount INTEGER DEFAULT 0; - """ - ) - -# Here we add another field to the database, always add never edit! - -async def m004_addtip_wallet(db): - """ - Add total to templates table - """ - await db.execute( - """ - ALTER TABLE myextension.maintable ADD lnurlwithdraw TEXT; - """ - ) - -# Here we add another field to the database - -async def m005_addtip_wallet(db): - """ - Add total to templates table - """ - await db.execute( - """ - ALTER TABLE myextension.maintable ADD lnurlpay TEXT; + ALTER TABLE myextension.maintable ADD ticker INTEGER DEFAULT 1; """ ) \ No newline at end of file diff --git a/static/confetti.min.js b/static/confetti.min.js deleted file mode 100644 index 0a09dd0..0000000 --- a/static/confetti.min.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Minified by jsDelivr using Terser v5.3.5. - * Original file: /npm/canvas-confetti@1.4.0/dist/confetti.browser.js - * - * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files - */ -!function(t,e){!function t(e,n,a,i){var o=!!(e.Worker&&e.Blob&&e.Promise&&e.OffscreenCanvas&&e.OffscreenCanvasRenderingContext2D&&e.HTMLCanvasElement&&e.HTMLCanvasElement.prototype.transferControlToOffscreen&&e.URL&&e.URL.createObjectURL);function r(){}function l(t){var a=n.exports.Promise,i=void 0!==a?a:e.Promise;return"function"==typeof i?new i(t):(t(r,r),null)}var c,s,u,d,f,h,g,m,b=(u=Math.floor(1e3/60),d={},f=0,"function"==typeof requestAnimationFrame&&"function"==typeof cancelAnimationFrame?(c=function(t){var e=Math.random();return d[e]=requestAnimationFrame((function n(a){f===a||f+u-1 None: myextension = await get_myextension(myextension_id) # update something in the db - data_to_update = { "total": myextension.total + payment.amount } - + await update_myextension(myextension_id=myextension_id, **data_to_update) # here we could send some data to a websocket on wss:///api/v1/ws/ diff --git a/templates/myextension/index.html b/templates/myextension/index.html index d6c2738..24e2ca1 100644 --- a/templates/myextension/index.html +++ b/templates/myextension/index.html @@ -419,6 +419,7 @@ openUrlDialog(id) { this.urlDialog.data = _.findWhere(this.temps, {id}) this.qrValue = this.urlDialog.data.lnurlpay + console.log(this.urlDialog.data.id) this.connectWebocket(this.urlDialog.data.id) this.urlDialog.show = true },