From 5a0b217d635937026fc5767241bc6466d27750ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Thu, 26 Jan 2023 10:43:12 +0100 Subject: [PATCH 1/4] TEST: LNbits as fundingsource in regtest (#1402) * lnbits funding source * add create_fake_user to workflow * change quotes in workflow ymal * not use interactive docker exec * update tools/create_fake_admin to not use lnbits imports * formatting Co-authored-by: calle <93376500+callebtc@users.noreply.github.com> --- .dockerignore | 1 - .github/workflows/regtest.yml | 45 +++++++++++++++++++- Dockerfile | 2 +- tools/create_fake_admin.py | 80 +++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 tools/create_fake_admin.py diff --git a/.dockerignore b/.dockerignore index 005f64cc..edbbf189 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,7 +4,6 @@ docker docs tests venv -tools lnbits/static/css/* lnbits/static/bundle.js diff --git a/.github/workflows/regtest.yml b/.github/workflows/regtest.yml index 57b7e1f4..eb2630db 100644 --- a/.github/workflows/regtest.yml +++ b/.github/workflows/regtest.yml @@ -134,6 +134,49 @@ jobs: uses: codecov/codecov-action@v3 with: file: ./coverage.xml + LNbitsWallet: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9"] + poetry-version: ["1.3.1"] + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Set up Poetry ${{ matrix.poetry-version }} + uses: abatilo/actions-poetry@v2 + with: + poetry-version: ${{ matrix.poetry-version }} + - name: Setup Regtest + run: | + docker build -t lnbitsdocker/lnbits-legend . + git clone https://github.com/lnbits/legend-regtest-enviroment.git docker + cd docker + chmod +x ./tests + ./tests + sudo chmod -R a+rwx . + docker exec lnbits-legend-lnbits-1 /bin/bash -c "poetry run python tools/create_fake_admin.py" + - name: Install dependencies + run: | + poetry install + - name: Run tests + env: + PYTHONUNBUFFERED: 1 + PORT: 5123 + LNBITS_DATA_FOLDER: ./data + LNBITS_BACKEND_WALLET_CLASS: LNbitsWallet + LNBITS_ENDPOINT: http://localhost:5001 + LNBITS_KEY: "d08a3313322a4514af75d488bcc27eee" + run: | + sudo chmod -R a+rwx . && rm -rf ./data && mkdir -p ./data + make test-real-wallet + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + file: ./coverage.xml EclairWallet: runs-on: ubuntu-latest strategy: @@ -176,4 +219,4 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: - file: ./coverage.xml + file: ./coverage.xml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index cc3a14bd..a7a3be9f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN mkdir -p lnbits/data COPY . . RUN poetry config virtualenvs.create false -RUN poetry install --only main --no-root +RUN poetry install --only main RUN poetry run python build.py ENV LNBITS_PORT="5000" diff --git a/tools/create_fake_admin.py b/tools/create_fake_admin.py new file mode 100644 index 00000000..9edb0529 --- /dev/null +++ b/tools/create_fake_admin.py @@ -0,0 +1,80 @@ +# Python script to create a fake admin user for sqlite3, +# for regtest setup as LNbits funding source + +import os +import sqlite3 +import sys +import time +from uuid import uuid4 + +import shortuuid + +adminkey = "d08a3313322a4514af75d488bcc27eee" +sqfolder = "./data" + +if not sqfolder or not os.path.isdir(sqfolder): + print("missing LNBITS_DATA_FOLDER") + sys.exit(1) + +file = os.path.join(sqfolder, "database.sqlite3") +conn = sqlite3.connect(file) +cursor = conn.cursor() + +old_account = cursor.execute( + "SELECT * FROM accounts WHERE id = ?", (adminkey,) +).fetchone() +if old_account: + print("fake admin does already exist") + sys.exit(1) + + +cursor.execute("INSERT INTO accounts (id) VALUES (?)", (adminkey,)) + +wallet_id = uuid4().hex +cursor.execute( + """ + INSERT INTO wallets (id, name, "user", adminkey, inkey) + VALUES (?, ?, ?, ?, ?) + """, + ( + wallet_id, + "TEST WALLET", + adminkey, + adminkey, + uuid4().hex, # invoice key is not important + ), +) + +expiration_date = time.time() + 420 + +# 1 btc in sats +amount = 100_000_000 +internal_id = f"internal_{shortuuid.uuid()}" + +cursor.execute( + """ + INSERT INTO apipayments + (wallet, checking_id, bolt11, hash, preimage, + amount, pending, memo, fee, extra, webhook, expiry) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + wallet_id, + internal_id, + "test_admin_internal", + "test_admin_internal", + None, + amount * 1000, + False, + "test_admin_internal", + 0, + None, + "", + expiration_date, + ), +) + +print(f"created test admin: {adminkey} with {amount} sats") + +conn.commit() +cursor.close() From f0d58a83657196cfd4fafe914603f83ca03531d2 Mon Sep 17 00:00:00 2001 From: calle <93376500+callebtc@users.noreply.github.com> Date: Thu, 26 Jan 2023 11:08:40 +0100 Subject: [PATCH 2/4] Wallets: add custom invoice expiry (#1396) * expiry for fakewallet * expiry for lnd * lnbits backend * fix: eclair descriptionHash fixed and expiry added * cln and sparko * test expiry * Eclair from AdminUI and bugfix for nonexistent payments * add to settings and .env and remove lntxbot * remove duplicate and format * add invoice expiry * add min max and step * UI works now * test should fail, sanity check, will revert * revert, ready for merge Co-authored-by: Tiago Vasconcelos --- .env.example | 3 + lnbits/core/services.py | 2 + lnbits/core/templates/admin/_tab_funding.html | 58 ++++++----- lnbits/core/templates/admin/index.html | 43 ++++---- lnbits/core/templates/core/_api_docs.html | 9 +- lnbits/core/views/api.py | 2 + lnbits/settings.py | 16 ++- lnbits/wallets/cliche.py | 1 + lnbits/wallets/cln.py | 2 + lnbits/wallets/eclair.py | 97 +++++++++++-------- lnbits/wallets/fake.py | 2 + lnbits/wallets/lnbits.py | 3 + lnbits/wallets/lndgrpc.py | 5 +- lnbits/wallets/lndrest.py | 2 + lnbits/wallets/spark.py | 1 + tests/core/views/test_api.py | 15 +++ 16 files changed, 166 insertions(+), 95 deletions(-) diff --git a/.env.example b/.env.example index a367bec6..37e62ce3 100644 --- a/.env.example +++ b/.env.example @@ -63,6 +63,9 @@ LNBITS_BACKEND_WALLET_CLASS=VoidWallet # VoidWallet is just a fallback that works without any actual Lightning capabilities, # just so you can see the UI before dealing with this file. +# Invoice expiry for LND, CLN, Eclair, LNbits funding sources +LIGHTNING_INVOICE_EXPIRY=600 + # Set one of these blocks depending on the wallet kind you chose above: # ClicheWallet diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 44664513..ee750302 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -64,6 +64,7 @@ async def create_invoice( memo: str, description_hash: Optional[bytes] = None, unhashed_description: Optional[bytes] = None, + expiry: Optional[int] = None, extra: Optional[Dict] = None, webhook: Optional[str] = None, internal: Optional[bool] = False, @@ -79,6 +80,7 @@ async def create_invoice( memo=invoice_memo, description_hash=description_hash, unhashed_description=unhashed_description, + expiry=expiry or settings.lightning_invoice_expiry, ) if not ok: raise InvoiceFailure(error_message or "unexpected backend error.") diff --git a/lnbits/core/templates/admin/_tab_funding.html b/lnbits/core/templates/admin/_tab_funding.html index 3887e151..35349e38 100644 --- a/lnbits/core/templates/admin/_tab_funding.html +++ b/lnbits/core/templates/admin/_tab_funding.html @@ -16,7 +16,7 @@
-
+

Active Funding (Requires server restart)

@@ -30,28 +30,40 @@
-
-
-

Fee reserve

-
-
- - -
-
- +
+
+
+

Invoice Expiry

+ + +
+
+

Fee reserve

+
+
+ + +
+
+ +
diff --git a/lnbits/core/templates/admin/index.html b/lnbits/core/templates/admin/index.html index 3e688fd6..f4d5c601 100644 --- a/lnbits/core/templates/admin/index.html +++ b/lnbits/core/templates/admin/index.html @@ -9,6 +9,7 @@ :disabled="!checkChanges" > Save your changes + + Restart the server for changes to take effect + + Add funds to a wallet. + +
+
@@ -70,16 +77,19 @@ label="Funding" @update="val => tab = val.name" > + + +
+ {% include "admin/_tab_funding.html" %} {% include @@ -98,10 +109,12 @@
+

TopUp a wallet

+
+
+
+
+ Cancel
- {% endblock %} {% block scripts %} {{ window_vars(user) }}