From 6935589dad70b9bcca1f36ef1ed0271623bfb2dd Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Thu, 14 Oct 2021 22:30:47 +0100 Subject: [PATCH] watchonly done --- lnbits/extensions/watchonly/__init__.py | 18 ++- lnbits/extensions/watchonly/crud.py | 12 +- lnbits/extensions/watchonly/models.py | 12 +- .../templates/watchonly/_api_docs.html | 16 +-- .../watchonly/templates/watchonly/index.html | 1 - lnbits/extensions/watchonly/views.py | 40 +++--- lnbits/extensions/watchonly/views_api.py | 118 +++++++++--------- 7 files changed, 119 insertions(+), 98 deletions(-) diff --git a/lnbits/extensions/watchonly/__init__.py b/lnbits/extensions/watchonly/__init__.py index b8df3197..8a1632f5 100644 --- a/lnbits/extensions/watchonly/__init__.py +++ b/lnbits/extensions/watchonly/__init__.py @@ -1,13 +1,25 @@ -from quart import Blueprint +import asyncio + +from fastapi import APIRouter + from lnbits.db import Database +from lnbits.helpers import template_renderer db = Database("ext_watchonly") -watchonly_ext: Blueprint = Blueprint( - "watchonly", __name__, static_folder="static", template_folder="templates" +watchonly_ext: APIRouter = APIRouter( + prefix="/watchonly", + tags=["watchonly"] ) +def watchonly_renderer(): + return template_renderer( + [ + "lnbits/extensions/watchonly/templates", + ] + ) + from .views_api import * # noqa from .views import * # noqa diff --git a/lnbits/extensions/watchonly/crud.py b/lnbits/extensions/watchonly/crud.py index bd301eb4..f9b92c2f 100644 --- a/lnbits/extensions/watchonly/crud.py +++ b/lnbits/extensions/watchonly/crud.py @@ -74,8 +74,9 @@ def parse_key(masterpub: str): return desc, network -async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Wallets: +async def create_watch_wallet(user: str, masterpub: str, title: str) -> Wallets: # check the masterpub is fine, it will raise an exception if not + print("PARSE", parse_key(masterpub)) parse_key(masterpub) wallet_id = urlsafe_short_hash() await db.execute( @@ -131,19 +132,20 @@ async def delete_watch_wallet(wallet_id: str) -> None: async def get_derive_address(wallet_id: str, num: int): wallet = await get_watch_wallet(wallet_id) - key = wallet[2] + key = wallet.masterpub desc, network = parse_key(key) return desc.derive(num).address(network=network) async def get_fresh_address(wallet_id: str) -> Optional[Addresses]: wallet = await get_watch_wallet(wallet_id) + if not wallet: return None - address = await get_derive_address(wallet_id, wallet[4] + 1) + address = await get_derive_address(wallet_id, wallet.address_no + 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.address_no + 1) masterpub_id = urlsafe_short_hash() await db.execute( """ @@ -181,7 +183,7 @@ async def get_addresses(wallet_id: str) -> List[Addresses]: async def create_mempool(user: str) -> Optional[Mempool]: await db.execute( """ - INSERT INTO watchonly.mempool ("user",endpoint) + INSERT INTO watchonly.mempool ("user",endpoint) VALUES (?, ?) """, (user, "https://mempool.space"), diff --git a/lnbits/extensions/watchonly/models.py b/lnbits/extensions/watchonly/models.py index b9faa601..2fc4bf2e 100644 --- a/lnbits/extensions/watchonly/models.py +++ b/lnbits/extensions/watchonly/models.py @@ -1,8 +1,12 @@ from sqlite3 import Row -from typing import NamedTuple +from fastapi.param_functions import Query +from pydantic import BaseModel +class CreateWallet(BaseModel): + masterpub: str = Query("") + title: str = Query("") -class Wallets(NamedTuple): +class Wallets(BaseModel): id: str user: str masterpub: str @@ -15,7 +19,7 @@ class Wallets(NamedTuple): return cls(**dict(row)) -class Mempool(NamedTuple): +class Mempool(BaseModel): user: str endpoint: str @@ -24,7 +28,7 @@ class Mempool(NamedTuple): return cls(**dict(row)) -class Addresses(NamedTuple): +class Addresses(BaseModel): id: str address: str wallet: str diff --git a/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html b/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html index 97fdb8a9..9d6bb6ac 100644 --- a/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html +++ b/lnbits/extensions/watchonly/templates/watchonly/_api_docs.html @@ -38,7 +38,7 @@
Curl example
curl -X GET {{ request.url_root }}api/v1/wallet -H "X-Api-Key: {{ - g.user.wallets[0].inkey }}" + user.wallets[0].inkey }}" @@ -67,7 +67,7 @@
Curl example
curl -X GET {{ request.url_root }}api/v1/wallet/<wallet_id> - -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" + -H "X-Api-Key: {{ user.wallets[0].inkey }}" @@ -91,7 +91,7 @@ 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 }}" + application/json" -H "X-Api-Key: {{ user.wallets[0].adminkey }}" @@ -117,7 +117,7 @@ curl -X DELETE {{ request.url_root }}api/v1/wallet/<wallet_id> -H "X-Api-Key: {{ - g.user.wallets[0].adminkey }}" + user.wallets[0].adminkey }}" @@ -143,7 +143,7 @@ curl -X GET {{ request.url_root }}api/v1/addresses/<wallet_id> -H "X-Api-Key: {{ - g.user.wallets[0].inkey }}" + user.wallets[0].inkey }}" @@ -174,7 +174,7 @@
Curl example
curl -X GET {{ request.url_root }}api/v1/address/<wallet_id> - -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" + -H "X-Api-Key: {{ user.wallets[0].inkey }}" @@ -203,7 +203,7 @@
Curl example
curl -X GET {{ request.url_root }}api/v1/mempool -H "X-Api-Key: {{ - g.user.wallets[0].adminkey }}" + user.wallets[0].adminkey }}" @@ -235,7 +235,7 @@ 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 }}" + {{ user.wallets[0].adminkey }}" diff --git a/lnbits/extensions/watchonly/templates/watchonly/index.html b/lnbits/extensions/watchonly/templates/watchonly/index.html index 5230e298..e70f8a23 100644 --- a/lnbits/extensions/watchonly/templates/watchonly/index.html +++ b/lnbits/extensions/watchonly/templates/watchonly/index.html @@ -287,7 +287,6 @@ {% endraw %} {% endblock %} {% block scripts %} {{ window_vars(user) }} -