From 3d8bb8610f43bae5f37ae3382899014bcfefb615 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Fri, 14 Jul 2023 11:13:15 +0200 Subject: [PATCH 1/9] fix broken link for tag, boltcard -> boltcards (name of the extension) --- lnurl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lnurl.py b/lnurl.py index 82cbccb..e3ac7f1 100644 --- a/lnurl.py +++ b/lnurl.py @@ -125,7 +125,7 @@ async def lnurl_callback( wallet_id=card.wallet, payment_request=pr, max_sat=card.tx_limit, - extra={"tag": "boltcard", "hit": hit.id}, + extra={"tag": "boltcards", "hit": hit.id}, ) return {"status": "OK"} except Exception as exc: From 3ccebf81405d16ba5b7f5b1a56ae6dcc220004e3 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Wed, 13 Sep 2023 22:44:18 +0200 Subject: [PATCH 2/9] add support for LUD-19 in bolt cards extension --- lnurl.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lnurl.py b/lnurl.py index e3ac7f1..35aadc2 100644 --- a/lnurl.py +++ b/lnurl.py @@ -75,14 +75,23 @@ async def api_scan(p, c, request: Request, external_id: str): if hits_amount > card.daily_limit: return {"status": "ERROR", "reason": "Max daily limit spent."} hit = await create_hit(card.id, ip, agent, card.counter, ctr_int) - lnurlpay = lnurl_encode(str(request.url_for("boltcards.lnurlp_response", hit_id=hit.id))) + + #the raw lnurl + lnurlpay_raw = str(request.url_for("boltcards.lnurlp_response", hit_id=hit.id)) + #bech32 encoded lnurl + lnurlpay_bech32 = lnurl_encode(lnurlpay_raw) + #create a lud17 lnurlp to support lud19, add to payLink field of the withdrawRequest + lnurlpay_nonbech32_lud17 = lnurlpay_raw.replace("https://", "lnurlp://").replace("http://","lnurlp://") + + return { "tag": "withdrawRequest", "callback": str(request.url_for("boltcards.lnurl_callback", hit_id=hit.id)), "k1": hit.id, "minWithdrawable": 1 * 1000, "maxWithdrawable": card.tx_limit * 1000, - "defaultDescription": f"Boltcard (refund address lnurl://{lnurlpay})", + "defaultDescription": f"Boltcard (refund address lnurl://{lnurlpay_bech32})", + "payLink": lnurlpay_nonbech32_lud17, #LUD-19 compatibility } From d5905cd8bb41c03d346540ae26b043af2079bf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dvo=C5=99=C3=A1k?= <80678558+gorrdy@users.noreply.github.com> Date: Thu, 14 Sep 2023 05:12:03 +0200 Subject: [PATCH 3/9] Apply suggestions from code review Co-authored-by: Pavol Rusnak --- lnurl.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lnurl.py b/lnurl.py index 35aadc2..0ddb429 100644 --- a/lnurl.py +++ b/lnurl.py @@ -76,14 +76,13 @@ async def api_scan(p, c, request: Request, external_id: str): return {"status": "ERROR", "reason": "Max daily limit spent."} hit = await create_hit(card.id, ip, agent, card.counter, ctr_int) - #the raw lnurl + # the raw lnurl lnurlpay_raw = str(request.url_for("boltcards.lnurlp_response", hit_id=hit.id)) - #bech32 encoded lnurl + # bech32 encoded lnurl lnurlpay_bech32 = lnurl_encode(lnurlpay_raw) - #create a lud17 lnurlp to support lud19, add to payLink field of the withdrawRequest + # create a lud17 lnurlp to support lud19, add to payLink field of the withdrawRequest lnurlpay_nonbech32_lud17 = lnurlpay_raw.replace("https://", "lnurlp://").replace("http://","lnurlp://") - return { "tag": "withdrawRequest", "callback": str(request.url_for("boltcards.lnurl_callback", hit_id=hit.id)), From b4efd61af6589502e68ed02af9dd9acbd1d1f496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dvo=C5=99=C3=A1k?= <80678558+gorrdy@users.noreply.github.com> Date: Thu, 14 Sep 2023 05:12:35 +0200 Subject: [PATCH 4/9] Apply suggestions from code review Co-authored-by: Pavol Rusnak --- lnurl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lnurl.py b/lnurl.py index 0ddb429..4b39374 100644 --- a/lnurl.py +++ b/lnurl.py @@ -90,7 +90,7 @@ async def api_scan(p, c, request: Request, external_id: str): "minWithdrawable": 1 * 1000, "maxWithdrawable": card.tx_limit * 1000, "defaultDescription": f"Boltcard (refund address lnurl://{lnurlpay_bech32})", - "payLink": lnurlpay_nonbech32_lud17, #LUD-19 compatibility + "payLink": lnurlpay_nonbech32_lud17, # LUD-19 compatibility } From 136fb51a07fcb78b52c7f7e656204814ac58ff85 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Wed, 4 Oct 2023 15:38:40 +0200 Subject: [PATCH 5/9] do not check the duplicate UID - not necessary, more pain than gain --- crud.py | 4 ++-- migrations.py | 7 +++++++ views_api.py | 13 ------------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/crud.py b/crud.py index 0a678e7..b4f5c61 100644 --- a/crud.py +++ b/crud.py @@ -87,7 +87,7 @@ 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.upper(),) @@ -98,7 +98,7 @@ async def get_card_by_uid(card_uid: str) -> Optional[Card]: card = dict(**row) return Card.parse_obj(card) - +""" async def get_card_by_external_id(external_id: str) -> Optional[Card]: row = await db.fetchone( diff --git a/migrations.py b/migrations.py index 43d5bb0..3920774 100644 --- a/migrations.py +++ b/migrations.py @@ -55,3 +55,10 @@ async def m001_initial(db): ); """ ) + +async def m002_remove_constraint_unique_uid(db): + """ + Do not check the duplicate UID so remove the constraint from DB. + """ + + await db.execute("ALTER TABLE boltcards.cards DROP CONSTRAINT cards_uid_key;") diff --git a/views_api.py b/views_api.py index b710518..1aa2490 100644 --- a/views_api.py +++ b/views_api.py @@ -12,7 +12,6 @@ delete_card, enable_disable_card, get_card, - get_card_by_uid, get_cards, get_hits, get_refunds, @@ -82,12 +81,6 @@ async def api_card_update( raise HTTPException( detail="Not your card.", status_code=HTTPStatus.FORBIDDEN ) - checkUid = await get_card_by_uid(data.uid) - if checkUid and checkUid.id != card_id: - raise HTTPException( - detail="UID already registered. Delete registered card and try again.", - status_code=HTTPStatus.BAD_REQUEST, - ) card = await update_card(card_id, **data.dict()) assert card, "update_card should always return a card" return card @@ -102,12 +95,6 @@ async def api_card_create( data: CreateCardData, wallet: WalletTypeInfo = Depends(require_admin_key), ) -> Card: - checkUid = await get_card_by_uid(data.uid) - if checkUid: - raise HTTPException( - detail="UID already registered. Delete registered card and try again.", - status_code=HTTPStatus.BAD_REQUEST, - ) card = await create_card(wallet_id=wallet.wallet.id, data=data) assert card, "create_card should always return a card" return card From b10a183bec67b25599b3125a25b1e3d20a98c4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dvo=C5=99=C3=A1k?= <80678558+gorrdy@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:52:34 +0200 Subject: [PATCH 6/9] Update migrations.py --- migrations.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/migrations.py b/migrations.py index 3920774..385858d 100644 --- a/migrations.py +++ b/migrations.py @@ -60,5 +60,31 @@ async def m002_remove_constraint_unique_uid(db): """ Do not check the duplicate UID so remove the constraint from DB. """ - - await db.execute("ALTER TABLE boltcards.cards DROP CONSTRAINT cards_uid_key;") + await db.execute( + """ + CREATE TABLE cardsnew ( + id TEXT PRIMARY KEY UNIQUE, + wallet TEXT NOT NULL, + card_name TEXT NOT NULL, + uid TEXT NOT NULL, + external_id TEXT NOT NULL UNIQUE, + counter INT NOT NULL DEFAULT 0, + tx_limit TEXT NOT NULL, + daily_limit TEXT NOT NULL, + enable BOOL NOT NULL, + k0 TEXT NOT NULL DEFAULT '00000000000000000000000000000000', + k1 TEXT NOT NULL DEFAULT '00000000000000000000000000000000', + k2 TEXT NOT NULL DEFAULT '00000000000000000000000000000000', + prev_k0 TEXT NOT NULL DEFAULT '00000000000000000000000000000000', + prev_k1 TEXT NOT NULL DEFAULT '00000000000000000000000000000000', + prev_k2 TEXT NOT NULL DEFAULT '00000000000000000000000000000000', + otp TEXT NOT NULL DEFAULT '', + time TIMESTAMP NOT NULL DEFAULT """ + + db.timestamp_now + + """ + ); + INSERT INTO boltcards.cardsnew SELECT * FROM boltcards.cards; + DROP TABLE boltcards.cards; + ALTER TABLE boltcards.cardsnew RENAME TO boltcards.cards; + """ + ) From 0ab06f5d4291d6fbe6a649fa950a9c004245be2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dvo=C5=99=C3=A1k?= <80678558+gorrdy@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:58:32 +0200 Subject: [PATCH 7/9] Update migrations.py --- migrations.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/migrations.py b/migrations.py index 385858d..ca96acd 100644 --- a/migrations.py +++ b/migrations.py @@ -83,8 +83,24 @@ async def m002_remove_constraint_unique_uid(db): + db.timestamp_now + """ ); + """ + ) + await db.execute( + """ INSERT INTO boltcards.cardsnew SELECT * FROM boltcards.cards; + """ + ) + await db.execute( + """ DROP TABLE boltcards.cards; + """ + ) + await db.execute( + """ ALTER TABLE boltcards.cardsnew RENAME TO boltcards.cards; """ ) + + + + From 16860b1c6b5c2ec96b62d3ee0a7e2e57e09f8025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dvo=C5=99=C3=A1k?= <80678558+gorrdy@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:05:36 +0200 Subject: [PATCH 8/9] Update migrations.py --- migrations.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/migrations.py b/migrations.py index ca96acd..3d17b02 100644 --- a/migrations.py +++ b/migrations.py @@ -85,21 +85,9 @@ async def m002_remove_constraint_unique_uid(db): ); """ ) - await db.execute( - """ - INSERT INTO boltcards.cardsnew SELECT * FROM boltcards.cards; - """ - ) - await db.execute( - """ - DROP TABLE boltcards.cards; - """ - ) - await db.execute( - """ - ALTER TABLE boltcards.cardsnew RENAME TO boltcards.cards; - """ - ) + await db.execute("INSERT INTO boltcards.cardsnew SELECT * FROM boltcards.cards;") + await db.execute("DROP TABLE boltcards.cards;") + await db.execute("ALTER TABLE boltcards.cardsnew RENAME TO boltcards.cards;") From 1a40042db21a451a7679f87796bfca34004f9f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dvo=C5=99=C3=A1k?= <80678558+gorrdy@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:08:13 +0200 Subject: [PATCH 9/9] Update migrations.py --- migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations.py b/migrations.py index 3d17b02..e40f865 100644 --- a/migrations.py +++ b/migrations.py @@ -87,7 +87,7 @@ async def m002_remove_constraint_unique_uid(db): ) await db.execute("INSERT INTO boltcards.cardsnew SELECT * FROM boltcards.cards;") await db.execute("DROP TABLE boltcards.cards;") - await db.execute("ALTER TABLE boltcards.cardsnew RENAME TO boltcards.cards;") + await db.execute("ALTER TABLE boltcards.cardsnew RENAME TO cards;")