lnbits-legend/lnbits/core/views/api.py

624 lines
20 KiB
Python
Raw Normal View History

import asyncio
2021-07-31 02:01:19 +02:00
import hashlib
2021-08-29 19:38:42 +02:00
import json
from binascii import unhexlify
2021-08-29 19:38:42 +02:00
from http import HTTPStatus
2022-07-19 18:51:35 +02:00
from typing import Dict, List, Optional, Union, Tuple
2021-08-29 19:38:42 +02:00
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
2021-10-29 14:06:59 +02:00
2021-08-29 19:38:42 +02:00
import httpx
from fastapi import Depends, Header, Query, Request
2021-08-29 19:38:42 +02:00
from fastapi.exceptions import HTTPException
from fastapi.params import Body
2022-07-16 14:23:03 +02:00
from loguru import logger
2021-08-29 19:38:42 +02:00
from pydantic import BaseModel
2022-01-14 13:19:30 +01:00
from pydantic.fields import Field
2021-10-29 14:06:59 +02:00
from sse_starlette.sse import EventSourceResponse
2021-07-31 02:01:19 +02:00
from lnbits import bolt11, lnurl
from lnbits.core.models import Payment, Wallet
2021-10-17 19:33:29 +02:00
from lnbits.decorators import (
WalletTypeInfo,
get_key_type,
2022-05-16 12:21:30 +02:00
require_admin_key,
require_invoice_key,
2021-10-17 19:33:29 +02:00
)
2022-02-22 17:45:35 +01:00
from lnbits.helpers import url_for, urlsafe_short_hash
from lnbits.settings import LNBITS_ADMIN_USERS, LNBITS_SITE_TITLE
2022-01-14 13:19:30 +01:00
from lnbits.utils.exchange_rates import (
currencies,
fiat_amount_as_satoshis,
satoshis_amount_as_fiat,
)
from .. import core_app, db
2021-10-22 01:41:30 +02:00
from ..crud import (
2022-02-22 17:45:35 +01:00
create_payment,
2021-10-22 01:41:30 +02:00
get_payments,
2021-10-29 14:06:59 +02:00
get_standalone_payment,
2022-01-31 17:29:42 +01:00
get_wallet,
2022-03-07 09:08:08 +01:00
get_wallet_for_key,
2022-02-22 17:45:35 +01:00
save_balance_check,
2022-01-31 17:29:42 +01:00
update_payment_status,
2022-02-22 17:45:35 +01:00
update_wallet,
2021-10-22 01:41:30 +02:00
)
2021-10-17 19:33:29 +02:00
from ..services import (
InvoiceFailure,
PaymentFailure,
2021-10-29 14:06:59 +02:00
check_invoice_status,
2021-10-17 19:33:29 +02:00
create_invoice,
pay_invoice,
perform_lnurlauth,
)
from ..tasks import api_invoice_listeners
2021-08-29 19:38:42 +02:00
@core_app.get("/api/v1/wallet")
async def api_wallet(wallet: WalletTypeInfo = Depends(get_key_type)):
2021-11-09 18:44:05 +01:00
if wallet.wallet_type == 0:
return {
"id": wallet.wallet.id,
"name": wallet.wallet.name,
"balance": wallet.wallet.balance_msat,
}
else:
2021-11-12 05:14:55 +01:00
return {"name": wallet.wallet.name, "balance": wallet.wallet.balance_msat}
2021-10-17 19:33:29 +02:00
2021-08-16 20:27:39 +02:00
2022-01-31 17:29:42 +01:00
@core_app.put("/api/v1/wallet/balance/{amount}")
2022-02-16 22:42:27 +01:00
async def api_update_balance(
amount: int, wallet: WalletTypeInfo = Depends(get_key_type)
):
2022-02-14 12:31:42 +01:00
if wallet.wallet.user not in LNBITS_ADMIN_USERS:
2022-02-16 22:42:27 +01:00
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not an admin user"
)
2022-01-31 17:29:42 +01:00
payHash = urlsafe_short_hash()
await create_payment(
wallet_id=wallet.wallet.id,
checking_id=payHash,
payment_request="selfPay",
payment_hash=payHash,
2022-02-01 21:51:40 +01:00
amount=amount * 1000,
2022-01-31 17:29:42 +01:00
memo="selfPay",
fee=0,
)
await update_payment_status(checking_id=payHash, pending=False)
updatedWallet = await get_wallet(wallet.wallet.id)
return {
"id": wallet.wallet.id,
"name": wallet.wallet.name,
"balance": amount,
2022-01-31 17:29:42 +01:00
}
2021-08-23 00:05:39 +02:00
@core_app.put("/api/v1/wallet/{new_name}")
2022-02-16 22:42:27 +01:00
async def api_update_wallet(
2022-05-16 12:21:30 +02:00
new_name: str, wallet: WalletTypeInfo = Depends(require_admin_key)
2022-02-16 22:42:27 +01:00
):
2021-08-29 19:38:42 +02:00
await update_wallet(wallet.wallet.id, new_name)
return {
"id": wallet.wallet.id,
"name": wallet.wallet.name,
"balance": wallet.wallet.balance_msat,
}
2021-08-06 12:15:07 +02:00
2021-08-16 20:27:39 +02:00
@core_app.get("/api/v1/payments")
async def api_payments(
limit: Optional[int] = None,
offset: Optional[int] = None,
wallet: WalletTypeInfo = Depends(get_key_type),
):
2022-02-16 22:42:27 +01:00
pendingPayments = await get_payments(
wallet_id=wallet.wallet.id,
pending=True,
exclude_uncheckable=True,
limit=limit,
offset=offset,
2022-02-16 22:42:27 +01:00
)
for payment in pendingPayments:
2022-02-16 22:42:27 +01:00
await check_invoice_status(
wallet_id=payment.wallet_id, payment_hash=payment.payment_hash
)
return await get_payments(
wallet_id=wallet.wallet.id,
pending=True,
complete=True,
limit=limit,
offset=offset,
)
2021-10-17 19:33:29 +02:00
2021-08-20 22:31:01 +02:00
class CreateInvoiceData(BaseModel):
2021-09-19 09:31:16 +02:00
out: Optional[bool] = True
2022-02-16 22:41:12 +01:00
amount: float = Query(None, ge=0)
2022-03-24 12:50:57 +01:00
memo: Optional[str] = None
2021-11-12 05:14:55 +01:00
unit: Optional[str] = "sat"
2021-11-02 17:29:15 +01:00
description_hash: Optional[str] = None
2021-10-17 19:33:29 +02:00
lnurl_callback: Optional[str] = None
lnurl_balance_check: Optional[str] = None
extra: Optional[dict] = None
webhook: Optional[str] = None
internal: Optional[bool] = False
2021-09-19 09:31:16 +02:00
bolt11: Optional[str] = None
2021-10-17 19:33:29 +02:00
2021-08-29 19:38:42 +02:00
async def api_payments_create_invoice(data: CreateInvoiceData, wallet: Wallet):
2021-11-03 13:03:48 +01:00
if data.description_hash:
2021-08-20 22:31:01 +02:00
description_hash = unhexlify(data.description_hash)
memo = ""
else:
description_hash = b""
2022-02-17 11:42:08 +01:00
memo = data.memo or LNBITS_SITE_TITLE
2021-11-12 05:14:55 +01:00
if data.unit == "sat":
2022-02-16 22:41:12 +01:00
amount = int(data.amount)
else:
assert data.unit is not None, "unit not set"
2021-08-20 22:31:01 +02:00
price_in_sats = await fiat_amount_as_satoshis(data.amount, data.unit)
amount = price_in_sats
async with db.connect() as conn:
try:
payment_hash, payment_request = await create_invoice(
2021-08-29 19:38:42 +02:00
wallet_id=wallet.id,
amount=amount,
memo=memo,
description_hash=description_hash,
2021-08-20 22:31:01 +02:00
extra=data.extra,
webhook=data.webhook,
internal=data.internal,
conn=conn,
)
except InvoiceFailure as e:
2021-10-17 19:33:29 +02:00
raise HTTPException(status_code=520, detail=str(e))
except Exception as exc:
raise exc
invoice = bolt11.decode(payment_request)
lnurl_response: Union[None, bool, str] = None
2021-08-20 22:31:01 +02:00
if data.lnurl_callback:
2022-02-02 00:11:26 +01:00
if "lnurl_balance_check" in data:
assert (
data.lnurl_balance_check is not None
), "lnurl_balance_check is required"
2022-07-19 18:51:35 +02:00
await save_balance_check(wallet.id, data.lnurl_balance_check)
2021-04-17 23:27:15 +02:00
async with httpx.AsyncClient() as client:
try:
2020-12-31 18:50:16 +01:00
r = await client.get(
2021-08-20 22:31:01 +02:00
data.lnurl_callback,
2021-04-17 23:27:15 +02:00
params={
"pr": payment_request,
"balanceNotify": url_for(
f"/withdraw/notify/{urlparse(data.lnurl_callback).netloc}",
external=True,
2022-02-02 00:11:26 +01:00
wal=wallet.id,
2021-04-17 23:27:15 +02:00
),
},
2020-12-31 18:50:16 +01:00
timeout=10,
)
if r.is_error:
lnurl_response = r.text
else:
resp = json.loads(r.text)
if resp["status"] != "OK":
lnurl_response = resp["reason"]
else:
lnurl_response = True
except (httpx.ConnectError, httpx.RequestError):
lnurl_response = False
return {
"payment_hash": invoice.payment_hash,
"payment_request": payment_request,
# maintain backwards compatibility with API clients:
"checking_id": invoice.payment_hash,
"lnurl_response": lnurl_response,
}
2021-08-29 19:38:42 +02:00
async def api_payments_pay_invoice(bolt11: str, wallet: Wallet):
try:
2021-10-17 19:33:29 +02:00
payment_hash = await pay_invoice(wallet_id=wallet.id, payment_request=bolt11)
2020-04-16 17:10:53 +02:00
except ValueError as e:
2021-10-17 19:33:29 +02:00
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
2020-04-16 17:10:53 +02:00
except PermissionError as e:
2021-10-17 19:33:29 +02:00
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail=str(e))
except PaymentFailure as e:
2021-10-17 19:33:29 +02:00
raise HTTPException(status_code=520, detail=str(e))
except Exception as exc:
raise exc
return {
"payment_hash": payment_hash,
# maintain backwards compatibility with API clients:
"checking_id": payment_hash,
}
2021-10-17 19:33:29 +02:00
@core_app.post(
"/api/v1/payments",
2021-11-02 17:29:15 +01:00
# deprecated=True,
# description="DEPRECATED. Use /api/v2/TBD and /api/v2/TBD instead",
2021-10-17 19:33:29 +02:00
status_code=HTTPStatus.CREATED,
)
async def api_payments_create(
wallet: WalletTypeInfo = Depends(require_invoice_key),
2021-10-17 19:33:29 +02:00
invoiceData: CreateInvoiceData = Body(...),
):
2021-09-19 09:31:16 +02:00
if invoiceData.out is True and wallet.wallet_type == 0:
if not invoiceData.bolt11:
2021-10-17 19:33:29 +02:00
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="BOLT11 string is invalid or not given",
)
2022-02-16 22:42:27 +01:00
return await api_payments_pay_invoice(
invoiceData.bolt11, wallet.wallet
) # admin key
elif not invoiceData.out:
# invoice key
return await api_payments_create_invoice(invoiceData, wallet.wallet)
else:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Invoice (or Admin) key required.",
)
2021-10-17 19:33:29 +02:00
2021-08-20 22:31:01 +02:00
class CreateLNURLData(BaseModel):
2021-10-17 19:33:29 +02:00
description_hash: str
callback: str
amount: int
comment: Optional[str] = None
description: Optional[str] = None
2021-09-19 13:25:39 +02:00
@core_app.post("/api/v1/payments/lnurl")
2022-02-16 22:42:27 +01:00
async def api_payments_pay_lnurl(
data: CreateLNURLData, wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-08-20 22:31:01 +02:00
domain = urlparse(data.callback).netloc
async with httpx.AsyncClient() as client:
try:
r = await client.get(
2021-08-20 22:31:01 +02:00
data.callback,
params={"amount": data.amount, "comment": data.comment},
2020-12-31 18:50:16 +01:00
timeout=40,
)
if r.is_error:
2022-07-19 18:51:35 +02:00
raise httpx.ConnectError("LNURL Callback Connection Error")
except (httpx.ConnectError, httpx.RequestError):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
2021-10-17 19:33:29 +02:00
detail=f"Failed to connect to {domain}.",
2021-07-31 02:01:19 +02:00
)
params = json.loads(r.text)
if params.get("status") == "ERROR":
raise HTTPException(
2021-10-17 19:33:29 +02:00
status_code=HTTPStatus.BAD_REQUEST,
detail=f"{domain} said: '{params.get('reason', '')}'",
)
invoice = bolt11.decode(params["pr"])
2021-08-20 22:31:01 +02:00
if invoice.amount_msat != data.amount:
raise HTTPException(
2021-10-17 19:33:29 +02:00
status_code=HTTPStatus.BAD_REQUEST,
2022-02-01 22:22:20 +01:00
detail=f"{domain} returned an invalid invoice. Expected {data.amount} msat, got {invoice.amount_msat}.",
)
2021-10-17 19:33:29 +02:00
2022-02-16 22:41:12 +01:00
# if invoice.description_hash != data.description_hash:
# raise HTTPException(
# status_code=HTTPStatus.BAD_REQUEST,
# detail=f"{domain} returned an invalid invoice. Expected description_hash == {data.description_hash}, got {invoice.description_hash}.",
# )
extra = {}
if params.get("successAction"):
extra["success_action"] = params["successAction"]
2021-08-20 22:31:01 +02:00
if data.comment:
extra["comment"] = data.comment
assert data.description is not None, "description is required"
payment_hash = await pay_invoice(
2021-09-19 13:25:39 +02:00
wallet_id=wallet.wallet.id,
payment_request=params["pr"],
2021-08-20 22:31:01 +02:00
description=data.description,
extra=extra,
)
return {
"success_action": params.get("successAction"),
"payment_hash": payment_hash,
# maintain backwards compatibility with API clients:
"checking_id": payment_hash,
}
2021-10-17 19:33:29 +02:00
async def subscribe(request: Request, wallet: Wallet):
this_wallet_id = wallet.id
payment_queue: asyncio.Queue[Payment] = asyncio.Queue(0)
logger.debug("adding sse listener", payment_queue)
api_invoice_listeners.append(payment_queue)
2022-07-19 18:51:35 +02:00
send_queue: asyncio.Queue[Tuple[str, Payment]] = asyncio.Queue(0)
async def payment_received() -> None:
while True:
payment: Payment = await payment_queue.get()
if payment.wallet_id == this_wallet_id:
logger.debug("payment receieved", payment)
await send_queue.put(("payment-received", payment))
asyncio.create_task(payment_received())
try:
while True:
typ, data = await send_queue.get()
if data:
jdata = json.dumps(dict(data.dict(), pending=False))
2021-10-17 19:33:29 +02:00
# yield dict(id=1, event="this", data="1234")
# await asyncio.sleep(2)
yield dict(data=jdata, event=typ)
# yield dict(data=jdata.encode("utf-8"), event=typ.encode("utf-8"))
except asyncio.CancelledError:
return
@core_app.get("/api/v1/payments/sse")
2022-02-16 22:42:27 +01:00
async def api_payments_sse(
request: Request, wallet: WalletTypeInfo = Depends(get_key_type)
):
return EventSourceResponse(
subscribe(request, wallet.wallet), ping=20, media_type="text/event-stream"
2022-02-16 22:42:27 +01:00
)
2021-08-29 19:38:42 +02:00
@core_app.get("/api/v1/payments/{payment_hash}")
2022-03-07 09:08:08 +01:00
async def api_payment(payment_hash, X_Api_Key: Optional[str] = Header(None)):
# We use X_Api_Key here because we want this call to work with and without keys
# If a valid key is given, we also return the field "details", otherwise not
2022-06-29 11:55:54 +02:00
wallet = None
try:
2022-07-19 18:51:35 +02:00
assert X_Api_Key is not None
# TODO: type above is Optional[str] how can that have .extra?
2022-06-29 11:55:54 +02:00
if X_Api_Key.extra:
2022-07-17 22:25:37 +02:00
logger.warning("No key")
2022-06-29 11:55:54 +02:00
except:
2022-07-19 18:51:35 +02:00
if X_Api_Key is not None:
wallet = await get_wallet_for_key(X_Api_Key)
payment = await get_standalone_payment(
payment_hash, wallet_id=wallet.id if wallet else None
) # we have to specify the wallet id here, because postgres and sqlite return internal payments in different order
# and get_standalone_payment otherwise just fetches the first one, causing unpredictable results
if payment is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Payment does not exist."
)
2022-05-20 15:35:00 +02:00
await check_invoice_status(payment.wallet_id, payment_hash)
payment = await get_standalone_payment(
payment_hash, wallet_id=wallet.id if wallet else None
)
2021-08-29 19:38:42 +02:00
if not payment:
2022-02-16 22:42:27 +01:00
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Payment does not exist."
)
2021-08-29 19:38:42 +02:00
elif not payment.pending:
2022-03-07 09:08:08 +01:00
if wallet and wallet.id == payment.wallet_id:
return {"paid": True, "preimage": payment.preimage, "details": payment}
2021-09-11 11:02:48 +02:00
return {"paid": True, "preimage": payment.preimage}
2021-08-29 19:38:42 +02:00
try:
await payment.check_pending()
except Exception:
2022-03-07 09:08:08 +01:00
if wallet and wallet.id == payment.wallet_id:
return {"paid": False, "details": payment}
2021-09-11 11:02:48 +02:00
return {"paid": False}
2021-08-29 19:38:42 +02:00
2022-03-07 09:08:08 +01:00
if wallet and wallet.id == payment.wallet_id:
2022-06-01 14:53:05 +02:00
return {
"paid": not payment.pending,
"preimage": payment.preimage,
"details": payment,
}
return {"paid": not payment.pending, "preimage": payment.preimage}
2021-10-17 19:33:29 +02:00
@core_app.get("/api/v1/lnurlscan/{code}")
async def api_lnurlscan(code: str, wallet: WalletTypeInfo = Depends(get_key_type)):
try:
2021-07-31 02:01:19 +02:00
url = lnurl.decode(code)
domain = urlparse(url).netloc
except:
# parse internet identifier (user@domain.com)
name_domain = code.split("@")
if len(name_domain) == 2 and len(name_domain[1].split(".")) == 2:
name, domain = name_domain
2022-02-16 22:42:27 +01:00
url = (
("http://" if domain.endswith(".onion") else "https://")
+ domain
+ "/.well-known/lnurlp/"
+ name
)
2021-07-31 02:01:19 +02:00
# will proceed with these values
else:
2022-02-16 22:42:27 +01:00
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="invalid lnurl"
)
# params is what will be returned to the client
params: Dict = {"domain": domain}
2021-07-31 02:01:19 +02:00
if "tag=login" in url:
params.update(kind="auth")
2021-07-31 02:01:19 +02:00
params.update(callback=url) # with k1 already in it
lnurlauth_key = wallet.wallet.lnurlauth_key(domain)
2022-01-30 20:43:30 +01:00
params.update(pubkey=lnurlauth_key.verifying_key.to_string("compressed").hex())
else:
async with httpx.AsyncClient() as client:
2021-07-31 02:01:19 +02:00
r = await client.get(url, timeout=5)
if r.is_error:
raise HTTPException(
2021-10-17 19:33:29 +02:00
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
2022-01-30 20:43:30 +01:00
detail={"domain": domain, "message": "failed to get parameters"},
)
try:
2021-07-31 02:01:19 +02:00
data = json.loads(r.text)
except json.decoder.JSONDecodeError:
raise HTTPException(
2021-10-17 19:33:29 +02:00
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
detail={
"domain": domain,
"message": f"got invalid response '{r.text[:200]}'",
},
)
2021-07-31 02:01:19 +02:00
try:
tag = data["tag"]
if tag == "channelRequest":
raise HTTPException(
2021-10-17 19:33:29 +02:00
status_code=HTTPStatus.BAD_REQUEST,
detail={
"domain": domain,
"kind": "channel",
"message": "unsupported",
},
2021-07-31 02:01:19 +02:00
)
params.update(**data)
if tag == "withdrawRequest":
params.update(kind="withdraw")
2022-01-30 20:43:30 +01:00
params.update(fixed=data["minWithdrawable"] == data["maxWithdrawable"])
2021-07-31 02:01:19 +02:00
# callback with k1 already in it
parsed_callback: ParseResult = urlparse(data["callback"])
qs: Dict = parse_qs(parsed_callback.query)
qs["k1"] = data["k1"]
# balanceCheck/balanceNotify
if "balanceCheck" in data:
params.update(balanceCheck=data["balanceCheck"])
# format callback url and send to client
2022-02-16 22:42:27 +01:00
parsed_callback = parsed_callback._replace(
query=urlencode(qs, doseq=True)
)
2021-07-31 02:01:19 +02:00
params.update(callback=urlunparse(parsed_callback))
if tag == "payRequest":
params.update(kind="pay")
params.update(fixed=data["minSendable"] == data["maxSendable"])
2022-02-16 22:42:27 +01:00
params.update(
description_hash=hashlib.sha256(
data["metadata"].encode("utf-8")
).hexdigest()
)
2021-07-31 02:01:19 +02:00
metadata = json.loads(data["metadata"])
for [k, v] in metadata:
if k == "text/plain":
params.update(description=v)
if k == "image/jpeg;base64" or k == "image/png;base64":
data_uri = "data:" + k + "," + v
params.update(image=data_uri)
if k == "text/email" or k == "text/identifier":
params.update(targetUser=v)
params.update(commentAllowed=data.get("commentAllowed", 0))
except KeyError as exc:
raise HTTPException(
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
detail={
"domain": domain,
"message": f"lnurl JSON response invalid: {exc}",
2021-10-17 19:33:29 +02:00
},
)
2021-08-20 22:31:01 +02:00
return params
2022-03-24 12:50:57 +01:00
class DecodePayment(BaseModel):
data: str
@core_app.post("/api/v1/payments/decode")
2022-03-24 12:50:57 +01:00
async def api_payments_decode(data: DecodePayment):
payment_str = data.data
try:
2022-03-24 12:50:57 +01:00
if payment_str[:5] == "LNURL":
url = lnurl.decode(payment_str)
return {"domain": url}
else:
2022-03-24 12:50:57 +01:00
invoice = bolt11.decode(payment_str)
return {
"payment_hash": invoice.payment_hash,
"amount_msat": invoice.amount_msat,
"description": invoice.description,
"description_hash": invoice.description_hash,
"payee": invoice.payee,
"date": invoice.date,
"expiry": invoice.expiry,
"secret": invoice.secret,
"route_hints": invoice.route_hints,
"min_final_cltv_expiry": invoice.min_final_cltv_expiry,
}
except:
return {"message": "Failed to decode"}
class Callback(BaseModel):
callback: str = Query(...)
@core_app.post("/api/v1/lnurlauth")
async def api_perform_lnurlauth(
callback: Callback, wallet: WalletTypeInfo = Depends(require_admin_key)
):
err = await perform_lnurlauth(callback.callback, wallet=wallet)
2020-11-11 03:01:55 +01:00
if err:
2022-02-16 22:42:27 +01:00
raise HTTPException(
status_code=HTTPStatus.SERVICE_UNAVAILABLE, detail=err.reason
)
return ""
2021-08-29 19:38:42 +02:00
@core_app.get("/api/v1/currencies")
async def api_list_currencies_available():
2021-08-20 22:31:01 +02:00
return list(currencies.keys())
class ConversionData(BaseModel):
2022-01-30 20:43:30 +01:00
from_: str = Field("sat", alias="from")
amount: float
2022-01-30 20:43:30 +01:00
to: str = Query("usd")
@core_app.post("/api/v1/conversion")
async def api_fiat_as_sats(data: ConversionData):
2022-01-14 13:19:30 +01:00
output = {}
2022-01-30 20:43:30 +01:00
if data.from_ == "sat":
2022-07-19 18:51:35 +02:00
output["sats"] = data.amount
2022-01-14 13:19:30 +01:00
output["BTC"] = data.amount / 100000000
2022-01-30 20:43:30 +01:00
for currency in data.to.split(","):
2022-02-16 22:42:27 +01:00
output[currency.strip().upper()] = await satoshis_amount_as_fiat(
data.amount, currency.strip()
)
2022-01-14 13:19:30 +01:00
return output
else:
output[data.from_.upper()] = data.amount
2022-03-10 13:16:51 +01:00
output["sats"] = await fiat_amount_as_satoshis(data.amount, data.from_)
2022-01-14 13:19:30 +01:00
output["BTC"] = output["sats"] / 100000000
return output