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
- - - + + +
- +