Wallet check payment hash (#649)

* check preimage

* make format

* adjust logs

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
lollerfirst
2024-11-05 13:51:53 +01:00
committed by GitHub
parent 80ff0f1a7c
commit b5afdfd598
2 changed files with 15 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ from os import listdir
from os.path import isdir, join from os.path import isdir, join
from typing import Optional, Union from typing import Optional, Union
import bolt11
import click import click
from click import Context from click import Context
from loguru import logger from loguru import logger
@@ -49,6 +50,7 @@ from ..cli.cli_helpers import (
verify_mint, verify_mint,
) )
from ..helpers import ( from ..helpers import (
check_payment_preimage,
deserialize_token_from_string, deserialize_token_from_string,
init_wallet, init_wallet,
list_mints, list_mints,
@@ -209,6 +211,7 @@ async def pay(
wallet: Wallet = ctx.obj["WALLET"] wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint() await wallet.load_mint()
await print_balance(ctx) await print_balance(ctx)
payment_hash = bolt11.decode(invoice).payment_hash
quote = await wallet.melt_quote(invoice, amount) quote = await wallet.melt_quote(invoice, amount)
logger.debug(f"Quote: {quote}") logger.debug(f"Quote: {quote}")
total_amount = quote.amount + quote.fee_reserve total_amount = quote.amount + quote.fee_reserve
@@ -252,9 +255,11 @@ async def pay(
melt_response.payment_preimage melt_response.payment_preimage
and melt_response.payment_preimage != "0" * 64 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}).") print(f" (Preimage: {melt_response.payment_preimage}).")
else: else:
print(".") print(" Mint did not provide a preimage.")
elif MintQuoteState(melt_response.state) == MintQuoteState.pending: elif MintQuoteState(melt_response.state) == MintQuoteState.pending:
print(" Invoice pending.") print(" Invoice pending.")
elif MintQuoteState(melt_response.state) == MintQuoteState.unpaid: elif MintQuoteState(melt_response.state) == MintQuoteState.unpaid:

View File

@@ -1,3 +1,4 @@
import hashlib
import os import os
from typing import Optional from typing import Optional
@@ -169,3 +170,11 @@ async def send(
await wallet.set_reserved(send_proofs, reserved=True) await wallet.set_reserved(send_proofs, reserved=True)
return wallet.available_balance, token 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()