lnbits-legend/lnbits/wallets/fake.py

84 lines
2.7 KiB
Python
Raw Normal View History

2022-01-30 10:41:20 +00:00
import asyncio
import json
import httpx
from os import getenv
2022-01-30 19:43:30 +00:00
from datetime import datetime, timedelta
2022-01-30 10:41:20 +00:00
from typing import Optional, Dict, AsyncGenerator
2022-01-31 00:12:59 +00:00
import random
import string
from lnbits.helpers import urlsafe_short_hash
2022-01-30 10:41:20 +00:00
import hashlib
2022-01-31 11:07:29 +00:00
from ..bolt11 import encode, decode
2022-01-30 10:41:20 +00:00
from .base import (
StatusResponse,
InvoiceResponse,
PaymentResponse,
PaymentStatus,
Wallet,
)
2022-01-31 11:58:39 +00:00
2022-02-01 20:51:40 +00:00
class FakeWallet(Wallet):
2022-01-31 00:12:59 +00:00
async def status(self) -> StatusResponse:
2022-01-30 19:43:30 +00:00
print(
2022-01-31 16:52:35 +00:00
"FakeWallet funding source is for using LNbits as a centralised, stand-alone payment system with brrrrrr."
2022-01-30 10:41:20 +00:00
)
2022-01-31 11:58:39 +00:00
return StatusResponse(None, float("inf"))
2022-01-31 16:29:42 +00:00
2022-01-30 10:41:20 +00:00
async def create_invoice(
self,
amount: int,
memo: Optional[str] = None,
description_hash: Optional[bytes] = None,
) -> InvoiceResponse:
secret = getenv("FAKE_WALLET_SECRET")
2022-02-01 20:51:40 +00:00
data: Dict = {
"out": False,
"amount": amount,
"currency": "bc",
"privkey": hashlib.pbkdf2_hmac('sha256', secret.encode("utf-8"), ("FakeWallet").encode("utf-8"), 2048, 32).hex(),
2022-02-01 20:51:40 +00:00
"memo": None,
"description_hash": None,
"description": "",
"fallback": None,
"expires": None,
"route": None,
}
2022-02-01 23:16:38 +00:00
data["amount"] = amount * 1000
2022-02-01 20:51:40 +00:00
data["timestamp"] = datetime.now().timestamp()
2022-01-30 10:41:20 +00:00
if description_hash:
2022-02-01 20:51:40 +00:00
data["tags_set"] = ["h"]
2022-02-01 23:04:40 +00:00
data["description_hash"] = description_hash.hex()
2022-01-30 10:41:20 +00:00
else:
2022-02-01 20:51:40 +00:00
data["tags_set"] = ["d"]
data["memo"] = memo
data["description"] = memo
randomHash = data["privkey"][:6] + hashlib.sha256(
2022-01-31 16:29:42 +00:00
str(random.getrandbits(256)).encode("utf-8")
).hexdigest()[6:]
2022-02-01 20:51:40 +00:00
data["paymenthash"] = randomHash
payment_request = encode(data)
2022-01-30 10:41:20 +00:00
checking_id = randomHash
2022-01-31 00:12:59 +00:00
return InvoiceResponse(True, checking_id, payment_request)
2022-01-30 10:41:20 +00:00
async def pay_invoice(self, bolt11: str) -> PaymentResponse:
invoice = decode(bolt11)
if hasattr(invoice, 'checking_id') and invoice.checking_id[6:] == data["privkey"][:6]:
return PaymentResponse(True, invoice.payment_hash, 0)
else:
return PaymentResponse(ok = False, error_message="Only internal invoices can be used!")
2022-01-30 10:41:20 +00:00
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return PaymentStatus(False)
2022-01-30 10:41:20 +00:00
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
return PaymentStatus(False)
2022-01-30 10:41:20 +00:00
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
2022-01-31 11:07:29 +00:00
self.queue = asyncio.Queue(0)
while True:
value = await self.queue.get()
yield value