Compare commits

...

10 commits

Author SHA1 Message Date
PatMulligan
e66f997853
FIX: Ensure valid json (#39)
Some checks failed
CI / lint (push) Has been cancelled
/ release (push) Has been cancelled
CI / tests (push) Has been cancelled
/ pullrequest (push) Has been cancelled
* Build EVENT message with json.dumps instead of string interpolation

Ensures outbound Nostr messages are valid JSON and safely escaped by
constructing the payload as Python objects and serializing with
json.dumps

* improve logs

* remove log causing check failure
2025-09-15 13:42:33 +03:00
blackcoffeexbt
02af516903
Update default relay list (#38)
nodestr.fmt.wiz.biz and the ZBD relays no longer exist. Added relay.nostrconnect.com as a new relay.
2025-09-10 11:11:39 +02:00
blackcoffeexbt
89f7c99f75
Merge pull request #37 from lnbits/feat/uv 2025-09-10 09:40:42 +01:00
dni ⚡
bd355a8a01
fixup! 2025-09-10 10:12:57 +02:00
dni ⚡
ed67ad3294
feat: use uv for dev 2025-09-10 10:08:24 +02:00
PatMulligan
c42735ca85
add urlsafe=True parameter (#34) 2025-07-01 12:10:38 +03:00
Tiago Vasconcelos
896f818da5
Merge pull request #33 from lnbits/add_description_md
Create description.md
2024-12-12 14:43:37 +00:00
Tiago Vasconcelos
47d52d7ec3
Create description.md 2024-12-11 14:14:24 +00:00
Vlad Stan
f5c048b22d fix: missing status 2024-11-06 11:47:05 +02:00
dni ⚡
db20915756
v1 in the middle (#32) 2024-10-31 13:15:31 +02:00
18 changed files with 2451 additions and 2681 deletions

View file

@ -11,14 +11,9 @@ jobs:
tests:
runs-on: ubuntu-latest
needs: [lint]
strategy:
matrix:
python-version: ['3.9', '3.10']
steps:
- uses: actions/checkout@v4
- uses: lnbits/lnbits/.github/actions/prepare@dev
with:
python-version: ${{ matrix.python-version }}
- name: Run pytest
uses: pavelzw/pytest-action@v2
env:
@ -30,5 +25,5 @@ jobs:
job-summary: true
emoji: false
click-to-expand: true
custom-pytest: poetry run pytest
report-title: 'test (${{ matrix.python-version }})'
custom-pytest: uv run pytest
report-title: 'test'

View file

@ -5,27 +5,27 @@ format: prettier black ruff
check: mypy pyright checkblack checkruff checkprettier
prettier:
poetry run ./node_modules/.bin/prettier --write .
uv run ./node_modules/.bin/prettier --write .
pyright:
poetry run ./node_modules/.bin/pyright
uv run ./node_modules/.bin/pyright
mypy:
poetry run mypy .
uv run mypy .
black:
poetry run black .
uv run black .
ruff:
poetry run ruff check . --fix
uv run ruff check . --fix
checkruff:
poetry run ruff check .
uv run ruff check .
checkprettier:
poetry run ./node_modules/.bin/prettier --check .
uv run ./node_modules/.bin/prettier --check .
checkblack:
poetry run black --check .
uv run black --check .
checkeditorconfig:
editorconfig-checker
@ -33,14 +33,14 @@ checkeditorconfig:
test:
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest
uv run pytest
install-pre-commit-hook:
@echo "Installing pre-commit hook to git"
@echo "Uninstall the hook with poetry run pre-commit uninstall"
poetry run pre-commit install
@echo "Uninstall the hook with uv run pre-commit uninstall"
uv run pre-commit install
pre-commit:
poetry run pre-commit run --all-files
uv run pre-commit run --all-files
checkbundle:

View file

@ -4,7 +4,7 @@ from fastapi import APIRouter
from loguru import logger
from .crud import db
from .nostr_client import all_routers, nostr_client
from .router import all_routers, nostr_client
from .tasks import check_relays, init_relays, subscribe_events
from .views import nostrclient_generic_router
from .views_api import nostrclient_api_router
@ -53,7 +53,7 @@ def nostrclient_start():
__all__ = [
"db",
"nostrclient_ext",
"nostrclient_start",
"nostrclient_static_files",
"nostrclient_stop",
"nostrclient_start",
]

View file

@ -2,6 +2,6 @@
"name": "Nostr Client",
"short_description": "Nostr client for extensions",
"tile": "/nostrclient/static/images/nostr-bitcoin.png",
"contributors": ["calle", "motorina0"],
"min_lnbits_version": "0.12.0"
"contributors": ["calle", "motorina0", "dni"],
"min_lnbits_version": "1.0.0"
}

77
crud.py
View file

@ -1,59 +1,52 @@
import json
from typing import Optional
from lnbits.db import Database
from .models import Config, Relay
from .models import Config, Relay, UserConfig
db = Database("ext_nostrclient")
async def get_relays() -> list[Relay]:
rows = await db.fetchall("SELECT * FROM nostrclient.relays")
return [Relay.from_row(r) for r in rows]
async def add_relay(relay: Relay) -> None:
await db.execute(
"""
INSERT INTO nostrclient.relays (
id,
url,
active
)
VALUES (?, ?, ?)
""",
(relay.id, relay.url, relay.active),
return await db.fetchall(
"SELECT * FROM nostrclient.relays",
model=Relay,
)
async def add_relay(relay: Relay) -> Relay:
await db.insert("nostrclient.relays", relay)
return relay
async def delete_relay(relay: Relay) -> None:
await db.execute("DELETE FROM nostrclient.relays WHERE url = ?", (relay.url,))
if not relay.url:
return
await db.execute(
"DELETE FROM nostrclient.relays WHERE url = :url", {"url": relay.url}
)
######################CONFIG#######################
async def create_config() -> Config:
config = Config()
await db.execute(
async def create_config(owner_id: str) -> Config:
admin_config = UserConfig(owner_id=owner_id)
await db.insert("nostrclient.config", admin_config)
return admin_config.extra
async def update_config(owner_id: str, config: Config) -> Config:
user_config = UserConfig(owner_id=owner_id, extra=config)
await db.update("nostrclient.config", user_config, "WHERE owner_id = :owner_id")
return user_config.extra
async def get_config(owner_id: str) -> Config | None:
user_config: UserConfig = await db.fetchone(
"""
INSERT INTO nostrclient.config (json_data)
VALUES (?)
SELECT * FROM nostrclient.config
WHERE owner_id = :owner_id
""",
(json.dumps(config.dict()),),
{"owner_id": owner_id},
model=UserConfig,
)
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
return json.loads(row[0], object_hook=lambda d: Config(**d))
async def update_config(config: Config) -> Optional[Config]:
await db.execute(
"""UPDATE nostrclient.config SET json_data = ?""",
(json.dumps(config.dict()),),
)
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
return json.loads(row[0], object_hook=lambda d: Config(**d))
async def get_config() -> Optional[Config]:
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
return json.loads(row[0], object_hook=lambda d: Config(**d)) if row else None
if user_config:
return user_config.extra
return None

1
description.md Normal file
View file

@ -0,0 +1 @@
An always-on extension that can open multiple connections to nostr relays and act as a multiplexer for other clients: You open a single websocket to nostrclient which then sends the data to multiple relays. The responses from these relays are then sent back to the client.

View file

@ -23,3 +23,10 @@ async def m002_create_config_table(db):
json_data TEXT NOT NULL
);"""
)
async def m003_update_config_table(db):
await db.execute("ALTER TABLE nostrclient.config RENAME COLUMN json_data TO extra")
await db.execute(
"ALTER TABLE nostrclient.config ADD COLUMN owner_id TEXT DEFAULT 'admin'"
)

View file

@ -1,38 +1,39 @@
from sqlite3 import Row
from typing import List, Optional
from lnbits.helpers import urlsafe_short_hash
from pydantic import BaseModel
from pydantic import BaseModel, Field
class RelayStatus(BaseModel):
num_sent_events: Optional[int] = 0
num_received_events: Optional[int] = 0
error_counter: Optional[int] = 0
error_list: Optional[List] = []
notice_list: Optional[List] = []
num_sent_events: int | None = 0
num_received_events: int | None = 0
error_counter: int | None = 0
error_list: list | None = []
notice_list: list | None = []
class Relay(BaseModel):
id: Optional[str] = None
url: Optional[str] = None
connected: Optional[bool] = None
connected_string: Optional[str] = None
status: Optional[RelayStatus] = None
active: Optional[bool] = None
ping: Optional[int] = None
id: str | None = None
url: str | None = None
active: bool | None = None
connected: bool | None = Field(default=None, no_database=True)
connected_string: str | None = Field(default=None, no_database=True)
status: RelayStatus | None = Field(default=None, no_database=True)
ping: int | None = Field(default=None, no_database=True)
def _init__(self):
if not self.id:
self.id = urlsafe_short_hash()
@classmethod
def from_row(cls, row: Row) -> "Relay":
return cls(**dict(row))
class RelayDb(BaseModel):
id: str
url: str
active: bool | None = True
class TestMessage(BaseModel):
sender_private_key: Optional[str]
sender_private_key: str | None
reciever_public_key: str
message: str
@ -46,3 +47,8 @@ class TestMessageResponse(BaseModel):
class Config(BaseModel):
private_ws: bool = True
public_ws: bool = False
class UserConfig(BaseModel):
owner_id: str
extra: Config = Config()

View file

@ -6,10 +6,12 @@ from ..relay_manager import RelayManager
class NostrClient:
relay_manager = RelayManager()
relay_manager: RelayManager
running: bool
def __init__(self):
self.running = True
self.relay_manager = RelayManager()
def connect(self, relays):
for relay in relays:

View file

@ -1,5 +0,0 @@
from .nostr.client.client import NostrClient
from .router import NostrRouter
nostr_client: NostrClient = NostrClient()
all_routers: list[NostrRouter] = []

2531
poetry.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,47 +1,45 @@
[tool.poetry]
[project]
name = "lnbits-nostrclient"
version = "0.0.0"
requires-python = ">=3.10,<3.13"
description = "LNbits, free and open-source Lightning wallet and accounts system."
authors = ["Alan Bits <alan@lnbits.com>"]
authors = [{ name = "Alan Bits", email = "alan@lnbits.com" }]
urls = { Homepage = "https://lnbits.com", Repository = "https://github.com/lnbits/nostrclient" }
dependencies = [ "lnbits>1" ]
[tool.poetry.dependencies]
python = "^3.10 | ^3.9"
lnbits = "*"
[tool.poetry]
package-mode = false
[tool.poetry.group.dev.dependencies]
black = "^24.3.0"
pytest-asyncio = "^0.21.0"
pytest = "^7.3.2"
mypy = "^1.5.1"
pre-commit = "^3.2.2"
ruff = "^0.3.2"
types-cffi = "^1.16.0.20240331"
pytest-md = "^0.2.0"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.uv]
dev-dependencies = [
"black",
"pytest-asyncio",
"pytest",
"mypy",
"pre-commit",
"ruff",
"pytest-md",
"types-cffi",
]
[tool.mypy]
exclude = "(nostr/*)"
plugins = ["pydantic.mypy"]
[[tool.mypy.overrides]]
module = [
"nostr.*",
"lnbits.*",
"lnurl.*",
"loguru.*",
"fastapi.*",
"pydantic.*",
"pyqrcode.*",
"shortuuid.*",
"httpx.*",
"secp256k1.*",
"websocket.*",
]
follow_imports = "skip"
ignore_missing_imports = "True"
[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true
[tool.pytest.ini_options]
log_cli = false
testpaths = [

View file

@ -1,28 +1,35 @@
import asyncio
import json
from typing import Dict, List
from typing import ClassVar
from fastapi import WebSocket, WebSocketDisconnect
from lnbits.helpers import urlsafe_short_hash
from loguru import logger
from . import nostr_client
from .nostr.client.client import NostrClient
# from . import nostr_client
from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage
nostr_client: NostrClient = NostrClient()
all_routers: list["NostrRouter"] = []
class NostrRouter:
received_subscription_events: dict[str, List[EventMessage]]
received_subscription_notices: list[NoticeMessage]
received_subscription_eosenotices: dict[str, EndOfStoredEventsMessage]
received_subscription_events: ClassVar[dict[str, list[EventMessage]]] = {}
received_subscription_notices: ClassVar[list[NoticeMessage]] = []
received_subscription_eosenotices: ClassVar[dict[str, EndOfStoredEventsMessage]] = (
{}
)
def __init__(self, websocket: WebSocket):
self.connected: bool = True
self.websocket: WebSocket = websocket
self.tasks: List[asyncio.Task] = []
self.original_subscription_ids: Dict[str, str] = {}
self.tasks: list[asyncio.Task] = []
self.original_subscription_ids: dict[str, str] = {}
@property
def subscriptions(self) -> List[str]:
def subscriptions(self) -> list[str]:
return list(self.original_subscription_ids.keys())
def start(self):
@ -70,6 +77,7 @@ class NostrRouter:
self._handle_notices()
except Exception as e:
logger.debug(f"Failed to handle response for client: '{e!s}'.")
await asyncio.sleep(1)
await asyncio.sleep(0.1)
async def _handle_subscriptions(self):
@ -103,10 +111,14 @@ class NostrRouter:
# this reconstructs the original response from the relay
# reconstruct original subscription id
s_original = self.original_subscription_ids[s]
event_to_forward = f"""["EVENT", "{s_original}", {event_json}]"""
event_to_forward = json.dumps(
["EVENT", s_original, json.loads(event_json)]
)
await self.websocket.send_text(event_to_forward)
except Exception as e:
logger.debug(e) # there are 2900 errors here
logger.warning(
f"[NOSTRCLIENT] Error in _handle_received_subscription_events: {e}"
)
def _handle_notices(self):
while len(NostrRouter.received_subscription_notices):

View file

@ -3,10 +3,9 @@ import threading
from loguru import logger
from . import nostr_client
from .crud import get_relays
from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage
from .router import NostrRouter
from .router import NostrRouter, nostr_client
async def init_relays():

View file

@ -71,7 +71,7 @@
<q-table
flat
dense
:data="nostrrelayLinks"
:rows="nostrrelayLinks"
row-key="id"
:columns="relayTable.columns"
:pagination.sync="relayTable.pagination"
@ -372,12 +372,11 @@
{% endraw %} {% endblock %} {% block scripts %} {{ window_vars(user) }}
<script>
Vue.component(VueQrcode.name, VueQrcode)
var maplrelays = obj => {
obj._data = _.clone(obj)
obj.theTime = obj.time * 60 - (Date.now() / 1000 - obj.timestamp)
obj.time = obj.time + 'mins'
obj.status = obj.status || {}
obj.status = {
sentEvents: obj.status.num_sent_events,
receveidEvents: obj.status.num_received_events,
@ -391,7 +390,7 @@
if (obj.time_elapsed) {
obj.date = 'Time elapsed'
} else {
obj.date = Quasar.utils.date.formatDate(
obj.date = Quasar.date.formatDate(
new Date((obj.theTime - 3600) * 1000),
'HH:mm:ss'
)
@ -399,7 +398,7 @@
return obj
}
new Vue({
window.app = Vue.createApp({
el: '#vue',
mixins: [windowMixin],
data: function () {
@ -467,8 +466,7 @@
predefinedRelays: [
'wss://relay.damus.io',
'wss://nostr-pub.wellorder.net',
'wss://nostr.zebedee.cloud',
'wss://nodestr.fmt.wiz.biz',
'wss://relay.nostrconnect.com',
'wss://nostr.oxtr.dev',
'wss://nostr.wine'
]
@ -624,7 +622,7 @@
try {
const {data} = await LNbits.api.request(
'PUT',
'/nostrclient/api/v1/relay/test?usr=' + this.g.user.id,
'/nostrclient/api/v1/relay/test',
this.g.user.wallets[0].adminkey,
{
sender_private_key: this.testData.senderPrivateKey,

2299
uv.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,9 @@
from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from lnbits.core.models import User
from lnbits.decorators import check_admin
from lnbits.helpers import template_renderer
templates = Jinja2Templates(directory="templates")
nostrclient_generic_router = APIRouter()
@ -17,5 +14,5 @@ def nostr_renderer():
@nostrclient_generic_router.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_admin)):
return nostr_renderer().TemplateResponse(
"nostrclient/index.html", {"request": request, "user": user.dict()}
"nostrclient/index.html", {"request": request, "user": user.json()}
)

View file

@ -17,8 +17,7 @@ from .crud import (
from .helpers import normalize_public_key
from .models import Config, Relay, RelayStatus, TestMessage, TestMessageResponse
from .nostr.key import EncryptedDirectMessage, PrivateKey
from .nostr_client import all_routers, nostr_client
from .router import NostrRouter
from .router import NostrRouter, all_routers, nostr_client
nostrclient_api_router = APIRouter()
@ -114,13 +113,13 @@ async def api_test_endpoint(data: TestMessage) -> TestMessageResponse:
) from ex
@nostrclient_api_router.websocket("/api/v1/{id}")
@nostrclient_api_router.websocket("/api/v1/{ws_id}")
async def ws_relay(ws_id: str, websocket: WebSocket) -> None:
"""Relay multiplexer: one client (per endpoint) <-> multiple relays"""
logger.info("New websocket connection at: '/api/v1/relay'")
try:
config = await get_config()
config = await get_config(owner_id="admin")
assert config, "Failed to get config"
if not config.private_ws and not config.public_ws:
@ -132,7 +131,7 @@ async def ws_relay(ws_id: str, websocket: WebSocket) -> None:
else:
if not config.private_ws:
raise ValueError("Private websocket connections not accepted.")
if decrypt_internal_message(ws_id) != "relay":
if decrypt_internal_message(ws_id, urlsafe=True) != "relay":
raise ValueError("Invalid websocket endpoint.")
await websocket.accept()
@ -166,15 +165,15 @@ async def ws_relay(ws_id: str, websocket: WebSocket) -> None:
@nostrclient_api_router.get("/api/v1/config", dependencies=[Depends(check_admin)])
async def api_get_config() -> Config:
config = await get_config()
config = await get_config(owner_id="admin")
if not config:
config = await create_config()
config = await create_config(owner_id="admin")
assert config, "Failed to create config"
return config
@nostrclient_api_router.put("/api/v1/config", dependencies=[Depends(check_admin)])
async def api_update_config(data: Config):
config = await update_config(data)
config = await update_config(owner_id="admin", config=data)
assert config
return config.dict()