mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-27 20:36:16 +02:00
feat: generate invoice for amount
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
from .crud import get_cashu
|
from .models import Cashu
|
||||||
from .mint_helper import derive_keys, derive_pubkeys
|
from .mint_helper import derive_keys, derive_pubkeys
|
||||||
|
|
||||||
|
|
||||||
@@ -9,4 +9,15 @@ def get_pubkeys(xpriv: str):
|
|||||||
keys = derive_keys(xpriv)
|
keys = derive_keys(xpriv)
|
||||||
pub_keys = derive_pubkeys(keys)
|
pub_keys = derive_pubkeys(keys)
|
||||||
|
|
||||||
return {a: p.serialize().hex() for a, p in pub_keys.items()}
|
return {a: p.serialize().hex() for a, p in pub_keys.items()}
|
||||||
|
|
||||||
|
async def request_mint(mint: Cashu, amount):
|
||||||
|
"""Returns Lightning invoice and stores it in the db."""
|
||||||
|
payment_request, checking_id = await self._request_lightning_invoice(amount)
|
||||||
|
invoice = Invoice(
|
||||||
|
amount=amount, pr=payment_request, hash=checking_id, issued=False
|
||||||
|
)
|
||||||
|
if not payment_request or not checking_id:
|
||||||
|
raise Exception(f"Could not create Lightning invoice.")
|
||||||
|
await store_lightning_invoice(invoice, db=self.db)
|
||||||
|
return payment_request, checking_id
|
@@ -204,7 +204,6 @@ async def api_cashu_check_invoice(cashu_id: str, payment_hash: str):
|
|||||||
@cashu_ext.get("/api/v1/mint/keys/{cashu_id}", status_code=HTTPStatus.OK)
|
@cashu_ext.get("/api/v1/mint/keys/{cashu_id}", status_code=HTTPStatus.OK)
|
||||||
async def keys(cashu_id: str = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)):
|
async def keys(cashu_id: str = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||||
"""Get the public keys of the mint"""
|
"""Get the public keys of the mint"""
|
||||||
print('############################')
|
|
||||||
mint = await get_cashu(cashu_id)
|
mint = await get_cashu(cashu_id)
|
||||||
if mint is None:
|
if mint is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@@ -213,10 +212,27 @@ async def keys(cashu_id: str = Query(False), wallet: WalletTypeInfo = Depends(ge
|
|||||||
return get_pubkeys(mint.prvkey)
|
return get_pubkeys(mint.prvkey)
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.get("/mint")
|
@cashu_ext.get("/api/v1/mint/{cashu_id}")
|
||||||
async def mint_pay_request(amount: int = 0, cashu_id: str = Query(None)):
|
async def mint_pay_request(amount: int = 0, cashu_id: str = Query(None)):
|
||||||
"""Request minting of tokens. Server responds with a Lightning invoice."""
|
"""Request minting of tokens. Server responds with a Lightning invoice."""
|
||||||
payment_request, payment_hash = await request_mint(amount, cashu_id)
|
print('############################')
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if cashu is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.NOT_FOUND, detail="Mint does not exist."
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
payment_hash, payment_request = await create_invoice(
|
||||||
|
wallet_id=cashu.wallet,
|
||||||
|
amount=amount,
|
||||||
|
memo=f"{cashu.name}",
|
||||||
|
extra={"tag": "cashu"},
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
print(f"Lightning invoice: {payment_request}")
|
print(f"Lightning invoice: {payment_request}")
|
||||||
return {"pr": payment_request, "hash": payment_hash}
|
return {"pr": payment_request, "hash": payment_hash}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user