2020-09-01 03:12:46 +02:00
|
|
|
from typing import List, Optional, Dict
|
2020-04-16 17:10:53 +02:00
|
|
|
from uuid import uuid4
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
from lnbits.db import open_db
|
2020-04-21 15:44:02 +02:00
|
|
|
from lnbits.settings import DEFAULT_WALLET_NAME
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
from .models import User, Wallet, Payment
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
# accounts
|
|
|
|
# --------
|
|
|
|
|
|
|
|
|
|
|
|
def create_account() -> User:
|
|
|
|
with open_db() as db:
|
|
|
|
user_id = uuid4().hex
|
|
|
|
db.execute("INSERT INTO accounts (id) VALUES (?)", (user_id,))
|
|
|
|
|
2020-04-26 13:28:19 +02:00
|
|
|
new_account = get_account(user_id=user_id)
|
|
|
|
assert new_account, "Newly created account couldn't be retrieved"
|
|
|
|
|
|
|
|
return new_account
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_account(user_id: str) -> Optional[User]:
|
|
|
|
with open_db() as db:
|
|
|
|
row = db.fetchone("SELECT id, email, pass as password FROM accounts WHERE id = ?", (user_id,))
|
|
|
|
|
|
|
|
return User(**row) if row else None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user(user_id: str) -> Optional[User]:
|
|
|
|
with open_db() as db:
|
|
|
|
user = db.fetchone("SELECT id, email FROM accounts WHERE id = ?", (user_id,))
|
|
|
|
|
|
|
|
if user:
|
|
|
|
extensions = db.fetchall("SELECT extension FROM extensions WHERE user = ? AND active = 1", (user_id,))
|
|
|
|
wallets = db.fetchall(
|
|
|
|
"""
|
2020-04-21 15:44:02 +02:00
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
|
2020-03-04 23:11:15 +01:00
|
|
|
FROM wallets
|
|
|
|
WHERE user = ?
|
|
|
|
""",
|
2020-04-21 15:44:02 +02:00
|
|
|
(user_id,),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
User(**{**user, **{"extensions": [e[0] for e in extensions], "wallets": [Wallet(**w) for w in wallets]}})
|
|
|
|
if user
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def update_user_extension(*, user_id: str, extension: str, active: int) -> None:
|
|
|
|
with open_db() as db:
|
|
|
|
db.execute(
|
|
|
|
"""
|
|
|
|
INSERT OR REPLACE INTO extensions (user, extension, active)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
""",
|
|
|
|
(user_id, extension, active),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# wallets
|
|
|
|
# -------
|
|
|
|
|
|
|
|
|
2020-03-10 23:12:22 +01:00
|
|
|
def create_wallet(*, user_id: str, wallet_name: Optional[str] = None) -> Wallet:
|
2020-03-04 23:11:15 +01:00
|
|
|
with open_db() as db:
|
|
|
|
wallet_id = uuid4().hex
|
|
|
|
db.execute(
|
|
|
|
"""
|
|
|
|
INSERT INTO wallets (id, name, user, adminkey, inkey)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
""",
|
2020-03-08 23:00:41 +01:00
|
|
|
(wallet_id, wallet_name or DEFAULT_WALLET_NAME, user_id, uuid4().hex, uuid4().hex),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
2020-04-26 13:28:19 +02:00
|
|
|
new_wallet = get_wallet(wallet_id=wallet_id)
|
|
|
|
assert new_wallet, "Newly created wallet couldn't be retrieved"
|
|
|
|
|
|
|
|
return new_wallet
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def delete_wallet(*, user_id: str, wallet_id: str) -> None:
|
|
|
|
with open_db() as db:
|
|
|
|
db.execute(
|
|
|
|
"""
|
|
|
|
UPDATE wallets AS w
|
|
|
|
SET
|
|
|
|
user = 'del:' || w.user,
|
|
|
|
adminkey = 'del:' || w.adminkey,
|
|
|
|
inkey = 'del:' || w.inkey
|
|
|
|
WHERE id = ? AND user = ?
|
|
|
|
""",
|
|
|
|
(wallet_id, user_id),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_wallet(wallet_id: str) -> Optional[Wallet]:
|
|
|
|
with open_db() as db:
|
|
|
|
row = db.fetchone(
|
|
|
|
"""
|
2020-04-21 15:44:02 +02:00
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
|
2020-03-04 23:11:15 +01:00
|
|
|
FROM wallets
|
|
|
|
WHERE id = ?
|
|
|
|
""",
|
2020-04-21 15:44:02 +02:00
|
|
|
(wallet_id,),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return Wallet(**row) if row else None
|
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]:
|
|
|
|
with open_db() as db:
|
|
|
|
row = db.fetchone(
|
2020-06-13 03:46:40 +02:00
|
|
|
"""
|
2020-04-21 15:44:02 +02:00
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
|
2020-03-04 23:11:15 +01:00
|
|
|
FROM wallets
|
2020-05-05 09:59:15 +02:00
|
|
|
WHERE adminkey = ? OR inkey = ?
|
2020-03-04 23:11:15 +01:00
|
|
|
""",
|
2020-05-05 09:59:15 +02:00
|
|
|
(key, key),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
2020-05-05 09:59:15 +02:00
|
|
|
if not row:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if key_type == "admin" and row["adminkey"] != key:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return Wallet(**row)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
# wallet payments
|
|
|
|
# ---------------
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]:
|
2020-03-04 23:11:15 +01:00
|
|
|
with open_db() as db:
|
|
|
|
row = db.fetchone(
|
|
|
|
"""
|
2020-09-01 03:12:46 +02:00
|
|
|
SELECT *
|
|
|
|
FROM apipayments
|
|
|
|
WHERE wallet = ? AND hash = ?
|
2020-03-04 23:11:15 +01:00
|
|
|
""",
|
2020-09-01 03:12:46 +02:00
|
|
|
(wallet_id, payment_hash),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
return Payment.from_row(row) if row else None
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-08-31 05:24:57 +02:00
|
|
|
def get_wallet_payments(
|
|
|
|
wallet_id: str, *, complete: bool = False, pending: bool = False, outgoing: bool = False, incoming: bool = False
|
|
|
|
) -> List[Payment]:
|
|
|
|
"""
|
|
|
|
Filters payments to be returned by complete | pending | outgoing | incoming.
|
|
|
|
"""
|
|
|
|
|
|
|
|
clause = ""
|
|
|
|
if complete and pending:
|
|
|
|
clause += ""
|
|
|
|
elif complete:
|
|
|
|
clause += "AND ((amount > 0 AND pending = 0) OR amount < 0)"
|
|
|
|
elif pending:
|
|
|
|
clause += "AND pending = 1"
|
|
|
|
else:
|
|
|
|
raise TypeError("at least one of [complete, pending] must be True.")
|
|
|
|
|
|
|
|
if outgoing and incoming:
|
|
|
|
clause += ""
|
|
|
|
elif outgoing:
|
|
|
|
clause += "AND amount < 0"
|
|
|
|
elif incoming:
|
|
|
|
clause += "AND amount > 0"
|
|
|
|
else:
|
|
|
|
raise TypeError("at least one of [outgoing, incoming] must be True.")
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-08-31 05:24:57 +02:00
|
|
|
with open_db() as db:
|
2020-03-04 23:11:15 +01:00
|
|
|
rows = db.fetchall(
|
2020-03-07 22:27:00 +01:00
|
|
|
f"""
|
2020-09-01 03:12:46 +02:00
|
|
|
SELECT *
|
2020-03-04 23:11:15 +01:00
|
|
|
FROM apipayments
|
2020-08-31 05:24:57 +02:00
|
|
|
WHERE wallet = ? {clause}
|
2020-03-04 23:11:15 +01:00
|
|
|
ORDER BY time DESC
|
|
|
|
""",
|
2020-03-07 22:27:00 +01:00
|
|
|
(wallet_id,),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
return [Payment.from_row(row) for row in rows]
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-04-17 21:13:57 +02:00
|
|
|
def delete_wallet_payments_expired(wallet_id: str, *, seconds: int = 86400) -> None:
|
|
|
|
with open_db() as db:
|
|
|
|
db.execute(
|
|
|
|
"""
|
|
|
|
DELETE
|
2020-09-01 03:12:46 +02:00
|
|
|
FROM apipayments WHERE wallet = ? AND pending = 1 AND time < strftime('%s', 'now') - ?
|
2020-04-17 21:13:57 +02:00
|
|
|
""",
|
|
|
|
(wallet_id, seconds),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
# payments
|
|
|
|
# --------
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
def create_payment(
|
2020-09-01 03:12:46 +02:00
|
|
|
*,
|
|
|
|
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,
|
2020-03-31 19:05:25 +02:00
|
|
|
) -> Payment:
|
2020-03-04 23:11:15 +01:00
|
|
|
with open_db() as db:
|
|
|
|
db.execute(
|
|
|
|
"""
|
2020-09-01 03:12:46 +02:00
|
|
|
INSERT INTO apipayments
|
|
|
|
(wallet, checking_id, bolt11, hash, preimage,
|
|
|
|
amount, pending, memo, fee, extra)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2020-03-04 23:11:15 +01:00
|
|
|
""",
|
2020-09-01 03:12:46 +02:00
|
|
|
(wallet_id, checking_id, payment_request, payment_hash, preimage, amount, int(pending), memo, fee, extra),
|
2020-03-04 23:11:15 +01:00
|
|
|
)
|
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
new_payment = get_wallet_payment(wallet_id, payment_hash)
|
2020-04-26 13:28:19 +02:00
|
|
|
assert new_payment, "Newly created payment couldn't be retrieved"
|
|
|
|
|
|
|
|
return new_payment
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
def update_payment_status(checking_id: str, pending: bool) -> None:
|
2020-03-04 23:11:15 +01:00
|
|
|
with open_db() as db:
|
2020-09-01 03:12:46 +02:00
|
|
|
db.execute("UPDATE apipayments SET pending = ? WHERE checking_id = ?", (int(pending), checking_id,))
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
def delete_payment(checking_id: str) -> None:
|
2020-03-07 22:27:00 +01:00
|
|
|
with open_db() as db:
|
2020-09-01 03:12:46 +02:00
|
|
|
db.execute("DELETE FROM apipayments WHERE checking_id = ?", (checking_id,))
|
2020-08-19 18:53:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_internal(payment_hash: str) -> None:
|
|
|
|
with open_db() as db:
|
2020-09-01 03:12:46 +02:00
|
|
|
row = db.fetchone("SELECT checking_id FROM apipayments WHERE hash = ?", (payment_hash,))
|
2020-08-19 18:53:27 +02:00
|
|
|
if not row:
|
|
|
|
return False
|
|
|
|
else:
|
2020-09-01 03:12:46 +02:00
|
|
|
return row["checking_id"]
|