chore: code format
This commit is contained in:
parent
366dae2082
commit
58723a387f
6 changed files with 52 additions and 42 deletions
|
|
@ -106,8 +106,6 @@ class NostrClientConnection:
|
|||
] = None
|
||||
self.get_client_config: Optional[Callable[[], RelaySpec]] = None
|
||||
|
||||
|
||||
|
||||
async def start(self):
|
||||
await self.websocket.accept()
|
||||
while True:
|
||||
|
|
@ -177,14 +175,15 @@ class NostrClientConnection:
|
|||
self.authenticated = True
|
||||
return None
|
||||
|
||||
|
||||
if not self.authenticated and self.client_config.event_requires_auth(e.kind):
|
||||
await self._send_msg(["AUTH", self._current_auth_challenge()])
|
||||
resp_nip20 += [False, f"restricted: Relay requires authentication for events of kind '{e.kind}'"]
|
||||
resp_nip20 += [
|
||||
False,
|
||||
f"restricted: Relay requires authentication for events of kind '{e.kind}'",
|
||||
]
|
||||
await self._send_msg(resp_nip20)
|
||||
return None
|
||||
|
||||
|
||||
valid, message = await self._validate_write(e)
|
||||
if not valid:
|
||||
resp_nip20 += [valid, message]
|
||||
|
|
@ -259,7 +258,7 @@ class NostrClientConnection:
|
|||
self._remove_filter(subscription_id)
|
||||
|
||||
def _handle_auth(self):
|
||||
raise ValueError('Not supported')
|
||||
raise ValueError("Not supported")
|
||||
|
||||
def _can_add_filter(self) -> bool:
|
||||
return (
|
||||
|
|
@ -317,7 +316,9 @@ class NostrClientConnection:
|
|||
|
||||
return True, ""
|
||||
|
||||
async def _validate_storage(self, pubkey: str, event_size_bytes: int) -> Tuple[bool, str]:
|
||||
async def _validate_storage(
|
||||
self, pubkey: str, event_size_bytes: int
|
||||
) -> Tuple[bool, str]:
|
||||
if self.client_config.is_read_only_relay:
|
||||
return False, "Cannot write event, relay is read-only"
|
||||
|
||||
|
|
@ -329,7 +330,9 @@ class NostrClientConnection:
|
|||
return False, f"This is a paid relay: '{self.relay_id}'"
|
||||
|
||||
stored_bytes = await get_storage_for_public_key(self.relay_id, pubkey)
|
||||
total_available_storage = account.storage + self.client_config.free_storage_bytes_value
|
||||
total_available_storage = (
|
||||
account.storage + self.client_config.free_storage_bytes_value
|
||||
)
|
||||
if (stored_bytes + event_size_bytes) <= total_available_storage:
|
||||
return True, ""
|
||||
|
||||
|
|
@ -376,7 +379,9 @@ class NostrClientConnection:
|
|||
return True
|
||||
current_time_seconds = round(time.time())
|
||||
chanllenge_max_age_seconds = 300 # 5 min
|
||||
return (current_time_seconds - self._auth_challenge_created_at) >= chanllenge_max_age_seconds
|
||||
return (
|
||||
current_time_seconds - self._auth_challenge_created_at
|
||||
) >= chanllenge_max_age_seconds
|
||||
|
||||
def _current_auth_challenge(self):
|
||||
if self._auth_challenge_expired():
|
||||
|
|
|
|||
19
crud.py
19
crud.py
|
|
@ -13,6 +13,7 @@ from .models import (
|
|||
|
||||
########################## RELAYS ####################
|
||||
|
||||
|
||||
async def create_relay(user_id: str, r: NostrRelay) -> NostrRelay:
|
||||
await db.execute(
|
||||
"""
|
||||
|
|
@ -326,9 +327,9 @@ def build_select_events_query(relay_id: str, filter: NostrFilter):
|
|||
return query, values
|
||||
|
||||
|
||||
|
||||
########################## ACCOUNTS ####################
|
||||
|
||||
|
||||
async def create_account(relay_id: str, a: NostrAccount) -> NostrAccount:
|
||||
await db.execute(
|
||||
"""
|
||||
|
|
@ -357,25 +358,19 @@ async def update_account(relay_id: str, a: NostrAccount) -> NostrAccount:
|
|||
SET (sats, storage, paid_to_join, allowed, blocked) = (?, ?, ?, ?, ?)
|
||||
WHERE relay_id = ? AND pubkey = ?
|
||||
""",
|
||||
(
|
||||
a.sats,
|
||||
a.storage,
|
||||
a.paid_to_join,
|
||||
a.allowed,
|
||||
a.blocked,
|
||||
relay_id,
|
||||
a.pubkey
|
||||
),
|
||||
(a.sats, a.storage, a.paid_to_join, a.allowed, a.blocked, relay_id, a.pubkey),
|
||||
)
|
||||
|
||||
return a
|
||||
|
||||
|
||||
async def get_account(relay_id: str, pubkey: str,) -> Optional[NostrAccount]:
|
||||
async def get_account(
|
||||
relay_id: str,
|
||||
pubkey: str,
|
||||
) -> Optional[NostrAccount]:
|
||||
row = await db.fetchone(
|
||||
"SELECT * FROM nostrrelay.accounts WHERE relay_id = ? AND pubkey = ?",
|
||||
(relay_id, pubkey),
|
||||
)
|
||||
|
||||
return NostrAccount.from_row(row) if row else None
|
||||
|
||||
|
|
|
|||
|
|
@ -20,5 +20,6 @@ def normalize_public_key(pubkey: str) -> str:
|
|||
int(pubkey, 16)
|
||||
return pubkey
|
||||
|
||||
|
||||
def extract_domain(url: str) -> str:
|
||||
return urlparse(url).netloc
|
||||
10
models.py
10
models.py
|
|
@ -73,6 +73,7 @@ class AuthSpec(BaseModel):
|
|||
return False
|
||||
return kind not in self.skiped_auth_events
|
||||
|
||||
|
||||
class PaymentSpec(BaseModel):
|
||||
is_paid_relay = Field(False, alias="isPaidRelay")
|
||||
cost_to_join = Field(0, alias="costToJoin")
|
||||
|
|
@ -93,23 +94,23 @@ class AuthorSpec(Spec):
|
|||
# todo: check payment
|
||||
return p in self.allowed_public_keys
|
||||
|
||||
|
||||
class WalletSpec(Spec):
|
||||
wallet = Field("")
|
||||
|
||||
|
||||
class RelayPublicSpec(FilterSpec, EventSpec, StorageSpec, PaymentSpec):
|
||||
domain: str = ''
|
||||
domain: str = ""
|
||||
|
||||
@property
|
||||
def is_read_only_relay(self):
|
||||
self.free_storage_value == 0 and not self.is_paid_relay
|
||||
|
||||
|
||||
class RelaySpec(RelayPublicSpec, AuthorSpec, WalletSpec, AuthSpec):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
class NostrRelay(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
|
|
@ -340,7 +341,8 @@ class BuyOrder(BaseModel):
|
|||
units_to_buy = 0
|
||||
|
||||
def is_valid_action(self):
|
||||
return self.action in ['join', 'storage']
|
||||
return self.action in ["join", "storage"]
|
||||
|
||||
|
||||
class NostrAccount(BaseModel):
|
||||
pubkey: str
|
||||
|
|
|
|||
9
tasks.py
9
tasks.py
|
|
@ -35,11 +35,14 @@ async def on_invoice_paid(payment: Payment):
|
|||
await invoice_paid_for_storage(relay_id, pubkey, storage_to_buy)
|
||||
return
|
||||
|
||||
|
||||
async def invoice_paid_to_join(relay_id: str, pubkey: str):
|
||||
try:
|
||||
account = await get_account(relay_id, pubkey)
|
||||
if not account:
|
||||
await create_account(relay_id, NostrAccount(pubkey=pubkey, paid_to_join=True))
|
||||
await create_account(
|
||||
relay_id, NostrAccount(pubkey=pubkey, paid_to_join=True)
|
||||
)
|
||||
return
|
||||
|
||||
if account.blocked or account.paid_to_join:
|
||||
|
|
@ -56,7 +59,9 @@ async def invoice_paid_for_storage(relay_id: str, pubkey: str, storage_to_buy: i
|
|||
try:
|
||||
account = await get_account(relay_id, pubkey)
|
||||
if not account:
|
||||
await create_account(relay_id, NostrAccount(pubkey=pubkey, storage=storage_to_buy))
|
||||
await create_account(
|
||||
relay_id, NostrAccount(pubkey=pubkey, storage=storage_to_buy)
|
||||
)
|
||||
return
|
||||
|
||||
if account.blocked:
|
||||
|
|
|
|||
10
views_api.py
10
views_api.py
|
|
@ -48,7 +48,9 @@ async def websocket_endpoint(relay_id: str, websocket: WebSocket):
|
|||
|
||||
@nostrrelay_ext.post("/api/v1/relay")
|
||||
async def api_create_relay(
|
||||
data: NostrRelay, request: Request, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
data: NostrRelay,
|
||||
request: Request,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> NostrRelay:
|
||||
if len(data.id):
|
||||
await check_admin(UUID4(wallet.wallet.user))
|
||||
|
|
@ -166,11 +168,11 @@ async def api_pay_to_join(data: BuyOrder):
|
|||
detail="Relay not found",
|
||||
)
|
||||
|
||||
if data.action == 'join' and relay.is_free_to_join:
|
||||
if data.action == "join" and relay.is_free_to_join:
|
||||
raise ValueError("Relay is free to join")
|
||||
|
||||
storage_to_buy = 0
|
||||
if data.action == 'storage':
|
||||
if data.action == "storage":
|
||||
if relay.config.storage_cost_value == 0:
|
||||
raise ValueError("Relay storage cost is zero. Cannot buy!")
|
||||
if data.units_to_buy == 0:
|
||||
|
|
@ -188,7 +190,7 @@ async def api_pay_to_join(data: BuyOrder):
|
|||
"action": data.action,
|
||||
"relay_id": relay.id,
|
||||
"pubkey": pubkey,
|
||||
"storage_to_buy": storage_to_buy
|
||||
"storage_to_buy": storage_to_buy,
|
||||
},
|
||||
)
|
||||
print("### payment_request", payment_request)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue