From 4242a8202924d8de0f23fef83d233bda7f52a698 Mon Sep 17 00:00:00 2001 From: Lee Salminen Date: Sun, 21 Aug 2022 08:58:38 -0600 Subject: [PATCH] Add a secondary route with the card_uid appended to it. --- lnbits/extensions/boltcards/crud.py | 8 +++++++ lnbits/extensions/boltcards/views_api.py | 29 ++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lnbits/extensions/boltcards/crud.py b/lnbits/extensions/boltcards/crud.py index 5affe312..2e19c079 100644 --- a/lnbits/extensions/boltcards/crud.py +++ b/lnbits/extensions/boltcards/crud.py @@ -82,6 +82,14 @@ async def get_card(card_id: str) -> Optional[Card]: return Card.parse_obj(card) +async def get_card_by_uid(card_uid: str) -> Optional[Card]: + row = await db.fetchone("SELECT * FROM boltcards.cards WHERE uid = ?", (card_uid,)) + if not row: + return None + + card = dict(**row) + + return Card.parse_obj(card) async def get_card_by_otp(otp: str) -> Optional[Card]: row = await db.fetchone("SELECT * FROM boltcards.cards WHERE otp = ?", (otp,)) diff --git a/lnbits/extensions/boltcards/views_api.py b/lnbits/extensions/boltcards/views_api.py index 151aebcb..7c3a2e03 100644 --- a/lnbits/extensions/boltcards/views_api.py +++ b/lnbits/extensions/boltcards/views_api.py @@ -25,6 +25,7 @@ from .crud import ( get_all_cards, get_card, get_card_by_otp, + get_card_by_uid, get_cards, get_hits, update_card, @@ -131,25 +132,29 @@ async def api_hits( # /boltcards/api/v1/scan?p=00000000000000000000000000000000&c=0000000000000000 @boltcards_ext.get("/api/v1/scan") -async def api_scane(p, c, request: Request): +@boltcards_ext.get("/api/v1/scan/{card_uid}") +async def api_scane(p, c, card_uid: str = None, request: Request): # some wallets send everything as lower case, no bueno p = p.upper() c = c.upper() card = None counter = b"" - # since this route is common to all cards I don't know whitch 'meta key' to use - # so I try one by one until decrypted uid matches - for cand in await get_all_cards(): - if cand.k1: - try: - card_uid, counter = decryptSUN(bytes.fromhex(p), bytes.fromhex(cand.k1)) + if not card_uid: + # since this route is common to all cards I don't know whitch 'meta key' to use + # so I try one by one until decrypted uid matches + for cand in await get_all_cards(): + if cand.k1: + try: + card_uid, counter = decryptSUN(bytes.fromhex(p), bytes.fromhex(cand.k1)) - if card_uid.hex().upper() == cand.uid.upper(): - card = cand - break - except: - continue + if card_uid.hex().upper() == cand.uid.upper(): + card = cand + break + except: + continue + else: + card = await get_card_by_uid(card_uid) if card == None: return {"status": "ERROR", "reason": "Unknown card."}