lnurlwithdraw
This commit is contained in:
parent
9b7fa96fa2
commit
39339469bb
7 changed files with 17 additions and 25 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
38
lnurl.py
38
lnurl.py
|
|
@ -6,10 +6,11 @@ from http import HTTPStatus
|
||||||
from fastapi import Depends, Query, Request
|
from fastapi import Depends, Query, Request
|
||||||
from . import myextension_ext
|
from . import myextension_ext
|
||||||
from .crud import get_myextension
|
from .crud import get_myextension
|
||||||
from lnbits.core.services import create_invoice
|
from lnbits.core.services import create_invoice, pay_invoice
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from uuid import UUID
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from .crud import update_myextension
|
||||||
|
import shortuuid
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
########### A very simple LNURLpay ##############
|
########### A very simple LNURLpay ##############
|
||||||
|
|
@ -91,17 +92,11 @@ async def api_lnurl_pay(
|
||||||
myextension = await get_myextension(myextension_id)
|
myextension = await get_myextension(myextension_id)
|
||||||
if not myextension:
|
if not myextension:
|
||||||
return {"status": "ERROR", "reason": "No myextension found"}
|
return {"status": "ERROR", "reason": "No myextension found"}
|
||||||
k1 = UUID(myextension_id + str(myextension.ticker), version=4)
|
k1 = shortuuid.uuid(name=myextension.id + str(myextension.ticker))
|
||||||
data_to_update = {
|
|
||||||
"ticker": myextension.ticker + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
await update_myextension(myextension_id=myextension_id, **data_to_update)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"callback": str(request.url_for("myextension.api_lnurl_withdraw_callback", myextension_id=myextension_id)),
|
"callback": str(request.url_for("myextension.api_lnurl_withdraw_callback", myextension_id=myextension_id)),
|
||||||
"maxSendable": myextension.lnurlwithdrawamount,
|
"maxSendable": myextension.lnurlwithdrawamount * 1000,
|
||||||
"minSendable": myextension.lnurlwithdrawamount,
|
"minSendable": myextension.lnurlwithdrawamount * 1000,
|
||||||
"k1": k1,
|
"k1": k1,
|
||||||
"defaultDescription": myextension.name,
|
"defaultDescription": myextension.name,
|
||||||
"metadata":f"[[\"text/plain\", \"{myextension.name}\"]]",
|
"metadata":f"[[\"text/plain\", \"{myextension.name}\"]]",
|
||||||
|
|
@ -109,7 +104,7 @@ async def api_lnurl_pay(
|
||||||
}
|
}
|
||||||
|
|
||||||
@myextension_ext.get(
|
@myextension_ext.get(
|
||||||
"/api/v1/lnurl/pay/cb/{myextension_id}",
|
"/api/v1/lnurl/withdraw/cb/{myextension_id}",
|
||||||
status_code=HTTPStatus.OK,
|
status_code=HTTPStatus.OK,
|
||||||
name="myextension.api_lnurl_withdraw_callback",
|
name="myextension.api_lnurl_withdraw_callback",
|
||||||
)
|
)
|
||||||
|
|
@ -126,18 +121,13 @@ async def api_lnurl_withdraw_cb(
|
||||||
if not myextension:
|
if not myextension:
|
||||||
return {"status": "ERROR", "reason": "No myextension found"}
|
return {"status": "ERROR", "reason": "No myextension found"}
|
||||||
|
|
||||||
k1Check = UUID(myextension_id + str(myextension.ticker - 1), version=4)
|
k1Check = shortuuid.uuid(name=myextension.id + str(myextension.ticker - 1))
|
||||||
if k1Check != k1:
|
if k1Check != k1:
|
||||||
return {"status": "ERROR", "reason": "Already spent"}
|
return {"status": "ERROR", "reason": "Already spent"}
|
||||||
try:
|
await pay_invoice(
|
||||||
await pay_invoice(
|
wallet_id=myextension.wallet,
|
||||||
wallet_id=tpos.wallet,
|
payment_request=pr,
|
||||||
payment_request=pr,
|
max_sat=int(myextension.lnurlwithdrawamount * 1000),
|
||||||
max_sat=myextension.lnurlwithdrawamount * 1000,
|
extra={"tag": "MyExtension", "myextensionId": myextension_id,}
|
||||||
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"}
|
return {"status": "OK"}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ class CreateMyExtensionData(BaseModel):
|
||||||
total: Optional[int]
|
total: Optional[int]
|
||||||
lnurlpayamount: Optional[int]
|
lnurlpayamount: Optional[int]
|
||||||
lnurlwithdrawamount: Optional[int]
|
lnurlwithdrawamount: Optional[int]
|
||||||
|
ticker: Optional[int]
|
||||||
|
|
||||||
class MyExtension(BaseModel):
|
class MyExtension(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
|
|
@ -25,6 +26,7 @@ class MyExtension(BaseModel):
|
||||||
lnurlwithdrawamount: Optional[int]
|
lnurlwithdrawamount: Optional[int]
|
||||||
lnurlpay: Optional[str]
|
lnurlpay: Optional[str]
|
||||||
lnurlwithdraw: Optional[str]
|
lnurlwithdraw: Optional[str]
|
||||||
|
ticker: Optional[int]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_row(cls, row: Row) -> "MyExtension":
|
def from_row(cls, row: Row) -> "MyExtension":
|
||||||
|
|
|
||||||
2
tasks.py
2
tasks.py
|
|
@ -3,7 +3,7 @@ import asyncio
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.core.services import create_invoice, pay_invoice, websocketUpdater
|
from lnbits.core.services import create_invoice, websocketUpdater
|
||||||
from lnbits.helpers import get_current_extension_name
|
from lnbits.helpers import get_current_extension_name
|
||||||
from lnbits.tasks import register_invoice_listener
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue