From 26b3599325d3193f84f2a5dc8bca463ebacc32f9 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 11 Aug 2021 22:19:27 -0300 Subject: [PATCH] fix insertion bugs/incompatibilities with lastrowid stuff. --- lnbits/extensions/livestream/crud.py | 16 ++++++++++++---- lnbits/extensions/lnurlp/crud.py | 17 ++++++++--------- lnbits/extensions/lnurlp/models.py | 4 ++-- lnbits/extensions/offlineshop/crud.py | 14 +++++++++++--- lnbits/extensions/streamalerts/crud.py | 26 +++++++++++++++++++++----- lnbits/extensions/watchonly/crud.py | 1 - lnbits/lnurl.py | 11 ++++++++++- 7 files changed, 64 insertions(+), 25 deletions(-) diff --git a/lnbits/extensions/livestream/crud.py b/lnbits/extensions/livestream/crud.py index 360b2ceaa..ef596b270 100644 --- a/lnbits/extensions/livestream/crud.py +++ b/lnbits/extensions/livestream/crud.py @@ -1,20 +1,28 @@ from typing import List, Optional from lnbits.core.crud import create_account, create_wallet - +from lnbits.db import SQLITE from . import db from .models import Livestream, Track, Producer async def create_livestream(*, wallet_id: str) -> int: - result = await db.execute( - """ + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone + + result = await (method)( + f""" INSERT INTO livestream.livestreams (wallet) VALUES (?) + {returning} """, (wallet_id,), ) - return result._result_proxy.lastrowid + + if db.type == SQLITE: + return result._result_proxy.lastrowid + else: + return result[0] async def get_livestream(ls_id: int) -> Optional[Livestream]: diff --git a/lnbits/extensions/lnurlp/crud.py b/lnbits/extensions/lnurlp/crud.py index 98c9d540b..b1744a64e 100644 --- a/lnbits/extensions/lnurlp/crud.py +++ b/lnbits/extensions/lnurlp/crud.py @@ -1,5 +1,6 @@ from typing import List, Optional, Union +from lnbits.db import SQLITE from . import db from .models import PayLink @@ -17,12 +18,10 @@ async def create_pay_link( success_url: Optional[str] = None, ) -> PayLink: - if db.type == "POSTGRES" or db.type == "COCKROACH": - returning = "RETURNING id" - else: - returning = "" + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone - result = await db.fetchone( + result = await (method)( f""" INSERT INTO lnurlp.pay_links ( wallet, @@ -52,11 +51,11 @@ async def create_pay_link( currency, ), ) - if db.type == "POSTGRES" or db.type == "COCKROACH": - link_id = result[0] - else: + if db.type == SQLITE: link_id = result._result_proxy.lastrowid - + else: + link_id = result[0] + link = await get_pay_link(link_id) assert link, "Newly created link couldn't be retrieved" return link diff --git a/lnbits/extensions/lnurlp/models.py b/lnbits/extensions/lnurlp/models.py index e02d7ea6c..c08dca03b 100644 --- a/lnbits/extensions/lnurlp/models.py +++ b/lnbits/extensions/lnurlp/models.py @@ -3,7 +3,7 @@ from urllib.parse import urlparse, urlunparse, parse_qs, urlencode, ParseResult from quart import url_for from typing import NamedTuple, Optional, Dict from sqlite3 import Row -from lnurl import Lnurl, encode as lnurl_encode # type: ignore +from lnbits.lnurl import encode as lnurl_encode # type: ignore from lnurl.types import LnurlPayMetadata # type: ignore @@ -27,7 +27,7 @@ class PayLink(NamedTuple): return cls(**data) @property - def lnurl(self) -> Lnurl: + def lnurl(self) -> str: url = url_for("lnurlp.api_lnurl_response", link_id=self.id, _external=True) return lnurl_encode(url) diff --git a/lnbits/extensions/offlineshop/crud.py b/lnbits/extensions/offlineshop/crud.py index 0168536b8..2ee931cd7 100644 --- a/lnbits/extensions/offlineshop/crud.py +++ b/lnbits/extensions/offlineshop/crud.py @@ -1,19 +1,27 @@ from typing import List, Optional +from lnbits.db import SQLITE from . import db from .wordlists import animals from .models import Shop, Item async def create_shop(*, wallet_id: str) -> int: - result = await db.execute( - """ + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone + + result = await (method)( + f""" INSERT INTO offlineshop.shops (wallet, wordlist, method) VALUES (?, ?, 'wordlist') + {returning} """, (wallet_id, "\n".join(animals)), ) - return result._result_proxy.lastrowid + if db.type == SQLITE: + return result._result_proxy.lastrowid + else: + return result[0] async def get_shop(id: int) -> Optional[Shop]: diff --git a/lnbits/extensions/streamalerts/crud.py b/lnbits/extensions/streamalerts/crud.py index 2efc2d679..cbd9d3b04 100644 --- a/lnbits/extensions/streamalerts/crud.py +++ b/lnbits/extensions/streamalerts/crud.py @@ -1,7 +1,7 @@ from . import db from .models import Donation, Service -from ..satspay.crud import delete_charge +from ..satspay.crud import delete_charge # type: ignore import httpx @@ -10,6 +10,7 @@ from quart import jsonify from typing import Optional +from lnbits.db import SQLITE from lnbits.helpers import urlsafe_short_hash from lnbits.core.crud import get_wallet @@ -69,7 +70,10 @@ async def create_donation( """, (id, wallet, name, message, cur_code, sats, amount, service, posted), ) - return await get_donation(id) + + donation = await get_donation(id) + assert donation, "Newly created donation couldn't be retrieved" + return donation async def post_donation(donation_id: str) -> tuple: @@ -86,6 +90,8 @@ async def post_donation(donation_id: str) -> tuple: HTTPStatus.BAD_REQUEST, ) service = await get_service(donation.service) + assert service, "Couldn't fetch service to donate to" + if service.servicename == "Streamlabs": url = "https://streamlabs.com/api/v1.0/donations" data = { @@ -123,8 +129,12 @@ async def create_service( onchain: str = None, ) -> Service: """Create a new Service""" - result = await db.execute( - """ + + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone + + result = await (method)( + f""" INSERT INTO streamalerts.Services ( twitchuser, client_id, @@ -136,6 +146,7 @@ async def create_service( onchain ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) + {returning} """, ( twitchuser, @@ -148,8 +159,13 @@ async def create_service( onchain, ), ) - service_id = result._result_proxy.lastrowid + if db.type == SQLITE: + service_id = result._result_proxy.lastrowid + else: + service_id = result[0] + service = await get_service(service_id) + assert service return service diff --git a/lnbits/extensions/watchonly/crud.py b/lnbits/extensions/watchonly/crud.py index 1531b174a..bd301eb44 100644 --- a/lnbits/extensions/watchonly/crud.py +++ b/lnbits/extensions/watchonly/crud.py @@ -93,7 +93,6 @@ async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Walle # address_no is -1 so fresh address on empty wallet can get address with index 0 (wallet_id, user, masterpub, title, -1, 0), ) - # weallet_id = db.cursor.lastrowid return await get_watch_wallet(wallet_id) diff --git a/lnbits/lnurl.py b/lnbits/lnurl.py index 9dc06b375..4c285da17 100644 --- a/lnbits/lnurl.py +++ b/lnbits/lnurl.py @@ -1,7 +1,16 @@ -from bech32 import bech32_decode, convertbits +from bech32 import bech32_decode, bech32_encode, convertbits def decode(lnurl: str) -> str: hrp, data = bech32_decode(lnurl) + assert data bech32_data = convertbits(data, 5, 8, False) + assert bech32_data return bytes(bech32_data).decode("utf-8") + + +def encode(url: str) -> str: + bech32_data = convertbits(url.encode("utf-8"), 8, 5, True) + assert bech32_data + lnurl = bech32_encode("lnurl", bech32_data) + return lnurl.upper()