Skip to content

Commit

Permalink
Wallet check payment hash (#649)
Browse files Browse the repository at this point in the history
* check preimage

* make format

* adjust logs

---------

Co-authored-by: callebtc <[email protected]>
  • Loading branch information
lollerfirst and callebtc authored Nov 5, 2024
1 parent 80ff0f1 commit b5afdfd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from os.path import isdir, join
from typing import Optional, Union

import bolt11
import click
from click import Context
from loguru import logger
Expand Down Expand Up @@ -49,6 +50,7 @@
verify_mint,
)
from ..helpers import (
check_payment_preimage,
deserialize_token_from_string,
init_wallet,
list_mints,
Expand Down Expand Up @@ -209,6 +211,7 @@ async def pay(
wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint()
await print_balance(ctx)
payment_hash = bolt11.decode(invoice).payment_hash
quote = await wallet.melt_quote(invoice, amount)
logger.debug(f"Quote: {quote}")
total_amount = quote.amount + quote.fee_reserve
Expand Down Expand Up @@ -252,9 +255,11 @@ async def pay(
melt_response.payment_preimage
and melt_response.payment_preimage != "0" * 64
):
if not check_payment_preimage(payment_hash, melt_response.payment_preimage):
print(" Error: Invalid preimage!", end="", flush=True)
print(f" (Preimage: {melt_response.payment_preimage}).")
else:
print(".")
print(" Mint did not provide a preimage.")
elif MintQuoteState(melt_response.state) == MintQuoteState.pending:
print(" Invoice pending.")
elif MintQuoteState(melt_response.state) == MintQuoteState.unpaid:
Expand Down
9 changes: 9 additions & 0 deletions cashu/wallet/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import os
from typing import Optional

Expand Down Expand Up @@ -169,3 +170,11 @@ async def send(

await wallet.set_reserved(send_proofs, reserved=True)
return wallet.available_balance, token

def check_payment_preimage(
payment_hash: str,
preimage: str,
) -> bool:
return bytes.fromhex(payment_hash) == hashlib.sha256(
bytes.fromhex(preimage)
).digest()

0 comments on commit b5afdfd

Please sign in to comment.