|
|
@ -2,8 +2,8 @@ import json |
|
|
|
import datetime |
|
|
|
from uuid import uuid4 |
|
|
|
from typing import List, Optional, Dict |
|
|
|
from flask import g |
|
|
|
|
|
|
|
from lnbits.db import open_db |
|
|
|
from lnbits import bolt11 |
|
|
|
from lnbits.settings import DEFAULT_WALLET_NAME |
|
|
|
|
|
|
@ -15,9 +15,8 @@ from .models import User, Wallet, Payment |
|
|
|
|
|
|
|
|
|
|
|
def create_account() -> User: |
|
|
|
with open_db() as db: |
|
|
|
user_id = uuid4().hex |
|
|
|
db.execute("INSERT INTO accounts (id) VALUES (?)", (user_id,)) |
|
|
|
g.db.execute("INSERT INTO accounts (id) VALUES (?)", (user_id,)) |
|
|
|
|
|
|
|
new_account = get_account(user_id=user_id) |
|
|
|
assert new_account, "Newly created account couldn't be retrieved" |
|
|
@ -26,19 +25,17 @@ def create_account() -> User: |
|
|
|
|
|
|
|
|
|
|
|
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,)) |
|
|
|
row = g.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,)) |
|
|
|
user = g.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( |
|
|
|
extensions = g.db.fetchall("SELECT extension FROM extensions WHERE user = ? AND active = 1", (user_id,)) |
|
|
|
wallets = g.db.fetchall( |
|
|
|
""" |
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat |
|
|
|
FROM wallets |
|
|
@ -55,8 +52,7 @@ def get_user(user_id: str) -> Optional[User]: |
|
|
|
|
|
|
|
|
|
|
|
def update_user_extension(*, user_id: str, extension: str, active: int) -> None: |
|
|
|
with open_db() as db: |
|
|
|
db.execute( |
|
|
|
g.db.execute( |
|
|
|
""" |
|
|
|
INSERT OR REPLACE INTO extensions (user, extension, active) |
|
|
|
VALUES (?, ?, ?) |
|
|
@ -70,9 +66,8 @@ def update_user_extension(*, user_id: str, extension: str, active: int) -> None: |
|
|
|
|
|
|
|
|
|
|
|
def create_wallet(*, user_id: str, wallet_name: Optional[str] = None) -> Wallet: |
|
|
|
with open_db() as db: |
|
|
|
wallet_id = uuid4().hex |
|
|
|
db.execute( |
|
|
|
g.db.execute( |
|
|
|
""" |
|
|
|
INSERT INTO wallets (id, name, user, adminkey, inkey) |
|
|
|
VALUES (?, ?, ?, ?, ?) |
|
|
@ -87,8 +82,7 @@ def create_wallet(*, user_id: str, wallet_name: Optional[str] = None) -> Wallet: |
|
|
|
|
|
|
|
|
|
|
|
def delete_wallet(*, user_id: str, wallet_id: str) -> None: |
|
|
|
with open_db() as db: |
|
|
|
db.execute( |
|
|
|
g.db.execute( |
|
|
|
""" |
|
|
|
UPDATE wallets AS w |
|
|
|
SET |
|
|
@ -102,8 +96,7 @@ def delete_wallet(*, user_id: str, wallet_id: str) -> None: |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet(wallet_id: str) -> Optional[Wallet]: |
|
|
|
with open_db() as db: |
|
|
|
row = db.fetchone( |
|
|
|
row = g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat |
|
|
|
FROM wallets |
|
|
@ -116,8 +109,7 @@ def get_wallet(wallet_id: str) -> Optional[Wallet]: |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]: |
|
|
|
with open_db() as db: |
|
|
|
row = db.fetchone( |
|
|
|
row = g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat |
|
|
|
FROM wallets |
|
|
@ -140,8 +132,7 @@ def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]: |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]: |
|
|
|
with open_db() as db: |
|
|
|
row = db.fetchone( |
|
|
|
row = g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT * |
|
|
|
FROM apipayments |
|
|
@ -179,8 +170,7 @@ def get_wallet_payments( |
|
|
|
else: |
|
|
|
raise TypeError("at least one of [outgoing, incoming] must be True.") |
|
|
|
|
|
|
|
with open_db() as db: |
|
|
|
rows = db.fetchall( |
|
|
|
rows = g.db.fetchall( |
|
|
|
f""" |
|
|
|
SELECT * |
|
|
|
FROM apipayments |
|
|
@ -194,8 +184,7 @@ def get_wallet_payments( |
|
|
|
|
|
|
|
|
|
|
|
def delete_expired_invoices() -> None: |
|
|
|
with open_db() as db: |
|
|
|
rows = db.fetchall( |
|
|
|
rows = g.db.fetchall( |
|
|
|
""" |
|
|
|
SELECT bolt11 |
|
|
|
FROM apipayments |
|
|
@ -212,7 +201,7 @@ def delete_expired_invoices() -> None: |
|
|
|
if expiration_date > datetime.datetime.utcnow(): |
|
|
|
continue |
|
|
|
|
|
|
|
db.execute( |
|
|
|
g.db.execute( |
|
|
|
""" |
|
|
|
DELETE FROM apipayments |
|
|
|
WHERE pending = 1 AND hash = ? |
|
|
@ -238,8 +227,7 @@ def create_payment( |
|
|
|
pending: bool = True, |
|
|
|
extra: Optional[Dict] = None, |
|
|
|
) -> Payment: |
|
|
|
with open_db() as db: |
|
|
|
db.execute( |
|
|
|
g.db.execute( |
|
|
|
""" |
|
|
|
INSERT INTO apipayments |
|
|
|
(wallet, checking_id, bolt11, hash, preimage, |
|
|
@ -267,8 +255,7 @@ def create_payment( |
|
|
|
|
|
|
|
|
|
|
|
def update_payment_status(checking_id: str, pending: bool) -> None: |
|
|
|
with open_db() as db: |
|
|
|
db.execute( |
|
|
|
g.db.execute( |
|
|
|
"UPDATE apipayments SET pending = ? WHERE checking_id = ?", |
|
|
|
( |
|
|
|
int(pending), |
|
|
@ -278,13 +265,11 @@ def update_payment_status(checking_id: str, pending: bool) -> None: |
|
|
|
|
|
|
|
|
|
|
|
def delete_payment(checking_id: str) -> None: |
|
|
|
with open_db() as db: |
|
|
|
db.execute("DELETE FROM apipayments WHERE checking_id = ?", (checking_id,)) |
|
|
|
g.db.execute("DELETE FROM apipayments WHERE checking_id = ?", (checking_id,)) |
|
|
|
|
|
|
|
|
|
|
|
def check_internal(payment_hash: str) -> Optional[str]: |
|
|
|
with open_db() as db: |
|
|
|
row = db.fetchone( |
|
|
|
row = g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT checking_id FROM apipayments |
|
|
|
WHERE hash = ? AND pending AND amount > 0 |
|
|
|