lnbits-legend/lnbits/core/crud.py

487 lines
12 KiB
Python
Raw Normal View History

import json
import datetime
2020-04-16 17:10:53 +02:00
from uuid import uuid4
from typing import List, Optional, Dict, Any
2021-04-17 23:27:15 +02:00
from urllib.parse import urlparse
from lnbits import bolt11
2021-07-02 23:32:58 +02:00
from lnbits.db import Connection, POSTGRES, COCKROACH
2020-04-21 15:44:02 +02:00
from lnbits.settings import DEFAULT_WALLET_NAME
from . import db
2021-04-17 23:27:15 +02:00
from .models import User, Wallet, Payment, BalanceCheck
# accounts
# --------
async def create_account(conn: Optional[Connection] = None) -> User:
user_id = uuid4().hex
await (conn or db).execute("INSERT INTO accounts (id) VALUES (?)", (user_id,))
new_account = await get_account(user_id=user_id, conn=conn)
2020-04-26 13:28:19 +02:00
assert new_account, "Newly created account couldn't be retrieved"
return new_account
async def get_account(
user_id: str, conn: Optional[Connection] = None
) -> Optional[User]:
row = await (conn or db).fetchone(
"SELECT id, email, pass as password FROM accounts WHERE id = ?", (user_id,)
)
return User(**row) if row else None
async def get_user(user_id: str, conn: Optional[Connection] = None) -> Optional[User]:
user = await (conn or db).fetchone(
"SELECT id, email FROM accounts WHERE id = ?", (user_id,)
)
if user:
extensions = await (conn or db).fetchall(
2021-06-22 04:22:52 +02:00
"""SELECT extension FROM extensions WHERE "user" = ? AND active""",
(user_id,),
)
wallets = await (conn or db).fetchall(
"""
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
FROM wallets
2021-06-22 04:22:52 +02:00
WHERE "user" = ?
""",
(user_id,),
)
return (
User(
**{
**user,
**{
"extensions": [e[0] for e in extensions],
"wallets": [Wallet(**w) for w in wallets],
},
}
)
if user
else None
)
async def update_user_extension(
2021-06-22 04:22:52 +02:00
*, user_id: str, extension: str, active: bool, conn: Optional[Connection] = None
) -> None:
await (conn or db).execute(
"""
2021-06-22 04:22:52 +02:00
INSERT INTO extensions ("user", extension, active) VALUES (?, ?, ?)
ON CONFLICT ("user", extension) DO UPDATE SET active = ?
""",
2021-06-22 04:22:52 +02:00
(user_id, extension, active, active),
)
# wallets
# -------
async def create_wallet(
*,
user_id: str,
wallet_name: Optional[str] = None,
conn: Optional[Connection] = None,
) -> Wallet:
wallet_id = uuid4().hex
await (conn or db).execute(
"""
2021-06-22 04:22:52 +02:00
INSERT INTO wallets (id, name, "user", adminkey, inkey)
VALUES (?, ?, ?, ?, ?)
""",
(
wallet_id,
wallet_name or DEFAULT_WALLET_NAME,
user_id,
uuid4().hex,
uuid4().hex,
),
)
new_wallet = await get_wallet(wallet_id=wallet_id, conn=conn)
2020-04-26 13:28:19 +02:00
assert new_wallet, "Newly created wallet couldn't be retrieved"
return new_wallet
2021-08-06 12:15:07 +02:00
async def update_wallet(
wallet_id: str, new_name: str, conn: Optional[Connection] = None
) -> Optional[Wallet]:
await (conn or db).execute(
"""
UPDATE wallets SET
name = ?
WHERE id = ?
""",
(new_name, wallet_id)
)
async def delete_wallet(
*, user_id: str, wallet_id: str, conn: Optional[Connection] = None
) -> None:
await (conn or db).execute(
"""
UPDATE wallets AS w
SET
2021-06-22 04:22:52 +02:00
"user" = 'del:' || w."user",
adminkey = 'del:' || w.adminkey,
inkey = 'del:' || w.inkey
2021-06-22 04:22:52 +02:00
WHERE id = ? AND "user" = ?
""",
(wallet_id, user_id),
)
async def get_wallet(
wallet_id: str, conn: Optional[Connection] = None
) -> Optional[Wallet]:
row = await (conn or db).fetchone(
"""
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
FROM wallets
WHERE id = ?
""",
(wallet_id,),
)
return Wallet(**row) if row else None
async def get_wallet_for_key(
key: str, key_type: str = "invoice", conn: Optional[Connection] = None
) -> Optional[Wallet]:
row = await (conn or db).fetchone(
"""
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
FROM wallets
WHERE adminkey = ? OR inkey = ?
""",
(key, key),
)
if not row:
return None
if key_type == "admin" and row["adminkey"] != key:
return None
return Wallet(**row)
# wallet payments
# ---------------
async def get_standalone_payment(
checking_id_or_hash: str, conn: Optional[Connection] = None
) -> Optional[Payment]:
row = await (conn or db).fetchone(
2020-09-28 04:12:55 +02:00
"""
SELECT *
FROM apipayments
WHERE checking_id = ? OR hash = ?
2021-03-07 04:08:36 +01:00
LIMIT 1
2020-09-28 04:12:55 +02:00
""",
2021-03-07 04:08:36 +01:00
(checking_id_or_hash, checking_id_or_hash),
2020-09-28 04:12:55 +02:00
)
return Payment.from_row(row) if row else None
async def get_wallet_payment(
wallet_id: str, payment_hash: str, conn: Optional[Connection] = None
) -> Optional[Payment]:
row = await (conn or db).fetchone(
"""
SELECT *
FROM apipayments
WHERE wallet = ? AND hash = ?
""",
(wallet_id, payment_hash),
)
return Payment.from_row(row) if row else None
async def get_payments(
*,
wallet_id: Optional[str] = None,
complete: bool = False,
pending: bool = False,
outgoing: bool = False,
incoming: bool = False,
since: Optional[int] = None,
exclude_uncheckable: bool = False,
conn: Optional[Connection] = None,
) -> List[Payment]:
"""
Filters payments to be returned by complete | pending | outgoing | incoming.
"""
args: List[Any] = []
clause: List[str] = []
if since != None:
2021-06-22 04:22:52 +02:00
if db.type == POSTGRES:
clause.append("time > to_timestamp(?)")
2021-07-02 23:32:58 +02:00
elif db.type == COCKROACH:
clause.append("time > cast(? AS timestamp)")
2021-06-22 04:22:52 +02:00
else:
clause.append("time > ?")
args.append(since)
if wallet_id:
clause.append("wallet = ?")
args.append(wallet_id)
if complete and pending:
pass
elif complete:
2021-06-22 04:22:52 +02:00
clause.append("((amount > 0 AND pending = false) OR amount < 0)")
elif pending:
2021-06-22 04:22:52 +02:00
clause.append("pending = true")
else:
pass
if outgoing and incoming:
pass
elif outgoing:
clause.append("amount < 0")
elif incoming:
clause.append("amount > 0")
else:
pass
if exclude_uncheckable: # checkable means it has a checking_id that isn't internal
clause.append("checking_id NOT LIKE 'temp_%'")
clause.append("checking_id NOT LIKE 'internal_%'")
where = ""
if clause:
where = f"WHERE {' AND '.join(clause)}"
rows = await (conn or db).fetchall(
f"""
SELECT *
FROM apipayments
{where}
ORDER BY time DESC
""",
tuple(args),
)
return [Payment.from_row(row) for row in rows]
async def delete_expired_invoices(
conn: Optional[Connection] = None,
) -> None:
2021-05-14 18:12:05 +02:00
# first we delete all invoices older than one month
await (conn or db).execute(
2021-06-22 04:22:52 +02:00
f"""
2021-05-14 18:12:05 +02:00
DELETE FROM apipayments
2021-06-22 04:22:52 +02:00
WHERE pending = true AND amount > 0
AND time < {db.timestamp_now} - {db.interval_seconds(2592000)}
2021-05-14 18:12:05 +02:00
"""
)
# then we delete all expired invoices, checking one by one
rows = await (conn or db).fetchall(
2021-06-22 04:22:52 +02:00
f"""
SELECT bolt11
FROM apipayments
2021-06-22 04:22:52 +02:00
WHERE pending = true
2021-05-14 18:12:05 +02:00
AND bolt11 IS NOT NULL
2021-06-22 04:22:52 +02:00
AND amount > 0 AND time < {db.timestamp_now} - {db.interval_seconds(86400)}
2021-05-14 18:12:05 +02:00
"""
)
for (payment_request,) in rows:
try:
invoice = bolt11.decode(payment_request)
except:
continue
expiration_date = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry)
if expiration_date > datetime.datetime.utcnow():
continue
await (conn or db).execute(
"""
DELETE FROM apipayments
2021-06-22 04:22:52 +02:00
WHERE pending = true AND hash = ?
""",
(invoice.payment_hash,),
)
2020-04-17 21:13:57 +02:00
# payments
# --------
async def create_payment(
*,
wallet_id: str,
checking_id: str,
payment_request: str,
payment_hash: str,
amount: int,
memo: str,
fee: int = 0,
preimage: Optional[str] = None,
pending: bool = True,
extra: Optional[Dict] = None,
webhook: Optional[str] = None,
conn: Optional[Connection] = None,
) -> Payment:
await (conn or db).execute(
"""
INSERT INTO apipayments
(wallet, checking_id, bolt11, hash, preimage,
amount, pending, memo, fee, extra, webhook)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
wallet_id,
checking_id,
payment_request,
payment_hash,
preimage,
amount,
2021-06-22 04:22:52 +02:00
pending,
memo,
fee,
json.dumps(extra)
if extra and extra != {} and type(extra) is dict
else None,
webhook,
),
)
new_payment = await get_wallet_payment(wallet_id, payment_hash, conn=conn)
2020-04-26 13:28:19 +02:00
assert new_payment, "Newly created payment couldn't be retrieved"
return new_payment
async def update_payment_status(
checking_id: str,
pending: bool,
conn: Optional[Connection] = None,
) -> None:
await (conn or db).execute(
2021-03-07 23:18:02 +01:00
"UPDATE apipayments SET pending = ? WHERE checking_id = ?",
(
2021-06-22 04:22:52 +02:00
pending,
2021-03-07 23:18:02 +01:00
checking_id,
),
)
async def delete_payment(
checking_id: str,
conn: Optional[Connection] = None,
) -> None:
await (conn or db).execute(
"DELETE FROM apipayments WHERE checking_id = ?", (checking_id,)
)
2020-08-19 18:53:27 +02:00
async def check_internal(
payment_hash: str,
conn: Optional[Connection] = None,
) -> Optional[str]:
row = await (conn or db).fetchone(
"""
2020-11-19 18:23:35 +01:00
SELECT checking_id FROM apipayments
2021-08-06 12:15:07 +02:00
WHERE hash = ? AND pending AND amount > 0
2020-11-19 18:23:35 +01:00
""",
(payment_hash,),
)
if not row:
return None
else:
return row["checking_id"]
2021-04-17 23:27:15 +02:00
# balance_check
# -------------
async def save_balance_check(
wallet_id: str,
url: str,
conn: Optional[Connection] = None,
):
domain = urlparse(url).netloc
await (conn or db).execute(
"""
2021-06-22 04:22:52 +02:00
INSERT INTO balance_check (wallet, service, url) VALUES (?, ?, ?)
ON CONFLICT (wallet, service) DO UPDATE SET url = ?
2021-04-17 23:27:15 +02:00
""",
2021-06-22 04:22:52 +02:00
(wallet_id, domain, url, url),
2021-04-17 23:27:15 +02:00
)
async def get_balance_check(
wallet_id: str,
domain: str,
conn: Optional[Connection] = None,
) -> Optional[BalanceCheck]:
row = await (conn or db).fetchone(
"""
SELECT wallet, service, url
FROM balance_check
WHERE wallet = ? AND service = ?
""",
(wallet_id, domain),
)
return BalanceCheck.from_row(row) if row else None
async def get_balance_checks(conn: Optional[Connection] = None) -> List[BalanceCheck]:
rows = await (conn or db).fetchall("SELECT wallet, service, url FROM balance_check")
return [BalanceCheck.from_row(row) for row in rows]
# balance_notify
# --------------
async def save_balance_notify(
wallet_id: str,
url: str,
conn: Optional[Connection] = None,
):
await (conn or db).execute(
"""
2021-06-22 04:22:52 +02:00
INSERT INTO balance_notify (wallet, url) VALUES (?, ?)
ON CONFLICT (wallet) DO UPDATE SET url = ?
2021-04-17 23:27:15 +02:00
""",
2021-06-22 04:22:52 +02:00
(wallet_id, url, url),
2021-04-17 23:27:15 +02:00
)
async def get_balance_notify(
wallet_id: str,
conn: Optional[Connection] = None,
) -> Optional[str]:
row = await (conn or db).fetchone(
"""
SELECT url
FROM balance_notify
WHERE wallet = ?
""",
(wallet_id,),
)
return row[0] if row else None