From b2841ee909b8c78036c91269e299666047aad18a Mon Sep 17 00:00:00 2001
From: Ben Arc
Date: Sun, 4 Apr 2021 13:28:46 +0100
Subject: [PATCH] prettier and black
---
lnbits/extensions/watchonly/__init__.py | 3 +-
lnbits/extensions/watchonly/crud.py | 15 +-
lnbits/extensions/watchonly/migrations.py | 4 +-
lnbits/extensions/watchonly/models.py | 6 +-
.../templates/watchonly/_api_docs.html | 432 ++++++++++--------
.../watchonly/templates/watchonly/index.html | 312 +++++--------
lnbits/extensions/watchonly/views.py | 5 +-
lnbits/extensions/watchonly/views_api.py | 33 +-
8 files changed, 394 insertions(+), 416 deletions(-)
diff --git a/lnbits/extensions/watchonly/__init__.py b/lnbits/extensions/watchonly/__init__.py
index 19f2a5af..830f8fc4 100644
--- a/lnbits/extensions/watchonly/__init__.py
+++ b/lnbits/extensions/watchonly/__init__.py
@@ -4,7 +4,8 @@ from lnbits.db import Database
db = Database("ext_watchonly")
-watchonly_ext: Blueprint = Blueprint("watchonly", __name__, static_folder="static", template_folder="templates")
+watchonly_ext: Blueprint = Blueprint(
+ "watchonly", __name__, static_folder="static", template_folder="templates")
from .views_api import * # noqa
diff --git a/lnbits/extensions/watchonly/crud.py b/lnbits/extensions/watchonly/crud.py
index 3499eabb..3de06b17 100644
--- a/lnbits/extensions/watchonly/crud.py
+++ b/lnbits/extensions/watchonly/crud.py
@@ -24,7 +24,6 @@ from binascii import unhexlify, hexlify, a2b_base64, b2a_base64
import httpx
-
##########################WALLETS####################
async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Wallets:
@@ -43,7 +42,7 @@ async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Walle
""",
(wallet_id, user, masterpub, title, 0, 0),
)
- # weallet_id = db.cursor.lastrowid
+ # weallet_id = db.cursor.lastrowid
return await get_watch_wallet(wallet_id)
@@ -57,6 +56,7 @@ async def get_watch_wallets(user: str) -> List[Wallets]:
rows = await db.fetchall("SELECT * FROM wallets WHERE user = ?", (user,))
return [Wallets(**row) for row in rows]
+
async def update_watch_wallet(wallet_id: str, **kwargs) -> Optional[Wallets]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
@@ -70,13 +70,14 @@ async def delete_watch_wallet(wallet_id: str) -> None:
########################ADDRESSES#######################
+
async def get_derive_address(wallet_id: str, num: int):
wallet = await get_watch_wallet(wallet_id)
key = wallet[2]
k = bip32.HDKey.from_base58(key)
child = k.derive([0, num])
-
+
if key[0:4] == "xpub":
address = script.p2pkh(child).address()
elif key[0:4] == "zpub":
@@ -86,12 +87,13 @@ async def get_derive_address(wallet_id: str, num: int):
return address
+
async def get_fresh_address(wallet_id: str) -> Addresses:
wallet = await get_watch_wallet(wallet_id)
-
+
address = await get_derive_address(wallet_id, wallet[4] + 1)
- await update_watch_wallet(wallet_id = wallet_id, address_no = wallet[4] + 1)
+ await update_watch_wallet(wallet_id=wallet_id, address_no=wallet[4] + 1)
masterpub_id = urlsafe_short_hash()
await db.execute(
"""
@@ -113,12 +115,14 @@ async def get_address(address: str) -> Addresses:
row = await db.fetchone("SELECT * FROM addresses WHERE address = ?", (address,))
return Addresses.from_row(row) if row else None
+
async def get_addresses(wallet_id: str) -> List[Addresses]:
rows = await db.fetchall("SELECT * FROM addresses WHERE wallet = ?", (wallet_id,))
return [Addresses(**row) for row in rows]
######################MEMPOOL#######################
+
async def create_mempool(user: str) -> Mempool:
await db.execute(
"""
@@ -133,6 +137,7 @@ async def create_mempool(user: str) -> Mempool:
row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
return Mempool.from_row(row) if row else None
+
async def update_mempool(user: str, **kwargs) -> Optional[Mempool]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
diff --git a/lnbits/extensions/watchonly/migrations.py b/lnbits/extensions/watchonly/migrations.py
index 72ff4e33..0057f3e9 100644
--- a/lnbits/extensions/watchonly/migrations.py
+++ b/lnbits/extensions/watchonly/migrations.py
@@ -25,7 +25,7 @@ async def m001_initial(db):
);
"""
)
-
+
await db.execute(
"""
CREATE TABLE IF NOT EXISTS mempool (
@@ -33,4 +33,4 @@ async def m001_initial(db):
endpoint TEXT NOT NULL
);
"""
- )
\ No newline at end of file
+ )
diff --git a/lnbits/extensions/watchonly/models.py b/lnbits/extensions/watchonly/models.py
index 9f2649c0..b9faa601 100644
--- a/lnbits/extensions/watchonly/models.py
+++ b/lnbits/extensions/watchonly/models.py
@@ -1,6 +1,7 @@
from sqlite3 import Row
from typing import NamedTuple
+
class Wallets(NamedTuple):
id: str
user: str
@@ -17,11 +18,12 @@ class Wallets(NamedTuple):
class Mempool(NamedTuple):
user: str
endpoint: str
-
+
@classmethod
def from_row(cls, row: Row) -> "Mempool":
return cls(**dict(row))
+
class Addresses(NamedTuple):
id: str
address: str
@@ -30,4 +32,4 @@ class Addresses(NamedTuple):
@classmethod
def from_row(cls, row: Row) -> "Addresses":
- return cls(**dict(row))
\ No newline at end of file
+ return cls(**dict(row))
diff --git a/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html b/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html
index 5d8ca8b0..97fdb8a9 100644
--- a/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html
+++ b/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html
@@ -1,196 +1,244 @@
-
-
- Watch Only extension uses mempool.space
- For use with "account Extended Public Key" https://iancoleman.io/bip39/
-
-
Created by, Ben Arc (using, Embit)
-
-
+
+
+
+ Watch Only extension uses mempool.space
+ For use with "account Extended Public Key"
+ https://iancoleman.io/bip39/
+
+
Created by,
+ Ben Arc (using,
+ Embit)
+
+
-
-
-
-
-
-
- GET /watchonly/api/v1/wallet
- Headers
- {"X-Api-Key": <invoice_key>}
- Body (application/json)
-
- Returns 200 OK (application/json)
-
- [<wallets_object>, ...]
- Curl example
- curl -X GET {{ request.url_root }}api/v1/wallet -H "X-Api-Key: {{
- g.user.wallets[0].inkey }}"
-
-
-
-
-
-
-
- GET
- /watchonly/api/v1/wallet/<wallet_id>
- Headers
- {"X-Api-Key": <invoice_key>}
- Body (application/json)
-
- Returns 201 CREATED (application/json)
-
- [<wallet_object>, ...]
- Curl example
- curl -X GET {{ request.url_root }}api/v1/wallet/<wallet_id> -H
- "X-Api-Key: {{ g.user.wallets[0].inkey }}"
-
-
-
-
-
-
- POST /watchonly/api/v1/wallet
- Headers
- {"X-Api-Key": <admin_key>}
- Body (application/json)
-
- Returns 201 CREATED (application/json)
-
- [<wallet_object>, ...]
- Curl example
- curl -X POST {{ request.url_root }}api/v1/wallet -d
- '{"title": <string>, "masterpub": <string>}' -H
- "Content-type: application/json" -H "X-Api-Key: {{
- g.user.wallets[0].adminkey }}"
-
-
-
+
+
+
+ GET /watchonly/api/v1/wallet
+ Headers
+ {"X-Api-Key": <invoice_key>}
+
+ Body (application/json)
+
+
+ Returns 200 OK (application/json)
+
+ [<wallets_object>, ...]
+ Curl example
+ curl -X GET {{ request.url_root }}api/v1/wallet -H "X-Api-Key: {{
+ g.user.wallets[0].inkey }}"
+
+
+
+
+
+
+
+ GET
+ /watchonly/api/v1/wallet/<wallet_id>
+ Headers
+ {"X-Api-Key": <invoice_key>}
+
+ Body (application/json)
+
+
+ Returns 201 CREATED (application/json)
+
+ [<wallet_object>, ...]
+ Curl example
+ curl -X GET {{ request.url_root }}api/v1/wallet/<wallet_id>
+ -H "X-Api-Key: {{ g.user.wallets[0].inkey }}"
+
+
+
+
+
+
+
+ POST /watchonly/api/v1/wallet
+ Headers
+ {"X-Api-Key": <admin_key>}
+
+ Body (application/json)
+
+
+ Returns 201 CREATED (application/json)
+
+ [<wallet_object>, ...]
+ Curl example
+ curl -X POST {{ request.url_root }}api/v1/wallet -d '{"title":
+ <string>, "masterpub": <string>}' -H "Content-type:
+ application/json" -H "X-Api-Key: {{ g.user.wallets[0].adminkey }}"
+
+
+
+
+
+
+
+ DELETE
+ /watchonly/api/v1/wallet/<wallet_id>
+ Headers
+ {"X-Api-Key": <admin_key>}
+ Returns 204 NO CONTENT
+
+ Curl example
+ curl -X DELETE {{ request.url_root
+ }}api/v1/wallet/<wallet_id> -H "X-Api-Key: {{
+ g.user.wallets[0].adminkey }}"
+
+
+
+
+
+
+
+
+ GET
+ /watchonly/api/v1/addresses/<wallet_id>
+ Headers
+ {"X-Api-Key": <invoice_key>}
+
+ Body (application/json)
+
+
+ Returns 200 OK (application/json)
+
+ [<address_object>, ...]
+ Curl example
+ curl -X GET {{ request.url_root
+ }}api/v1/addresses/<wallet_id> -H "X-Api-Key: {{
+ g.user.wallets[0].inkey }}"
+
+
+
+
+
+
+
+
+ GET
+ /watchonly/api/v1/address/<wallet_id>
+ Headers
+ {"X-Api-Key": <invoice_key>}
+
+ Body (application/json)
+
+
+ Returns 200 OK (application/json)
+
+ [<address_object>, ...]
+ Curl example
+ curl -X GET {{ request.url_root }}api/v1/address/<wallet_id>
+ -H "X-Api-Key: {{ g.user.wallets[0].inkey }}"
+
+
+
+
+
+
+
+
+ GET /watchonly/api/v1/mempool
+ Headers
+ {"X-Api-Key": <admin_key>}
+
+ Body (application/json)
+
+
+ Returns 200 OK (application/json)
+
+ [<mempool_object>, ...]
+ Curl example
+ curl -X GET {{ request.url_root }}api/v1/mempool -H "X-Api-Key: {{
+ g.user.wallets[0].adminkey }}"
+
+
+
+
+
+
+
+
+ POST
+ /watchonly/api/v1/mempool
+ Headers
+ {"X-Api-Key": <admin_key>}
+
+ Body (application/json)
+
+
+ Returns 201 CREATED (application/json)
+
+ [<mempool_object>, ...]
+ Curl example
+ curl -X PUT {{ request.url_root }}api/v1/mempool -d '{"endpoint":
+ <string>}' -H "Content-type: application/json" -H "X-Api-Key:
+ {{ g.user.wallets[0].adminkey }}"
+
+
+
+
-
-
-
- DELETE
- /watchonly/api/v1/wallet/<wallet_id>
- Headers
- {"X-Api-Key": <admin_key>}
- Returns 204 NO CONTENT
-
- Curl example
- curl -X DELETE {{ request.url_root }}api/v1/wallet/<wallet_id>
- -H "X-Api-Key: {{ g.user.wallets[0].adminkey }}"
-
-
-
-
-
-
-
-
-
- GET /watchonly/api/v1/addresses/<wallet_id>
- Headers
- {"X-Api-Key": <invoice_key>}
- Body (application/json)
-
- Returns 200 OK (application/json)
-
- [<address_object>, ...]
- Curl example
- curl -X GET {{ request.url_root }}api/v1/addresses/<wallet_id> -H "X-Api-Key: {{
- g.user.wallets[0].inkey }}"
-
-
-
-
-
-
-
-
- GET /watchonly/api/v1/address/<wallet_id>
- Headers
- {"X-Api-Key": <invoice_key>}
- Body (application/json)
-
- Returns 200 OK (application/json)
-
- [<address_object>, ...]
- Curl example
- curl -X GET {{ request.url_root }}api/v1/address/<wallet_id> -H "X-Api-Key: {{
- g.user.wallets[0].inkey }}"
-
-
-
-
-
-
-
-
-
- GET /watchonly/api/v1/mempool
- Headers
- {"X-Api-Key": <admin_key>}
- Body (application/json)
-
- Returns 200 OK (application/json)
-
- [<mempool_object>, ...]
- Curl example
- curl -X GET {{ request.url_root }}api/v1/mempool -H "X-Api-Key: {{
- g.user.wallets[0].adminkey }}"
-
-
-
-
-
-
-
-
- POST /watchonly/api/v1/mempool
- Headers
- {"X-Api-Key": <admin_key>}
- Body (application/json)
-
- Returns 201 CREATED (application/json)
-
- [<mempool_object>, ...]
- Curl example
- curl -X PUT {{ request.url_root }}api/v1/mempool -d
- '{"endpoint": <string>}' -H
- "Content-type: application/json" -H "X-Api-Key: {{
- g.user.wallets[0].adminkey }}"
-
-
-
-
-
-
+
diff --git a/lnbits/extensions/watchonly/templates/watchonly/index.html b/lnbits/extensions/watchonly/templates/watchonly/index.html
index c99ba1f9..263897bd 100644
--- a/lnbits/extensions/watchonly/templates/watchonly/index.html
+++ b/lnbits/extensions/watchonly/templates/watchonly/index.html
@@ -4,38 +4,26 @@
-{% raw %}
- New wallet
-
-
-
- Point to another Mempool
-
- {{ this.mempool.endpoint }}
-
-
-
- set
- cancel
+ {% raw %}
+ New wallet
+
+
+
+ Point to another Mempool
+
+ {{ this.mempool.endpoint }}
+
+
+
+
+ set
+ cancel
-
-
+
+
-
+
@@ -46,23 +34,16 @@
Wallets
-
-
-
-
-
+
+
+
+
+
-
+
@@ -75,32 +56,19 @@
-
-
-
-
-
- Adresses
-
-
-
+
+
+
+
+
+ Adresses
+
+
+
-
+
{{ col.value }}
@@ -126,7 +94,7 @@
- {% include "watchonly/_api_docs.html" %}
+ {% include "watchonly/_api_docs.html" %}
@@ -135,35 +103,16 @@
-
-
+
+
+
- Create Watch-only Wallet
- Cancel
+ formDialog.data.title == null" type="submit">Create Watch-only Wallet
+ Cancel
@@ -172,78 +121,52 @@
{% raw %}
- Addresses
-
- Current:
+
Addresses
+
+ Current:
{{ currentaddress }}
-
-
-
+
+
+
-
+
-
-
-
- {{ data.address }}
-
-
-
-
+
+
+
+ {{ data.address }}
+
+
+
+
-
-
- Get fresh address
+ Get fresh address
Close
-{% endraw %}
+ {% endraw %}
{% endblock %} {% block scripts %} {{ window_vars(user) }}
+
-{% endblock %}
+{% endblock %}
\ No newline at end of file
diff --git a/lnbits/extensions/watchonly/views.py b/lnbits/extensions/watchonly/views.py
index f46fb0e5..a30dc888 100644
--- a/lnbits/extensions/watchonly/views.py
+++ b/lnbits/extensions/watchonly/views.py
@@ -15,6 +15,7 @@ async def index():
@watchonly_ext.route("/")
async def display(charge_id):
- link = get_payment(charge_id) or abort(HTTPStatus.NOT_FOUND, "Charge link does not exist.")
+ link = get_payment(charge_id) or abort(
+ HTTPStatus.NOT_FOUND, "Charge link does not exist.")
- return await render_template("watchonly/display.html", link=link)
\ No newline at end of file
+ return await render_template("watchonly/display.html", link=link)
diff --git a/lnbits/extensions/watchonly/views_api.py b/lnbits/extensions/watchonly/views_api.py
index 67c0c1eb..83365b23 100644
--- a/lnbits/extensions/watchonly/views_api.py
+++ b/lnbits/extensions/watchonly/views_api.py
@@ -1,7 +1,8 @@
import hashlib
from quart import g, jsonify, url_for, request
from http import HTTPStatus
-import httpx, json
+import httpx
+import json
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
@@ -22,6 +23,7 @@ from .crud import (
###################WALLETS#############################
+
@watchonly_ext.route("/api/v1/wallet", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_wallets_retrieve():
@@ -33,11 +35,12 @@ async def api_wallets_retrieve():
except:
return ""
+
@watchonly_ext.route("/api/v1/wallet/", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_wallet_retrieve(wallet_id):
wallet = await get_watch_wallet(wallet_id)
-
+
if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
@@ -54,7 +57,7 @@ async def api_wallet_retrieve(wallet_id):
)
async def api_wallet_create_or_update(wallet_id=None):
wallet = await create_watch_wallet(user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"])
- mempool = await get_mempool(g.wallet.user)
+ mempool = await get_mempool(g.wallet.user)
if not mempool:
create_mempool(user=g.wallet.user)
return jsonify(wallet._asdict()), HTTPStatus.CREATED
@@ -78,9 +81,9 @@ async def api_wallet_delete(wallet_id):
@watchonly_ext.route("/api/v1/address/", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_fresh_address(wallet_id):
- await get_fresh_address(wallet_id)
-
- addresses = await get_addresses(wallet_id)
+ await get_fresh_address(wallet_id)
+
+ addresses = await get_addresses(wallet_id)
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK
@@ -90,16 +93,16 @@ async def api_fresh_address(wallet_id):
async def api_get_addresses(wallet_id):
print(wallet_id)
- wallet = await get_watch_wallet(wallet_id)
-
+ wallet = await get_watch_wallet(wallet_id)
+
if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
- addresses = await get_addresses(wallet_id)
+ addresses = await get_addresses(wallet_id)
if not addresses:
await get_fresh_address(wallet_id)
- addresses = await get_addresses(wallet_id)
+ addresses = await get_addresses(wallet_id)
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK
@@ -115,17 +118,13 @@ async def api_get_addresses(wallet_id):
)
async def api_update_mempool():
mempool = await update_mempool(user=g.wallet.user, **g.data)
- return jsonify(mempool._asdict()), HTTPStatus.OK
+ return jsonify(mempool._asdict()), HTTPStatus.OK
+
@watchonly_ext.route("/api/v1/mempool", methods=["GET"])
@api_check_wallet_key("admin")
async def api_get_mempool():
- mempool = await get_mempool(g.wallet.user)
+ mempool = await get_mempool(g.wallet.user)
if not mempool:
mempool = await create_mempool(user=g.wallet.user)
return jsonify(mempool._asdict()), HTTPStatus.OK
-
-
-
-
-
\ No newline at end of file