2022-01-30 11:41:20 +01:00
|
|
|
import asyncio
|
2022-07-16 14:23:03 +02:00
|
|
|
import hashlib
|
|
|
|
from datetime import datetime
|
2023-09-25 12:06:54 +02:00
|
|
|
from os import urandom
|
2023-11-13 13:06:59 +01:00
|
|
|
from typing import AsyncGenerator, Dict, Optional, Set
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2023-09-25 12:06:54 +02:00
|
|
|
from bolt11 import (
|
|
|
|
Bolt11,
|
|
|
|
Bolt11Exception,
|
|
|
|
MilliSatoshi,
|
|
|
|
TagChar,
|
|
|
|
Tags,
|
|
|
|
decode,
|
|
|
|
encode,
|
|
|
|
)
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
2022-10-03 16:46:46 +02:00
|
|
|
from lnbits.settings import settings
|
|
|
|
|
2022-01-30 11:41:20 +01:00
|
|
|
from .base import (
|
|
|
|
InvoiceResponse,
|
2024-03-13 16:17:33 +01:00
|
|
|
PaymentPendingStatus,
|
2022-01-30 11:41:20 +01:00
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
2022-07-16 14:23:03 +02:00
|
|
|
StatusResponse,
|
2022-01-30 11:41:20 +01:00
|
|
|
Wallet,
|
|
|
|
)
|
|
|
|
|
2022-01-31 12:58:39 +01:00
|
|
|
|
2022-02-01 21:51:40 +01:00
|
|
|
class FakeWallet(Wallet):
|
2022-12-05 12:18:59 +01:00
|
|
|
queue: asyncio.Queue = asyncio.Queue(0)
|
2023-11-13 13:06:59 +01:00
|
|
|
payment_secrets: Dict[str, str] = dict()
|
|
|
|
paid_invoices: Set[str] = set()
|
2022-10-05 13:08:22 +02:00
|
|
|
secret: str = settings.fake_wallet_secret
|
2022-10-04 09:51:47 +02:00
|
|
|
privkey: str = hashlib.pbkdf2_hmac(
|
|
|
|
"sha256",
|
2022-12-29 16:50:05 +01:00
|
|
|
secret.encode(),
|
|
|
|
("FakeWallet").encode(),
|
2022-10-04 09:51:47 +02:00
|
|
|
2048,
|
|
|
|
32,
|
|
|
|
).hex()
|
|
|
|
|
2022-01-31 01:12:59 +01:00
|
|
|
async def status(self) -> StatusResponse:
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.info(
|
2023-08-24 11:26:09 +02:00
|
|
|
"FakeWallet funding source is for using LNbits as a centralised,"
|
|
|
|
" stand-alone payment system with brrrrrr."
|
2022-01-30 11:41:20 +01:00
|
|
|
)
|
2022-07-19 19:20:20 +02:00
|
|
|
return StatusResponse(None, 1000000000)
|
2022-01-31 17:29:42 +01:00
|
|
|
|
2022-01-30 11:41:20 +01:00
|
|
|
async def create_invoice(
|
|
|
|
self,
|
|
|
|
amount: int,
|
|
|
|
memo: Optional[str] = None,
|
|
|
|
description_hash: Optional[bytes] = None,
|
2022-08-13 14:29:04 +02:00
|
|
|
unhashed_description: Optional[bytes] = None,
|
2023-09-25 12:06:54 +02:00
|
|
|
expiry: Optional[int] = None,
|
|
|
|
payment_secret: Optional[bytes] = None,
|
|
|
|
**_,
|
2022-01-30 11:41:20 +01:00
|
|
|
) -> InvoiceResponse:
|
2023-09-25 12:06:54 +02:00
|
|
|
tags = Tags()
|
|
|
|
|
2022-01-30 11:41:20 +01:00
|
|
|
if description_hash:
|
2023-09-25 12:06:54 +02:00
|
|
|
tags.add(TagChar.description_hash, description_hash.hex())
|
2022-08-13 14:29:04 +02:00
|
|
|
elif unhashed_description:
|
2023-09-25 12:06:54 +02:00
|
|
|
tags.add(
|
|
|
|
TagChar.description_hash,
|
|
|
|
hashlib.sha256(unhashed_description).hexdigest(),
|
|
|
|
)
|
2022-01-30 11:41:20 +01:00
|
|
|
else:
|
2023-09-25 12:06:54 +02:00
|
|
|
tags.add(TagChar.description, memo or "")
|
|
|
|
|
|
|
|
if expiry:
|
|
|
|
tags.add(TagChar.expire_time, expiry)
|
|
|
|
|
|
|
|
if payment_secret:
|
|
|
|
secret = payment_secret.hex()
|
|
|
|
else:
|
|
|
|
secret = urandom(32).hex()
|
|
|
|
tags.add(TagChar.payment_secret, secret)
|
|
|
|
|
2023-11-13 13:06:59 +01:00
|
|
|
payment_hash = hashlib.sha256(secret.encode()).hexdigest()
|
|
|
|
|
|
|
|
tags.add(TagChar.payment_hash, payment_hash)
|
|
|
|
|
|
|
|
self.payment_secrets[payment_hash] = secret
|
|
|
|
|
2023-09-25 12:06:54 +02:00
|
|
|
bolt11 = Bolt11(
|
|
|
|
currency="bc",
|
|
|
|
amount_msat=MilliSatoshi(amount * 1000),
|
|
|
|
date=int(datetime.now().timestamp()),
|
|
|
|
tags=tags,
|
|
|
|
)
|
|
|
|
|
|
|
|
payment_request = encode(bolt11, self.privkey)
|
2022-01-30 11:41:20 +01:00
|
|
|
|
2023-11-13 13:06:59 +01:00
|
|
|
return InvoiceResponse(
|
|
|
|
ok=True, checking_id=payment_hash, payment_request=payment_request
|
|
|
|
)
|
2022-01-31 01:12:59 +01:00
|
|
|
|
2023-02-02 13:57:36 +01:00
|
|
|
async def pay_invoice(self, bolt11: str, _: int) -> PaymentResponse:
|
2023-09-25 12:06:54 +02:00
|
|
|
try:
|
|
|
|
invoice = decode(bolt11)
|
|
|
|
except Bolt11Exception as exc:
|
|
|
|
return PaymentResponse(ok=False, error_message=str(exc))
|
2023-02-02 13:57:36 +01:00
|
|
|
|
2023-11-13 13:06:59 +01:00
|
|
|
if invoice.payment_hash in self.payment_secrets:
|
2022-10-04 09:51:47 +02:00
|
|
|
await self.queue.put(invoice)
|
2023-11-13 13:06:59 +01:00
|
|
|
self.paid_invoices.add(invoice.payment_hash)
|
|
|
|
return PaymentResponse(
|
|
|
|
ok=True,
|
|
|
|
checking_id=invoice.payment_hash,
|
|
|
|
fee_msat=0,
|
|
|
|
preimage=self.payment_secrets.get(invoice.payment_hash) or "0" * 64,
|
|
|
|
)
|
2022-02-03 13:52:51 +01:00
|
|
|
else:
|
2022-03-16 07:20:15 +01:00
|
|
|
return PaymentResponse(
|
|
|
|
ok=False, error_message="Only internal invoices can be used!"
|
|
|
|
)
|
2022-01-30 11:41:20 +01:00
|
|
|
|
2023-11-13 13:06:59 +01:00
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
|
|
|
paid = checking_id in self.paid_invoices
|
|
|
|
return PaymentStatus(paid)
|
2022-01-30 11:41:20 +01:00
|
|
|
|
2023-02-02 13:57:36 +01:00
|
|
|
async def get_payment_status(self, _: str) -> PaymentStatus:
|
2024-03-13 16:17:33 +01:00
|
|
|
return PaymentPendingStatus()
|
2022-01-30 11:41:20 +01:00
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
2022-01-31 12:07:29 +01:00
|
|
|
while True:
|
2023-09-25 12:06:54 +02:00
|
|
|
value: Bolt11 = await self.queue.get()
|
2022-10-04 09:51:47 +02:00
|
|
|
yield value.payment_hash
|