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

fakewallet: return preimage #358

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
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
26 changes: 14 additions & 12 deletions cashu/lightning/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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:
Expand All @@ -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(
Expand Down
16 changes: 14 additions & 2 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
Loading