Skip to content

Commit

Permalink
Merge branch 'main' into wallet/lightning_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 19, 2023
2 parents cbdcd38 + 8a4813a commit d17e65c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ repos:
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.0
hooks:
- id: mypy
args: [--ignore-missing]
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.6.0
# hooks:
# - id: mypy
# args: [--ignore-missing]
31 changes: 17 additions & 14 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,34 +337,37 @@ async def melt(
# verify amounts
total_provided = sum_proofs(proofs)
invoice_obj = bolt11.decode(invoice)
assert invoice_obj.amount_msat, "Amountless invoices not supported."
assert invoice_obj.amount_msat, "invoice has no amount."
invoice_amount = math.ceil(invoice_obj.amount_msat / 1000)
if settings.mint_max_peg_out and invoice_amount > settings.mint_max_peg_out:
raise NotAllowedError(
f"Maximum melt amount is {settings.mint_max_peg_out} sat."
)
fees_sat = await self.get_melt_fees(invoice)
reserve_fees_sat = await self.get_melt_fees(invoice)
# verify overspending attempt
assert total_provided >= invoice_amount + fees_sat, TransactionError(
assert (
total_provided >= invoice_amount + reserve_fees_sat
), TransactionError(
"provided proofs not enough for Lightning payment. Provided:"
f" {total_provided}, needed: {invoice_amount + fees_sat}"
f" {total_provided}, needed: {invoice_amount + reserve_fees_sat}"
)

# verify spending inputs, outputs, and spending conditions
await self.verify_inputs_and_outputs(proofs, outputs)
# verify spending inputs and their spending conditions
await self.verify_inputs_and_outputs(proofs)

if settings.lightning:
logger.trace(f"paying lightning invoice {invoice}")
status, preimage, fee_msat = await self._pay_lightning_invoice(
invoice, fees_sat * 1000
status, preimage, paid_fee_msat = await self._pay_lightning_invoice(
invoice, reserve_fees_sat * 1000
)
preimage = preimage or ""
logger.trace("paid lightning invoice")
else:
status, preimage, fee_msat = True, "preimage", 0
status, preimage, paid_fee_msat = True, "preimage", 0

logger.debug(
f"Melt status: {status}: preimage: {preimage}, fee_msat: {fee_msat}"
f"Melt status: {status}: preimage: {preimage}, fee_msat:"
f" {paid_fee_msat}"
)

if not status:
Expand All @@ -375,11 +378,11 @@ async def melt(

# prepare change to compensate wallet for overpaid fees
return_promises: List[BlindedSignature] = []
if outputs and fee_msat is not None:
if outputs and paid_fee_msat is not None:
return_promises = await self._generate_change_promises(
total_provided=total_provided,
invoice_amount=invoice_amount,
ln_fee_msat=fee_msat,
ln_fee_msat=paid_fee_msat,
outputs=outputs,
)

Expand Down Expand Up @@ -407,7 +410,7 @@ async def get_melt_fees(self, pr: str) -> int:
amount_msat = 0
if settings.lightning:
decoded_invoice = bolt11.decode(pr)
assert decoded_invoice.amount_msat, "Amountless invoices not supported."
assert decoded_invoice.amount_msat, "invoice has no amount."
amount_msat = int(decoded_invoice.amount_msat)
logger.trace(
"get_melt_fees: checking lightning invoice:"
Expand Down Expand Up @@ -624,7 +627,7 @@ async def _set_proofs_pending(
Raises:
Exception: At least one proof already in pending table.
"""
# first we check whether these proofs are pending aready
# first we check whether these proofs are pending already
async with self.proofs_pending_lock:
await self._validate_proofs_pending(proofs, conn)
for p in proofs:
Expand Down
6 changes: 2 additions & 4 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ def _verify_no_duplicate_outputs(self, outputs: List[BlindedMessage]) -> bool:
return True

def _verify_amount(self, amount: int) -> int:
"""Any amount used should be a positive integer not larger than 2^MAX_ORDER."""
valid = (
isinstance(amount, int) and amount > 0 and amount < 2**settings.max_order
)
"""Any amount used should be positive and not larger than 2^MAX_ORDER."""
valid = amount > 0 and amount < 2**settings.max_order
logger.trace(f"Verifying amount {amount} is valid: {valid}")
if not valid:
raise NotAllowedError("invalid amount: " + str(amount))
Expand Down
2 changes: 1 addition & 1 deletion cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ async def get_pay_amount_with_fees(self, invoice: str):
total amount (amount+fees) to be paid.
"""
decoded_invoice = bolt11.decode(invoice)
assert decoded_invoice.amount_msat, "Amountless invoices not supported."
assert decoded_invoice.amount_msat, "invoices has no amount."
# check if it's an internal payment
fees = int((await self.check_fees(invoice))["fee"])
logger.debug(f"Mint wants {fees} sat as fee reserve.")
Expand Down

0 comments on commit d17e65c

Please sign in to comment.