Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Melt quotes use case-insensitive request #479

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,18 @@ async def mint_quote(self, quote_request: PostMintQuoteRequest) -> MintQuote:
# get invoice expiry time
invoice_obj = bolt11.decode(invoice_response.payment_request)

# NOTE: we normalize the request to lowercase to avoid case sensitivity
# This works with Lightning but might not work with other methods
request = invoice_response.payment_request.lower()

expiry = None
if invoice_obj.expiry is not None:
expiry = invoice_obj.date + invoice_obj.expiry

quote = MintQuote(
quote=random_hash(),
method=method.name,
request=invoice_response.payment_request,
request=request,
checking_id=invoice_response.checking_id,
unit=quote_request.unit,
amount=quote_request.amount,
Expand Down Expand Up @@ -469,14 +473,17 @@ async def melt_quote(
"""
unit = Unit[melt_quote.unit]
method = Method.bolt11
# NOTE: we normalize the request to lowercase to avoid case sensitivity
# This works with Lightning but might not work with other methods
request = melt_quote.request.lower()
invoice_obj = bolt11.decode(melt_quote.request)
assert invoice_obj.amount_msat, "invoice has no amount."

# check if there is a mint quote with the same payment request
# so that we can handle the transaction internally without lightning
# and respond with zero fees
mint_quote = await self.crud.get_mint_quote_by_request(
request=melt_quote.request, db=self.db
request=request, db=self.db
)
if mint_quote:
# internal transaction, validate and return amount from
Expand All @@ -485,9 +492,7 @@ async def melt_quote(
Amount(unit, mint_quote.amount).to(Unit.msat).amount
== invoice_obj.amount_msat
), "amounts do not match"
assert (
melt_quote.request == mint_quote.request
), "bolt11 requests do not match"
assert request == mint_quote.request, "bolt11 requests do not match"
assert mint_quote.unit == melt_quote.unit, "units do not match"
assert mint_quote.method == method.name, "methods do not match"
assert not mint_quote.paid, "mint quote already paid"
Expand All @@ -499,14 +504,12 @@ async def melt_quote(
fee=Amount(unit=Unit.msat, amount=0),
)
logger.info(
f"Issuing internal melt quote: {melt_quote.request} ->"
f"Issuing internal melt quote: {request} ->"
f" {mint_quote.quote} ({mint_quote.amount} {mint_quote.unit})"
)
else:
# not internal, get quote by backend
payment_quote = await self.backends[method][unit].get_payment_quote(
melt_quote.request
)
payment_quote = await self.backends[method][unit].get_payment_quote(request)
assert payment_quote.checking_id, "quote has no checking id"

expiry = None
Expand All @@ -516,9 +519,9 @@ async def melt_quote(
quote = MeltQuote(
quote=random_hash(),
method=method.name,
request=melt_quote.request,
request=request,
checking_id=payment_quote.checking_id,
unit=melt_quote.unit,
unit=unit.name,
amount=payment_quote.amount.to(unit).amount,
paid=False,
fee_reserve=payment_quote.fee.to(unit).amount,
Expand Down
Loading