mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-23 14:40:47 +01:00
Refactor: Calculate invoice expiry outside of the crud (#1849)
* expiry before crud
* using Bolt11 as argument instead of payment_request
* fix missing expiry
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
This commit is contained in:
parent
80529dee4b
commit
b2384c10cc
2 changed files with 18 additions and 13 deletions
|
@ -5,7 +5,6 @@ from urllib.parse import urlparse
|
|||
from uuid import UUID, uuid4
|
||||
|
||||
import shortuuid
|
||||
from bolt11.decode import decode
|
||||
|
||||
from lnbits.core.db import db
|
||||
from lnbits.core.models import WalletType
|
||||
|
@ -535,6 +534,7 @@ async def create_payment(
|
|||
memo: str,
|
||||
fee: int = 0,
|
||||
preimage: Optional[str] = None,
|
||||
expiry: Optional[datetime.datetime] = None,
|
||||
pending: bool = True,
|
||||
extra: Optional[Dict] = None,
|
||||
webhook: Optional[str] = None,
|
||||
|
@ -545,14 +545,6 @@ async def create_payment(
|
|||
previous_payment = await get_standalone_payment(checking_id, conn=conn)
|
||||
assert previous_payment is None, "Payment already exists"
|
||||
|
||||
invoice = decode(payment_request)
|
||||
|
||||
if invoice.expiry:
|
||||
expiration_date = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry)
|
||||
else:
|
||||
# assume maximum bolt11 expiry of 31 days to be on the safe side
|
||||
expiration_date = datetime.datetime.now() + datetime.timedelta(days=31)
|
||||
|
||||
await (conn or db).execute(
|
||||
"""
|
||||
INSERT INTO apipayments
|
||||
|
@ -576,7 +568,7 @@ async def create_payment(
|
|||
else None
|
||||
),
|
||||
webhook,
|
||||
db.datetime_to_timestamp(expiration_date),
|
||||
db.datetime_to_timestamp(expiry) if expiry else None,
|
||||
),
|
||||
)
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
import datetime
|
||||
import json
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
@ -6,6 +7,8 @@ from typing import Dict, List, Optional, Tuple, TypedDict
|
|||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import httpx
|
||||
from bolt11 import Bolt11
|
||||
from bolt11 import decode as bolt11_decode
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from fastapi import Depends, WebSocket
|
||||
from lnurl import LnurlErrorResponse
|
||||
|
@ -14,7 +17,6 @@ from loguru import logger
|
|||
from py_vapid import Vapid
|
||||
from py_vapid.utils import b64urlencode
|
||||
|
||||
from lnbits import bolt11
|
||||
from lnbits.core.db import db
|
||||
from lnbits.db import Connection
|
||||
from lnbits.decorators import WalletTypeInfo, require_admin_key
|
||||
|
@ -138,7 +140,7 @@ async def create_invoice(
|
|||
if not ok or not payment_request or not checking_id:
|
||||
raise InvoiceFailure(error_message or "unexpected backend error.")
|
||||
|
||||
invoice = bolt11.decode(payment_request)
|
||||
invoice = bolt11_decode(payment_request)
|
||||
|
||||
amount_msat = 1000 * amount_sat
|
||||
await create_payment(
|
||||
|
@ -147,6 +149,7 @@ async def create_invoice(
|
|||
payment_request=payment_request,
|
||||
payment_hash=invoice.payment_hash,
|
||||
amount=amount_msat,
|
||||
expiry=get_bolt11_expiry(invoice),
|
||||
memo=memo,
|
||||
extra=extra,
|
||||
webhook=webhook,
|
||||
|
@ -175,7 +178,7 @@ async def pay_invoice(
|
|||
If the payment is still in flight, we hope that some other process
|
||||
will regularly check for the payment.
|
||||
"""
|
||||
invoice = bolt11.decode(payment_request)
|
||||
invoice = bolt11_decode(payment_request)
|
||||
|
||||
if not invoice.amount_msat or not invoice.amount_msat > 0:
|
||||
raise ValueError("Amountless invoices not supported.")
|
||||
|
@ -203,6 +206,7 @@ async def pay_invoice(
|
|||
payment_hash: str
|
||||
amount: int
|
||||
memo: str
|
||||
expiry: Optional[datetime.datetime]
|
||||
extra: Optional[Dict]
|
||||
|
||||
payment_kwargs: PaymentKwargs = PaymentKwargs(
|
||||
|
@ -210,6 +214,7 @@ async def pay_invoice(
|
|||
payment_request=payment_request,
|
||||
payment_hash=invoice.payment_hash,
|
||||
amount=-invoice.amount_msat,
|
||||
expiry=get_bolt11_expiry(invoice),
|
||||
memo=description or invoice.description or "",
|
||||
extra=extra,
|
||||
)
|
||||
|
@ -650,3 +655,11 @@ async def get_balance_delta() -> Tuple[int, int, int]:
|
|||
if error_message:
|
||||
raise Exception(error_message)
|
||||
return node_balance - total_balance, node_balance, total_balance
|
||||
|
||||
|
||||
def get_bolt11_expiry(invoice: Bolt11) -> datetime.datetime:
|
||||
if invoice.expiry:
|
||||
return datetime.datetime.fromtimestamp(invoice.date + invoice.expiry)
|
||||
else:
|
||||
# assume maximum bolt11 expiry of 31 days to be on the safe side
|
||||
return datetime.datetime.now() + datetime.timedelta(days=31)
|
||||
|
|
Loading…
Add table
Reference in a new issue