2022-01-30 11:41:20 +01:00
|
|
|
import asyncio
|
2022-07-16 14:23:03 +02:00
|
|
|
import hashlib
|
2022-01-31 01:12:59 +01:00
|
|
|
import random
|
2022-07-16 14:23:03 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from os import getenv
|
|
|
|
from typing import AsyncGenerator, Dict, Optional
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2022-07-17 14:34:25 +02:00
|
|
|
from environs import Env # type: ignore
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
2022-01-31 01:12:59 +01:00
|
|
|
from lnbits.helpers import urlsafe_short_hash
|
2022-07-16 14:23:03 +02:00
|
|
|
|
|
|
|
from ..bolt11 import decode, encode
|
2022-01-30 11:41:20 +01:00
|
|
|
from .base import (
|
|
|
|
InvoiceResponse,
|
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
2022-07-16 14:23:03 +02:00
|
|
|
StatusResponse,
|
2022-01-30 11:41:20 +01:00
|
|
|
Wallet,
|
|
|
|
)
|
|
|
|
|
2022-07-17 14:34:25 +02:00
|
|
|
env = Env()
|
|
|
|
env.read_env()
|
|
|
|
|
2022-01-31 12:58:39 +01:00
|
|
|
|
2022-02-01 21:51:40 +01:00
|
|
|
class FakeWallet(Wallet):
|
2022-01-31 01:12:59 +01:00
|
|
|
async def status(self) -> StatusResponse:
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.info(
|
2022-01-31 17:52:35 +01: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-01-31 12:58:39 +01:00
|
|
|
return StatusResponse(None, float("inf"))
|
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,
|
|
|
|
) -> InvoiceResponse:
|
2022-07-17 14:34:25 +02:00
|
|
|
# we set a default secret since FakeWallet is used for internal=True invoices
|
|
|
|
# and the user might not have configured a secret yet
|
|
|
|
secret = env.str("FAKE_WALLET_SECTRET", default="ToTheMoon1")
|
2022-02-01 21:51:40 +01:00
|
|
|
data: Dict = {
|
|
|
|
"out": False,
|
|
|
|
"amount": amount,
|
|
|
|
"currency": "bc",
|
2022-03-16 07:20:15 +01:00
|
|
|
"privkey": hashlib.pbkdf2_hmac(
|
|
|
|
"sha256",
|
|
|
|
secret.encode("utf-8"),
|
|
|
|
("FakeWallet").encode("utf-8"),
|
|
|
|
2048,
|
|
|
|
32,
|
|
|
|
).hex(),
|
2022-02-01 21:51:40 +01:00
|
|
|
"memo": None,
|
|
|
|
"description_hash": None,
|
|
|
|
"description": "",
|
|
|
|
"fallback": None,
|
|
|
|
"expires": None,
|
|
|
|
"route": None,
|
|
|
|
}
|
2022-02-02 00:16:38 +01:00
|
|
|
data["amount"] = amount * 1000
|
2022-02-01 21:51:40 +01:00
|
|
|
data["timestamp"] = datetime.now().timestamp()
|
2022-01-30 11:41:20 +01:00
|
|
|
if description_hash:
|
2022-02-01 21:51:40 +01:00
|
|
|
data["tags_set"] = ["h"]
|
2022-02-02 00:04:40 +01:00
|
|
|
data["description_hash"] = description_hash.hex()
|
2022-01-30 11:41:20 +01:00
|
|
|
else:
|
2022-02-01 21:51:40 +01:00
|
|
|
data["tags_set"] = ["d"]
|
|
|
|
data["memo"] = memo
|
|
|
|
data["description"] = memo
|
2022-03-16 07:20:15 +01:00
|
|
|
randomHash = (
|
|
|
|
data["privkey"][:6]
|
|
|
|
+ hashlib.sha256(str(random.getrandbits(256)).encode("utf-8")).hexdigest()[
|
|
|
|
6:
|
|
|
|
]
|
|
|
|
)
|
2022-02-01 21:51:40 +01:00
|
|
|
data["paymenthash"] = randomHash
|
|
|
|
payment_request = encode(data)
|
2022-01-30 11:41:20 +01:00
|
|
|
checking_id = randomHash
|
|
|
|
|
2022-01-31 01:12:59 +01:00
|
|
|
return InvoiceResponse(True, checking_id, payment_request)
|
|
|
|
|
2022-03-16 07:20:15 +01:00
|
|
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
|
|
|
invoice = decode(bolt11)
|
|
|
|
if (
|
|
|
|
hasattr(invoice, "checking_id")
|
|
|
|
and invoice.checking_id[6:] == data["privkey"][:6]
|
|
|
|
):
|
2022-02-03 13:52:51 +01:00
|
|
|
return PaymentResponse(True, invoice.payment_hash, 0)
|
|
|
|
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
|
|
|
|
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
2022-07-19 12:33:28 +02:00
|
|
|
return PaymentStatus(None)
|
2022-01-30 11:41:20 +01:00
|
|
|
|
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2022-07-19 12:33:28 +02:00
|
|
|
return PaymentStatus(None)
|
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
|
|
|
self.queue = asyncio.Queue(0)
|
|
|
|
while True:
|
|
|
|
value = await self.queue.get()
|
|
|
|
yield value
|