From a220acb5837f6624c550d8f161c4d61ebd890e8c Mon Sep 17 00:00:00 2001 From: benarc Date: Thu, 1 Dec 2022 14:41:57 +0000 Subject: [PATCH] Removed id, using param instead --- lnbits/bolt11.py | 6 +++--- lnbits/core/services.py | 12 ++++++------ lnbits/core/views/api.py | 14 +++++++++++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lnbits/bolt11.py b/lnbits/bolt11.py index 32b43feb..08f1f1e5 100644 --- a/lnbits/bolt11.py +++ b/lnbits/bolt11.py @@ -166,7 +166,7 @@ def lnencode(addr, privkey): if addr.amount: amount = Decimal(str(addr.amount)) # We can only send down to millisatoshi. - if amount * 10**12 % 10: + if amount * 10 ** 12 % 10: raise ValueError( "Cannot encode {}: too many decimal places".format(addr.amount) ) @@ -271,7 +271,7 @@ class LnAddr(object): def shorten_amount(amount): """Given an amount in bitcoin, shorten it""" # Convert to pico initially - amount = int(amount * 10**12) + amount = int(amount * 10 ** 12) units = ["p", "n", "u", "m", ""] for unit in units: if amount % 1000 == 0: @@ -290,7 +290,7 @@ def _unshorten_amount(amount: str) -> int: # * `u` (micro): multiply by 0.000001 # * `n` (nano): multiply by 0.000000001 # * `p` (pico): multiply by 0.000000000001 - units = {"p": 10**12, "n": 10**9, "u": 10**6, "m": 10**3} + units = {"p": 10 ** 12, "n": 10 ** 9, "u": 10 ** 6, "m": 10 ** 3} unit = str(amount)[-1] # BOLT #11: diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 8a88411a..623f7813 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -329,12 +329,12 @@ async def perform_lnurlauth( sign_len = 6 + r_len + s_len signature = BytesIO() - signature.write(0x30.to_bytes(1, "big", signed=False)) + signature.write(0x30 .to_bytes(1, "big", signed=False)) signature.write((sign_len - 2).to_bytes(1, "big", signed=False)) - signature.write(0x02.to_bytes(1, "big", signed=False)) + signature.write(0x02 .to_bytes(1, "big", signed=False)) signature.write(r_len.to_bytes(1, "big", signed=False)) signature.write(r) - signature.write(0x02.to_bytes(1, "big", signed=False)) + signature.write(0x02 .to_bytes(1, "big", signed=False)) signature.write(s_len.to_bytes(1, "big", signed=False)) signature.write(s) @@ -388,9 +388,9 @@ class WebsocketConnectionManager: def __init__(self): self.active_connections: List[WebSocket] = [] - async def connect(self, websocket: WebSocket, item_id: str): + async def connect(self, websocket: WebSocket): await websocket.accept() - websocket.id = item_id + logger.debug(websocket) self.active_connections.append(websocket) def disconnect(self, websocket: WebSocket): @@ -398,7 +398,7 @@ class WebsocketConnectionManager: async def send_data(self, message: str, item_id: str): for connection in self.active_connections: - if connection.id == item_id: + if connection.path_params["item_id"] == item_id: await connection.send_text(message) diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index a2a93736..f78219bf 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -9,9 +9,18 @@ from io import BytesIO from typing import Dict, List, Optional, Tuple, Union from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse +import async_timeout import httpx import pyqrcode -from fastapi import Depends, Header, Query, Response, Request, WebSocket, WebSocketDisconnect +from fastapi import ( + Depends, + Header, + Query, + Request, + Response, + WebSocket, + WebSocketDisconnect, +) from fastapi.exceptions import HTTPException from fastapi.params import Body from loguru import logger @@ -20,7 +29,6 @@ from pydantic.fields import Field from sse_starlette.sse import EventSourceResponse, ServerSentEvent from starlette.responses import HTMLResponse, StreamingResponse -import async_timeout from lnbits import bolt11, lnurl from lnbits.core.models import Payment, Wallet from lnbits.decorators import ( @@ -706,7 +714,7 @@ async def api_auditor(wallet: WalletTypeInfo = Depends(get_key_type)): @core_app.websocket("/api/v1/ws/{item_id}") async def websocket_connect(websocket: WebSocket, item_id: str): - await websocketManager.connect(websocket, item_id) + await websocketManager.connect(websocket) try: while True: data = await websocket.receive_text()