lnbits-legend/lnbits/wallets/fake.py

82 lines
2.4 KiB
Python
Raw Normal View History

2022-01-30 11:41:20 +01:00
import asyncio
import json
import httpx
from os import getenv
2022-01-30 20:43:30 +01:00
from datetime import datetime, timedelta
2022-01-30 11:41:20 +01:00
from typing import Optional, Dict, AsyncGenerator
2022-01-31 01:12:59 +01:00
import random
import string
from lnbits.helpers import urlsafe_short_hash
2022-01-30 11:41:20 +01:00
import hashlib
2022-01-31 12:07:29 +01:00
from ..bolt11 import encode, decode
2022-01-30 11:41:20 +01:00
from .base import (
StatusResponse,
InvoiceResponse,
PaymentResponse,
PaymentStatus,
Wallet,
)
class FakeWallet(Wallet):
2022-01-31 01:12:59 +01:00
def __init__(self):
self.amount = 0
self.timestamp = 0
self.currency = "bc"
self.paymenthash = ""
self.privkey = getenv("FAKE_WALLET_KEY")
self.memo = ""
self.description_hashed = ""
self.description = ""
self.fallback = None
self.expires = None
self.route = None
2022-01-31 12:58:39 +01:00
2022-01-31 01:12:59 +01:00
async def status(self) -> StatusResponse:
2022-01-30 20:43:30 +01:00
print(
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-01-31 12:58:39 +01:00
2022-01-31 01:12:59 +01:00
self.amount = amount
self.timestamp = datetime.now().timestamp()
2022-01-30 11:41:20 +01:00
if description_hash:
2022-01-31 01:12:59 +01:00
self.tags_set = {"h"}
2022-02-01 21:17:42 +01:00
self.description_hashed = description_hash.hex()
2022-01-30 11:41:20 +01:00
else:
2022-01-31 01:12:59 +01:00
self.tags_set = {"d"}
self.memo = memo
self.description = memo
letters = string.ascii_lowercase
2022-01-31 17:29:42 +01:00
randomHash = hashlib.sha256(
str(random.getrandbits(256)).encode("utf-8")
).hexdigest()
2022-01-31 01:12:59 +01:00
self.paymenthash = randomHash
payment_request = encode(self)
2022-01-30 20:43:30 +01:00
print(payment_request)
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-01-30 11:41:20 +01:00
async def pay_invoice(self, bolt11: str) -> PaymentResponse:
2022-01-31 12:07:29 +01:00
invoice = decode(bolt11)
return PaymentResponse(True, invoice.payment_hash, 0)
2022-01-30 11:41:20 +01:00
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return PaymentStatus(False)
2022-01-30 11:41:20 +01:00
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
return PaymentStatus(False)
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