From 17f4bb72186562cf2cba8b46a25d2a4193a710ec Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 24 Jul 2024 14:18:57 +0000 Subject: [PATCH] code cleanup --- .github/{xworkflows => workflows}/main.yml | 0 .github/{xworkflows => workflows}/release.yml | 0 config.json | 2 +- crud.py | 37 +++++++++++-------- description.md | 10 ----- models.py | 5 +-- tasks.py | 21 ++++++----- toc.md | 22 ----------- views.py | 5 --- views_api.py | 6 +-- 10 files changed, 35 insertions(+), 73 deletions(-) rename .github/{xworkflows => workflows}/main.yml (100%) rename .github/{xworkflows => workflows}/release.yml (100%) delete mode 100644 description.md delete mode 100644 toc.md diff --git a/.github/xworkflows/main.yml b/.github/workflows/main.yml similarity index 100% rename from .github/xworkflows/main.yml rename to .github/workflows/main.yml diff --git a/.github/xworkflows/release.yml b/.github/workflows/release.yml similarity index 100% rename from .github/xworkflows/release.yml rename to .github/workflows/release.yml diff --git a/config.json b/config.json index 988bb50..66d391d 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "name": "NWC Service Provider", - "short_description": "A NWC service provider for lnbits", + "short_description": "A NWC service provider for LNbits", "tile": "/nwcprovider/static/image/nwcprovider.png", "min_lnbits_version": "0.12.5", "contributors": [ diff --git a/crud.py b/crud.py index 3182937..d2a9960 100644 --- a/crud.py +++ b/crud.py @@ -12,10 +12,11 @@ async def create_nwc( expires_at: int , permissions:List[str] , budgets: Optional[List[NWCNewBudget]] = None -): +) -> NWCKey: # Check if the key already exists if await get_nwc(pubkey, None, True): raise Exception("Public key already used") + # If not, create it now=int(time.time()) await db.execute( """ @@ -24,7 +25,7 @@ async def create_nwc( """, (pubkey, wallet_id, description, " ".join(permissions), now,int(expires_at) if expires_at else 0, now) ) - + # Add budgets if budgets: for budget in budgets: await db.execute( @@ -34,6 +35,7 @@ async def create_nwc( """, (pubkey, budget.budget_msats, budget.refresh_window, budget.created_at) ) + # Return the created key return NWCKey( pubkey=pubkey, wallet=wallet_id, @@ -44,6 +46,7 @@ async def create_nwc( last_used=now ) + async def delete_nwc( pubkey: str, wallet_id:str @@ -69,6 +72,7 @@ async def get_wallet_nwcs( ) return [NWCKey(**row) for row in rows] + async def get_nwc( pubkey: str, wallet_id: Optional[str] = None, @@ -95,6 +99,7 @@ async def get_nwc( ) return NWCKey(**row) + async def get_budgets_nwc(pubkey, calculate_spent=False): rows = await db.fetchall( "SELECT * FROM nwcprovider.budgets WHERE pubkey = ?", (pubkey) @@ -114,19 +119,19 @@ async def get_budgets_nwc(pubkey, calculate_spent=False): return budgets -async def log_nwc( - pubkey: str, - payload:Optional[Any] = None -): - if not payload: payload="" - payload = json.dumps(payload) - await db.execute( - """ - INSERT INTO nwcprovider.logs (pubkey, payload, created_at) - VALUES (?, ?, ?) - """, - (pubkey, payload, int(time.time())) - ) +# async def log_nwc( +# pubkey: str, +# payload:Optional[Any] = None +# ): +# if not payload: payload="" +# payload = json.dumps(payload) +# await db.execute( +# """ +# INSERT INTO nwcprovider.logs (pubkey, payload, created_at) +# VALUES (?, ?, ?) +# """, +# (pubkey, payload, int(time.time())) +# ) async def tracked_spend_nwc( @@ -145,7 +150,7 @@ async def tracked_spend_nwc( SELECT SUM(amount_msats) FROM nwcprovider.spent WHERE pubkey = ? AND created_at >= ? AND created_at < ? """, (pubkey, last_cycle, next_cycle) - )[0] or 0 # Ensure we get an int, default to 0 if None + )[0] or 0 if tot_spent_in_range_msats + amount_msats > budget.budget_msats: in_budget = False break diff --git a/description.md b/description.md deleted file mode 100644 index b5c097e..0000000 --- a/description.md +++ /dev/null @@ -1,10 +0,0 @@ -NWCService can be used as a template for building new extensions, it includes a bunch of functions that can be edited/deleted as you need them. - -This is a longform description that will be used in the advanced description when users click on the "more" button on the extension cards. - -Adding some bullets is nice covering: - -* Functionality -* Use cases - -...and some other text about just how great this etension is. diff --git a/models.py b/models.py index fbae213..b2e0b4f 100644 --- a/models.py +++ b/models.py @@ -22,11 +22,9 @@ class NWCKey(BaseModel): try: return cls.permissions.split(" ") except: - # TODO: log error return [] - @classmethod def from_row(cls, row: Dict[str, Any]) -> "NWCKey": return cls(**row) @@ -51,7 +49,6 @@ class NWCBudget(BaseModel): next_cycle = last_cycle + cls.refresh_window return last_cycle, next_cycle - @classmethod def from_row(cls, row: Row) -> "NWCBudget": return cls(**dict(row)) @@ -68,12 +65,12 @@ class NWCLog(BaseModel): return cls(**dict(row)) - class NWCNewBudget(BaseModel): budget_msats: int refresh_window: int created_at: int + class NWCRegistrationRequest(BaseModel): permissions: List[str] description: str diff --git a/tasks.py b/tasks.py index acd81a6..94feaa8 100644 --- a/tasks.py +++ b/tasks.py @@ -6,7 +6,8 @@ from lnbits.core.models import Payment from lnbits.core.services import create_invoice, pay_invoice, check_transaction_status, PaymentError from bolt11 import decode as bolt11_decode from lnbits.core.crud import get_wallet_payment, get_payments, get_wallet -from .crud import get_nwc, tracked_spend_nwc, log_nwc, get_config_nwc +from .crud import get_nwc, tracked_spend_nwc, get_config_nwc +#from .crud import log_nwc from . import execution_queue from .NWCServiceProvider import NWCServiceProvider from typing import Dict, List, Tuple @@ -113,7 +114,7 @@ async def _on_pay_invoice(sp: NWCServiceProvider, pubkey: str, payload: Dict) -> out = { "preimage": preimage, } - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return [(out,None, [])] @@ -125,7 +126,7 @@ async def _on_multi_pay_invoice(sp: NWCServiceProvider, pubkey: str, payload: Di params = payload.get("params", {}) invoices = params.get("invoices", []) results = [] - + # Ensures all invoices are provided for i in invoices: invoice = i.get("invoice",None) @@ -157,7 +158,7 @@ async def _on_multi_pay_invoice(sp: NWCServiceProvider, pubkey: str, payload: Di "code": "INTERNAL", "message": str(e) })) - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return results @@ -198,7 +199,7 @@ async def _on_make_invoice(sp: NWCServiceProvider, pubkey: str, payload: Dict) - } if expiry: res["expires_at"] = int(time.time()) + int(expiry) - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return [(res, None, [])] @@ -238,7 +239,7 @@ async def _on_lookup_invoice(sp: NWCServiceProvider, pubkey: str, payload: Dict) } if invoice_data.description_hash: res["description_hash"] = invoice_data.description_hash - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return [(res, None, [])] @@ -288,7 +289,7 @@ async def _on_list_transactions(sp: NWCServiceProvider, pubkey: str, payload: Di "settled_at": p.time if is_settled else None, "metadata": {} }) - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return [({ "transactions": transactions }, None, [])] @@ -304,7 +305,7 @@ async def _on_get_balance(sp: NWCServiceProvider, pubkey: str, payload: Dict) -> if not wallet: raise Exception("Wallet not found") balance = wallet.balance_msat - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return [({ "balance": balance }, None, [])] @@ -317,6 +318,7 @@ async def _on_get_info(sp: NWCServiceProvider, pubkey: str, payload: Dict) -> Li return [(None, error, [])] sp_methods = sp.getSupportedMethods() permissions = nwc.getPermissions() + # Filter only methods supported by the extension and allowed by the permissions account_methods = [] for spm in sp_methods: for p in permissions: @@ -324,7 +326,7 @@ async def _on_get_info(sp: NWCServiceProvider, pubkey: str, payload: Dict) -> Li if spm in allowed_methods: account_methods.append(spm) break - await log_nwc(pubkey, payload) + #await log_nwc(pubkey, payload) return [({ "alias": settings.lnbits_site_title, "color": "", @@ -336,7 +338,6 @@ async def _on_get_info(sp: NWCServiceProvider, pubkey: str, payload: Dict) -> Li - async def handle_nwc(): priv_key = await get_config_nwc("provider_key") relay = await get_config_nwc("relay") diff --git a/toc.md b/toc.md deleted file mode 100644 index fc97b10..0000000 --- a/toc.md +++ /dev/null @@ -1,22 +0,0 @@ -# Terms and Conditions for LNbits Extension - -## 1. Acceptance of Terms -By installing and using the LNbits extension ("Extension"), you agree to be bound by these terms and conditions ("Terms"). If you do not agree to these Terms, do not use the Extension. - -## 2. License -The Extension is free and open-source software, released under [specify the FOSS license here, e.g., GPL-3.0, MIT, etc.]. You are permitted to use, copy, modify, and distribute the Extension under the terms of that license. - -## 3. No Warranty -The Extension is provided "as is" and with all faults, and the developer expressly disclaims all warranties of any kind, whether express, implied, statutory, or otherwise, including but not limited to warranties of merchantability, fitness for a particular purpose, non-infringement, and any warranties arising out of course of dealing or usage of trade. No advice or information, whether oral or written, obtained from the developer or elsewhere will create any warranty not expressly stated in this Terms. - -## 4. Limitation of Liability -In no event will the developer be liable to you or any third party for any direct, indirect, incidental, special, consequential, or punitive damages, including lost profit, lost revenue, loss of data, or other damages arising out of or in connection with your use of the Extension, even if the developer has been advised of the possibility of such damages. The foregoing limitation of liability shall apply to the fullest extent permitted by law in the applicable jurisdiction. - -## 5. Modification of Terms -The developer reserves the right to modify these Terms at any time. You are advised to review these Terms periodically for any changes. Changes to these Terms are effective when they are posted on the appropriate location within or associated with the Extension. - -## 6. General Provisions -If any provision of these Terms is held to be invalid or unenforceable, that provision will be enforced to the maximum extent permissible, and the other provisions of these Terms will remain in full force and effect. These Terms constitute the entire agreement between you and the developer regarding the use of the Extension. - -## 7. Contact Information -If you have any questions about these Terms, please contact the developer at [developer's contact information]. diff --git a/views.py b/views.py index 346d08c..df5e63f 100644 --- a/views.py +++ b/views.py @@ -1,13 +1,8 @@ -from http import HTTPStatus from fastapi import Depends, Request -from fastapi.templating import Jinja2Templates -from starlette.exceptions import HTTPException from starlette.responses import HTMLResponse - from lnbits.core.models import User from lnbits.decorators import check_user_exists -from lnbits.settings import settings from lnbits.decorators import check_admin from . import nwcprovider_ext,nwcprovider_renderer diff --git a/views_api.py b/views_api.py index 5e18380..5ca0a0a 100644 --- a/views_api.py +++ b/views_api.py @@ -1,16 +1,12 @@ from http import HTTPStatus import json from typing import List, Optional, Dict -from .models import NWCKey, NWCBudget,NWCRegistrationRequest , NWCNewBudget,NWCGetResponse +from .models import NWCRegistrationRequest, NWCGetResponse from fastapi import Depends, Request -from loguru import logger from lnbits.decorators import ( WalletTypeInfo, - get_key_type, require_admin_key ) -from pydantic import BaseModel,Field -from fastapi import HTTPException from fastapi.responses import JSONResponse from . import nwcprovider_ext