|
|
@ -14,9 +14,9 @@ from .models import User, Wallet, Payment |
|
|
|
# -------- |
|
|
|
|
|
|
|
|
|
|
|
def create_account() -> User: |
|
|
|
async def create_account() -> User: |
|
|
|
user_id = uuid4().hex |
|
|
|
g.db.execute("INSERT INTO accounts (id) VALUES (?)", (user_id,)) |
|
|
|
await 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" |
|
|
@ -24,18 +24,18 @@ def create_account() -> User: |
|
|
|
return new_account |
|
|
|
|
|
|
|
|
|
|
|
def get_account(user_id: str) -> Optional[User]: |
|
|
|
row = g.db.fetchone("SELECT id, email, pass as password FROM accounts WHERE id = ?", (user_id,)) |
|
|
|
async def get_account(user_id: str) -> Optional[User]: |
|
|
|
row = await 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]: |
|
|
|
user = g.db.fetchone("SELECT id, email FROM accounts WHERE id = ?", (user_id,)) |
|
|
|
async def get_user(user_id: str) -> Optional[User]: |
|
|
|
user = await g.db.fetchone("SELECT id, email FROM accounts WHERE id = ?", (user_id,)) |
|
|
|
|
|
|
|
if user: |
|
|
|
extensions = g.db.fetchall("SELECT extension FROM extensions WHERE user = ? AND active = 1", (user_id,)) |
|
|
|
wallets = g.db.fetchall( |
|
|
|
extensions = await g.db.fetchall("SELECT extension FROM extensions WHERE user = ? AND active = 1", (user_id,)) |
|
|
|
wallets = await g.db.fetchall( |
|
|
|
""" |
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat |
|
|
|
FROM wallets |
|
|
@ -51,8 +51,8 @@ def get_user(user_id: str) -> Optional[User]: |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def update_user_extension(*, user_id: str, extension: str, active: int) -> None: |
|
|
|
g.db.execute( |
|
|
|
async def update_user_extension(*, user_id: str, extension: str, active: int) -> None: |
|
|
|
await g.db.execute( |
|
|
|
""" |
|
|
|
INSERT OR REPLACE INTO extensions (user, extension, active) |
|
|
|
VALUES (?, ?, ?) |
|
|
@ -65,9 +65,9 @@ def update_user_extension(*, user_id: str, extension: str, active: int) -> None: |
|
|
|
# ------- |
|
|
|
|
|
|
|
|
|
|
|
def create_wallet(*, user_id: str, wallet_name: Optional[str] = None) -> Wallet: |
|
|
|
async def create_wallet(*, user_id: str, wallet_name: Optional[str] = None) -> Wallet: |
|
|
|
wallet_id = uuid4().hex |
|
|
|
g.db.execute( |
|
|
|
await g.db.execute( |
|
|
|
""" |
|
|
|
INSERT INTO wallets (id, name, user, adminkey, inkey) |
|
|
|
VALUES (?, ?, ?, ?, ?) |
|
|
@ -81,8 +81,8 @@ def create_wallet(*, user_id: str, wallet_name: Optional[str] = None) -> Wallet: |
|
|
|
return new_wallet |
|
|
|
|
|
|
|
|
|
|
|
def delete_wallet(*, user_id: str, wallet_id: str) -> None: |
|
|
|
g.db.execute( |
|
|
|
async def delete_wallet(*, user_id: str, wallet_id: str) -> None: |
|
|
|
await g.db.execute( |
|
|
|
""" |
|
|
|
UPDATE wallets AS w |
|
|
|
SET |
|
|
@ -95,8 +95,8 @@ def delete_wallet(*, user_id: str, wallet_id: str) -> None: |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet(wallet_id: str) -> Optional[Wallet]: |
|
|
|
row = g.db.fetchone( |
|
|
|
async def get_wallet(wallet_id: str) -> Optional[Wallet]: |
|
|
|
row = await g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat |
|
|
|
FROM wallets |
|
|
@ -108,8 +108,8 @@ def get_wallet(wallet_id: str) -> Optional[Wallet]: |
|
|
|
return Wallet(**row) if row else None |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]: |
|
|
|
row = g.db.fetchone( |
|
|
|
async def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]: |
|
|
|
row = await g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat |
|
|
|
FROM wallets |
|
|
@ -131,8 +131,8 @@ def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]: |
|
|
|
# --------------- |
|
|
|
|
|
|
|
|
|
|
|
def get_standalone_payment(checking_id: str) -> Optional[Payment]: |
|
|
|
row = g.db.fetchone( |
|
|
|
async def get_standalone_payment(checking_id: str) -> Optional[Payment]: |
|
|
|
row = await g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT * |
|
|
|
FROM apipayments |
|
|
@ -144,8 +144,8 @@ def get_standalone_payment(checking_id: str) -> Optional[Payment]: |
|
|
|
return Payment.from_row(row) if row else None |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]: |
|
|
|
row = g.db.fetchone( |
|
|
|
async def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]: |
|
|
|
row = await g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT * |
|
|
|
FROM apipayments |
|
|
@ -157,7 +157,7 @@ def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]: |
|
|
|
return Payment.from_row(row) if row else None |
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_payments( |
|
|
|
async def get_wallet_payments( |
|
|
|
wallet_id: str, |
|
|
|
*, |
|
|
|
complete: bool = False, |
|
|
@ -197,7 +197,7 @@ def get_wallet_payments( |
|
|
|
clause += "AND checking_id NOT LIKE 'temp_%' " |
|
|
|
clause += "AND checking_id NOT LIKE 'internal_%' " |
|
|
|
|
|
|
|
rows = g.db.fetchall( |
|
|
|
rows = await g.db.fetchall( |
|
|
|
f""" |
|
|
|
SELECT * |
|
|
|
FROM apipayments |
|
|
@ -210,8 +210,8 @@ def get_wallet_payments( |
|
|
|
return [Payment.from_row(row) for row in rows] |
|
|
|
|
|
|
|
|
|
|
|
def delete_expired_invoices() -> None: |
|
|
|
rows = g.db.fetchall( |
|
|
|
async def delete_expired_invoices() -> None: |
|
|
|
rows = await g.db.fetchall( |
|
|
|
""" |
|
|
|
SELECT bolt11 |
|
|
|
FROM apipayments |
|
|
@ -228,7 +228,7 @@ def delete_expired_invoices() -> None: |
|
|
|
if expiration_date > datetime.datetime.utcnow(): |
|
|
|
continue |
|
|
|
|
|
|
|
g.db.execute( |
|
|
|
await g.db.execute( |
|
|
|
""" |
|
|
|
DELETE FROM apipayments |
|
|
|
WHERE pending = 1 AND hash = ? |
|
|
@ -241,7 +241,7 @@ def delete_expired_invoices() -> None: |
|
|
|
# -------- |
|
|
|
|
|
|
|
|
|
|
|
def create_payment( |
|
|
|
async def create_payment( |
|
|
|
*, |
|
|
|
wallet_id: str, |
|
|
|
checking_id: str, |
|
|
@ -254,7 +254,7 @@ def create_payment( |
|
|
|
pending: bool = True, |
|
|
|
extra: Optional[Dict] = None, |
|
|
|
) -> Payment: |
|
|
|
g.db.execute( |
|
|
|
await g.db.execute( |
|
|
|
""" |
|
|
|
INSERT INTO apipayments |
|
|
|
(wallet, checking_id, bolt11, hash, preimage, |
|
|
@ -275,14 +275,14 @@ def create_payment( |
|
|
|
), |
|
|
|
) |
|
|
|
|
|
|
|
new_payment = get_wallet_payment(wallet_id, payment_hash) |
|
|
|
new_payment = await get_wallet_payment(wallet_id, payment_hash) |
|
|
|
assert new_payment, "Newly created payment couldn't be retrieved" |
|
|
|
|
|
|
|
return new_payment |
|
|
|
|
|
|
|
|
|
|
|
def update_payment_status(checking_id: str, pending: bool) -> None: |
|
|
|
g.db.execute( |
|
|
|
async def update_payment_status(checking_id: str, pending: bool) -> None: |
|
|
|
await g.db.execute( |
|
|
|
"UPDATE apipayments SET pending = ? WHERE checking_id = ?", |
|
|
|
( |
|
|
|
int(pending), |
|
|
@ -291,12 +291,12 @@ def update_payment_status(checking_id: str, pending: bool) -> None: |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def delete_payment(checking_id: str) -> None: |
|
|
|
g.db.execute("DELETE FROM apipayments WHERE checking_id = ?", (checking_id,)) |
|
|
|
async def delete_payment(checking_id: str) -> None: |
|
|
|
await g.db.execute("DELETE FROM apipayments WHERE checking_id = ?", (checking_id,)) |
|
|
|
|
|
|
|
|
|
|
|
def check_internal(payment_hash: str) -> Optional[str]: |
|
|
|
row = g.db.fetchone( |
|
|
|
async def check_internal(payment_hash: str) -> Optional[str]: |
|
|
|
row = await g.db.fetchone( |
|
|
|
""" |
|
|
|
SELECT checking_id FROM apipayments |
|
|
|
WHERE hash = ? AND pending AND amount > 0 |
|
|
|