Skip to content

Commit

Permalink
feat: separate endpoints for aonimous create
Browse files Browse the repository at this point in the history
  • Loading branch information
motorina0 committed Sep 9, 2024
1 parent f7c061e commit f74f9f9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 29 deletions.
39 changes: 39 additions & 0 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import httpx
from lnbits.core.crud import get_standalone_payment, get_user
from lnbits.core.services import create_invoice
from lnbits.db import Filters, Page
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
from loguru import logger
Expand Down Expand Up @@ -114,6 +115,44 @@ async def get_identifier_status(
)


async def request_user_address(
domain: Domain,
address_data: CreateAddressData,
wallet_id: str,
user_id: str,
):
address = await create_address(domain, address_data, wallet_id, user_id)
assert (
address.config.price_in_sats
), f"Cannot compute price for '{address_data.local_part}'."

if address_data.create_invoice:
# in case the user pays, but the identifier is no longer available
payment_hash, payment_request = await create_invoice(
wallet_id=domain.wallet,
amount=int(address.config.price_in_sats),
memo=f"Payment of {address.config.price} {address.config.currency} "
f"for NIP-05 for {address_data.local_part}@{domain.domain}",
extra={
"tag": "nostrnip5",
"domain_id": domain.id,
"address_id": address.id,
"action": "activate",
"reimburse_wallet_id": wallet_id,
},
)
else:
payment_hash, payment_request = None, None

resp = {
"payment_hash": payment_hash,
"payment_request": payment_request,
**dict(address),
}

return resp


async def create_address(
domain: Domain,
data: CreateAddressData,
Expand Down
56 changes: 27 additions & 29 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
get_user_domains,
get_valid_addresses_for_owner,
refresh_buckets,
request_user_address,
update_identifiers,
update_ln_address,
)
Expand Down Expand Up @@ -461,6 +462,30 @@ async def api_request_user_address(
user_id: Optional[str] = Depends(optional_user_id),
):

if not user_id:
raise HTTPException(HTTPStatus.UNAUTHORIZED)

# make sure the address belongs to the user
domain = await get_domain_by_id(address_data.domain_id)
assert domain, "Domain does not exist."

assert address_data.domain_id == domain_id, "Domain ID missmatch"

wallet_id = (await get_wallets(user_id))[0].id

return await request_user_address(domain, address_data, wallet_id, user_id)


@http_try_except
@nostrnip5_api_router.post(
"/api/v1/public/domain/{domain_id}/address", status_code=HTTPStatus.CREATED
)
async def api_request_public_user_address(
address_data: CreateAddressData,
domain_id: str,
user_id: Optional[str] = Depends(optional_user_id),
):

# make sure the address belongs to the user
domain = await get_domain_by_id(address_data.domain_id)
assert domain, "Domain does not exist."
Expand All @@ -470,37 +495,10 @@ async def api_request_user_address(
wallet_id = (await get_wallets(user_id))[0].id if user_id else None
# used when the user is not authenticated
temp_user_id = rotation_secret_prefix + uuid4().hex
address = await create_address(

resp = await request_user_address(
domain, address_data, wallet_id, user_id or temp_user_id
)
assert (
address.config.price_in_sats
), f"Cannot compute price for '{address_data.local_part}'."

if address_data.create_invoice:
# in case the user pays, but the identifier is no longer available

payment_hash, payment_request = await create_invoice(
wallet_id=domain.wallet,
amount=int(address.config.price_in_sats),
memo=f"Payment of {address.config.price} {address.config.currency} "
f"for NIP-05 for {address_data.local_part}@{domain.domain}",
extra={
"tag": "nostrnip5",
"domain_id": domain_id,
"address_id": address.id,
"action": "activate",
"reimburse_wallet_id": wallet_id,
},
)
else:
payment_hash, payment_request = None, None

resp = {
"payment_hash": payment_hash,
"payment_request": payment_request,
**dict(address),
}
if not user_id:
resp["rotation_secret"] = temp_user_id

Expand Down

0 comments on commit f74f9f9

Please sign in to comment.