From 63bb8ef8bd701a947dc0610c9d54e1afa153059c Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:11:38 -0300 Subject: [PATCH] fakewallet: return preimage (#358) --- cashu/lightning/fake.py | 26 ++++++++++++++------------ cashu/wallet/cli/cli.py | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/cashu/lightning/fake.py b/cashu/lightning/fake.py index a79c1a81..7a14d3f0 100644 --- a/cashu/lightning/fake.py +++ b/cashu/lightning/fake.py @@ -3,7 +3,7 @@ import random from datetime import datetime from os import urandom -from typing import AsyncGenerator, Optional, Set +from typing import AsyncGenerator, Dict, Optional, Set from bolt11 import ( Bolt11, @@ -31,6 +31,7 @@ class FakeWallet(Wallet): """https://github.com/lnbits/lnbits""" queue: asyncio.Queue[Bolt11] = asyncio.Queue(0) + payment_secrets: Dict[str, str] = dict() paid_invoices: Set[str] = set() secret: str = "FAKEWALLET SECRET" privkey: str = hashlib.pbkdf2_hmac( @@ -68,20 +69,18 @@ async def create_invoice( if expiry: tags.add(TagChar.expire_time, expiry) - # random hash - checking_id = ( - self.privkey[:6] - + hashlib.sha256(str(random.getrandbits(256)).encode()).hexdigest()[6:] - ) - - tags.add(TagChar.payment_hash, checking_id) - if payment_secret: secret = payment_secret.hex() else: secret = urandom(32).hex() tags.add(TagChar.payment_secret, secret) + payment_hash = hashlib.sha256(secret.encode()).hexdigest() + + tags.add(TagChar.payment_hash, payment_hash) + + self.payment_secrets[payment_hash] = secret + bolt11 = Bolt11( currency="bc", amount_msat=MilliSatoshi(amount * 1000), @@ -92,7 +91,7 @@ async def create_invoice( payment_request = encode(bolt11, self.privkey) return InvoiceResponse( - ok=True, checking_id=checking_id, payment_request=payment_request + ok=True, checking_id=payment_hash, payment_request=payment_request ) async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse: @@ -101,11 +100,14 @@ async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse if DELAY_PAYMENT: await asyncio.sleep(5) - if invoice.payment_hash[:6] == self.privkey[:6] or BRR: + if invoice.payment_hash in self.payment_secrets or BRR: await self.queue.put(invoice) self.paid_invoices.add(invoice.payment_hash) return PaymentResponse( - ok=True, checking_id=invoice.payment_hash, fee_msat=0 + ok=True, + checking_id=invoice.payment_hash, + fee_msat=0, + preimage=self.payment_secrets.get(invoice.payment_hash) or "0" * 64, ) else: return PaymentResponse( diff --git a/cashu/wallet/cli/cli.py b/cashu/wallet/cli/cli.py index ca2b52e3..4be2cbea 100644 --- a/cashu/wallet/cli/cli.py +++ b/cashu/wallet/cli/cli.py @@ -178,13 +178,25 @@ async def pay(ctx: Context, invoice: str, yes: bool): default=True, ) - print("Paying Lightning invoice ...") + print("Paying Lightning invoice ...", end="", flush=True) assert total_amount > 0, "amount is not positive" if wallet.available_balance < total_amount: print("Error: Balance too low.") return _, send_proofs = await wallet.split_to_send(wallet.proofs, total_amount) - await wallet.pay_lightning(send_proofs, invoice, fee_reserve_sat) + try: + melt_response = await wallet.pay_lightning( + send_proofs, invoice, fee_reserve_sat + ) + + except Exception as e: + print(f"\nError paying invoice: {str(e)}") + return + print(" Invoice paid", end="", flush=True) + if melt_response.preimage and melt_response.preimage != "0" * 64: + print(f" (Proof: {melt_response.preimage}).") + else: + print(".") wallet.status()