From 4e63662f42fc87a77c61cdcdbe92ae664f586349 Mon Sep 17 00:00:00 2001 From: Lee Salminen Date: Mon, 29 Aug 2022 14:51:18 -0600 Subject: [PATCH] add external_id field for external use --- lnbits/extensions/boltcards/README.md | 7 ++++--- lnbits/extensions/boltcards/crud.py | 18 +++++++++++++++++- lnbits/extensions/boltcards/lnurl.py | 8 ++++---- lnbits/extensions/boltcards/migrations.py | 1 + lnbits/extensions/boltcards/models.py | 1 + lnbits/extensions/boltcards/static/js/index.js | 1 + .../boltcards/templates/boltcards/index.html | 3 ++- 7 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lnbits/extensions/boltcards/README.md b/lnbits/extensions/boltcards/README.md index 896fb54b..ca40f44f 100644 --- a/lnbits/extensions/boltcards/README.md +++ b/lnbits/extensions/boltcards/README.md @@ -22,7 +22,8 @@ The key #00, K0 (also know as auth key) is skipped to be use as authentification So far, regarding the keys, the app can only write a new key set on an empty card (with zero keys). **When you write non zero (and 'non debug') keys, they can't be rewrite with this app.** You have to do it on your computer. - Read the card with the app. Note UID so you can fill it in the extension later. -- Write the link on the card. It shoud be like `YOUR_LNBITS_DOMAIN/boltcards/api/v1/scan/{card_uid}` +- Write the link on the card. It shoud be like `YOUR_LNBITS_DOMAIN/boltcards/api/v1/scan/{external_id}` + - `{external_id}` should be replaced with the External ID found in the LNBits dialog. - Add new card in the extension. - Set a max sats per transaction. Any transaction greater than this amount will be rejected. @@ -43,7 +44,7 @@ So far, regarding the keys, the app can only write a new key set on an empty car Follow the guide. -The URI should be `lnurlw://YOUR-DOMAIN.COM/boltcards/api/v1/scan/{YOUR_card_uid}?p=00000000000000000000000000000000&c=0000000000000000` +The URI should be `lnurlw://YOUR-DOMAIN.COM/boltcards/api/v1/scan/{YOUR_card_external_id}?p=00000000000000000000000000000000&c=0000000000000000` Then fill up the card parameters in the extension. Card Auth key (K0) can be omitted. Initical counter can be 0. @@ -52,7 +53,7 @@ Then fill up the card parameters in the extension. Card Auth key (K0) can be omi - In the TagWriter app tap Write tags - New Data Set > Link - Set URI type to Custom URL -- URL should look like lnurlw://YOUR_LNBITS_DOMAIN/boltcards/api/v1/scan/{YOUR_card_uid}?p=00000000000000000000000000000000&c=0000000000000000 +- URL should look like lnurlw://YOUR_LNBITS_DOMAIN/boltcards/api/v1/scan/{YOUR_card_external_id}?p=00000000000000000000000000000000&c=0000000000000000 - click Configure mirroring options - Select Card Type NTAG 424 DNA - Check Enable SDM Mirroring diff --git a/lnbits/extensions/boltcards/crud.py b/lnbits/extensions/boltcards/crud.py index be9b2961..63c04d73 100644 --- a/lnbits/extensions/boltcards/crud.py +++ b/lnbits/extensions/boltcards/crud.py @@ -9,11 +9,14 @@ from .models import Card, CreateCardData, Hit, Refund async def create_card(data: CreateCardData, wallet_id: str) -> Card: card_id = urlsafe_short_hash().upper() + extenal_id = urlsafe_short_hash().lower() + await db.execute( """ INSERT INTO boltcards.cards ( id, uid, + external_id, wallet, card_name, counter, @@ -25,11 +28,12 @@ async def create_card(data: CreateCardData, wallet_id: str) -> Card: k2, otp ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( card_id, data.uid.upper(), + extenal_id, wallet_id, data.card_name, data.counter, @@ -95,6 +99,18 @@ async def get_card_by_uid(card_uid: str) -> Optional[Card]: return Card.parse_obj(card) +async def get_card_by_external_id(external_id: str) -> Optional[Card]: + row = await db.fetchone( + "SELECT * FROM boltcards.cards WHERE external_id = ?", (external_id.lower(),) + ) + 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,)) if not row: diff --git a/lnbits/extensions/boltcards/lnurl.py b/lnbits/extensions/boltcards/lnurl.py index 8dba51bf..64efdd2d 100644 --- a/lnbits/extensions/boltcards/lnurl.py +++ b/lnbits/extensions/boltcards/lnurl.py @@ -26,8 +26,8 @@ from . import boltcards_ext from .crud import ( create_hit, get_card, + get_card_by_external_id, get_card_by_otp, - get_card_by_uid, get_hit, get_hits_today, spend_hit, @@ -41,14 +41,14 @@ from .nxp424 import decryptSUN, getSunMAC ###############LNURLWITHDRAW################# # /boltcards/api/v1/scan?p=00000000000000000000000000000000&c=0000000000000000 -@boltcards_ext.get("/api/v1/scan/{card_uid}") -async def api_scan(p, c, request: Request, card_uid: str = None): +@boltcards_ext.get("/api/v1/scan/{external_id}") +async def api_scan(p, c, request: Request, external_id: str = None): # some wallets send everything as lower case, no bueno p = p.upper() c = c.upper() card = None counter = b"" - card = await get_card_by_uid(card_uid) + card = await get_card_by_external_id(external_id) if not card: return {"status": "ERROR", "reason": "No card."} if not card.enable: diff --git a/lnbits/extensions/boltcards/migrations.py b/lnbits/extensions/boltcards/migrations.py index 5be3d08f..08126013 100644 --- a/lnbits/extensions/boltcards/migrations.py +++ b/lnbits/extensions/boltcards/migrations.py @@ -9,6 +9,7 @@ async def m001_initial(db): wallet TEXT NOT NULL, card_name TEXT NOT NULL, uid TEXT NOT NULL UNIQUE, + external_id TEXT NOT NULL UNIQUE, counter INT NOT NULL DEFAULT 0, tx_limit TEXT NOT NULL, daily_limit TEXT NOT NULL, diff --git a/lnbits/extensions/boltcards/models.py b/lnbits/extensions/boltcards/models.py index 21096640..47ca1df0 100644 --- a/lnbits/extensions/boltcards/models.py +++ b/lnbits/extensions/boltcards/models.py @@ -18,6 +18,7 @@ class Card(BaseModel): wallet: str card_name: str uid: str + external_id: str counter: int tx_limit: int daily_limit: int diff --git a/lnbits/extensions/boltcards/static/js/index.js b/lnbits/extensions/boltcards/static/js/index.js index c23a2583..a2376de3 100644 --- a/lnbits/extensions/boltcards/static/js/index.js +++ b/lnbits/extensions/boltcards/static/js/index.js @@ -259,6 +259,7 @@ new Vue({ link: window.location.origin + '/boltcards/api/v1/auth?a=' + card.otp, name: card.card_name, uid: card.uid, + external_id: card.external_id, k0: card.k0, k1: card.k1, k2: card.k2 diff --git a/lnbits/extensions/boltcards/templates/boltcards/index.html b/lnbits/extensions/boltcards/templates/boltcards/index.html index 1338b859..96d9c9d0 100644 --- a/lnbits/extensions/boltcards/templates/boltcards/index.html +++ b/lnbits/extensions/boltcards/templates/boltcards/index.html @@ -376,6 +376,7 @@

Name: {{ qrCodeDialog.data.name }}
UID: {{ qrCodeDialog.data.uid }}
+ External ID: {{ qrCodeDialog.data.external_id }}
Lock key: {{ qrCodeDialog.data.k0 }}
Meta key: {{ qrCodeDialog.data.k1 }}
File key: {{ qrCodeDialog.data.k2 }}
@@ -386,7 +387,7 @@ unelevated outline color="grey" - @click="copyText(lnurlLink + qrCodeDialog.data.uid)" + @click="copyText(lnurlLink + qrCodeDialog.data.external_id)" label="Base url (LNURL://)" >