Repository: lnbits/lnbits

Issue:
[Feature request] LNURLp links should have a random ID, not sequential
**Problem**
LNURLp links look like this right now: https://legend.lnbits.com/lnurlp/**123456** and the number goes up with every new link. 

**Solution**
Use a UUID instead of a counter.


Discussion:

Resolving patch:
diff --git a/lnbits/extensions/lnurlp/crud.py b/lnbits/extensions/lnurlp/crud.py
--- a/lnbits/extensions/lnurlp/crud.py
+++ b/lnbits/extensions/lnurlp/crud.py
@@ -1,19 +1,18 @@
 from typing import List, Optional, Union
 
-from lnbits.db import SQLITE
+from lnbits.helpers import urlsafe_short_hash
 
 from . import db
 from .models import CreatePayLinkData, PayLink
 
 
 async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
+    link_id = urlsafe_short_hash()
 
-    returning = "" if db.type == SQLITE else "RETURNING ID"
-    method = db.execute if db.type == SQLITE else db.fetchone
-
-    result = await (method)(
+    result = await db.execute(
         f"""
         INSERT INTO lnurlp.pay_links (
+            id,
             wallet,
             description,
             min,
@@ -29,10 +28,11 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
             currency,
             fiat_base_multiplier
         )
-        VALUES (?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?)
+        VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?)
         {returning}
         """,
         (
+            link_id,
             wallet_id,
             data.description,
             data.min,
@@ -47,10 +47,6 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
             data.fiat_base_multiplier,
         ),
     )
-    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"
diff --git a/lnbits/extensions/lnurlp/migrations.py b/lnbits/extensions/lnurlp/migrations.py
--- a/lnbits/extensions/lnurlp/migrations.py
+++ b/lnbits/extensions/lnurlp/migrations.py
@@ -68,3 +68,76 @@ async def m005_webhook_headers_and_body(db):
     """
     await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN webhook_headers TEXT;")
     await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN webhook_body TEXT;")
+
+
+async def m006_redux(db):
+    """
+    Add UUID ID's to links and migrates existing data
+    """
+    await db.execute("ALTER TABLE lnurlp.pay_links RENAME TO pay_links_old")
+    await db.execute(
+        f"""
+        CREATE TABLE lnurlp.pay_links (
+            id TEXT PRIMARY KEY,
+            wallet TEXT NOT NULL,
+            description TEXT NOT NULL,
+            min INTEGER NOT NULL,
+            max INTEGER,
+            currency TEXT,
+            fiat_base_multiplier INTEGER DEFAULT 1,
+            served_meta INTEGER NOT NULL,
+            served_pr INTEGER NOT NULL,
+            webhook_url TEXT,
+            success_text TEXT,
+            success_url TEXT,
+            comment_chars INTEGER DEFAULT 0,
+            webhook_headers TEXT,
+            webhook_body TEXT
+            );
+        """
+    )
+
+    for row in [
+        list(row) for row in await db.fetchall("SELECT * FROM lnurlp.pay_links_old")
+    ]:
+        await db.execute(
+            """
+            INSERT INTO lnurlp.pay_links (
+                id,
+                wallet,
+                description,
+                min,
+                served_meta,
+                served_pr,
+                webhook_url,
+                success_text,
+                success_url,
+                currency,
+                comment_chars,
+                max,
+                fiat_base_multiplier,
+                webhook_headers,
+                webhook_body
+            )
+            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+            """,
+            (
+                row[0],
+                row[1],
+                row[2],
+                row[3],
+                row[4],
+                row[5],
+                row[6],
+                row[7],
+                row[8],
+                row[9],
+                row[10],
+                row[11],
+                row[12],
+                row[13],
+                row[14],
+            ),
+        )
+
+    await db.execute("DROP TABLE lnurlp.pay_links_old")
diff --git a/lnbits/extensions/lnurlp/models.py b/lnbits/extensions/lnurlp/models.py
--- a/lnbits/extensions/lnurlp/models.py
+++ b/lnbits/extensions/lnurlp/models.py
@@ -26,7 +26,7 @@ class CreatePayLinkData(BaseModel):
 
 
 class PayLink(BaseModel):
-    id: int
+    id: str
     wallet: str
     description: str
     min: float


Test patch: