This commit is contained in:
Ben Arc 2021-07-03 16:08:18 +01:00
parent 50e3d3803b
commit 0c44c0c048
19 changed files with 148 additions and 57 deletions

View File

@ -161,9 +161,9 @@ def _trim_to_bytes(barr):
def _readable_scid(short_channel_id: int) -> str:
return "{blockheight}x{transactionindex}x{outputindex}".format(
blockheight=((short_channel_id >> 40) & 0xFFFFFF),
transactionindex=((short_channel_id >> 16) & 0xFFFFFF),
outputindex=(short_channel_id & 0xFFFF),
blockheight=((short_channel_id >> 40) & 0xffffff),
transactionindex=((short_channel_id >> 16) & 0xffffff),
outputindex=(short_channel_id & 0xffff),
)

View File

@ -42,7 +42,9 @@ async def create_bleskomat(
async def get_bleskomat(bleskomat_id: str) -> Optional[Bleskomat]:
row = await db.fetchone("SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,))
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,)
)
return Bleskomat(**row) if row else None
@ -66,9 +68,12 @@ async def get_bleskomats(wallet_ids: Union[str, List[str]]) -> List[Bleskomat]:
async def update_bleskomat(bleskomat_id: str, **kwargs) -> Optional[Bleskomat]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE bleskomat.bleskomats SET {q} WHERE id = ?", (*kwargs.values(), bleskomat_id)
f"UPDATE bleskomat.bleskomats SET {q} WHERE id = ?",
(*kwargs.values(), bleskomat_id),
)
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,)
)
row = await db.fetchone("SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,))
return Bleskomat(**row) if row else None
@ -108,5 +113,7 @@ async def create_bleskomat_lnurl(
async def get_bleskomat_lnurl(secret: str) -> Optional[BleskomatLnurl]:
hash = generate_bleskomat_lnurl_hash(secret)
row = await db.fetchone("SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = ?", (hash,))
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = ?", (hash,)
)
return BleskomatLnurl(**row) if row else None

View File

@ -30,7 +30,9 @@ async def create_captcha(
async def get_captcha(captcha_id: str) -> Optional[Captcha]:
row = await db.fetchone("SELECT * FROM captcha.captchas WHERE id = ?", (captcha_id,))
row = await db.fetchone(
"SELECT * FROM captcha.captchas WHERE id = ?", (captcha_id,)
)
return Captcha.from_row(row) if row else None

View File

@ -57,16 +57,21 @@ def update_diagonalleys_product(product_id: str, **kwargs) -> Optional[Indexers]
with open_ext_db("diagonalley") as db:
db.execute(
f"UPDATE diagonalley.products SET {q} WHERE id = ?", (*kwargs.values(), product_id)
f"UPDATE diagonalley.products SET {q} WHERE id = ?",
(*kwargs.values(), product_id),
)
row = db.fetchone(
"SELECT * FROM diagonalley.products WHERE id = ?", (product_id,)
)
row = db.fetchone("SELECT * FROM diagonalley.products WHERE id = ?", (product_id,))
return get_diagonalleys_indexer(product_id)
def get_diagonalleys_product(product_id: str) -> Optional[Products]:
with open_ext_db("diagonalley") as db:
row = db.fetchone("SELECT * FROM diagonalley.products WHERE id = ?", (product_id,))
row = db.fetchone(
"SELECT * FROM diagonalley.products WHERE id = ?", (product_id,)
)
return Products(**row) if row else None
@ -131,16 +136,21 @@ def update_diagonalleys_indexer(indexer_id: str, **kwargs) -> Optional[Indexers]
with open_ext_db("diagonalley") as db:
db.execute(
f"UPDATE diagonalley.indexers SET {q} WHERE id = ?", (*kwargs.values(), indexer_id)
f"UPDATE diagonalley.indexers SET {q} WHERE id = ?",
(*kwargs.values(), indexer_id),
)
row = db.fetchone(
"SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,)
)
row = db.fetchone("SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,))
return get_diagonalleys_indexer(indexer_id)
def get_diagonalleys_indexer(indexer_id: str) -> Optional[Indexers]:
with open_ext_db("diagonalley") as db:
roww = db.fetchone("SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,))
roww = db.fetchone(
"SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,)
)
try:
x = httpx.get(roww["indexeraddress"] + "/" + roww["ratingkey"])
if x.status_code == 200:
@ -166,7 +176,9 @@ def get_diagonalleys_indexer(indexer_id: str) -> Optional[Indexers]:
except:
print("An exception occurred")
with open_ext_db("diagonalley") as db:
row = db.fetchone("SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,))
row = db.fetchone(
"SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,)
)
return Indexers(**row) if row else None
@ -285,7 +297,8 @@ def get_diagonalleys_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
),
)
rows = db.fetchall(
f"SELECT * FROM diagonalley.orders WHERE wallet IN ({q})", (*wallet_ids,)
f"SELECT * FROM diagonalley.orders WHERE wallet IN ({q})",
(*wallet_ids,),
)
return [Orders(**row) for row in rows]

View File

@ -250,7 +250,9 @@ async def api_diagonalleys_order_shipped(order_id):
order_id,
),
)
order = db.fetchone("SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,))
order = db.fetchone(
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
)
return (
jsonify(
@ -268,12 +270,16 @@ async def api_diagonalleys_order_shipped(order_id):
)
async def api_diagonalleys_stall_products(indexer_id):
with open_ext_db("diagonalley") as db:
rows = db.fetchone("SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,))
rows = db.fetchone(
"SELECT * FROM diagonalley.indexers WHERE id = ?", (indexer_id,)
)
print(rows[1])
if not rows:
return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND
products = db.fetchone("SELECT * FROM diagonalley.products WHERE wallet = ?", (rows[1],))
products = db.fetchone(
"SELECT * FROM diagonalley.products WHERE wallet = ?", (rows[1],)
)
if not products:
return jsonify({"message": "No products"}), HTTPStatus.NOT_FOUND
@ -293,7 +299,9 @@ async def api_diagonalleys_stall_products(indexer_id):
)
async def api_diagonalleys_stall_checkshipped(checking_id):
with open_ext_db("diagonalley") as db:
rows = db.fetchone("SELECT * FROM diagonalley.orders WHERE invoiceid = ?", (checking_id,))
rows = db.fetchone(
"SELECT * FROM diagonalley.orders WHERE invoiceid = ?", (checking_id,)
)
return jsonify({"shipped": rows["shipped"]}), HTTPStatus.OK

View File

@ -151,13 +151,18 @@ async def delete_event(event_id: str) -> None:
async def get_event_tickets(event_id: str, wallet_id: str) -> List[Tickets]:
rows = await db.fetchall(
"SELECT * FROM events.ticket WHERE wallet = ? AND event = ?", (wallet_id, event_id)
"SELECT * FROM events.ticket WHERE wallet = ? AND event = ?",
(wallet_id, event_id),
)
return [Tickets(**row) for row in rows]
async def reg_ticket(ticket_id: str) -> List[Tickets]:
await db.execute("UPDATE events.ticket SET registered = ? WHERE id = ?", (True, ticket_id))
await db.execute(
"UPDATE events.ticket SET registered = ? WHERE id = ?", (True, ticket_id)
)
ticket = await db.fetchone("SELECT * FROM events.ticket WHERE id = ?", (ticket_id,))
rows = await db.fetchall("SELECT * FROM events.ticket WHERE event = ?", (ticket[1],))
rows = await db.fetchall(
"SELECT * FROM events.ticket WHERE event = ?", (ticket[1],)
)
return [Tickets(**row) for row in rows]

View File

@ -13,7 +13,9 @@ async def m001_initial(db):
amount_tickets INTEGER NOT NULL,
price_per_ticket INTEGER NOT NULL,
sold INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)
@ -27,7 +29,9 @@ async def m001_initial(db):
name TEXT NOT NULL,
email TEXT NOT NULL,
registered BOOLEAN NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)
@ -45,7 +49,9 @@ async def m002_changed(db):
email TEXT NOT NULL,
registered BOOLEAN NOT NULL,
paid BOOLEAN NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)

View File

@ -20,8 +20,9 @@ async def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel):
async for payment in invoice_paid_chan:
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
if "jukebox" != payment.extra.get("tag"):
# not a jukebox invoice
return
await update_jukebox_payment(payment.payment_hash, paid=True)
if "jukebox" != payment.extra.get("tag"):
# not a jukebox invoice
return
await update_jukebox_payment(payment.payment_hash, paid=True)

View File

@ -30,7 +30,9 @@ async def create_ticket(
async def set_ticket_paid(payment_hash: str) -> Tickets:
row = await db.fetchone("SELECT * FROM lnticket.ticket WHERE id = ?", (payment_hash,))
row = await db.fetchone(
"SELECT * FROM lnticket.ticket WHERE id = ?", (payment_hash,)
)
if row[7] == False:
await db.execute(
"""
@ -124,7 +126,9 @@ async def create_form(
async def update_form(form_id: str, **kwargs) -> Forms:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(f"UPDATE lnticket.form SET {q} WHERE id = ?", (*kwargs.values(), form_id))
await db.execute(
f"UPDATE lnticket.form SET {q} WHERE id = ?", (*kwargs.values(), form_id)
)
row = await db.fetchone("SELECT * FROM lnticket.form WHERE id = ?", (form_id,))
assert row, "Newly updated form couldn't be retrieved"
return Forms(**row)

View File

@ -9,7 +9,9 @@ async def m001_initial(db):
description TEXT NOT NULL,
costpword INTEGER NOT NULL,
amountmade INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)
@ -24,7 +26,9 @@ async def m001_initial(db):
name TEXT NOT NULL,
wallet TEXT NOT NULL,
sats INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)
@ -43,12 +47,16 @@ async def m002_changed(db):
wallet TEXT NOT NULL,
sats INTEGER NOT NULL,
paid BOOLEAN NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)
for row in [list(row) for row in await db.fetchall("SELECT * FROM lnticket.tickets")]:
for row in [
list(row) for row in await db.fetchall("SELECT * FROM lnticket.tickets")
]:
usescsv = ""
for i in range(row[5]):
@ -97,7 +105,9 @@ async def m003_changed(db):
description TEXT NOT NULL,
costpword INTEGER NOT NULL,
amountmade INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)

View File

@ -22,7 +22,9 @@ async def get_shop(id: int) -> Optional[Shop]:
async def get_or_create_shop_by_wallet(wallet: str) -> Optional[Shop]:
row = await db.fetchone("SELECT * FROM offlineshop.shops WHERE wallet = ?", (wallet,))
row = await db.fetchone(
"SELECT * FROM offlineshop.shops WHERE wallet = ?", (wallet,)
)
if not row:
# create on the fly
@ -83,7 +85,9 @@ async def update_item(
async def get_item(id: int) -> Optional[Item]:
row = await db.fetchone("SELECT * FROM offlineshop.items WHERE id = ? LIMIT 1", (id,))
row = await db.fetchone(
"SELECT * FROM offlineshop.items WHERE id = ? LIMIT 1", (id,)
)
return Item(**dict(row)) if row else None

View File

@ -8,8 +8,8 @@ def hotp(key, counter, digits=6, digest="sha1"):
key = base64.b32decode(key.upper() + "=" * ((8 - len(key)) % 8))
counter = struct.pack(">Q", counter)
mac = hmac.new(key, counter, digest).digest()
offset = mac[-1] & 0x0F
binary = struct.unpack(">L", mac[offset : offset + 4])[0] & 0x7FFFFFFF
offset = mac[-1] & 0x0f
binary = struct.unpack(">L", mac[offset : offset + 4])[0] & 0x7fffffff
return str(binary)[-digits:].zfill(digits)

View File

@ -30,7 +30,9 @@ async def create_paywall(
async def get_paywall(paywall_id: str) -> Optional[Paywall]:
row = await db.fetchone("SELECT * FROM paywall.paywalls WHERE id = ?", (paywall_id,))
row = await db.fetchone(
"SELECT * FROM paywall.paywalls WHERE id = ?", (paywall_id,)
)
return Paywall.from_row(row) if row else None

View File

@ -5,13 +5,17 @@ from .models import Target
async def get_targets(source_wallet: str) -> List[Target]:
rows = await db.fetchall("SELECT * FROM splitpayments.targets WHERE source = ?", (source_wallet,))
rows = await db.fetchall(
"SELECT * FROM splitpayments.targets WHERE source = ?", (source_wallet,)
)
return [Target(**dict(row)) for row in rows]
async def set_targets(source_wallet: str, targets: List[Target]):
async with db.connect() as conn:
await conn.execute("DELETE FROM splitpayments.targets WHERE source = ?", (source_wallet,))
await conn.execute(
"DELETE FROM splitpayments.targets WHERE source = ?", (source_wallet,)
)
for target in targets:
await conn.execute(
"""

View File

@ -152,13 +152,17 @@ async def update_domain(domain_id: str, **kwargs) -> Domains:
await db.execute(
f"UPDATE subdomains.domain SET {q} WHERE id = ?", (*kwargs.values(), domain_id)
)
row = await db.fetchone("SELECT * FROM subdomains.domain WHERE id = ?", (domain_id,))
row = await db.fetchone(
"SELECT * FROM subdomains.domain WHERE id = ?", (domain_id,)
)
assert row, "Newly updated domain couldn't be retrieved"
return Domains(**row)
async def get_domain(domain_id: str) -> Optional[Domains]:
row = await db.fetchone("SELECT * FROM subdomains.domain WHERE id = ?", (domain_id,))
row = await db.fetchone(
"SELECT * FROM subdomains.domain WHERE id = ?", (domain_id,)
)
return Domains(**row) if row else None

View File

@ -13,7 +13,9 @@ async def m001_initial(db):
cost INTEGER NOT NULL,
amountmade INTEGER NOT NULL,
allowed_record_types TEXT NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)
@ -31,7 +33,9 @@ async def m001_initial(db):
duration INTEGER NOT NULL,
paid BOOLEAN NOT NULL,
record_type TEXT NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """ + db.timestamp_now + """
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
);
"""
)

View File

@ -56,7 +56,9 @@ async def get_usermanager_user(user_id: str) -> Optional[Users]:
async def get_usermanager_users(user_id: str) -> List[Users]:
rows = await db.fetchall("SELECT * FROM usermanager.users WHERE admin = ?", (user_id,))
rows = await db.fetchall(
"SELECT * FROM usermanager.users WHERE admin = ?", (user_id,)
)
return [Users(**row) for row in rows]
@ -89,17 +91,23 @@ async def create_usermanager_wallet(
async def get_usermanager_wallet(wallet_id: str) -> Optional[Wallets]:
row = await db.fetchone("SELECT * FROM usermanager.wallets WHERE id = ?", (wallet_id,))
row = await db.fetchone(
"SELECT * FROM usermanager.wallets WHERE id = ?", (wallet_id,)
)
return Wallets(**row) if row else None
async def get_usermanager_wallets(admin_id: str) -> Optional[Wallets]:
rows = await db.fetchall("SELECT * FROM usermanager.wallets WHERE admin = ?", (admin_id,))
rows = await db.fetchall(
"SELECT * FROM usermanager.wallets WHERE admin = ?", (admin_id,)
)
return [Wallets(**row) for row in rows]
async def get_usermanager_users_wallets(user_id: str) -> Optional[Wallets]:
rows = await db.fetchall("""SELECT * FROM usermanager.wallets WHERE "user" = ?""", (user_id,))
rows = await db.fetchall(
"""SELECT * FROM usermanager.wallets WHERE "user" = ?""", (user_id,)
)
return [Wallets(**row) for row in rows]

View File

@ -57,7 +57,9 @@ async def create_withdraw_link(
async def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
row = await db.fetchone("SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,))
row = await db.fetchone(
"SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,)
)
if not row:
return None
@ -97,9 +99,12 @@ async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[Withdraw
async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE withdraw.withdraw_link SET {q} WHERE id = ?", (*kwargs.values(), link_id)
f"UPDATE withdraw.withdraw_link SET {q} WHERE id = ?",
(*kwargs.values(), link_id),
)
row = await db.fetchone(
"SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,)
)
row = await db.fetchone("SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,))
return WithdrawLink.from_row(row) if row else None
@ -134,7 +139,9 @@ async def create_hash_check(
async def get_hash_check(the_hash: str, lnurl_id: str) -> Optional[HashCheck]:
rowid = await db.fetchone("SELECT * FROM withdraw.hash_check WHERE id = ?", (the_hash,))
rowid = await db.fetchone(
"SELECT * FROM withdraw.hash_check WHERE id = ?", (the_hash,)
)
rowlnurl = await db.fetchone(
"SELECT * FROM withdraw.hash_check WHERE lnurl_id = ?", (lnurl_id,)
)

View File

@ -47,7 +47,9 @@ async def m002_change_withdraw_table(db):
"""
)
for row in [list(row) for row in await db.fetchall("SELECT * FROM withdraw.withdraw_links")]:
for row in [
list(row) for row in await db.fetchall("SELECT * FROM withdraw.withdraw_links")
]:
usescsv = ""
for i in range(row[5]):