fix mypy for nostrnip5 ext

This commit is contained in:
dni ⚡ 2022-12-30 09:46:45 +01:00
parent efc156689f
commit bf69e91cae
5 changed files with 47 additions and 34 deletions

View file

@ -139,7 +139,7 @@ async def delete_domain(domain_id) -> bool:
return True return True
async def delete_address(address_id) -> bool: async def delete_address(address_id):
await db.execute( await db.execute(
""" """
DELETE FROM nostrnip5.addresses WHERE id = ? DELETE FROM nostrnip5.addresses WHERE id = ?

View file

@ -1,9 +1,9 @@
import asyncio import asyncio
import json
from loguru import logger
from lnbits.core.models import Payment from lnbits.core.models import Payment
from lnbits.helpers import urlsafe_short_hash from lnbits.tasks import register_invoice_listener
from lnbits.tasks import internal_invoice_queue, register_invoice_listener
from .crud import activate_address from .crud import activate_address
@ -18,17 +18,18 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None: async def on_invoice_paid(payment: Payment) -> None:
if not payment.extra:
return
if payment.extra.get("tag") != "nostrnip5": if payment.extra.get("tag") != "nostrnip5":
# not relevant
return return
domain_id = payment.extra.get("domain_id") domain_id = payment.extra.get("domain_id")
address_id = payment.extra.get("address_id") address_id = payment.extra.get("address_id")
print("Activating NOSTR NIP-05") if domain_id and address_id:
print(domain_id) logger.info("Activating NOSTR NIP-05")
print(address_id) logger.info(domain_id)
logger.info(address_id)
active = await activate_address(domain_id, address_id) await activate_address(domain_id, address_id)
return return

View file

@ -1,8 +1,7 @@
from datetime import datetime from datetime import datetime
from http import HTTPStatus from http import HTTPStatus
from fastapi import FastAPI, Request from fastapi import Depends, Request
from fastapi.params import Depends
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse from starlette.responses import HTMLResponse
@ -24,7 +23,7 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
@nostrnip5_ext.get("/signup/{domain_id}", response_class=HTMLResponse) @nostrnip5_ext.get("/signup/{domain_id}", response_class=HTMLResponse)
async def index(request: Request, domain_id: str): async def signup(request: Request, domain_id: str):
domain = await get_domain(domain_id) domain = await get_domain(domain_id)
if not domain: if not domain:
@ -43,7 +42,7 @@ async def index(request: Request, domain_id: str):
@nostrnip5_ext.get("/rotate/{domain_id}/{address_id}", response_class=HTMLResponse) @nostrnip5_ext.get("/rotate/{domain_id}/{address_id}", response_class=HTMLResponse)
async def index(request: Request, domain_id: str, address_id: str): async def rotate(request: Request, domain_id: str, address_id: str):
domain = await get_domain(domain_id) domain = await get_domain(domain_id)
address = await get_address(domain_id, address_id) address = await get_address(domain_id, address_id)

View file

@ -1,10 +1,8 @@
import re import re
from http import HTTPStatus from http import HTTPStatus
from typing import Optional
from bech32 import bech32_decode, convertbits from bech32 import bech32_decode, convertbits
from fastapi import Query, Request, Response from fastapi import Depends, Query, Response
from fastapi.params import Depends
from loguru import logger from loguru import logger
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
@ -38,7 +36,10 @@ async def api_domains(
): ):
wallet_ids = [wallet.wallet.id] wallet_ids = [wallet.wallet.id]
if all_wallets: if all_wallets:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids user = await get_user(wallet.wallet.user)
if not user:
return []
wallet_ids = user.wallet_ids
return [domain.dict() for domain in await get_domains(wallet_ids)] return [domain.dict() for domain in await get_domains(wallet_ids)]
@ -49,13 +50,20 @@ async def api_addresses(
): ):
wallet_ids = [wallet.wallet.id] wallet_ids = [wallet.wallet.id]
if all_wallets: if all_wallets:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids user = await get_user(wallet.wallet.user)
if not user:
return []
wallet_ids = user.wallet_ids
return [address.dict() for address in await get_all_addresses(wallet_ids)] return [address.dict() for address in await get_all_addresses(wallet_ids)]
@nostrnip5_ext.get("/api/v1/domain/{domain_id}", status_code=HTTPStatus.OK) @nostrnip5_ext.get(
async def api_invoice(domain_id: str, wallet: WalletTypeInfo = Depends(get_key_type)): "/api/v1/domain/{domain_id}",
status_code=HTTPStatus.OK,
dependencies=[Depends(get_key_type)],
)
async def api_invoice(domain_id: str):
domain = await get_domain(domain_id) domain = await get_domain(domain_id)
if not domain: if not domain:
raise HTTPException( raise HTTPException(
@ -104,11 +112,11 @@ async def api_address_delete(
@nostrnip5_ext.post( @nostrnip5_ext.post(
"/api/v1/domain/{domain_id}/address/{address_id}/activate", "/api/v1/domain/{domain_id}/address/{address_id}/activate",
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
dependencies=[Depends(require_admin_key)],
) )
async def api_address_activate( async def api_address_activate(
domain_id: str, domain_id: str,
address_id: str, address_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
): ):
await activate_address(domain_id, address_id) await activate_address(domain_id, address_id)
@ -126,9 +134,11 @@ async def api_address_rotate(
): ):
if post_data.pubkey.startswith("npub"): if post_data.pubkey.startswith("npub"):
hrp, data = bech32_decode(post_data.pubkey) _, data = bech32_decode(post_data.pubkey)
decoded_data = convertbits(data, 5, 8, False) if data:
post_data.pubkey = bytes(decoded_data).hex() decoded_data = convertbits(data, 5, 8, False)
if decoded_data:
post_data.pubkey = bytes(decoded_data).hex()
if len(bytes.fromhex(post_data.pubkey)) != 32: if len(bytes.fromhex(post_data.pubkey)) != 32:
raise HTTPException( raise HTTPException(
@ -173,10 +183,12 @@ async def api_address_create(
status_code=HTTPStatus.NOT_FOUND, detail="Local part already exists." status_code=HTTPStatus.NOT_FOUND, detail="Local part already exists."
) )
if post_data.pubkey.startswith("npub"): if post_data and post_data.pubkey.startswith("npub"):
hrp, data = bech32_decode(post_data.pubkey) _, data = bech32_decode(post_data.pubkey)
decoded_data = convertbits(data, 5, 8, False) if data:
post_data.pubkey = bytes(decoded_data).hex() decoded_data = convertbits(data, 5, 8, False)
if decoded_data:
post_data.pubkey = bytes(decoded_data).hex()
if len(bytes.fromhex(post_data.pubkey)) != 32: if len(bytes.fromhex(post_data.pubkey)) != 32:
raise HTTPException( raise HTTPException(
@ -233,15 +245,17 @@ async def api_get_nostr_json(
output = {} output = {}
for address in addresses: for address in addresses:
local_part = address.get("local_part").lower() local_part = address.get("local_part")
if not local_part:
continue
if address.get("active") == False: if address.get("active") == False:
continue continue
if name and name.lower() != local_part: if name and name.lower() != local_part.lower():
continue continue
output[local_part] = address.get("pubkey") output[local_part.lower()] = address.get("pubkey")
response.headers["Access-Control-Allow-Origin"] = "*" response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS" response.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS"

View file

@ -103,7 +103,6 @@ exclude = """(?x)(
| ^lnbits/extensions/lnurldevice. | ^lnbits/extensions/lnurldevice.
| ^lnbits/extensions/lnurlp. | ^lnbits/extensions/lnurlp.
| ^lnbits/extensions/lnurlpayout. | ^lnbits/extensions/lnurlpayout.
| ^lnbits/extensions/nostrnip5.
| ^lnbits/extensions/offlineshop. | ^lnbits/extensions/offlineshop.
| ^lnbits/extensions/paywall. | ^lnbits/extensions/paywall.
| ^lnbits/extensions/satspay. | ^lnbits/extensions/satspay.