feat: generate invoice for amount

This commit is contained in:
Vlad Stan
2022-10-07 10:23:42 +03:00
committed by dni ⚡
parent b7cd0f4d45
commit 7720525eb8
2 changed files with 32 additions and 5 deletions

View File

@@ -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()}
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

View File

@@ -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}