From 1ea8593de188e40a2156a630842b31f8ea3ef24b Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 7 Oct 2022 10:23:42 +0300 Subject: [PATCH] feat: generate invoice for amount --- lnbits/extensions/cashu/mint.py | 15 +++++++++++++-- lnbits/extensions/cashu/views_api.py | 22 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lnbits/extensions/cashu/mint.py b/lnbits/extensions/cashu/mint.py index 703bf426d..70b6895e4 100644 --- a/lnbits/extensions/cashu/mint.py +++ b/lnbits/extensions/cashu/mint.py @@ -1,5 +1,5 @@ -from .crud import get_cashu +from .models import Cashu from .mint_helper import derive_keys, derive_pubkeys @@ -9,4 +9,15 @@ def get_pubkeys(xpriv: str): keys = derive_keys(xpriv) pub_keys = derive_pubkeys(keys) - return {a: p.serialize().hex() for a, p in pub_keys.items()} \ No newline at end of file + 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 \ No newline at end of file diff --git a/lnbits/extensions/cashu/views_api.py b/lnbits/extensions/cashu/views_api.py index 65323715a..a4f8a2d8f 100644 --- a/lnbits/extensions/cashu/views_api.py +++ b/lnbits/extensions/cashu/views_api.py @@ -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) async def keys(cashu_id: str = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)): """Get the public keys of the mint""" - print('############################') mint = await get_cashu(cashu_id) if mint is None: raise HTTPException( @@ -213,10 +212,27 @@ async def keys(cashu_id: str = Query(False), wallet: WalletTypeInfo = Depends(ge 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)): """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}") return {"pr": payment_request, "hash": payment_hash}