diff --git a/lnbits/extensions/nostr/__init__.py b/lnbits/extensions/nostr/__init__.py index 775960e3d..9774a2790 100644 --- a/lnbits/extensions/nostr/__init__.py +++ b/lnbits/extensions/nostr/__init__.py @@ -11,7 +11,5 @@ nostr_ext: APIRouter = APIRouter(prefix="/nostr", tags=["nostr"]) def nostr_renderer(): return template_renderer(["lnbits/extensions/nostr/templates"]) - -from .lnurl import * # noqa from .views import * # noqa from .views_api import * # noqa diff --git a/lnbits/extensions/nostr/crud.py b/lnbits/extensions/nostr/crud.py index 55e99ec67..0b30bc9a7 100644 --- a/lnbits/extensions/nostr/crud.py +++ b/lnbits/extensions/nostr/crud.py @@ -1,33 +1,31 @@ from typing import List, Optional, Union from lnbits.helpers import urlsafe_short_hash - +import shortuuid from . import db -from .models import nostrKeys, nostrNotes, nostrRelays, nostrConnections +from .models import nostrKeys, nostrNotes, nostrCreateRelays, nostrRelays, nostrConnections, nostrCreateConnections ###############KEYS################## async def create_nostrkeys( data: nostrKeys ) -> nostrKeys: - nostrkey_id = urlsafe_short_hash() await db.execute( """ INSERT INTO nostr.keys ( - id, pubkey, privkey ) - VALUES (?, ?, ?) + VALUES (?, ?) """, - (nostrkey_id, data.pubkey, data.privkey), + (data.pubkey, data.privkey), ) return await get_nostrkeys(nostrkey_id) -async def get_nostrkeys(nostrkey_id: str) -> nostrKeys: +async def get_nostrkeys(pubkey: str) -> nostrKeys: row = await db.fetchone( - "SELECT * FROM nostr.keys WHERE id = ?", - (lnurldevicepayment_id,), + "SELECT * FROM nostr.keys WHERE pubkey = ?", + (pubkey,), ) return nostrKeys(**row) if row else None @@ -64,9 +62,12 @@ async def get_nostrnotes(nostrnote_id: str) -> nostrNotes: ###############RELAYS################## async def create_nostrrelays( - relay: str -) -> nostrRelays: - nostrrelay_id = urlsafe_short_hash() + data: nostrCreateRelays +) -> nostrCreateRelays: + nostrrelay_id = shortuuid.uuid(name=relay) + + if await get_nostrrelays(nostrrelay_id): + return "error" await db.execute( """ INSERT INTO nostr.relays ( @@ -75,7 +76,7 @@ async def create_nostrrelays( ) VALUES (?, ?) """, - (nostrrelay_id, relay), + (nostrrelay_id, data.relay), ) return await get_nostrnotes(nostrrelay_id) @@ -90,45 +91,25 @@ async def get_nostrrelays(nostrrelay_id: str) -> nostrRelays: ###############CONNECTIONS################## async def create_nostrconnections( - data: nostrNotes -) -> nostrNotes: - nostrkey_id = urlsafe_short_hash() + data: nostrCreateConnections +) -> nostrCreateConnections: + nostrrelay_id = shortuuid.uuid(name=data.relayid + data.pubkey) await db.execute( """ - INSERT INTO nostr.notes ( + INSERT INTO nostr.connections ( id, pubkey, - created_at, - kind, - tags, - content, - sig + relayid ) - VALUES (?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?) """, - (data.id, data.pubkey, data.created_at, data.kind, data.tags, data.content, data.sig), + (data.id, data.pubkey, data.relayid), ) - return await get_nostrnotes(data.id) + return await get_nostrconnections(data.id) -async def get_nostrnotes(nostrnote_id: str) -> nostrNotes: +async def get_nostrconnections(nostrconnections_id: str) -> nostrConnections: row = await db.fetchone( - "SELECT * FROM nostr.notes WHERE id = ?", - (nostrnote_id,), + "SELECT * FROM nostr.connections WHERE id = ?", + (nostrconnections_id,), ) - return nostrNotes(**row) if row else None - - - -async def update_lnurldevicepayment( - lnurldevicepayment_id: str, **kwargs -) -> Optional[lnurldevicepayment]: - q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) - await db.execute( - f"UPDATE lnurldevice.lnurldevicepayment SET {q} WHERE id = ?", - (*kwargs.values(), lnurldevicepayment_id), - ) - row = await db.fetchone( - "SELECT * FROM lnurldevice.lnurldevicepayment WHERE id = ?", - (lnurldevicepayment_id,), - ) - return lnurldevicepayment(**row) if row else None \ No newline at end of file + return nostrConnections(**row) if row else None \ No newline at end of file diff --git a/lnbits/extensions/nostr/migrations.py b/lnbits/extensions/nostr/migrations.py index de32a5787..0c616110e 100644 --- a/lnbits/extensions/nostr/migrations.py +++ b/lnbits/extensions/nostr/migrations.py @@ -24,7 +24,7 @@ async def m001_initial(db): kind INT NOT NULL, tags TEXT NOT NULL, content TEXT NOT NULL, - sig TEXT NOT NULL, + sig TEXT NOT NULL ); """ ) diff --git a/lnbits/extensions/nostr/models.py b/lnbits/extensions/nostr/models.py index ba0c87a5e..e21e5d76f 100644 --- a/lnbits/extensions/nostr/models.py +++ b/lnbits/extensions/nostr/models.py @@ -3,15 +3,10 @@ from sqlite3 import Row from typing import Optional from fastapi import Request -from lnurl import Lnurl -from lnurl import encode as lnurl_encode # type: ignore -from lnurl.models import LnurlPaySuccessAction, UrlAction # type: ignore -from lnurl.types import LnurlPayMetadata # type: ignore from pydantic import BaseModel from pydantic.main import BaseModel class nostrKeys(BaseModel): - id: str pubkey: str privkey: str @@ -24,6 +19,13 @@ class nostrNotes(BaseModel): content: str sig: str +class nostrCreateRelays(BaseModel): + relay: str + +class nostrCreateConnections(BaseModel): + pubkey: str + relayid: str + class nostrRelays(BaseModel): id: str relay: str diff --git a/lnbits/extensions/nostr/templates/lnurldevice/_api_docs.html b/lnbits/extensions/nostr/templates/lnurldevice/_api_docs.html deleted file mode 100644 index af69b76e3..000000000 --- a/lnbits/extensions/nostr/templates/lnurldevice/_api_docs.html +++ /dev/null @@ -1,169 +0,0 @@ - - -

- Register LNURLDevice devices to receive payments in your LNbits wallet.
- Build your own here - https://github.com/arcbtc/bitcoinpos
- - Created by, Ben Arc -

-
- - - - - /lnurldevice/api/v1/lnurlpos -
Headers
- {"X-Api-Key": <admin_key>}
-
- Body (application/json) -
-
- Returns 200 OK (application/json) -
- [<lnurldevice_object>, ...] -
Curl example
- curl -X POST {{ request.base_url }}api/v1/lnurldevice -d '{"title": - <string>, "message":<string>, "currency": - <integer>}' -H "Content-type: application/json" -H "X-Api-Key: - {{user.wallets[0].adminkey }}" - -
-
-
- - - - PUT - /lnurldevice/api/v1/lnurlpos/<lnurldevice_id> -
Headers
- {"X-Api-Key": <admin_key>}
-
- Body (application/json) -
-
- Returns 200 OK (application/json) -
- [<lnurldevice_object>, ...] -
Curl example
- curl -X POST {{ request.base_url - }}api/v1/lnurlpos/<lnurldevice_id> -d ''{"title": - <string>, "message":<string>, "currency": - <integer>} -H "Content-type: application/json" -H "X-Api-Key: - {{user.wallets[0].adminkey }}" - -
-
-
- - - - - GET - /lnurldevice/api/v1/lnurlpos/<lnurldevice_id> -
Headers
- {"X-Api-Key": <invoice_key>}
-
- Body (application/json) -
-
- Returns 200 OK (application/json) -
- [<lnurldevice_object>, ...] -
Curl example
- curl -X GET {{ request.base_url - }}api/v1/lnurlpos/<lnurldevice_id> -H "X-Api-Key: {{ - user.wallets[0].inkey }}" - -
-
-
- - - - GET - /lnurldevice/api/v1/lnurlposs -
Headers
- {"X-Api-Key": <invoice_key>}
-
- Body (application/json) -
-
- Returns 200 OK (application/json) -
- [<lnurldevice_object>, ...] -
Curl example
- curl -X GET {{ request.base_url }}api/v1/lnurldevices -H - "X-Api-Key: {{ user.wallets[0].inkey }}" - -
-
-
- - - - DELETE - /lnurldevice/api/v1/lnurlpos/<lnurldevice_id> -
Headers
- {"X-Api-Key": <admin_key>}
-
Returns 204 NO CONTENT
- -
Curl example
- curl -X DELETE {{ request.base_url - }}api/v1/lnurlpos/<lnurldevice_id> -H "X-Api-Key: {{ - user.wallets[0].adminkey }}" - -
-
-
-
-
diff --git a/lnbits/extensions/nostr/templates/lnurldevice/error.html b/lnbits/extensions/nostr/templates/lnurldevice/error.html deleted file mode 100644 index d8e418329..000000000 --- a/lnbits/extensions/nostr/templates/lnurldevice/error.html +++ /dev/null @@ -1,34 +0,0 @@ -{% extends "public.html" %} {% block page %} -
-
- - -
-

LNURL-pay not paid

-
- - -
-
-
-
-
- - {% endblock %} {% block scripts %} - - - - {% endblock %} -
diff --git a/lnbits/extensions/nostr/templates/lnurldevice/index.html b/lnbits/extensions/nostr/templates/lnurldevice/index.html deleted file mode 100644 index b51e25568..000000000 --- a/lnbits/extensions/nostr/templates/lnurldevice/index.html +++ /dev/null @@ -1,534 +0,0 @@ -{% extends "base.html" %} {% from "macros.jinja" import window_vars with context -%} {% block page %} -
-
- - - {% raw %} - New LNURLDevice instance - - - - - - -
-
-
lNURLdevice
-
- -
- - - - Export to CSV -
-
- - - - - {% endraw %} - -
-
-
- -
- - -
- {{SITE_TITLE}} LNURLDevice Extension -
-
- - - {% include "lnurldevice/_api_docs.html" %} - -
-
- - - -
LNURLDevice device string
- {% raw - %}{{location}}/lnurldevice/api/v1/lnurl/{{settingsDialog.data.id}}, - {{settingsDialog.data.key}}, {{settingsDialog.data.currency}}{% endraw - %} Click to copy URL - - -
- -
-
-
- - - - - - - - - - - - -
- Update lnurldevice - Create lnurldevice - Cancel -
-
-
-
-
-{% endblock %} {% block scripts %} {{ window_vars(user) }} - - -{% endblock %} diff --git a/lnbits/extensions/nostr/templates/lnurldevice/paid.html b/lnbits/extensions/nostr/templates/lnurldevice/paid.html deleted file mode 100644 index c185ecce6..000000000 --- a/lnbits/extensions/nostr/templates/lnurldevice/paid.html +++ /dev/null @@ -1,27 +0,0 @@ -{% extends "public.html" %} {% block page %} -
-
- - -
-

{{ pin }}

-
-
-
-
-
- - {% endblock %} {% block scripts %} - - - - {% endblock %} -
diff --git a/lnbits/extensions/nostr/templates/nostr/index.html b/lnbits/extensions/nostr/templates/nostr/index.html new file mode 100644 index 000000000..f17d0243b --- /dev/null +++ b/lnbits/extensions/nostr/templates/nostr/index.html @@ -0,0 +1,159 @@ +{% extends "base.html" %} {% from "macros.jinja" import window_vars with context +%} {% block page %} +
+
+ + +
+
+
Nostr
+
+ +
+ + + + Export to CSV +
+
+ + {% raw %} + + + + {% endraw %} + +
+
+
+ +
+ + +
{{SITE_TITLE}} Nostr Extension
+ + Okay +
+
+
+
+{% endblock %} {% block scripts %} {{ window_vars(user) }} + + +{% endblock %} diff --git a/lnbits/extensions/nostr/views_api.py b/lnbits/extensions/nostr/views_api.py index a479cad8f..9e7ccfffa 100644 --- a/lnbits/extensions/nostr/views_api.py +++ b/lnbits/extensions/nostr/views_api.py @@ -7,42 +7,18 @@ from starlette.exceptions import HTTPException from lnbits.core.crud import get_user from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key -from lnbits.extensions.lnurldevice import lnurldevice_ext +from lnbits.extensions.nostr import nostr_ext from lnbits.utils.exchange_rates import currencies -from . import lnurldevice_ext +from . import nostr_ext from .crud import ( - create_lnurldevice, - delete_lnurldevice, - get_lnurldevice, - get_lnurldevices, - update_lnurldevice, + create_nostrkeys, + get_nostrkeys, + create_nostrnotes, + get_nostrnotes, + create_nostrrelays, + get_nostrrelays, + create_nostrconnections, + get_nostrconnections, ) -from .models import createLnurldevice - - -@nostr_ext.get("/api/v1/lnurlpos") -async def api_check_daemon(wallet: WalletTypeInfo = Depends(get_key_type)): - wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids - try: - return [ - {**lnurldevice.dict()} for lnurldevice in await get_lnurldevices(wallet_ids) - ] - except: - return "" - -@nostr_ext.delete("/api/v1/lnurlpos/{lnurldevice_id}") -async def api_lnurldevice_delete( - wallet: WalletTypeInfo = Depends(require_admin_key), - lnurldevice_id: str = Query(None), -): - lnurldevice = await get_lnurldevice(lnurldevice_id) - - if not lnurldevice: - raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, detail="Wallet link does not exist." - ) - - await delete_lnurldevice(lnurldevice_id) - - return "", HTTPStatus.NO_CONTENT \ No newline at end of file +from .models import nostrKeys \ No newline at end of file