Allow to set maximum peg in/out for mint (#209)

* Allow to set maximum peg in/out for mint

* Make format

* remove duplicate error

* move business logic to ledger

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
sihamon
2023-05-13 20:41:24 +02:00
committed by GitHub
parent aa9b5bd55c
commit e7df2028fa
4 changed files with 25 additions and 7 deletions

View File

@@ -116,6 +116,7 @@ class GetInfoResponse(BaseModel):
contact: Optional[List[List[str]]] = None
nuts: Optional[List[str]] = None
motd: Optional[str] = None
parameter: Optional[dict] = None
# ------- API: KEYS -------

View File

@@ -55,6 +55,8 @@ class MintSettings(CashuSettings):
mint_lightning_backend: str = Field(default="LNbitsWallet")
mint_database: str = Field(default="data/mint")
mint_peg_out_only: bool = Field(default=False)
mint_max_peg_in: int = Field(default=None)
mint_max_peg_out: int = Field(default=None)
mint_lnbits_endpoint: str = Field(default=None)
mint_lnbits_key: str = Field(default=None)

View File

@@ -536,6 +536,12 @@ class Ledger:
Returns:
Tuple[str, str]: Bolt11 invoice and a hash (for looking it up later)
"""
if settings.mint_max_peg_in and amount > settings.mint_max_peg_in:
raise Exception(f"Maximum mint amount is {settings.mint_max_peg_in} sats.")
if settings.mint_peg_out_only:
raise Exception("Mint does not allow minting new tokens.")
payment_request, payment_hash = await self._request_lightning_invoice(amount)
assert payment_request and payment_hash, Exception(
"could not fetch invoice from Lightning backend"
@@ -619,6 +625,10 @@ class Ledger:
total_provided = sum_proofs(proofs)
invoice_obj = bolt11.decode(invoice)
invoice_amount = math.ceil(invoice_obj.amount_msat / 1000)
if settings.mint_max_peg_out and invoice_amount > settings.mint_max_peg_out:
raise Exception(
f"Maximum melt amount is {settings.mint_max_peg_out} sats."
)
fees_msat = await self.check_fees(invoice)
assert total_provided >= invoice_amount + fees_msat / 1000, Exception(
"provided proofs not enough for Lightning payment."

View File

@@ -45,6 +45,11 @@ async def info():
contact=settings.mint_info_contact,
nuts=settings.mint_info_nuts,
motd=settings.mint_info_motd,
parameter={
"max_peg_in": settings.mint_max_peg_in,
"max_peg_out": settings.mint_max_peg_out,
"peg_out_only": settings.mint_peg_out_only,
},
)
@@ -99,10 +104,13 @@ async def request_mint(amount: int = 0) -> Union[GetMintResponse, CashuError]:
"""
if settings.mint_peg_out_only:
return CashuError(code=0, error="Mint does not allow minting new tokens.")
payment_request, hash = await ledger.request_mint(amount)
print(f"Lightning invoice: {payment_request}")
resp = GetMintResponse(pr=payment_request, hash=hash)
return resp
try:
payment_request, hash = await ledger.request_mint(amount)
print(f"Lightning invoice: {payment_request}")
resp = GetMintResponse(pr=payment_request, hash=hash)
return resp
except Exception as exc:
return CashuError(code=0, error=str(exc))
@router.post(
@@ -120,14 +128,11 @@ async def mint(
Call this endpoint after `GET /mint`.
"""
if settings.mint_peg_out_only:
return CashuError(code=0, error="Mint does not allow minting new tokens.")
try:
# BEGIN: backwards compatibility < 0.12 where we used to lookup payments with payment_hash
# We use the payment_hash to lookup the hash from the database and pass that one along.
hash = payment_hash or hash
# END: backwards compatibility < 0.12
promises = await ledger.mint(payload.outputs, hash=hash)
blinded_signatures = PostMintResponse(promises=promises)
return blinded_signatures